-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -760,6 +760,29 @@ function get_hooked_blocks( $name ) { | |
return $hooked_blocks; | ||
} | ||
|
||
function insert_hooked_blocks( $block, $parent = null, $block_index = null, $chunk_index = null ) { | ||
$hooked_blocks = get_hooked_blocks( $block['blockName'] ); | ||
foreach ( $hooked_blocks as $hooked_block_type => $relative_position ) { | ||
$hooked_block = array( | ||
'blockName' => $hooked_block_type, | ||
'attrs' => array(), | ||
'innerHTML' => '', | ||
'innerContent' => array(), | ||
'innerBlocks' => array(), | ||
); | ||
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 ); | ||
Comment on lines
+773
to
+776
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add checks to make sure |
||
} elseif ( 'first_child' === $relative_position ) { | ||
prepend_inner_block( $block, $hooked_block ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One observation. In this scenario, I assume that By the way, for consistency, the variable could become There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
They're different, see https://github.com/WordPress/wordpress-develop/pull/5247/files#diff-8c99af92e4ec0fdb307ddd9b42be1e1ef1efe4a9f31287c23f346244dddd1ce9R763.
|
||
} elseif ( 'last_child' === $relative_position ) { | ||
append_inner_block( $block, $hooked_block ); | ||
} | ||
} | ||
return $block; | ||
} | ||
|
||
/** | ||
* Insert a parsed block into a parent block's inner blocks. | ||
* | ||
|
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 probablyafter
) insertion isn't going to work like this in combination with tree traversal and serialization 😕 The reason is that we're iterating overparent
'sinnerBlocks
array intraverse_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 removeinsert_inner_block
and friends. The bad news is that I'll have to change the function signature of the callback intraverse_and_serialize_blocks
once again 😬