Make WordPress Core


Ignore:
Timestamp:
02/20/2024 03:20:39 PM (11 months ago)
Author:
Bernhard Reiter
Message:

Block Hooks: Allow hooked_block filters to return null.

Allow returning null from the hooked_block and hooked_block_{$hooked_block_type} filters to suppress a hooked block from being inserted. This is required to allow extenders conditionally inserting a hooked block based on e.g. the value of an attribute of the anchor block.

Props swissspidy, gziolo, joshuatf, tomjcafferkey.
Fixes 60580.

File:
1 edited

Legend:

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

    r57666 r57668  
    145145        );
    146146    }
     147
     148    /**
     149     * @ticket 60580
     150     *
     151     * @covers ::set_ignored_hooked_blocks_metadata
     152     */
     153    public function test_set_ignored_hooked_blocks_metadata_for_block_suppressed_by_filter() {
     154        $anchor_block = array(
     155            'blockName' => 'tests/anchor-block',
     156            'attrs'     => array(),
     157        );
     158
     159        $hooked_blocks = array(
     160            'tests/anchor-block' => array(
     161                'after' => array( 'tests/hooked-block', 'tests/hooked-block-suppressed-by-filter' ),
     162            ),
     163        );
     164
     165        $filter = function ( $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block ) {
     166            if ( 'tests/anchor-block' === $parsed_anchor_block['blockName'] && 'after' === $relative_position ) {
     167                return null;
     168            }
     169
     170            return $parsed_hooked_block;
     171        };
     172
     173        add_filter( 'hooked_block_tests/hooked-block-suppressed-by-filter', $filter, 10, 4 );
     174        set_ignored_hooked_blocks_metadata( $anchor_block, 'after', $hooked_blocks, null );
     175        remove_filter( 'hooked_block_tests/hooked-block-suppressed-by-filter', $filter );
     176
     177        $this->assertSame( array( 'tests/hooked-block' ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] );
     178    }
    147179}
Note: See TracChangeset for help on using the changeset viewer.