Make WordPress Core


Ignore:
Timestamp:
09/19/2023 12:48:41 PM (18 months ago)
Author:
gziolo
Message:

Blocks: Introduce a variation of serialize blocks helper with traversing

Introduces two new functions traverse_and_serialize_blocks and traverse_and_serialize_block with the additional $callback argument. It is possible to pass parent block, block index, chunk index to the callback argument.

Reverts changes applied to serialize_blocks and serialize_block in #59327 with [56557].

Props ockham, mukesh27.
See #59313.

File:
1 edited

Legend:

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

    r56557 r56620  
    1414    /**
    1515     * @dataProvider data_serialize_identity_from_parsed
     16     *
     17     * @param string $original Original block markup.
    1618     */
    1719    public function test_serialize_identity_from_parsed( $original ) {
    1820        $blocks = parse_blocks( $original );
    1921
    20         $actual   = serialize_blocks( $blocks );
    21         $expected = $original;
     22        $actual = serialize_blocks( $blocks );
    2223
    23         $this->assertSame( $expected, $actual );
     24        $this->assertSame( $original, $actual );
    2425    }
    2526
     
    5960     * @ticket 59327
    6061     *
    61      * @covers ::serialize_blocks
     62     * @covers ::traverse_and_serialize_blocks
    6263     */
    63     public function test_callback_argument() {
     64    public function test_traverse_and_serialize_blocks() {
    6465        $markup = "<!-- wp:outer --><!-- wp:inner {\"key\":\"value\"} -->Example.<!-- /wp:inner -->\n\nExample.\n\n<!-- wp:void /--><!-- /wp:outer -->";
    6566        $blocks = parse_blocks( $markup );
    6667
    67         $actual = serialize_blocks( $blocks, array( __CLASS__, 'add_attribute_to_inner_block' ) );
     68        $actual = traverse_and_serialize_blocks( $blocks, array( __CLASS__, 'add_attribute_to_inner_block' ) );
    6869
    6970        $this->assertSame(
     
    7980        return $block;
    8081    }
     82
     83    /**
     84     * @ticket 59327
     85     *
     86     * @covers ::traverse_and_serialize_blocks
     87     *
     88     * @dataProvider data_serialize_identity_from_parsed
     89     *
     90     * @param string $original Original block markup.
     91     */
     92    public function test_traverse_and_serialize_identity_from_parsed( $original ) {
     93        $blocks = parse_blocks( $original );
     94
     95        $actual = traverse_and_serialize_blocks(
     96            $blocks,
     97            function ( $block ) {
     98                return $block;
     99            }
     100        );
     101
     102        $this->assertSame( $original, $actual );
     103    }
    81104}
Note: See TracChangeset for help on using the changeset viewer.