Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Copy link
Contributor Author

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 😬

} elseif ( 'after' === $relative_position ) {
insert_inner_block( $parent, $block_index + 1, $chunk_index, $hooked_block );
Comment on lines +773 to +776
Copy link
Contributor Author

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 😬

} elseif ( 'first_child' === $relative_position ) {
prepend_inner_block( $block, $hooked_block );
Copy link
Member

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.

Copy link
Contributor Author

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).

} elseif ( 'last_child' === $relative_position ) {
append_inner_block( $block, $hooked_block );
}
}
return $block;
}

/**
* Insert a parsed block into a parent block's inner blocks.
*
Expand Down