Make WordPress Core


Ignore:
Timestamp:
01/25/2024 01:46:49 PM (9 months ago)
Author:
Bernhard Reiter
Message:

Block Hooks: Introduce a new hooked_block_{$block_type} filter.

Add a new hooked_block_{$block_type} filter that allows modifying a hooked block (in parsed block format) prior to insertion, while providing read access to its anchor block (in the same format).

This allows block authors to e.g. set a hooked block's attributes, or its inner blocks; the filter can peruse information about the anchor block when doing so. As such, this filter provides a solution to both #59572 and #60126.

The new filter is designed to strike a good balance and separation of concerns with regard to the existing `hooked_block_types` filter, which allows addition or removal of a block to the list of hooked blocks for a given anchor block -- all of which are identified only by their block types. This new filter, on the other hand, only applies to one hooked block at a time, and allows modifying the entire (parsed) hooked block; it also gives (read) access to the parsed anchor block.

Props gziolo, tomjcafferkey, andrewserong, isabel_brison, timbroddin, yansern.
Fixes #59572, #60126.

File:
1 edited

Legend:

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

    r57172 r57354  
    1212 */
    1313class Tests_Blocks_GetHookedBlockMarkup extends WP_UnitTestCase {
     14    const HOOKED_BLOCK_TYPE = 'tests/hooked-block';
     15    const HOOKED_BLOCK      = array(
     16        'blockName'    => 'tests/different-hooked-block',
     17        'attrs'        => array(),
     18        'innerContent' => array(),
     19    );
     20
    1421    /**
     22     * @ticket 59572
    1523     * @ticket 60008
     24     * @ticket 60126
    1625     *
    1726     * @covers ::get_hooked_block_markup
     
    2231        );
    2332
    24         $actual = get_hooked_block_markup( $anchor_block, 'tests/hooked-block' );
    25         $this->assertSame( array( 'tests/hooked-block' ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] );
    26         $this->assertSame( '<!-- wp:tests/hooked-block /-->', $actual );
     33        $actual = get_hooked_block_markup( self::HOOKED_BLOCK, self::HOOKED_BLOCK_TYPE, $anchor_block );
     34        $this->assertSame(
     35            array( self::HOOKED_BLOCK_TYPE ),
     36            $anchor_block['attrs']['metadata']['ignoredHookedBlocks'],
     37            "Hooked block type wasn't added to ignoredHookedBlocks metadata."
     38        );
     39        $this->assertSame(
     40            '<!-- wp:' . self::HOOKED_BLOCK['blockName'] . ' /-->',
     41            $actual,
     42            "Markup for hooked block wasn't generated correctly."
     43        );
    2744    }
    2845
    2946    /**
     47     * @ticket 59572
    3048     * @ticket 60008
     49     * @ticket 60126
    3150     *
    3251     * @covers ::get_hooked_block_markup
     
    3756            'attrs'     => array(
    3857                'metadata' => array(
    39                     'ignoredHookedBlocks' => array( 'tests/hooked-block' ),
     58                    'ignoredHookedBlocks' => array( self::HOOKED_BLOCK_TYPE ),
    4059                ),
    4160            ),
    4261        );
    4362
    44         $actual = get_hooked_block_markup( $anchor_block, 'tests/hooked-block' );
    45         $this->assertSame( array( 'tests/hooked-block' ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] );
    46         $this->assertSame( '', $actual );
     63        $actual = get_hooked_block_markup( self::HOOKED_BLOCK, self::HOOKED_BLOCK_TYPE, $anchor_block );
     64        $this->assertSame(
     65            array( self::HOOKED_BLOCK_TYPE ),
     66            $anchor_block['attrs']['metadata']['ignoredHookedBlocks'],
     67            "ignoredHookedBlocks metadata shouldn't have been modified."
     68        );
     69        $this->assertSame(
     70            '',
     71            $actual,
     72            "No markup should've been generated for ignored hooked block."
     73        );
    4774    }
    4875
    4976    /**
     77     * @ticket 59572
    5078     * @ticket 60008
     79     * @ticket 60126
    5180     *
    5281     * @covers ::get_hooked_block_markup
    5382     */
    5483    public function test_get_hooked_block_markup_adds_to_ignored_hooked_blocks() {
     84        $other_hooked_block_type = 'tests/other-hooked-block';
     85        $other_hooked_block      = array(
     86            'blockName'    => $other_hooked_block_type,
     87            'attrs'        => array(),
     88            'innerContent' => array(),
     89        );
     90
    5591        $anchor_block = array(
    5692            'blockName' => 'tests/anchor-block',
    5793            'attrs'     => array(
    5894                'metadata' => array(
    59                     'ignoredHookedBlocks' => array( 'tests/hooked-block' ),
     95                    'ignoredHookedBlocks' => array( self::HOOKED_BLOCK_TYPE ),
    6096                ),
    6197            ),
    6298        );
    6399
    64         $actual = get_hooked_block_markup( $anchor_block, 'tests/other-hooked-block' );
    65         $this->assertSame( array( 'tests/hooked-block', 'tests/other-hooked-block' ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] );
    66         $this->assertSame( '<!-- wp:tests/other-hooked-block /-->', $actual );
     100        $actual = get_hooked_block_markup( $other_hooked_block, $other_hooked_block_type, $anchor_block );
     101        $this->assertSame(
     102            array( self::HOOKED_BLOCK_TYPE, $other_hooked_block_type ),
     103            $anchor_block['attrs']['metadata']['ignoredHookedBlocks'],
     104            "Newly hooked block should've been added to ignoredHookedBlocks metadata while retaining previously ignored one."
     105        );
     106        $this->assertSame(
     107            '<!-- wp:' . $other_hooked_block_type . ' /-->',
     108            $actual,
     109            "Markup for newly hooked block should've been generated."
     110        );
    67111    }
    68112}
Note: See TracChangeset for help on using the changeset viewer.