Make WordPress Core


Ignore:
Timestamp:
10/31/2023 07:23:25 PM (7 months ago)
Author:
Bernhard Reiter
Message:

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 $previous 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r57032 r57038  
    781781     * `first_child`, respectively, to the serialized markup for the given block.
    782782     *
    783      * @param array $block        The block to inject the theme attribute into, and hooked blocks before.
    784      * @param array $parent_block The parent block of the given block.
    785      * @param array $prev         The previous sibling block of the given block.
     783     * @param array $block        The block to inject the theme attribute into, and hooked blocks before. Passed by reference.
     784     * @param array $parent_block The parent block of the given block. Passed by reference. Default null.
     785     * @param array $prev         The previous sibling block of the given block. Default null.
    786786     * @return string The serialized markup for the given block, with the markup for any hooked blocks prepended to it.
    787787     */
    788     return function ( &$block, $parent_block = null, $prev = null ) use ( $hooked_blocks, $context ) {
     788    return function ( &$block, &$parent_block = null, $prev = null ) use ( $hooked_blocks, $context ) {
    789789        _inject_theme_attribute_in_template_part_block( $block );
    790790
     
    854854     * `last_child`, respectively, to the serialized markup for the given block.
    855855     *
    856      * @param array $block        The block to inject the hooked blocks after.
    857      * @param array $parent_block The parent block of the given block.
    858      * @param array $next         The next sibling block of the given block.
     856     * @param array $block        The block to inject the hooked blocks after. Passed by reference.
     857     * @param array $parent_block The parent block of the given block. Passed by reference. Default null.
     858     * @param array $next         The next sibling block of the given block. Default null.
    859859     * @return string The serialized markup for the given block, with the markup for any hooked blocks appended to it.
    860860     */
    861     return function ( &$block, $parent_block = null, $next = null ) use ( $hooked_blocks, $context ) {
     861    return function ( &$block, &$parent_block = null, $next = null ) use ( $hooked_blocks, $context ) {
    862862        $markup = '';
    863863
     
    10661066                $block_content .= call_user_func_array(
    10671067                    $pre_callback,
    1068                     array( &$inner_block, $block, $prev )
     1068                    array( &$inner_block, &$block, $prev )
    10691069                );
    10701070            }
     
    10771077                $post_markup = call_user_func_array(
    10781078                    $post_callback,
    1079                     array( &$inner_block, $block, $next )
     1079                    array( &$inner_block, &$block, $next )
    10801080                );
    10811081            }
     
    11321132 */
    11331133function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_callback = null ) {
    1134     $result = '';
     1134    $result       = '';
     1135    $parent_block = null; // At the top level, there is no parent block to pass to the callbacks; yet the callbacks expect a reference.
     1136
    11351137    foreach ( $blocks as $index => $block ) {
    11361138        if ( is_callable( $pre_callback ) ) {
     
    11411143            $result .= call_user_func_array(
    11421144                $pre_callback,
    1143                 array( &$block, null, $prev ) // At the top level, there is no parent block to pass to the callback.
     1145                array( &$block, &$parent_block, $prev )
    11441146            );
    11451147        }
     
    11521154            $post_markup = call_user_func_array(
    11531155                $post_callback,
    1154                 array( &$block, null, $next ) // At the top level, there is no parent block to pass to the callback.
     1156                array( &$block, &$parent_block, $next )
    11551157            );
    11561158        }
Note: See TracChangeset for help on using the changeset viewer.