Make WordPress Core


Ignore:
Timestamp:
09/26/2023 11:47:18 AM (18 months 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.

File:
1 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 );
Note: See TracChangeset for help on using the changeset viewer.