From 790aff42aa16d1e9f765956dd38076ab5fb17647 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 7 Mar 2025 17:55:24 +0000 Subject: [PATCH] Editor: Fix unexpected behavior due to conflicting custom block template. This changeset fixes both a visual and functional bug related to template selection in the editor that occurred when having a custom block template registered that was using the same slug as another block template already registered by the theme, including the default block templates. Props aljullu, antonvlasenko, apermo, audrasjb, azaozz, ntsekouras. Fixes #62319. Built from https://develop.svn.wordpress.org/trunk@59951 git-svn-id: http://core.svn.wordpress.org/trunk@59293 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/block-template-utils.php | 30 ++++++++++++++++++++++++++-- wp-includes/version.php | 2 +- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/wp-includes/block-template-utils.php b/wp-includes/block-template-utils.php index 89ccada17e..faea7d5e83 100644 --- a/wp-includes/block-template-utils.php +++ b/wp-includes/block-template-utils.php @@ -1185,9 +1185,35 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' ) * over the theme-provided ones, so we skip querying and building them. */ $query['slug__not_in'] = wp_list_pluck( $query_result, 'slug' ); - $template_files = _get_block_templates_files( $template_type, $query ); + /* + * We need to unset the post_type query param because some templates + * would be excluded otherwise, like `page.html` when looking for + * `page` templates. We need all templates so we can exclude duplicates + * from plugin-registered templates. + * See: https://github.com/WordPress/gutenberg/issues/65584 + */ + $template_files_query = $query; + unset( $template_files_query['post_type'] ); + $template_files = _get_block_templates_files( $template_type, $template_files_query ); foreach ( $template_files as $template_file ) { - $query_result[] = _build_block_template_result_from_file( $template_file, $template_type ); + // If the query doesn't specify a post type, or it does and the template matches the post type, add it. + if ( + ! isset( $query['post_type'] ) || + ( + isset( $template_file['postTypes'] ) && + in_array( $query['post_type'], $template_file['postTypes'], true ) + ) + ) { + $query_result[] = _build_block_template_result_from_file( $template_file, $template_type ); + } elseif ( ! isset( $template_file['postTypes'] ) ) { + // The custom templates with no associated post types are available for all post types as long + // as they are not default templates. + $candidate = _build_block_template_result_from_file( $template_file, $template_type ); + $default_template_types = get_default_block_template_types(); + if ( ! isset( $default_template_types[ $candidate->slug ] ) ) { + $query_result[] = $candidate; + } + } } if ( 'wp_template' === $template_type ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index 8496a1273d..81b42852ed 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.8-beta1-59950'; +$wp_version = '6.8-beta1-59951'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.