%2$s
%4$s ', esc_html( $block_template->title ), __( 'This page is blank because the template is empty. You can reset or customize it in the Site Editor.' ), get_edit_post_link( $block_template->wp_id, 'site-editor' ), __( 'Edit template' ) ); } /** * Finds a block template with equal or higher specificity than a given PHP template file. * * Internally, this communicates the block content that needs to be used by the template canvas through a global variable. * * @since 5.8.0 * @since 6.3.0 Added `$_wp_current_template_id` global for editing of current template directly from the admin bar. * * @global string $_wp_current_template_content * @global string $_wp_current_template_id * * @param string $template Path to the template. See locate_template(). * @param string $type Sanitized filename without extension. * @param string[] $templates A list of template candidates, in descending order of priority. * @return string The path to the Site Editor template canvas file, or the fallback PHP template. */ function locate_block_template( $template, $type, array $templates ) { global $_wp_current_template_content, $_wp_current_template_id; if ( ! current_theme_supports( 'block-templates' ) ) { return $template; } if ( $template ) { /* * locate_template() has found a PHP template at the path specified by $template. * That means that we have a fallback candidate if we cannot find a block template * with higher specificity. * * Thus, before looking for matching block themes, we shorten our list of candidate * templates accordingly. */ // Locate the index of $template (without the theme directory path) in $templates. $relative_template_path = str_replace( array( get_stylesheet_directory() . '/', get_template_directory() . '/' ), '', $template ); $index = array_search( $relative_template_path, $templates, true ); // If the template hierarchy algorithm has successfully located a PHP template file, // we will only consider block templates with higher or equal specificity. $templates = array_slice( $templates, 0, $index + 1 ); } $block_template = resolve_block_template( $type, $templates, $template ); if ( $block_template ) { $_wp_current_template_id = $block_template->id; if ( empty( $block_template->content ) ) { if ( is_user_logged_in() ) { $_wp_current_template_content = wp_render_empty_block_template_warning( $block_template ); } else { if ( $block_template->has_theme_file ) { // Show contents from theme template if user is not logged in. $theme_template = _get_block_template_file( 'wp_template', $block_template->slug ); $_wp_current_template_content = file_get_contents( $theme_template['path'] ); } else { $_wp_current_template_content = $block_template->content; } } } elseif ( ! empty( $block_template->content ) ) { $_wp_current_template_content = $block_template->content; } if ( isset( $_GET['_wp-find-template'] ) ) { wp_send_json_success( $block_template ); } } else { if ( $template ) { return $template; } if ( 'index' === $type ) { if ( isset( $_GET['_wp-find-template'] ) ) { wp_send_json_error( array( 'message' => __( 'No matching template found.' ) ) ); } } else { return ''; // So that the template loader keeps looking for templates. } } // Add hooks for template canvas. // Add viewport meta tag. add_action( 'wp_head', '_block_template_viewport_meta_tag', 0 ); // Render title tag with content, regardless of whether theme has title-tag support. remove_action( 'wp_head', '_wp_render_title_tag', 1 ); // Remove conditional title tag rendering... add_action( 'wp_head', '_block_template_render_title_tag', 1 ); // ...and make it unconditional. // This file will be included instead of the theme's template file. return ABSPATH . WPINC . '/template-canvas.php'; } /** * Returns the correct 'wp_template' to render for the request template type. * * @access private * @since 5.8.0 * @since 5.9.0 Added the `$fallback_template` parameter. * * @param string $template_type The current template type. * @param string[] $template_hierarchy The current template hierarchy, ordered by priority. * @param string $fallback_template A PHP fallback template to use if no matching block template is found. * @return WP_Block_Template|null template A template object, or null if none could be found. */ function resolve_block_template( $template_type, $template_hierarchy, $fallback_template ) { if ( ! $template_type ) { return null; } if ( empty( $template_hierarchy ) ) { $template_hierarchy = array( $template_type ); } $slugs = array_map( '_strip_template_file_suffix', $template_hierarchy ); // Find all potential templates 'wp_template' post matching the hierarchy. $query = array( 'slug__in' => $slugs, ); $templates = get_block_templates( $query ); // Order these templates per slug priority. // Build map of template slugs to their priority in the current hierarchy. $slug_priorities = array_flip( $slugs ); usort( $templates, static function ( $template_a, $template_b ) use ( $slug_priorities ) { return $slug_priorities[ $template_a->slug ] - $slug_priorities[ $template_b->slug ]; } ); $theme_base_path = get_stylesheet_directory() . DIRECTORY_SEPARATOR; $parent_theme_base_path = get_template_directory() . DIRECTORY_SEPARATOR; // Is the active theme a child theme, and is the PHP fallback template part of it? if ( str_starts_with( $fallback_template, $theme_base_path ) && ! str_contains( $fallback_template, $parent_theme_base_path ) ) { $fallback_template_slug = substr( $fallback_template, // Starting position of slug. strpos( $fallback_template, $theme_base_path ) + strlen( $theme_base_path ), // Remove '.php' suffix. -4 ); // Is our candidate block template's slug identical to our PHP fallback template's? if ( count( $templates ) && $fallback_template_slug === $templates[0]->slug && 'theme' === $templates[0]->source ) { // Unfortunately, we cannot trust $templates[0]->theme, since it will always // be set to the active theme's slug by _build_block_template_result_from_file(), // even if the block template is really coming from the active theme's parent. // (The reason for this is that we want it to be associated with the active theme // -- not its parent -- once we edit it and store it to the DB as a wp_template CPT.) // Instead, we use _get_block_template_file() to locate the block template file. $template_file = _get_block_template_file( 'wp_template', $fallback_template_slug ); if ( $template_file && get_template() === $template_file['theme'] ) { // The block template is part of the parent theme, so we // have to give precedence to the child theme's PHP template. array_shift( $templates ); } } } return count( $templates ) ? $templates[0] : null; } /** * Displays title tag with content, regardless of whether theme has title-tag support. * * @access private * @since 5.8.0 * * @see _wp_render_title_tag() */ function _block_template_render_title_tag() { echo '