Make WordPress Core


Ignore:
Timestamp:
09/25/2023 08:42:45 AM (3 years ago)
Author:
Bernhard Reiter
Message:

Blocks: Introduce filter to allow easy addition of hooked blocks.

Introduce a hooked_block_types filter that allows easier conditional addition (or removal) of hooked blocks for a given anchor block and relative position.

function insert_shopping_cart_hooked_block( $hooked_blocks, $position, $anchor_block, $context ) {
        if ( 'after' === $position && 'core/navigation' === $anchor_block && /** $context is header template part **/ ) {
                $hooked_blocks[] = 'mycommerce/shopping-cart';
        }
        return $hooked_blocks;
}
add_filter( 'hooked_block_types', 'insert_shopping_cart_hooked_block', 10, 4 );

Props gziolo, nerrad, dmsnell, ndiego.
Fixes #59424.

File:
1 edited

Legend:

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

    r56634 r56673  
    5050                        array(
    5151                                'block_hooks' => array(
    52                                         'tests/hooked-at-before' => 'before',
    53                                         'tests/hooked-at-after'  => 'after',
     52                                        'tests/hooked-at-before'           => 'before',
     53                                        'tests/hooked-at-after'            => 'after',
     54                                        'tests/hooked-at-before-and-after' => 'before',
    5455                                ),
    5556                        )
     
    5960                        array(
    6061                                'block_hooks' => array(
    61                                         'tests/hooked-at-before'      => 'before',
    62                                         'tests/hooked-at-after'       => 'after',
    63                                         'tests/hooked-at-first-child' => 'first_child',
    64                                         'tests/hooked-at-last-child'  => 'last_child',
     62                                        'tests/hooked-at-before'           => 'before',
     63                                        'tests/hooked-at-after'            => 'after',
     64                                        'tests/hooked-at-before-and-after' => 'after',
     65                                        'tests/hooked-at-first-child'      => 'first_child',
     66                                        'tests/hooked-at-last-child'       => 'last_child',
    6567                                ),
    6668                        )
     
    9799                        'block hooked at the last child position'
    98100                );
     101                $this->assertSame(
     102                        array(
     103                                'tests/injected-one' => 'before',
     104                                'tests/injected-two' => 'after',
     105                        ),
     106                        get_hooked_blocks( 'tests/hooked-at-before-and-after' ),
     107                        'block hooked before one block and after another'
     108                );
     109                $this->assertSame(
     110                        array(
     111                                'tests/injected-one' => 'before',
     112                        ),
     113                        get_hooked_blocks( 'tests/hooked-at-before-and-after', 'before' ),
     114                        'block hooked before one block and after another, filtered for before'
     115                );
    99116        }
    100117}
Note: See TracChangeset for help on using the changeset viewer.