Skip to content

Commit

Permalink
Maybe process magic tags for file-based templates
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0ttkclark committed Oct 1, 2024
1 parent 099ba47 commit dc72c71
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 36 deletions.
17 changes: 14 additions & 3 deletions components/Migrate-PHP/Migrate-PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,27 @@ private function migrate_template( $object_id, bool $cleanup ) {

$this->setup_file_path( $file_path );

$template_code = $object->get_description();

$extra_headers = '';

if ( false !== strpos( $template_code, '{@' ) ) {
$extra_headers = <<<PHPTEMPLATE
* Magic Tags: Enabled
PHPTEMPLATE;

}

$contents = <<<PHPTEMPLATE
<?php
/**
* Pod Template: {$object->get_label()}
* Pod Template: {$object->get_label()}{$extra_headers}
*
* @var Pods \$obj
*/
?>
{$object->get_description()}
{$template_code}
PHPTEMPLATE;

if ( ! $wp_filesystem->put_contents( $file_path, $contents, FS_CHMOD_FILE ) ) {
Expand Down Expand Up @@ -306,7 +317,7 @@ private function migrate_page( $object_id, bool $cleanup ) {
$contents = <<<PHPTEMPLATE
<?php
/**
* Pod Page Template: {$object->get_label()}
* Pod Page URI: {$object->get_label()}
*
* @var Pods \$pods
*/
Expand Down
117 changes: 89 additions & 28 deletions components/Templates/Templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,8 @@ public static function template( $template_name, $code = null, $obj = null, $dep

$info = $check_access ? pods_info_from_args( [ 'pods' => $obj ] ) : [];

$process_magic_tags = false;

ob_start();

if ( ! empty( $code ) ) {
Expand All @@ -640,7 +642,7 @@ public static function template( $template_name, $code = null, $obj = null, $dep
continue;
}

echo self::do_template( $code, $obj );
echo self::do_template( $code, $obj, true );
}
} else {
$info['item_id'] = $obj->id();
Expand All @@ -652,40 +654,61 @@ public static function template( $template_name, $code = null, $obj = null, $dep
&& ! pods_access_bypass_private_post( $info )
)
) {
echo self::do_template( $code, $obj );
echo self::do_template( $code, $obj, true );
}
}
} elseif ( $template_name == trim( preg_replace( '/[^a-zA-Z0-9_\-\/]/', '', $template_name ), ' /-' ) ) {
$default_templates = self::get_templates_for_pod_template( $template, $obj );
$default_templates = self::get_templates_for_pod_template( $template, $obj );
$template_files_info = self::get_template_files_info( $default_templates );

if ( empty( $obj->id ) ) {
while ( $obj->fetch() ) {
$template_file = array_key_first( $template_files_info );

if ( $template_file ) {
if ( $template_files_info[ $template_file ]['MagicTags'] ) {
$process_magic_tags = true;
}

if ( empty( $obj->id ) ) {
while ( $obj->fetch() ) {
$info['item_id'] = $obj->id();

// Ensure the post is not password protected.
if (
$check_access
&& (
pods_access_bypass_post_with_password( $info )
|| pods_access_bypass_private_post( $info )
)
) {
continue;
}

$template_output = pods_template_part( $template_file, compact( array_keys( get_defined_vars() ) ), true );

if ( $process_magic_tags ) {
$template_output = self::do_template( $template_output, $obj );
}

echo $template_output;
}
} else {
$info['item_id'] = $obj->id();

// Ensure the post is not password protected.
if (
$check_access
&& (
pods_access_bypass_post_with_password( $info )
|| pods_access_bypass_private_post( $info )
! $check_access
|| (
! pods_access_bypass_post_with_password( $info )
&& ! pods_access_bypass_private_post( $info )
)
) {
continue;
}
$template_output = pods_template_part( $template_file, compact( array_keys( get_defined_vars() ) ), true );

pods_template_part( $default_templates, compact( array_keys( get_defined_vars() ) ) );
}
} else {
$info['item_id'] = $obj->id();
if ( $process_magic_tags ) {
$template_output = self::do_template( $template_output, $obj );
}

if (
! $check_access
|| (
! pods_access_bypass_post_with_password( $info )
&& ! pods_access_bypass_private_post( $info )
)
) {
pods_template_part( $default_templates, compact( array_keys( get_defined_vars() ) ) );
echo $template_output;
}
}
}
}//end if
Expand Down Expand Up @@ -731,16 +754,54 @@ public static function get_templates_for_pod_template( $template, $obj = null ):
return (array) apply_filters( 'pods_template_default_templates', $default_templates, $template_name, $template, $obj );
}

/**
* Get templates for pod page.
*
* @since TBD
*
* @param array<int,string> $template_files The list of template files.
*
* @return array The list of template header information for each of the template files.
*/
public static function get_template_files_info( array $template_files ): array {
$template_files_info = [];

foreach ( $template_files as $template_file ) {
$file_path = locate_template( $template_file );

// Skip if template was not found.
if ( '' === $file_path ) {
continue;
}

$data = get_file_data( $file_path, [
'PodTemplate' => 'Pod Template',
'MagicTags' => 'Magic Tags',
] );

$data['MagicTags'] = pods_is_truthy( $data['MagicTags'] );

$template_files_info[ $template_file ] = $data;
}

return $template_files_info;
}

/**
* Parse a template string
*
* @param string $code The template string to parse
* @param object $obj The Pods object
* @param string $code The template string to parse
* @param object $obj The Pods object
* @param bool $process_php Whether to process PHP -- this will be removed in Pods 3.3+.
*
* @since 1.8.5
* @return mixed|string|void
*/
public static function do_template( $code, $obj = null ) {
public static function do_template(
$code,
$obj = null,
$process_php = false
) {
if ( ! empty( $obj ) ) {
self::$obj =& $obj;
} else {
Expand All @@ -757,7 +818,7 @@ public static function do_template( $code, $obj = null ) {
// @todo Remove this code in Pods 3.3 and completely ignore any $code that starts with <? in the string.
_doing_it_wrong( 'Pods Templates', 'Pod Template PHP code is no longer actively supported and will be completely removed in Pods 3.3', '3.0' );

if ( ! PODS_DISABLE_EVAL ) {
if ( $process_php && ! PODS_DISABLE_EVAL ) {
pods_deprecated( 'Pod Template PHP code has been deprecated, please use WP Templates instead of embedding PHP.', '2.3' );

$code = str_replace( '$this->', '$obj->', $code );
Expand Down
11 changes: 6 additions & 5 deletions includes/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -2580,11 +2580,12 @@ function pods_is_truthy( $value ) {

// This is the list of strings we will support as truthy.
$supported_strings = [
'1' => true,
'true' => true,
'on' => true,
'yes' => true,
'y' => true,
'1' => true,
'true' => true,
'on' => true,
'yes' => true,
'y' => true,
'enabled' => true,
];

return isset( $supported_strings[ $value ] );
Expand Down

0 comments on commit dc72c71

Please sign in to comment.