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
This commit is contained in:
jorgefilipecosta 2024-04-08 16:47:16 +00:00
parent f4fb98f366
commit a0712d7758
2 changed files with 19 additions and 4 deletions

View File

@ -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;
}

View File

@ -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.