From dc72c71c19d968ba3a94bc9b88e90d281fc4870f Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Tue, 1 Oct 2024 10:52:49 -0500 Subject: [PATCH] Maybe process magic tags for file-based templates --- components/Migrate-PHP/Migrate-PHP.php | 17 +++- components/Templates/Templates.php | 117 +++++++++++++++++++------ includes/data.php | 11 +-- 3 files changed, 109 insertions(+), 36 deletions(-) diff --git a/components/Migrate-PHP/Migrate-PHP.php b/components/Migrate-PHP/Migrate-PHP.php index f140ac9c92..d019cbbd72 100644 --- a/components/Migrate-PHP/Migrate-PHP.php +++ b/components/Migrate-PHP/Migrate-PHP.php @@ -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 = <<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 ) ) { @@ -306,7 +317,7 @@ private function migrate_page( $object_id, bool $cleanup ) { $contents = <<get_label()} + * Pod Page URI: {$object->get_label()} * * @var Pods \$pods */ diff --git a/components/Templates/Templates.php b/components/Templates/Templates.php index ed71f48ec4..e13ac06933 100644 --- a/components/Templates/Templates.php +++ b/components/Templates/Templates.php @@ -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 ) ) { @@ -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(); @@ -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 @@ -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 $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 { @@ -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 ', '$obj->', $code ); diff --git a/includes/data.php b/includes/data.php index ea2f1d6b30..feda1cf966 100644 --- a/includes/data.php +++ b/includes/data.php @@ -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 ] );