Make WordPress Core

Changeset 56704


Ignore:
Timestamp:
09/26/2023 11:47:18 AM (3 years ago)
Author:
Bernhard Reiter
Message:

Blocks: Have get_hooked_blocks() return blocks grouped by position.

All existing calls of get_hooked_blocks() in non-test code are currently wrapped in an extra array_keys() call. This changeset absorbs that logic into the function and changes the structure of the return value accordingly.

Furthermore, this allows us to remove the extra $relative_position argument (introduced in [56673]) from the function again, as the same data can now be simply fetched via array access.

Props gziolo, spacedmonkey, mukesh27.
See #59383.

Location:
trunk
Files:
2 edited

Legend:

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

    r56683 r56704  
    724724
    725725/**
    726  * Retrieves block types (and positions) hooked into the given block.
     726 * Retrieves block types hooked into the given block, grouped by their relative position.
    727727 *
    728728 * @since 6.4.0
    729729 *
    730  * @param string $name              Block type name including namespace.
    731  * @param string $relative_position Optional. Relative position of the hooked block. Default empty string.
    732  * @return array Associative array of `$block_type_name => $position` pairs.
    733  */
    734 function get_hooked_blocks( $name, $relative_position = '' ) {
     730 * @param string $name Block type name including namespace.
     731 * @return array[] Array of block types grouped by their relative position.
     732 */
     733function get_hooked_blocks( $name ) {
    735734        $block_types   = WP_Block_Type_Registry::get_instance()->get_all_registered();
    736735        $hooked_blocks = array();
    … …  
    739738                        continue;
    740739                }
    741                 foreach ( $block_type->block_hooks as $anchor_block_type => $position ) {
     740                foreach ( $block_type->block_hooks as $anchor_block_type => $relative_position ) {
    742741                        if ( $anchor_block_type !== $name ) {
    743742                                continue;
    744743                        }
    745                         if ( $relative_position && $relative_position !== $position ) {
    746                                 continue;
    747                         }
    748                         $hooked_blocks[ $block_type->name ] = $position;
     744                        if ( ! isset( $hooked_blocks[ $relative_position ] ) ) {
     745                                $hooked_blocks[ $relative_position ] = array();
     746                        }
     747                        $hooked_blocks[ $relative_position ][] = $block_type->name;
    749748                }
    750749        }
    … …  
    788787                        $relative_position  = 'first_child';
    789788                        $anchor_block_type  = $parent_block['blockName'];
    790                         $hooked_block_types = array_keys( get_hooked_blocks( $anchor_block_type, $relative_position ) );
     789                        $hooked_block_types = get_hooked_blocks( $anchor_block_type );
     790                        $hooked_block_types = isset( $hooked_block_types[ $relative_position ] )
     791                                ? $hooked_block_types[ $relative_position ]
     792                                : array();
     793
    791794                        /**
    792795                         * Filters the list of hooked block types for a given anchor block type and relative position.
    … …  
    808811                $relative_position  = 'before';
    809812                $anchor_block_type  = $block['blockName'];
    810                 $hooked_block_types = array_keys( get_hooked_blocks( $anchor_block_type, $relative_position ) );
     813                $hooked_block_types = get_hooked_blocks( $anchor_block_type );
     814                $hooked_block_types = isset( $hooked_block_types[ $relative_position ] )
     815                        ? $hooked_block_types[ $relative_position ]
     816                        : array();
     817
    811818                /** This filter is documented in wp-includes/blocks.php */
    812819                $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
    … …  
    850857                $relative_position  = 'after';
    851858                $anchor_block_type  = $block['blockName'];
    852                 $hooked_block_types = array_keys( get_hooked_blocks( $anchor_block_type, $relative_position ) );
     859                $hooked_block_types = get_hooked_blocks( $anchor_block_type );
     860                $hooked_block_types = isset( $hooked_block_types[ $relative_position ] )
     861                        ? $hooked_block_types[ $relative_position ]
     862                        : array();
     863
    853864                /** This filter is documented in wp-includes/blocks.php */
    854865                $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
    … …  
    861872                        $relative_position  = 'last_child';
    862873                        $anchor_block_type  = $parent_block['blockName'];
    863                         $hooked_block_types = array_keys( get_hooked_blocks( $anchor_block_type, $relative_position ) );
     874                        $hooked_block_types = get_hooked_blocks( $anchor_block_type );
     875                        $hooked_block_types = isset( $hooked_block_types[ $relative_position ] )
     876                                ? $hooked_block_types[ $relative_position ]
     877                                : array();
     878
    864879                        /** This filter is documented in wp-includes/blocks.php */
    865880                        $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
  • trunk/tests/phpunit/tests/blocks/blockHooks.php

    r56673 r56704  
    7171                $this->assertSame(
    7272                        array(
    73                                 'tests/injected-one' => 'before',
    74                                 'tests/injected-two' => 'before',
     73                                'before' => array(
     74                                        'tests/injected-one',
     75                                        'tests/injected-two',
     76                                ),
    7577                        ),
    7678                        get_hooked_blocks( 'tests/hooked-at-before' ),
    … …  
    7981                $this->assertSame(
    8082                        array(
    81                                 'tests/injected-one' => 'after',
    82                                 'tests/injected-two' => 'after',
     83                                'after' => array(
     84                                        'tests/injected-one',
     85                                        'tests/injected-two',
     86                                ),
    8387                        ),
    8488                        get_hooked_blocks( 'tests/hooked-at-after' ),
    … …  
    8791                $this->assertSame(
    8892                        array(
    89                                 'tests/injected-two' => 'first_child',
     93                                'first_child' => array(
     94                                        'tests/injected-two',
     95                                ),
    9096                        ),
    9197                        get_hooked_blocks( 'tests/hooked-at-first-child' ),
    … …  
    94100                $this->assertSame(
    95101                        array(
    96                                 'tests/injected-two' => 'last_child',
     102                                'last_child' => array(
     103                                        'tests/injected-two',
     104                                ),
    97105                        ),
    98106                        get_hooked_blocks( 'tests/hooked-at-last-child' ),
    … …  
    101109                $this->assertSame(
    102110                        array(
    103                                 'tests/injected-one' => 'before',
    104                                 'tests/injected-two' => 'after',
     111                                'before' => array(
     112                                        'tests/injected-one',
     113                                ),
     114                                'after'  => array(
     115                                        'tests/injected-two',
     116                                ),
    105117                        ),
    106118                        get_hooked_blocks( 'tests/hooked-at-before-and-after' ),
    107119                        'block hooked before one block and after another'
    108120                );
    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                 );
    116121        }
    117122}
Note: See TracChangeset for help on using the changeset viewer.