From 657b0a814f24e4b6391b80b53b8f2a2d038b5695 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Tue, 29 Oct 2024 10:50:25 +0100 Subject: [PATCH 01/12] Fix flash when clicking on template name when a plugin registered template matches a default WP theme template --- src/wp-includes/block-template-utils.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index f974f6efbd9a5..415016358069c 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1167,10 +1167,16 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' ) * If the query has found some user templates, those have priority * 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 ); + $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 ( + ! isset( $query['post_type'] ) || + ( 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 ); + } } if ( 'wp_template' === $template_type ) { From 1d1af57b3b7e1c86b98a94ab5479125b1b516e65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Sun, 3 Nov 2024 12:26:29 +0100 Subject: [PATCH 02/12] Fix duplicate templates between database and theme --- src/wp-includes/block-template-utils.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 415016358069c..839910576009d 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1163,11 +1163,18 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' ) } if ( ! isset( $query['wp_id'] ) ) { + $template_files_query = $query; /* * If the query has found some user templates, those have priority * over the theme-provided ones, so we skip querying and building them. */ - $template_files_query = $query; + $template_files_query['slug__not_in'] = wp_list_pluck( $query_result, 'slug' ); + /* + * We need to unset the post_type query param because some templates + * would be excluded otherwise, like `page.html` when looking for + * `page` templates. + * See: https://github.com/WordPress/gutenberg/issues/65584 + */ unset( $template_files_query['post_type'] ); $template_files = _get_block_templates_files( $template_type, $template_files_query ); foreach ( $template_files as $template_file ) { From d32b25ef7b06812067f0aac4b845fbc30ae6c4ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Sun, 3 Nov 2024 12:38:10 +0100 Subject: [PATCH 03/12] Make sure the custom templates with no associated post types are available for all post types --- src/wp-includes/block-template-utils.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 839910576009d..7b57c646629ab 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1184,6 +1184,16 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' ) ) { $query_result[] = _build_block_template_result_from_file( $template_file, $template_type ); } + + // The custom templates with no associated post types are available for all post types. + if ( isset( $query['post_type'] ) && ! isset( $template_file['postTypes'] ) ) { + $candidate = _build_block_template_result_from_file( $template_file, $template_type ); + $default_template_types = get_default_block_template_types(); + $is_custom = ! isset( $default_template_types[ $candidate->slug ] ); + if ( $is_custom ) { + $query_result[] = $candidate; + } + } } if ( 'wp_template' === $template_type ) { 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 04/12] 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', + ), + ), + ); + } } From 1ce805e031dcdf91cad002c4e6b3f0eccce9c708 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Fri, 8 Nov 2024 08:42:28 +0100 Subject: [PATCH 05/12] Update src/wp-includes/block-template-utils.php Co-authored-by: Christoph Daum --- src/wp-includes/block-template-utils.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index e95adb6399d46..b6746e4b626d0 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1190,8 +1190,7 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' ) if ( isset( $query['post_type'] ) && ! isset( $template_file['postTypes'] ) ) { $candidate = _build_block_template_result_from_file( $template_file, $template_type ); $default_template_types = get_default_block_template_types(); - $is_custom = ! isset( $default_template_types[ $candidate->slug ] ); - if ( $is_custom ) { + if ( ! isset( $default_template_types[ $candidate->slug ] ) ) { $query_result[] = $candidate; } } From 8ef61a62f8be2c167b456e9007183d15ba25b037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Tue, 26 Nov 2024 14:23:24 +0100 Subject: [PATCH 06/12] Use 'elseif' instead of two separate 'if' --- src/wp-includes/block-template-utils.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index b6746e4b626d0..4c446af5c6ac5 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1179,20 +1179,17 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' ) unset( $template_files_query['post_type'] ); $template_files = _get_block_templates_files( $template_type, $template_files_query ); foreach ( $template_files as $template_file ) { - if ( - ! isset( $query['post_type'] ) || - ( 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 ); - } - - // The custom templates with no associated post types are available for all post types. - if ( isset( $query['post_type'] ) && ! isset( $template_file['postTypes'] ) ) { + if ( isset( $query['post_type'] ) && ! isset( $template_file['postTypes'] ) ) { // The custom templates with no associated post types are available for all post types. $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; } + } elseif ( + ! isset( $query['post_type'] ) || + ( 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 ); } } From 0cfab7c8d385656e94c88645f9f06299fec134c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Tue, 26 Nov 2024 14:36:09 +0100 Subject: [PATCH 07/12] Add an extra comment --- src/wp-includes/block-template-utils.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 4c446af5c6ac5..586851b8cccb6 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1179,13 +1179,15 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' ) unset( $template_files_query['post_type'] ); $template_files = _get_block_templates_files( $template_type, $template_files_query ); foreach ( $template_files as $template_file ) { - if ( isset( $query['post_type'] ) && ! isset( $template_file['postTypes'] ) ) { // The custom templates with no associated post types are available for all post types. + // The custom templates with no associated post types are available for all post types. + if ( isset( $query['post_type'] ) && ! isset( $template_file['postTypes'] ) ) { $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; } } elseif ( + // If the query doesn't specify a post type, or it does and the template has the post type, add it. ! isset( $query['post_type'] ) || ( isset( $query['post_type'] ) && isset( $template_file['postTypes'] ) && in_array( $query['post_type'], $template_file['postTypes'], true ) ) ) { From b5b24aef82376a0ad9272028e1cee94c8ece399b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Wed, 27 Nov 2024 08:32:01 +0100 Subject: [PATCH 08/12] Update src/wp-includes/block-template-utils.php Co-authored-by: Andrew Ozz <743931+azaozz@users.noreply.github.com> --- src/wp-includes/block-template-utils.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 586851b8cccb6..c3379dc360a8f 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1189,7 +1189,11 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' ) } elseif ( // If the query doesn't specify a post type, or it does and the template has the post type, add it. ! isset( $query['post_type'] ) || - ( isset( $query['post_type'] ) && isset( $template_file['postTypes'] ) && in_array( $query['post_type'], $template_file['postTypes'], true ) ) + ( + 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 ); } From cd07c085ba1c1c5e597b01596fbff44e385be4ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Wed, 4 Dec 2024 16:38:47 +0100 Subject: [PATCH 09/12] Make sure '['slug__not_in']' is being updated --- src/wp-includes/block-template-utils.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index c3379dc360a8f..d8562764447a1 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1163,12 +1163,11 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' ) } if ( ! isset( $query['wp_id'] ) ) { - $template_files_query = $query; /* * If the query has found some user templates, those have priority * over the theme-provided ones, so we skip querying and building them. */ - $template_files_query['slug__not_in'] = wp_list_pluck( $query_result, 'slug' ); + $query['slug__not_in'] = wp_list_pluck( $query_result, 'slug' ); /* * We need to unset the post_type query param because some templates * would be excluded otherwise, like `page.html` when looking for @@ -1176,6 +1175,7 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' ) * 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 ) { From 5cea08f4d6c186909daf9bfdbbd7828f1c437186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Thu, 5 Dec 2024 17:41:59 +0100 Subject: [PATCH 10/12] Refactor if ... else ... --- src/wp-includes/block-template-utils.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index d8562764447a1..263cbc729b122 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1179,23 +1179,23 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' ) unset( $template_files_query['post_type'] ); $template_files = _get_block_templates_files( $template_type, $template_files_query ); foreach ( $template_files as $template_file ) { - // The custom templates with no associated post types are available for all post types. - if ( isset( $query['post_type'] ) && ! isset( $template_file['postTypes'] ) ) { - $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; - } - } elseif ( - // If the query doesn't specify a post type, or it does and the template has the post type, add it. + // 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( $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; + } } } From 7e48e6e89a8e8d4e41ee4f1e2220d61af80bb121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Thu, 5 Dec 2024 17:43:17 +0100 Subject: [PATCH 11/12] Update tests/phpunit/tests/blocks/getBlockTemplates.php Co-authored-by: Anton Vlasenko <43744263+anton-vlasenko@users.noreply.github.com> --- tests/phpunit/tests/blocks/getBlockTemplates.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/blocks/getBlockTemplates.php b/tests/phpunit/tests/blocks/getBlockTemplates.php index 341c93c10fa78..07706e11bd42a 100644 --- a/tests/phpunit/tests/blocks/getBlockTemplates.php +++ b/tests/phpunit/tests/blocks/getBlockTemplates.php @@ -232,6 +232,8 @@ 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 + + @covers ::get_block_templates * * @param string $template_slug Default slug for the post type. * @param string $post_type Post type for query. From b8e5c50f56770762ad77da90b63fc596bd90e2bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Wed, 11 Dec 2024 09:53:08 +0100 Subject: [PATCH 12/12] Fix comment format --- tests/phpunit/tests/blocks/getBlockTemplates.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/blocks/getBlockTemplates.php b/tests/phpunit/tests/blocks/getBlockTemplates.php index 07706e11bd42a..791f449a163c5 100644 --- a/tests/phpunit/tests/blocks/getBlockTemplates.php +++ b/tests/phpunit/tests/blocks/getBlockTemplates.php @@ -232,8 +232,8 @@ 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 - - @covers ::get_block_templates + * + * @covers ::get_block_templates * * @param string $template_slug Default slug for the post type. * @param string $post_type Post type for query.