From 8ab1d7ace8aba69fe2e4704727a6c5360316cb6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Sun, 3 Nov 2024 12:50:57 +0100 Subject: [PATCH] Add unit test to make sure plugin-registered template with default post type slugs aren't leaked by get_block_templates() --- src/wp-includes/block-template-utils.php | 3 +- .../tests/blocks/getBlockTemplates.php | 58 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 7b57c646629ab..e95adb6399d46 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1172,7 +1172,8 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' ) /* * We need to unset the post_type query param because some templates * would be excluded otherwise, like `page.html` when looking for - * `page` templates. + * `page` templates. We need all templates so we can exclude duplicates + * from plugin-registered templates. * See: https://github.com/WordPress/gutenberg/issues/65584 */ unset( $template_files_query['post_type'] ); diff --git a/tests/phpunit/tests/blocks/getBlockTemplates.php b/tests/phpunit/tests/blocks/getBlockTemplates.php index 4db9093aae4a2..341c93c10fa78 100644 --- a/tests/phpunit/tests/blocks/getBlockTemplates.php +++ b/tests/phpunit/tests/blocks/getBlockTemplates.php @@ -228,4 +228,62 @@ public function data_get_block_templates_should_respect_posttypes_property() { ), ); } + + /** + * @dataProvider data_get_block_templates_should_not_leak_plugin_registered_templates_with_default_post_type_slugs + * @ticket 62319 + * + * @param string $template_slug Default slug for the post type. + * @param string $post_type Post type for query. + * @param array $expected Expected template IDs. + */ + public function test_get_block_templates_should_not_leak_plugin_registered_templates_with_default_post_type_slugs( $template_slug, $post_type, $expected ) { + $template_name = 'test-plugin//' . $template_slug; + $template_args = array( + 'content' => 'Template content', + 'title' => 'Test Template for ' . $post_type, + 'description' => 'Description of test template', + 'post_types' => array( $post_type ), + ); + register_block_template( $template_name, $template_args ); + + $templates = get_block_templates( array( 'post_type' => $post_type ) ); + + $this->assertSameSets( + $expected, + $this->get_template_ids( $templates ) + ); + + unregister_block_template( $template_name ); + } + + /** + * Data provider. + * + * Make sure that plugin-registered templates with default post type slugs (ie: `single` or `page`) + * don't leak into `get_block_templates()`. + * See: https://core.trac.wordpress.org/ticket/62319. + * + * @return array + */ + public function data_get_block_templates_should_not_leak_plugin_registered_templates_with_default_post_type_slugs() { + return array( + 'post' => array( + 'template_slug' => 'single', + 'post_type' => 'post', + 'expected' => array( + 'block-theme//custom-hero-template', + 'block-theme//custom-single-post-template', + ), + ), + 'page' => array( + 'template_slug' => 'page', + 'post_type' => 'page', + 'expected' => array( + 'block-theme//custom-hero-template', + 'block-theme//page-home', + ), + ), + ); + } }