Make WordPress Core

Changeset 57660

Timestamp:
02/20/2024 09:25:09 AM (5 months ago)
Author:
swissspidy
Message:

Block Hooks: Introduce a new hooked_block filter.

This is a counterpart to the dynamic hooked_block_{$block_type} filter introduced in [57354],
which makes it easier to modify all hooked blocks prior to insertion.

Also adds the hooked block type as an additional argument to both filters for consistency.

Props bernhard-reiter, swissspidy.
Fixes #60574.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r57644 r57660  
    896896         * Filters the parsed block array for a given hooked block.
    897897         *
     898
     899
     900
     901
     902
     903
     904
     905
     906
     907
     908
     909
     910
     911
    898912         * The dynamic portion of the hook name, `$hooked_block_type`, refers to the block type name of the specific hooked block.
    899913         *
     
    901915         *
    902916         * @param array                           $parsed_hooked_block The parsed block array for the given hooked block type.
     917
    903918         * @param string                          $relative_position   The relative position of the hooked block.
    904919         * @param array                           $parsed_anchor_block The anchor block, in parsed block array format.
     
    906921         *                                                             or pattern that the anchor block belongs to.
    907922         */
    908         $parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $relative_position, $parsed_anchor_block, $context );
    909 
    910         // It's possible that the `hooked_block_{$hooked_block_type}` filter returned a block of a different type,
    911         // so we explicitly look for the original `$hooked_block_type` in the `ignoredHookedBlocks` metadata.
     923        $parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $relative_position, $parsed_anchor_block, $context );
     924
     925        // It's possible that the
     926        // look for the original `$hooked_block_type` in the `ignoredHookedBlocks` metadata.
    912927        if (
    913928            ! isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ||
  • trunk/tests/phpunit/tests/blocks/insertHookedBlocks.php

    r57627 r57660  
    111111        );
    112112
    113         $filter = function ( $parsed_hooked_block, $relative_position, $parsed_anchor_block ) {
     113        $filter = function ( $parsed_hooked_block, $relative_position, $parsed_anchor_block ) {
    114114            // Is the hooked block adjacent to the anchor block?
    115115            if ( 'before' !== $relative_position && 'after' !== $relative_position ) {
     
    125125            return $parsed_hooked_block;
    126126        };
    127         add_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 );
     127        add_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, );
    128128        $actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() );
    129         remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 );
     129        remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter );
    130130
    131131        $this->assertSame(
     
    173173        add_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 );
    174174        $actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() );
    175         remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 );
     175        remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter );
    176176
    177177        $this->assertSame(
Note: See TracChangeset for help on using the changeset viewer.