Changeset 56704
- Timestamp:
- 09/26/2023 11:47:18 AM (18 months ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks.php
r56683 r56704 724 724 725 725 /** 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. 727 727 * 728 728 * @since 6.4.0 729 729 * 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 */ 733 function get_hooked_blocks( $name ) { 735 734 $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered(); 736 735 $hooked_blocks = array(); … … 739 738 continue; 740 739 } 741 foreach ( $block_type->block_hooks as $anchor_block_type => $ position ) {740 foreach ( $block_type->block_hooks as $anchor_block_type => $relative_position ) { 742 741 if ( $anchor_block_type !== $name ) { 743 742 continue; 744 743 } 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; 749 748 } 750 749 } … … 788 787 $relative_position = 'first_child'; 789 788 $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 791 794 /** 792 795 * Filters the list of hooked block types for a given anchor block type and relative position. … … 808 811 $relative_position = 'before'; 809 812 $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 811 818 /** This filter is documented in wp-includes/blocks.php */ 812 819 $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context ); … … 850 857 $relative_position = 'after'; 851 858 $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 853 864 /** This filter is documented in wp-includes/blocks.php */ 854 865 $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context ); … … 861 872 $relative_position = 'last_child'; 862 873 $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 864 879 /** This filter is documented in wp-includes/blocks.php */ 865 880 $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 71 71 $this->assertSame( 72 72 array( 73 'tests/injected-one' => 'before', 74 'tests/injected-two' => 'before', 73 'before' => array( 74 'tests/injected-one', 75 'tests/injected-two', 76 ), 75 77 ), 76 78 get_hooked_blocks( 'tests/hooked-at-before' ), … … 79 81 $this->assertSame( 80 82 array( 81 'tests/injected-one' => 'after', 82 'tests/injected-two' => 'after', 83 'after' => array( 84 'tests/injected-one', 85 'tests/injected-two', 86 ), 83 87 ), 84 88 get_hooked_blocks( 'tests/hooked-at-after' ), … … 87 91 $this->assertSame( 88 92 array( 89 'tests/injected-two' => 'first_child', 93 'first_child' => array( 94 'tests/injected-two', 95 ), 90 96 ), 91 97 get_hooked_blocks( 'tests/hooked-at-first-child' ), … … 94 100 $this->assertSame( 95 101 array( 96 'tests/injected-two' => 'last_child', 102 'last_child' => array( 103 'tests/injected-two', 104 ), 97 105 ), 98 106 get_hooked_blocks( 'tests/hooked-at-last-child' ), … … 101 109 $this->assertSame( 102 110 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 ), 105 117 ), 106 118 get_hooked_blocks( 'tests/hooked-at-before-and-after' ), 107 119 'block hooked before one block and after another' 108 120 ); 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 );116 121 } 117 122 }
Note: See TracChangeset
for help on using the changeset viewer.