Make WordPress Core


Ignore:
Timestamp:
03/07/2025 05:53:37 PM (12 months ago)
Author:
flixos90
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template-utils.php

    r59819 r59951  
    11861186         */
    11871187        $query['slug__not_in'] = wp_list_pluck( $query_result, 'slug' );
    1188         $template_files        = _get_block_templates_files( $template_type, $query );
     1188        /*
     1189         * We need to unset the post_type query param because some templates
     1190         * would be excluded otherwise, like `page.html` when looking for
     1191         * `page` templates. We need all templates so we can exclude duplicates
     1192         * from plugin-registered templates.
     1193         * See: https://github.com/WordPress/gutenberg/issues/65584
     1194         */
     1195        $template_files_query = $query;
     1196        unset( $template_files_query['post_type'] );
     1197        $template_files = _get_block_templates_files( $template_type, $template_files_query );
    11891198        foreach ( $template_files as $template_file ) {
    1190             $query_result[] = _build_block_template_result_from_file( $template_file, $template_type );
     1199            // If the query doesn't specify a post type, or it does and the template matches the post type, add it.
     1200            if (
     1201                ! isset( $query['post_type'] ) ||
     1202                (
     1203                    isset( $template_file['postTypes'] ) &&
     1204                    in_array( $query['post_type'], $template_file['postTypes'], true )
     1205                )
     1206            ) {
     1207                $query_result[] = _build_block_template_result_from_file( $template_file, $template_type );
     1208            } elseif ( ! isset( $template_file['postTypes'] ) ) {
     1209                // The custom templates with no associated post types are available for all post types as long
     1210                // as they are not default templates.
     1211                $candidate              = _build_block_template_result_from_file( $template_file, $template_type );
     1212                $default_template_types = get_default_block_template_types();
     1213                if ( ! isset( $default_template_types[ $candidate->slug ] ) ) {
     1214                    $query_result[] = $candidate;
     1215                }
     1216            }
    11911217        }
    11921218
Note: See TracChangeset for help on using the changeset viewer.