From 8a9a3dc15b2ae151429881e1b160887c1c1ea1eb Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Thu, 3 Oct 2024 00:01:10 -0500 Subject: [PATCH] More updates for PHP file-based template migration and support for Pod Page content templates --- components/Migrate-PHP/Migrate-PHP.php | 80 ++++++++++++++-- components/Pages.php | 128 ++++++++++++++++++++++++- components/Templates/Templates.php | 2 +- 3 files changed, 196 insertions(+), 14 deletions(-) diff --git a/components/Migrate-PHP/Migrate-PHP.php b/components/Migrate-PHP/Migrate-PHP.php index d019cbbd72..f044440643 100644 --- a/components/Migrate-PHP/Migrate-PHP.php +++ b/components/Migrate-PHP/Migrate-PHP.php @@ -282,18 +282,21 @@ private function migrate_page( $object_id, bool $cleanup ) { pods_error( sprintf( esc_html__( 'Unable to find the Pod Page by ID: %s', 'pods' ), $object_id ) ); } - $files = Pods_Pages::get_templates_for_pod_page( $object ); + $files = Pods_Pages::get_templates_for_pod_page( $object ); + $files_for_content = Pods_Pages::get_templates_for_pod_page_content( $object ); if ( count( $files ) < 2 ) { // translators: %s is the file paths found. pods_error( sprintf( esc_html__( 'Unable to detect the file path: %s', 'pods' ), json_encode( $files, JSON_PRETTY_PRINT ) ) ); } - $file_path = trailingslashit( get_stylesheet_directory() ) . array_shift( $files ); + $file_path = trailingslashit( get_stylesheet_directory() ) . array_shift( $files ); + $file_path_for_content = trailingslashit( get_stylesheet_directory() ) . array_shift( $files_for_content ); $this->setup_file_path( $file_path ); - $precode = (string) $object->get_arg( 'precode' ); + $precode = (string) $object->get_arg( 'precode' ); + $page_template = (string) $object->get_arg( 'page_template' ); if ( false !== strpos( $precode, '' ) ) { $precode .= "\n?>"; @@ -302,32 +305,91 @@ private function migrate_page( $object_id, bool $cleanup ) { $precode_template = ''; if ( ! empty( $precode ) ) { - $precode_template = << {$precode} +PHPTEMPLATE . "\n"; + } + + $template_code = trim( $object->get_description() ); + + $has_page_template = ! empty( $page_template ); + + $precode_has_end_tag = false !== strpos( $precode, '?>' ); + + if ( false === strpos( $template_code, '\n" . $template_code . ( ! $has_page_template ? '' : "\nsetup_file_path( $file_path_for_content ); + + if ( '_custom' !== $page_template && 'blank' !== $page_template ) { + $extra_notes .= "\n" . <<get_label()} + * Pod Page URI: {$object->get_label()}{$extra_headers}{$extra_notes} * * @var Pods \$pods */ {$precode_template} -?> -{$object->get_description()} +{$template_code} PHPTEMPLATE; + // Clean up the PHP tags that open and close too often. + $contents = preg_replace( '/\?>\s*<\?php(\s*)/Umi', '$1', $contents ); + $contents = preg_replace( '/\?>\s*<\?(\s*)/Umi', '$1', $contents ); + $contents = preg_replace( '/\n{3,}/', "\n\n", $contents ); + if ( ! $wp_filesystem->put_contents( $file_path, $contents, FS_CHMOD_FILE ) ) { // translators: %s is the file path. pods_error( sprintf( esc_html__( 'Unable to write to the file: %s', 'pods' ), $file_path ) ); diff --git a/components/Pages.php b/components/Pages.php index 40badbadd3..5a7476c33c 100644 --- a/components/Pages.php +++ b/components/Pages.php @@ -1212,7 +1212,31 @@ public static function content( $return = false, $pods_page = false ) { do_action( 'pods_content_pre', $pods_page, $content ); - if ( $content && 0 < strlen( $content ) ) { + $default_templates = self::get_templates_for_pod_page_content( $pods_page ); + $template_files_info = self::get_template_files_info( $default_templates ); + + $template_file = array_key_first( $template_files_info ); + + if ( $template_file ) { + // Check if we are running this content function from within a Pod Page PHP template already to prevent recursion. + if ( did_action( 'pods_page_loaded_template' ) ) { + $content = ''; + } else { + $content = pods_template_part( $template_file, compact( 'pods', 'pods_page' ), true );; + + if ( $template_files_info[ $template_file ]['MagicTags'] ) { + if ( is_object( $pods ) && ! empty( $pods->id ) ) { + $content = $pods->do_magic_tags( $content ); + } else { + _doing_it_wrong( 'Pods Pages', 'Pod Page template supports magic tags but cannot be processed because an associated Pod is not set in the Pod Page settings', '3.2.8' ); + + $content = ''; + } + } + } + + echo $content; + } elseif ( $content && 0 < strlen( $content ) ) { // @todo Remove this code in Pods 3.3 and completely ignore any $code that starts with $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, [ + 'URI' => 'Pod Page URI', + 'MagicTags' => 'Magic Tags', + ] ); + + $data['MagicTags'] = pods_is_truthy( $data['MagicTags'] ); + + $template_files_info[ $template_file ] = $data; + } + + return $template_files_info; + } } /** diff --git a/components/Templates/Templates.php b/components/Templates/Templates.php index e13ac06933..18a4465449 100644 --- a/components/Templates/Templates.php +++ b/components/Templates/Templates.php @@ -755,7 +755,7 @@ public static function get_templates_for_pod_template( $template, $obj = null ): } /** - * Get templates for pod page. + * Get the list of template header information for each of the template files. * * @since TBD *