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/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 ); } 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. *