Make WordPress Core


Ignore:
Timestamp:
09/21/2023 08:32:52 AM (17 months ago)
Author:
Bernhard Reiter
Message:

Blocks: Change traverse_and_serialize_block(s)'s callback signature.

During work on #59399, it was discovered that sibling block insertion wasn't likely going to work the way it was planned, which required devising an alternative solution. This new solution requires some changes to traverse_and_serialize_block(s):

  • Change the signature of the existing callback such that:
    • the return value is a string that will be prepended to the result of the inner block traversal and serialization;
    • the function arguments are: a reference to the current block (so it can be modified inline, which is important e.g. for theme attribute insertion), the parent block, and the previous block (instead of the block index and chunk index).
  • Add a second callback argument to traverse_and_serialize_block(s), which is called after the block is traversed and serialized.
    • Its function arguments are a reference to the current block, the parent block, and the next block.

Props gziolo.
Fixes #59412. See #59313.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/blocks/serialize.php

    r56620 r56644  
    5959    /**
    6060     * @ticket 59327
     61     * @ticket 59412
    6162     *
    6263     * @covers ::traverse_and_serialize_blocks
     
    7475    }
    7576
    76     public static function add_attribute_to_inner_block( $block ) {
     77    public static function add_attribute_to_inner_block( &$block ) {
    7778        if ( 'core/inner' === $block['blockName'] ) {
    7879            $block['attrs']['myattr'] = 'myvalue';
    7980        }
    80         return $block;
    8181    }
    8282
    8383    /**
    8484     * @ticket 59327
     85     * @ticket 59412
    8586     *
    8687     * @covers ::traverse_and_serialize_blocks
     
    9394        $blocks = parse_blocks( $original );
    9495
    95         $actual = traverse_and_serialize_blocks(
    96             $blocks,
    97             function ( $block ) {
    98                 return $block;
    99             }
    100         );
     96        $actual = traverse_and_serialize_blocks( $blocks );
    10197
    10298        $this->assertSame( $original, $actual );
Note: See TracChangeset for help on using the changeset viewer.