From a0712d7758a1599e00325e95ef9360b8faab67b0 Mon Sep 17 00:00:00 2001 From: jorgefilipecosta Date: Mon, 8 Apr 2024 16:47:16 +0000 Subject: [PATCH] Honor template_hierarchy filters when creating a template in the Site Editor. Currently, in blocks themes it's possible to use the ${type}_template_hierarchy filter to alter the template hierarchy. However, those filters are not taken into consideration by the Choose a pattern popup screen that appears when creating a new template in the Site Editor, causing a mismatch between the editor and the frontend. Props aljullu, mukesh27, ntsekouras, jorgefilipecosta, gziolo. Fixes #60846. Built from https://develop.svn.wordpress.org/trunk@57944 git-svn-id: http://core.svn.wordpress.org/trunk@57441 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/block-template-utils.php | 21 ++++++++++++++++++--- wp-includes/version.php | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/wp-includes/block-template-utils.php b/wp-includes/block-template-utils.php index bd8dfeefb2..6062b1ccbf 100644 --- a/wp-includes/block-template-utils.php +++ b/wp-includes/block-template-utils.php @@ -1397,13 +1397,16 @@ function wp_generate_block_templates_export_file() { */ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '' ) { if ( 'index' === $slug ) { - return array( 'index' ); + /** This filter is documented in wp-includes/template.php */ + return apply_filters( 'index_template_hierarchy', array( 'index' ) ); } if ( $is_custom ) { - return array( 'page', 'singular', 'index' ); + /** This filter is documented in wp-includes/template.php */ + return apply_filters( 'page_template_hierarchy', array( 'page', 'singular', 'index' ) ); } if ( 'front-page' === $slug ) { - return array( 'front-page', 'home', 'index' ); + /** This filter is documented in wp-includes/template.php */ + return apply_filters( 'frontpage_template_hierarchy', array( 'front-page', 'home', 'index' ) ); } $matches = array(); @@ -1469,6 +1472,18 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = ' $template_hierarchy[] = 'singular'; } $template_hierarchy[] = 'index'; + + $template_type = ''; + if ( ! empty( $template_prefix ) ) { + list( $template_type ) = explode( '-', $template_prefix ); + } else { + list( $template_type ) = explode( '-', $slug ); + } + $valid_template_types = array( '404', 'archive', 'attachment', 'author', 'category', 'date', 'embed', 'frontpage', 'home', 'index', 'page', 'paged', 'privacypolicy', 'search', 'single', 'singular', 'tag', 'taxonomy' ); + if ( in_array( $template_type, $valid_template_types ) ) { + /** This filter is documented in wp-includes/template.php */ + return apply_filters( "{$template_type}_template_hierarchy", $template_hierarchy ); + } return $template_hierarchy; } diff --git a/wp-includes/version.php b/wp-includes/version.php index ea06992e6d..36571f33c1 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.6-alpha-57943'; +$wp_version = '6.6-alpha-57944'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.