wp_id ) { return self::set_has_migrated_page( $template_slug, 'custom-template-exists' ); } // Use the page template if it exists, which we'll use over our default template if found. $page_template = self::get_page_template( $page ); $default_template = self::get_default_template( $page ); $template_content = $page_template ?: $default_template; // If at this point we have no content to migrate, bail. if ( ! $template_content ) { return self::set_has_migrated_page( $template_slug, 'no-content' ); } if ( self::create_custom_template( $block_template, $template_content ) ) { return self::set_has_migrated_page( $template_slug ); } } /** * Get template for a page following the page hierarchy. * * @param \WP_Post|null $page Page object. * @return string */ protected static function get_page_template( $page ) { $templates = array(); if ( $page && $page->ID ) { $template = get_page_template_slug( $page->ID ); if ( $template && 0 === validate_file( $template ) ) { $templates[] = $template; } $pagename = $page->post_name; if ( $pagename ) { $pagename_decoded = urldecode( $pagename ); if ( $pagename_decoded !== $pagename ) { $templates[] = "page-{$pagename_decoded}"; } $templates[] = "page-{$pagename}"; } } $block_template = false; foreach ( $templates as $template ) { $block_template = BlockTemplateUtils::get_block_template( get_stylesheet() . '//' . $template, 'wp_template' ); if ( $block_template && ! empty( $block_template->content ) ) { break; } } return $block_template ? $block_template->content : ''; } /** * Prepare default page template. * * @param \WP_Post $page Page object. * @return string */ protected static function get_default_template( $page ) { if ( ! $page || empty( $page->post_content ) ) { return ''; } $default_template_content = '