Make WordPress Core


Ignore:
Timestamp:
11/01/2023 12:44:02 PM (11 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.

Reviewed by hellofromTonya.
Merges [56970] to the 6.4 branch.

See #59646.
Props gziolo.
Fixes #59669.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/6.4/tests/phpunit/tests/blocks/serialize.php

    r56701 r57043  
    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.