Repository navigation
Conversation
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.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
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.
Fixes a fatal "Undefined array key 0" in the editor when a plugin-registered block template is included in the current template list.
See https://core.trac.wordpress.org/ticket/64216
What
get_block_templates()merges plugin-registered templates into the query results witharray_merge().WP_Block_Templates_Registry::get_by_query()keys its result by template name, soarray_filter()/array_map()preserve those string keys and the merged array is no longer a list.wp-includes/block-editor.phpthen reads$current_template[0]->content, which is undefined, so the editor dies with "Undefined array key 0" when a plugin-registered template is among the matches.Why
Plugin-registered templates are a supported extension point, and selecting one as the current template must not fatal. The reporter's diagnosis and suggested fix — wrap the registered templates in
array_values()— is confirmed in code: the registry keys results by template name, so reindexing before the merge restores the list shape the editor expects.How
In
src/wp-includes/block-template-utils.php,array_merge( $query_result, $matching_registered_templates )becomesarray_merge( $query_result, array_values( $matching_registered_templates ) ). This reindexes the registered templates so the merged result is a list, matching the rest of the template API. No other behaviour changes.Adds
test_get_block_templates_should_return_a_list_when_a_plugin_registered_template_is_includedintests/phpunit/tests/blocks/getBlockTemplates.phpwith@ticket 64216, assertingarray_is_list(),assertCount( 1, ... )and$templates[0]->slug.Testing Instructions
vendor/bin/phpunit tests/phpunit/tests/blocks/getBlockTemplates.php→OK (8 tests, 26 assertions).git checkout <base> -- src/wp-includes/block-template-utils.php) and re-run →Tests: 8, Assertions: 24, Failures: 1, failing exactlytest_get_block_templates_should_return_a_list_when_a_plugin_registered_template_is_included. Restoring the fix makes the run green again.vendor/bin/phpunit --test-suffix .php tests/phpunit/tests/blocks/→OK (576 tests, 3151 assertions).vendor/bin/phpcs --standard=phpcs.xml.dist src/wp-includes/block-template-utils.php tests/phpunit/tests/blocks/getBlockTemplates.php→ exit 0.composer lintandcomposer phpstanshow only pre-existing violations/errors (identical on base and branch);npm run buildpasses.