Block Hooks API: Create helper function for creating WP_Block_Template object for $context#6278
Block Hooks API: Create helper function for creating WP_Block_Template object for $context#6278tjcafferkey wants to merge 111 commits into
Conversation
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. |
|
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 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. |
9e83ee0 to
e80bfe5
Compare
|
Rebased. |
|
I've added some more unit test coverage, moved tests for cc/ @gziolo |
|
I need to have another look at the code, because the refactoring applied has a lot of nuances. However, the approach taken makes perfect sense, so it looks nearly ready to go. |
Co-authored-by: Greg Ziółkowski <grzegorz@gziolo.pl>
2166745 to
f0d1818
Compare
|
Rebased to include the |
|
Committed to Core |
|
Backported to the 6.5 branch in https://core.trac.wordpress.org/changeset/58041. |
The
$contextargument passed to filters such ashooked_block_types,hooked_block, andhooked_block_{$hooked_block_type}allows them to conditionally insert a hooked block. If the anchor block is contained in a template or template part,$contextwill be set to aWP_Block_Templateobject reflecting that template or part.The aforementioned filters are applied when hooked block insertion is run upon reading a template (or part) from the DB (and before sending the template/part content with hooked blocks inserted over the REST API to the client), but also upon writing to the DB, as that's when the
ignoredHookedBlocksmetadata attribute is set.Prior to this changeset, the
$contextpassed to Block Hooks related filters in the latter case reflected the template/part that was already stored in the database (if any), which is a bug; instead, it needs to reflect the template/part that will result from the incomingPOSTnetwork request that will trigger a database update.Those incoming changes are encapsulated in the
$changesargument passed to thereset_pre_insert_templateandreset_pre_insert_template_partfilters, respectively, and thus to theinject_ignored_hooked_blocks_metadata_attributesfunction that is hooked to them.$changesis of typestdClassand only contains the fields that need to be updated. That means that in order to create aWP_Block_Templateobject, a two-step process is needed:wp_templateorwp_template_partpost object in the database will look like by merging$changeson top of the existing$postobject fetched from the DB, or from the theme's block template (part) file, if any.WP_Block_Templatefrom the resulting object.To achieve the latter, a new helper method (
_build_block_template_object_from_post_object) is extracted from the existing_build_block_template_result_from_postfunction. (The latter cannot be used directly as it includes a few database calls that will fail if no post object for the template has existed yet in the database.)While somewhat complicated to implement, the overall change allows for better separation of concerns and isolation of entities. This is visible e.g. in the fact that
inject_ignored_hooked_blocks_metadata_attributesno longer requires a$requestargument, which is reflected by unit tests no longer needing to create a$requestobject to pass to it, thus decoupling the function from the templates endpoint controller.Unit tests for
inject_ignored_hooked_blocks_metadata_attributeshave been moved to a new, separate file. Test coverage has been added such that now, all three relevant scenarios are covered:Trac ticket: https://core.trac.wordpress.org/ticket/60754
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.