-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Blocks: Implement insert_hooked_blocks() #5247
Conversation
if ( 'before' === $relative_position ) { | ||
insert_inner_block( $parent, $block_index, $chunk_index, $hooked_block ); | ||
} elseif ( 'after' === $relative_position ) { | ||
insert_inner_block( $parent, $block_index + 1, $chunk_index, $hooked_block ); |
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.
Add checks to make sure $parent
(and $block_index
and $chunk_index
) exist -- 'cause they don't if $block
is at the top level of the tree 😬
3d9792a
to
65f8d2d
Compare
} elseif ( 'after' === $relative_position ) { | ||
insert_inner_block( $parent, $block_index + 1, $chunk_index, $hooked_block ); | ||
} elseif ( 'first_child' === $relative_position ) { | ||
prepend_inner_block( $block, $hooked_block ); |
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.
One observation. In this scenario, I assume that $parent
is the same as $block
. The code would be easier to follow if that was explicitly expresses as $parent
.
By the way, for consistency, the variable could become $parent_block
.
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.
One observation. In this scenario, I assume that
$parent
is the same as$block
.
They're different, see https://github.com/WordPress/wordpress-develop/pull/5247/files#diff-8c99af92e4ec0fdb307ddd9b42be1e1ef1efe4a9f31287c23f346244dddd1ce9R763.
$block
is the block that tree traversal is currently processing, $parent
is its parent. First child/last child insertion thus needs to happen to $block
(whereas sibling insertion needs to happen to $parent
's innerBlocks
; however, note my other comment).
'innerBlocks' => array(), | ||
); | ||
if ( 'before' === $relative_position ) { | ||
insert_inner_block( $parent, $block_index, $chunk_index, $hooked_block ); |
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.
I had a realization last night that before
(and probably after
) insertion isn't going to work like this in combination with tree traversal and serialization 😕 The reason is that we're iterating over parent
's innerBlocks
array in traverse_and_serialize_blocks
when we encounter this line, which then attempts to modify that array (by inserting another element before or after the current one, respectively).
I came up with an idea how to fix that. The good news is that if it works, we'll be able to get rid of our $chunk_index
arithmetics, and even remove insert_inner_block
and friends. The bad news is that I'll have to change the function signature of the callback in traverse_and_serialize_blocks
once again 😬
I assume we no longer need it after landing an alternative version. I see now that https://core.trac.wordpress.org/ticket/59399 is closed, so I will close this one, too. |
WIP. See
#59313
and #5158.For
#59313
, we need a function that will plug into parsed block tree traversal and serialization (#5246). For each block traversed, it will obtain the list of all hooked blocks that use it as their anchor block (get_hooked_blocks
, #5239), and insert those hooked blocks into the parsed block tree (insert_inner_block
,prepend_inner_block
,append_inner_block
, #5240).TODO:
For early testing, use the Like Button block and apply the following diff:
Trac ticket: https://core.trac.wordpress.org/ticket/59399
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.