Make WordPress Core


Ignore:
Timestamp:
03/07/2025 05:53:37 PM (8 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/tests/phpunit/tests/blocks/getBlockTemplates.php

    r58323 r59951  
    229229        );
    230230    }
     231
     232    /**
     233     * @dataProvider data_get_block_templates_should_not_leak_plugin_registered_templates_with_default_post_type_slugs
     234     * @ticket 62319
     235     *
     236     * @covers ::get_block_templates
     237     *
     238     * @param string $template_slug Default slug for the post type.
     239     * @param string $post_type     Post type for query.
     240     * @param array  $expected      Expected template IDs.
     241     */
     242    public function test_get_block_templates_should_not_leak_plugin_registered_templates_with_default_post_type_slugs( $template_slug, $post_type, $expected ) {
     243        $template_name = 'test-plugin//' . $template_slug;
     244        $template_args = array(
     245            'content'     => 'Template content',
     246            'title'       => 'Test Template for ' . $post_type,
     247            'description' => 'Description of test template',
     248            'post_types'  => array( $post_type ),
     249        );
     250        register_block_template( $template_name, $template_args );
     251
     252        $templates = get_block_templates( array( 'post_type' => $post_type ) );
     253
     254        $this->assertSameSets(
     255            $expected,
     256            $this->get_template_ids( $templates )
     257        );
     258
     259        unregister_block_template( $template_name );
     260    }
     261
     262    /**
     263     * Data provider.
     264     *
     265     * Make sure that plugin-registered templates with default post type slugs (ie: `single` or `page`)
     266     * don't leak into `get_block_templates()`.
     267     * See: https://core.trac.wordpress.org/ticket/62319.
     268     *
     269     * @return array
     270     */
     271    public function data_get_block_templates_should_not_leak_plugin_registered_templates_with_default_post_type_slugs() {
     272        return array(
     273            'post' => array(
     274                'template_slug' => 'single',
     275                'post_type'     => 'post',
     276                'expected'      => array(
     277                    'block-theme//custom-hero-template',
     278                    'block-theme//custom-single-post-template',
     279                ),
     280            ),
     281            'page' => array(
     282                'template_slug' => 'page',
     283                'post_type'     => 'page',
     284                'expected'      => array(
     285                    'block-theme//custom-hero-template',
     286                    'block-theme//page-home',
     287                ),
     288            ),
     289        );
     290    }
    231291}
Note: See TracChangeset for help on using the changeset viewer.