Make WordPress Core


Ignore:
Timestamp:
09/21/2023 08:32:52 AM (3 years 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/block-template-utils.php

    r56584 r56644  
    226226     */
    227227    public function test_inject_theme_attribute_in_template_part_block() {
    228         $template_part_block_without_theme_attribute = array(
     228        $template_part_block = array(
    229229            'blockName'    => 'core/template-part',
    230230            'attrs'        => array(
     
    239239        );
    240240
    241         $actual   = _inject_theme_attribute_in_template_part_block( $template_part_block_without_theme_attribute );
     241        _inject_theme_attribute_in_template_part_block( $template_part_block );
    242242        $expected = array(
    243243            'blockName'    => 'core/template-part',
     
    255255        $this->assertSame(
    256256            $expected,
    257             $actual,
     257            $template_part_block,
    258258            '`theme` attribute was not correctly injected in template part block.'
    259259        );
     
    266266     */
    267267    public function test_not_inject_theme_attribute_in_template_part_block_theme_attribute_exists() {
    268         $template_part_block_with_existing_theme_attribute = array(
     268        $template_part_block = array(
    269269            'blockName'    => 'core/template-part',
    270270            'attrs'        => array(
     
    280280        );
    281281
    282         $actual = _inject_theme_attribute_in_template_part_block( $template_part_block_with_existing_theme_attribute );
     282        $expected = $template_part_block;
     283        _inject_theme_attribute_in_template_part_block( $template_part_block );
    283284        $this->assertSame(
    284             $template_part_block_with_existing_theme_attribute,
    285             $actual,
     285            $expected,
     286            $template_part_block,
    286287            'Existing `theme` attribute in template part block was not respected by attribute injection.'
    287288        );
     
    302303        );
    303304
    304         $actual = _inject_theme_attribute_in_template_part_block( $non_template_part_block );
     305        $expected = $non_template_part_block;
     306        _inject_theme_attribute_in_template_part_block( $non_template_part_block );
    305307        $this->assertSame(
     308            $expected,
    306309            $non_template_part_block,
    307             $actual,
    308310            '`theme` attribute injection modified non-template-part block.'
    309311        );
Note: See TracChangeset for help on using the changeset viewer.