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

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Sep 19, 2023

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:

  • Add test coverage for all four possible relative insertion positions.

For early testing, use the Like Button block and apply the following diff:

diff --git a/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php
index a83b35185b..7f617dbbea 100644
--- a/src/wp-includes/class-wp-block-patterns-registry.php
+++ b/src/wp-includes/class-wp-block-patterns-registry.php
@@ -165,7 +165,10 @@ final class WP_Block_Patterns_Registry {
                        return null;
                }
 
-               return $this->registered_patterns[ $pattern_name ];
+               $pattern            = $this->registered_patterns[ $pattern_name ];
+               $blocks             = parse_blocks( $pattern['content'] );
+               $pattern['content'] = traverse_and_serialize_blocks( $blocks, 'insert_hooked_blocks' );
+               return $pattern;
        }
 
        /**

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.

@ockham ockham self-assigned this Sep 19, 2023
Comment on lines +773 to +776
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 );
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 😬

@ockham ockham assigned ockham and gziolo and unassigned ockham Sep 19, 2023
} 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 );
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).

'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 😬

@gziolo
Copy link
Member

gziolo commented Sep 22, 2023

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.

@gziolo gziolo closed this Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants