From 209d5dda54bd18995a75b667000826c4a102cf09 Mon Sep 17 00:00:00 2001 From: sarequl Date: Tue, 10 Nov 2026 03:54:37 +0000 Subject: [PATCH 1/2] Block Templates: Preserve list shape when merging registered templates WP_Block_Templates_Registry::get_by_query() returns templates keyed by template name. array_merge() preserves string keys, so a query that matched only a plugin-registered template returned a non-list array and block-editor.php could not read $current_template[0]. Wrap the registered templates in array_values() so the merged result stays a list. See #64216. --- src/wp-includes/block-template-utils.php | 2 +- .../tests/blocks/getBlockTemplates.php | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index c883ac73306a0..1687bdae800eb 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1270,7 +1270,7 @@ function ( $template ) { $matching_registered_templates ); - $query_result = array_merge( $query_result, $matching_registered_templates ); + $query_result = array_merge( $query_result, array_values( $matching_registered_templates ) ); } } diff --git a/tests/phpunit/tests/blocks/getBlockTemplates.php b/tests/phpunit/tests/blocks/getBlockTemplates.php index 7bbb5e5b06a2f..caeae9cf466e2 100644 --- a/tests/phpunit/tests/blocks/getBlockTemplates.php +++ b/tests/phpunit/tests/blocks/getBlockTemplates.php @@ -275,6 +275,33 @@ public function test_get_block_templates_should_not_leak_plugin_registered_templ unregister_block_template( $template_name ); } + /** + * Plugin-registered templates must be merged into a list so that callers + * can reliably access the first result by index. + * + * @ticket 64216 + * + * @covers ::get_block_templates + */ + public function test_get_block_templates_should_return_a_list_when_a_plugin_registered_template_is_included() { + $template_name = 'test-plugin//single-post-64216'; + $template_args = array( + 'content' => 'Template content', + 'title' => 'Test Template', + 'description' => 'Description of test template', + 'post_types' => array( 'post' ), + ); + register_block_template( $template_name, $template_args ); + + $templates = get_block_templates( array( 'slug__in' => array( 'single-post-64216' ) ) ); + + $this->assertTrue( array_is_list( $templates ) ); + $this->assertCount( 1, $templates ); + $this->assertSame( 'single-post-64216', $templates[0]->slug ); + + unregister_block_template( $template_name ); + } + /** * Data provider. * From 458ea8bd05a000ce3a8100a4a910f0e603835937 Mon Sep 17 00:00:00 2001 From: sarequl Date: Tue, 10 Nov 2026 04:26:57 +0000 Subject: [PATCH 2/2] Tests: Update registry template assertion for list shape get_block_templates() now returns a sequentially indexed list, so a registered template is no longer keyed by its full name. Assert the template is present via its plugin instead of relying on the old string key, which also prevented the registration from being cleaned up when the assertion failed. --- tests/phpunit/tests/block-template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/block-template.php b/tests/phpunit/tests/block-template.php index a70f60f86cc89..f851505ca0fc0 100644 --- a/tests/phpunit/tests/block-template.php +++ b/tests/phpunit/tests/block-template.php @@ -655,7 +655,7 @@ public function test_get_block_templates_from_registry() { $templates = get_block_templates(); - $this->assertArrayHasKey( $template_name, $templates ); + $this->assertContains( 'test-plugin', wp_list_pluck( $templates, 'plugin' ) ); unregister_block_template( $template_name ); }