-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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 #6278
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. |
src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php
Show resolved
Hide resolved
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 |
tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php
Outdated
Show resolved
Hide resolved
tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php
Outdated
Show resolved
Hide resolved
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
$context
argument 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,$context
will be set to aWP_Block_Template
object 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
ignoredHookedBlocks
metadata attribute is set.Prior to this changeset, the
$context
passed 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 incomingPOST
network request that will trigger a database update.Those incoming changes are encapsulated in the
$changes
argument passed to thereset_pre_insert_template
andreset_pre_insert_template_part
filters, respectively, and thus to theinject_ignored_hooked_blocks_metadata_attributes
function that is hooked to them.$changes
is of typestdClass
and only contains the fields that need to be updated. That means that in order to create aWP_Block_Template
object, a two-step process is needed:wp_template
orwp_template_part
post object in the database will look like by merging$changes
on top of the existing$post
object fetched from the DB, or from the theme's block template (part) file, if any.WP_Block_Template
from 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_post
function. (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_attributes
no longer requires a$request
argument, which is reflected by unit tests no longer needing to create a$request
object to pass to it, thus decoupling the function from the templates endpoint controller.Unit tests for
inject_ignored_hooked_blocks_metadata_attributes
have 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.