Skip to content

Commit

Permalink
More updates for PHP file-based template migration and support for Po…
Browse files Browse the repository at this point in the history
…d Page content templates
  • Loading branch information
sc0ttkclark committed Oct 3, 2024
1 parent cc321eb commit 8a9a3dc
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 14 deletions.
80 changes: 71 additions & 9 deletions components/Migrate-PHP/Migrate-PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<?' ) && false === strpos( $precode, '?>' ) ) {
$precode .= "\n?>";
Expand All @@ -302,32 +305,91 @@ private function migrate_page( $object_id, bool $cleanup ) {
$precode_template = '';
if ( ! empty( $precode ) ) {
$precode_template = <<<PHPTEMPLATE
<?php
$precode_template = "\n" . <<<PHPTEMPLATE
/*
* Precode goes below.
*/
?>
{$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, '<?' ) ) {
$template_code = "?>\n" . $template_code . ( ! $has_page_template ? '' : "\n<?php" );
} elseif ( ( ! $has_page_template || ! $precode_has_end_tag ) && 0 === strpos( $template_code, '<?php' ) ) {
$template_code = substr( $template_code, strlen( '<?php' ) );
} elseif ( ( ! $has_page_template || ! $precode_has_end_tag ) && 0 === strpos( $template_code, '<?' ) ) {
$template_code = substr( $template_code, strlen( '<?' ) );
}

$extra_headers = '';
$extra_notes = '';

if ( ! $has_page_template ) {
$start_tag = '';

if ( $precode_has_end_tag ) {
$start_tag = "\n<?php\n";
}

$template_code = $start_tag . <<<PHPTEMPLATE
get_header();
// Pod Page content goes here.
{$template_code}
get_sidebar();
get_footer();
PHPTEMPLATE;
} else {
// Set the code and save it for the content path.
$this->setup_file_path( $file_path_for_content );

if ( '_custom' !== $page_template && 'blank' !== $page_template ) {
$extra_notes .= "\n" . <<<PHPTEMPLATE
*
* @see {$page_template} for the template where this will get called from.
PHPTEMPLATE;
}

// Set the file path we will write to as the one for the content specific template.
$file_path = $file_path_for_content;
$extra_notes .= "\n" . <<<PHPTEMPLATE
*
* This template is only used for pods_content() calls.
PHPTEMPLATE;
}

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

$contents = <<<PHPTEMPLATE
<?php
/**
* Pod Page URI: {$object->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 ) );
Expand Down
128 changes: 124 additions & 4 deletions components/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <? in the string.
if ( false !== strpos( $content, '<?' ) ) {
_doing_it_wrong( 'Pods Pages', 'Pod Page Precode PHP code is no longer actively supported and will be completely removed in Pods 3.3', '3.0' );
Expand Down Expand Up @@ -1524,7 +1548,17 @@ public function template_include( $original_template ) {
$template = locate_template( $default_templates );

if ( '' !== $template ) {
// found the template and included it, we're good to go!
/**
* Allow determining whether a Pod Page template was loaded.
*
* @since TBD
*
* @param string $template The template file that was loaded.
* @param array $pod_page The Pods Page data.
*/
do_action( 'pods_page_loaded_template', $template, self::$exists );

load_template( $template );
} else {
$template = false;

Expand Down Expand Up @@ -1645,10 +1679,20 @@ public function template_redirect() {
} else {
$default_templates = self::get_templates_for_pod_page( self::$exists );

$template = locate_template( $default_templates, true );
$template = locate_template( $default_templates );

if ( '' !== $template ) {
// found the template and included it, we're good to go!
/**
* Allow determining whether a Pod Page template was loaded.
*
* @since TBD
*
* @param string $template The template file that was loaded.
* @param array $pod_page The Pods Page data.
*/
do_action( 'pods_page_loaded_template', $template, self::$exists );

load_template( $template );
} else {
$template = false;

Expand Down Expand Up @@ -1715,6 +1759,82 @@ public static function get_templates_for_pod_page( $pod_page ): array {
*/
return (array) apply_filters( 'pods_page_default_templates', $default_templates, $pod_page );
}

/**
* Get templates for pod page content.
*
* @since TBD
*
* @param array|Page $pod_page The pod page data.
*
* @return array The list of templates for the pod page content.
*/
public static function get_templates_for_pod_page_content( $pod_page ): array {
if ( $pod_page instanceof Page ) {
$pod_page = self::object_to_page( $pod_page );
}

$default_templates = [];

if ( ! empty( $pod_page ) ) {
$uri = explode( '?', $pod_page['uri'] );
$uri = explode( '#', $uri[0] );
$uri = $uri[0];

$page_path = explode( '/', $uri );

while ( $last = array_pop( $page_path ) ) {
$file_name = str_replace( '*', '-w-', implode( '/', $page_path ) . '/' . $last );
$file_name = sanitize_title( $file_name );
$file_name = trim( str_replace( '--', '-', $file_name ), ' -' );

$default_templates[] = 'pods/pages/content/' . $file_name . '.php';
}
}

/**
* Allow filtering the list of default templates for Pod Page content.
*
* @since unknown
*
* @param array $default_templates The list of default templates for Pod Page content.
* @param array $pod_page The current Pod Page data.
*/
return (array) apply_filters( 'pods_page_content_default_templates', $default_templates, $pod_page );
}

/**
* Get the list of template header information for each of the template files.
*
* @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, [
'URI' => 'Pod Page URI',
'MagicTags' => 'Magic Tags',
] );

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

$template_files_info[ $template_file ] = $data;
}

return $template_files_info;
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion components/Templates/Templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit 8a9a3dc

Please sign in to comment.