-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Block Hooks: Allow traversal callbacks to modify parent block #5598
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
Closed
ockham
wants to merge
7
commits into
WordPress:trunk
from
ockham:update/block-hooks-pass-parent-by-reference
Closed
Block Hooks: Allow traversal callbacks to modify parent block #5598
ockham
wants to merge
7
commits into
WordPress:trunk
from
ockham:update/block-hooks-pass-parent-by-reference
+15
−13
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hellofromtonya
approved these changes
Oct 31, 2023
Contributor
hellofromtonya
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As explained, these changes are needed as continued iteration for 6.5 revealed pass by reference is required. Doing these changes avoids a BC break in the 6.5 cycle.
The change also includes a little bit of girl scouting to improve the @param documentation.
Contributor
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
tl;dr: In 6.5, we'll need arguments to two functions introduced during the 6.4 cycle to become references. Since that can be deemed a back-compat breaking change, I'm suggesting to make that change already.
The callbacks returned by
make_before_block_visitorandmake_after_block_visitor, respectively (which are passed as arguments totraverse_and_serialize_block(s)) currently accept three arguments, all of which are block arrays (i.e. with propertiesblockName,attrs, etc.):&$block;$parent_block; and$previous block (formake_before_block_visitor), or the$nextblock (formake_after_block_visitor), respectively.Those arguments are passed to the "block visitor" callbacks by
traverse_and_serialize_block(s)during traversal. The block that the callback is currently visiting is passed by reference to allow modifying it, which is e.g. used to inject thethemeattribute into Template Part blocks.One major limitation with Block Hooks is that they currently only work with templates, parts, and patterns that don't have any user modifications (i.e. that come straight from the corresponding theme files, rather than the DB). For WP 6.5, we're planning to change that to also make Block Hooks work for templates, parts, and patterns that do have user modifications:
#59646.The way we'll make this work is by storing an attribute on the "anchor" block. This is currently being developed in #5523.
While working on that PR, I noticed that that means that we'll also need to allow the aforementioned callbacks to modify not only the currently visited
$block, but also the$parent_block-- i.e. to pass the latter argument by reference, too. This is consistent with the requirement of adding an attribute to an anchor block, as it's not only the currently visited block that can serve as an anchor block (in the case ofbeforeoraftersibling insertion), but also its parent (forfirst_childandlast_childinsertion).Per discussion with @hellofromtonya, changing the
$parent_blockargument to become a reference can be considered a backwards-compatibility breaking change. I'm thus suggesting to make that change already as part of 6.4, which is the cycle where the relevant functions were first introduced. This should have no impact on existing code, since nothing currently relies on$parent_blockremaining unmodified by the respective callback, nor is anything currently modifying that argument.Trac ticket: https://core.trac.wordpress.org/ticket/59776
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.