Repository navigation
Template Activation: fix array keys in get_block_templates() when merging registered templates - #80351
Template Activation: fix array keys in get_block_templates() when merging registered templates#80351vedant-ere wants to merge 1 commit into
Conversation
…ing registered templates gutenberg_get_block_templates() merges registered plugin templates into the query result with array_merge(). WP_Block_Templates_Registry::get_by_query() returns its matches keyed by the full registered template name (e.g. my-plugin//my-template), and array_merge() preserves those non-numeric string keys instead of renumbering them. When a plugin-registered template is the only match for a slug__in query, the result ends up keyed by that string instead of 0. Callers such as core's wp_get_post_content_block_attributes() read $templates[0] directly, so this produces "Undefined array key 0" and "Attempt to read property on null" warnings, which cascade into parse_blocks() deprecation notices when null content reaches the parser. Wrap the merge in array_values() so the result is always a zero-based list, matching what every consumer of get_block_templates() expects. See WordPress#80291
|
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @vedant-ere! In case you missed it, we'd love to have you join us in our Slack community. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
|
That experiment has been removed for now #82241 so I'm closing the issue for now. Thanks for the contribution. |
What?
Fixes the PHP warnings described in #80291, for the Template Activation experiment's copy of
get_block_templates().Why?
gutenberg_get_block_templates()(inlib/compat/wordpress-7.0/template-activate.php, hooked ontopre_get_block_templates) merges plugin-registered templates into the query result witharray_merge():WP_Block_Templates_Registry::get_by_query()returns its matches keyed by the full registered template name (e.g.my-plugin//my-template), not a numeric index.array_merge()preserves non-numeric string keys instead of renumbering them, so when a plugin-registered template is the only match for aslug__inquery,$query_resultends up looking like:instead of the expected:
Every caller of
get_block_templates()reads the first match as$templates[0](for example core'swp_get_post_content_block_attributes()). With a string-keyed result, that producesUndefined array key 0/Attempt to read property "content" on null, and the resultingnullflowing intoparse_blocks()then trips thepreg_match()/strlen()deprecation notices reported in the issue.How?
Wrap the merge in
array_values()so the result is always re-indexed from0, regardless of whether the match came from the DB query, a theme file, or the plugin registry.Testing Instructions
active_templatesexperiment.register_block_template( 'my-plugin//my-template', [...] ).get_block_templates( [ 'slug__in' => [ 'my-template' ] ] )and confirm the single result is$templates[0], not keyed by the registration name.phpunit/class-gutenberg-get-block-templates-registered-template-test.php— it fails before this patch and passes after.Note on scope
This only fixes the Template Activation experiment's copy of the function. The bug reported in #80291 by default (without the experiment enabled) comes from core's own
get_block_templates()inwp-includes/block-template-utils.php, which isn't duplicated in this plugin'slib/directory — that half needs a separate WordPress core Trac ticket and patch, tracked from the GitHub issue.