Make WordPress Core

Changeset 56970


Ignore:
Timestamp:
10/18/2023 07:30:40 PM (14 months ago)
Author:
Bernhard Reiter
Message:

Blocks: During traversal, allow post callback to modify block.

Both the $pre_callback and $post_callback functions that are given as arguments to traverse_and_serialize_block(s) receive a reference to the current block as their first argument. However, while any changes that the "pre" callback makes to the block are reflected by the serialized markup, the same wasn't true for the "post" callback: Any changes that it made were only applied after the block had already been serialized.

This commit changes the behavior such that $post_callback's changes to the current block are also reflected in the serialized markup.

See #59646.
Props gziolo.
Fixes #59669.

Location:
trunk
Files:
2 edited

Legend:

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

    r56960 r56970  
    10631063            }
    10641064
    1065             $block_content .= traverse_and_serialize_block( $inner_block, $pre_callback, $post_callback );
    1066 
    10671065            if ( is_callable( $post_callback ) ) {
    10681066                $next = count( $block['innerBlocks'] ) - 1 === $block_index
     
    10701068                    : $block['innerBlocks'][ $block_index + 1 ];
    10711069
    1072                 $block_content .= call_user_func_array(
     1070                $post_markup = call_user_func_array(
    10731071                    $post_callback,
    10741072                    array( &$inner_block, $block, $next )
    10751073                );
    10761074            }
     1075
     1076            $block_content .= traverse_and_serialize_block( $inner_block, $pre_callback, $post_callback );
     1077            $block_content .= isset( $post_markup ) ? $post_markup : '';
     1078
    10771079            ++$block_index;
    10781080        }
     
    11361138        }
    11371139
    1138         $result .= traverse_and_serialize_block( $block, $pre_callback, $post_callback );
    1139 
    11401140        if ( is_callable( $post_callback ) ) {
    11411141            $next = count( $blocks ) - 1 === $index
     
    11431143                : $blocks[ $index + 1 ];
    11441144
    1145             $result .= call_user_func_array(
     1145            $post_markup = call_user_func_array(
    11461146                $post_callback,
    11471147                array( &$block, null, $next ) // At the top level, there is no parent block to pass to the callback.
    11481148            );
    11491149        }
     1150
     1151        $result .= traverse_and_serialize_block( $block, $pre_callback, $post_callback );
     1152        $result .= isset( $post_markup ) ? $post_markup : '';
    11501153    }
    11511154
  • trunk/tests/phpunit/tests/blocks/serialize.php

    r56701 r56970  
    7575    }
    7676
     77    /**
     78     * @ticket 59669
     79     *
     80     * @covers ::traverse_and_serialize_blocks
     81     */
     82    public function test_traverse_and_serialize_blocks_post_callback_modifies_current_block() {
     83        $markup = "<!-- wp:outer --><!-- wp:inner {\"key\":\"value\"} -->Example.<!-- /wp:inner -->\n\nExample.\n\n<!-- wp:void /--><!-- /wp:outer -->";
     84        $blocks = parse_blocks( $markup );
     85
     86        $actual = traverse_and_serialize_blocks( $blocks, null, array( __CLASS__, 'add_attribute_to_inner_block' ) );
     87
     88        $this->assertSame(
     89            "<!-- wp:outer --><!-- wp:inner {\"key\":\"value\",\"myattr\":\"myvalue\"} -->Example.<!-- /wp:inner -->\n\nExample.\n\n<!-- wp:void /--><!-- /wp:outer -->",
     90            $actual
     91        );
     92    }
     93
    7794    public static function add_attribute_to_inner_block( &$block ) {
    7895        if ( 'core/inner' === $block['blockName'] ) {
Note: See TracChangeset for help on using the changeset viewer.