From 798991a0ff4684363c11bb7c2598dcef203b85e1 Mon Sep 17 00:00:00 2001 From: Bernhard Reiter Date: Tue, 31 Oct 2023 19:25:19 +0000 Subject: [PATCH] Block Hooks: Allow traversal callbacks to modify parent block. The callbacks returned by `make_before_block_visitor` and `make_after_block_visitor`, respectively, (which are passed as arguments to `traverse_and_serialize_block(s)`) currently accept three arguments, all of which are block arrays (i.e. with properties `blockName`, `attrs`, etc.): - A ''reference'' to the block they're currently visiting, `&$block`; - the block's `$parent_block`; and - the `$prev`ious block (for `make_before_block_visitor`), or the `$next` block (for `make_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 the `theme` attribute into Template Part blocks. One major limitation of 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 from the database). For WordPress 6.5, it is planned to change that to make Block Hooks work for templates, parts, and patterns that ''do'' have user modifications: #59646. This will be implemented by storing an attribute on the "anchor" block. While working on that feature, it was found that the aforementioned callbacks will need to modify not only the currently visited `$block`, but also the `$parent_block` -- i.e. that the latter argument needs to be passed by reference as well. 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 of `before` or `after` sibling insertion), but also its parent (for `first_child` and `last_child` insertion). If the `$parent_block` argument were to be changed to become a reference in a later WordPress version, this could be considered a backwards-compatibility breaking change. For this reason, this change is instead proposed for 6.4 already, which is the cycle during which the relevant functions were first introduced. This should have no impact on existing code, since nothing currently relies on `$parent_block` remaining unmodified by the respective callback, nor is anything currently modifying that argument. Props hellofromTonya. Fixes #59776. Built from https://develop.svn.wordpress.org/trunk@57038 git-svn-id: http://core.svn.wordpress.org/trunk@56549 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/blocks.php | 28 +++++++++++++++------------- wp-includes/version.php | 2 +- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index 1edc0762d3..52b20b3e55 100644 --- a/wp-includes/blocks.php +++ b/wp-includes/blocks.php @@ -780,12 +780,12 @@ function make_before_block_visitor( $hooked_blocks, $context ) { * Furthermore, prepend the markup for any blocks hooked `before` the given block and as its parent's * `first_child`, respectively, to the serialized markup for the given block. * - * @param array $block The block to inject the theme attribute into, and hooked blocks before. - * @param array $parent_block The parent block of the given block. - * @param array $prev The previous sibling block of the given block. + * @param array $block The block to inject the theme attribute into, and hooked blocks before. Passed by reference. + * @param array $parent_block The parent block of the given block. Passed by reference. Default null. + * @param array $prev The previous sibling block of the given block. Default null. * @return string The serialized markup for the given block, with the markup for any hooked blocks prepended to it. */ - return function ( &$block, $parent_block = null, $prev = null ) use ( $hooked_blocks, $context ) { + return function ( &$block, &$parent_block = null, $prev = null ) use ( $hooked_blocks, $context ) { _inject_theme_attribute_in_template_part_block( $block ); $markup = ''; @@ -853,12 +853,12 @@ function make_after_block_visitor( $hooked_blocks, $context ) { * Append the markup for any blocks hooked `after` the given block and as its parent's * `last_child`, respectively, to the serialized markup for the given block. * - * @param array $block The block to inject the hooked blocks after. - * @param array $parent_block The parent block of the given block. - * @param array $next The next sibling block of the given block. + * @param array $block The block to inject the hooked blocks after. Passed by reference. + * @param array $parent_block The parent block of the given block. Passed by reference. Default null. + * @param array $next The next sibling block of the given block. Default null. * @return string The serialized markup for the given block, with the markup for any hooked blocks appended to it. */ - return function ( &$block, $parent_block = null, $next = null ) use ( $hooked_blocks, $context ) { + return function ( &$block, &$parent_block = null, $next = null ) use ( $hooked_blocks, $context ) { $markup = ''; $relative_position = 'after'; @@ -1065,7 +1065,7 @@ function traverse_and_serialize_block( $block, $pre_callback = null, $post_callb $block_content .= call_user_func_array( $pre_callback, - array( &$inner_block, $block, $prev ) + array( &$inner_block, &$block, $prev ) ); } @@ -1076,7 +1076,7 @@ function traverse_and_serialize_block( $block, $pre_callback = null, $post_callb $post_markup = call_user_func_array( $post_callback, - array( &$inner_block, $block, $next ) + array( &$inner_block, &$block, $next ) ); } @@ -1131,7 +1131,9 @@ function traverse_and_serialize_block( $block, $pre_callback = null, $post_callb * @return string Serialized block markup. */ function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_callback = null ) { - $result = ''; + $result = ''; + $parent_block = null; // At the top level, there is no parent block to pass to the callbacks; yet the callbacks expect a reference. + foreach ( $blocks as $index => $block ) { if ( is_callable( $pre_callback ) ) { $prev = 0 === $index @@ -1140,7 +1142,7 @@ function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_cal $result .= call_user_func_array( $pre_callback, - array( &$block, null, $prev ) // At the top level, there is no parent block to pass to the callback. + array( &$block, &$parent_block, $prev ) ); } @@ -1151,7 +1153,7 @@ function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_cal $post_markup = call_user_func_array( $post_callback, - array( &$block, null, $next ) // At the top level, there is no parent block to pass to the callback. + array( &$block, &$parent_block, $next ) ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index aed69359f3..755ac5a0c8 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.5-alpha-57036'; +$wp_version = '6.5-alpha-57038'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.