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. Built from https://develop.svn.wordpress.org/trunk@56704 git-svn-id: http://core.svn.wordpress.org/trunk@56216 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
2d9b5cb098
commit
eeefd48208
|
@ -723,29 +723,28 @@ function get_dynamic_block_names() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves block types (and positions) hooked into the given block.
|
* Retrieves block types hooked into the given block, grouped by their relative position.
|
||||||
*
|
*
|
||||||
* @since 6.4.0
|
* @since 6.4.0
|
||||||
*
|
*
|
||||||
* @param string $name Block type name including namespace.
|
* @param string $name Block type name including namespace.
|
||||||
* @param string $relative_position Optional. Relative position of the hooked block. Default empty string.
|
* @return array[] Array of block types grouped by their relative position.
|
||||||
* @return array Associative array of `$block_type_name => $position` pairs.
|
|
||||||
*/
|
*/
|
||||||
function get_hooked_blocks( $name, $relative_position = '' ) {
|
function get_hooked_blocks( $name ) {
|
||||||
$block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
|
$block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
|
||||||
$hooked_blocks = array();
|
$hooked_blocks = array();
|
||||||
foreach ( $block_types as $block_type ) {
|
foreach ( $block_types as $block_type ) {
|
||||||
if ( ! ( $block_type instanceof WP_Block_Type ) || ! is_array( $block_type->block_hooks ) ) {
|
if ( ! ( $block_type instanceof WP_Block_Type ) || ! is_array( $block_type->block_hooks ) ) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
foreach ( $block_type->block_hooks as $anchor_block_type => $position ) {
|
foreach ( $block_type->block_hooks as $anchor_block_type => $relative_position ) {
|
||||||
if ( $anchor_block_type !== $name ) {
|
if ( $anchor_block_type !== $name ) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if ( $relative_position && $relative_position !== $position ) {
|
if ( ! isset( $hooked_blocks[ $relative_position ] ) ) {
|
||||||
continue;
|
$hooked_blocks[ $relative_position ] = array();
|
||||||
}
|
}
|
||||||
$hooked_blocks[ $block_type->name ] = $position;
|
$hooked_blocks[ $relative_position ][] = $block_type->name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $hooked_blocks;
|
return $hooked_blocks;
|
||||||
|
@ -787,7 +786,11 @@ function make_before_block_visitor( $context ) {
|
||||||
// Candidate for first-child insertion.
|
// Candidate for first-child insertion.
|
||||||
$relative_position = 'first_child';
|
$relative_position = 'first_child';
|
||||||
$anchor_block_type = $parent_block['blockName'];
|
$anchor_block_type = $parent_block['blockName'];
|
||||||
$hooked_block_types = array_keys( get_hooked_blocks( $anchor_block_type, $relative_position ) );
|
$hooked_block_types = get_hooked_blocks( $anchor_block_type );
|
||||||
|
$hooked_block_types = isset( $hooked_block_types[ $relative_position ] )
|
||||||
|
? $hooked_block_types[ $relative_position ]
|
||||||
|
: array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filters the list of hooked block types for a given anchor block type and relative position.
|
* Filters the list of hooked block types for a given anchor block type and relative position.
|
||||||
*
|
*
|
||||||
|
@ -807,7 +810,11 @@ function make_before_block_visitor( $context ) {
|
||||||
|
|
||||||
$relative_position = 'before';
|
$relative_position = 'before';
|
||||||
$anchor_block_type = $block['blockName'];
|
$anchor_block_type = $block['blockName'];
|
||||||
$hooked_block_types = array_keys( get_hooked_blocks( $anchor_block_type, $relative_position ) );
|
$hooked_block_types = get_hooked_blocks( $anchor_block_type );
|
||||||
|
$hooked_block_types = isset( $hooked_block_types[ $relative_position ] )
|
||||||
|
? $hooked_block_types[ $relative_position ]
|
||||||
|
: array();
|
||||||
|
|
||||||
/** This filter is documented in wp-includes/blocks.php */
|
/** This filter is documented in wp-includes/blocks.php */
|
||||||
$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
|
$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
|
||||||
foreach ( $hooked_block_types as $hooked_block_type ) {
|
foreach ( $hooked_block_types as $hooked_block_type ) {
|
||||||
|
@ -849,7 +856,11 @@ function make_after_block_visitor( $context ) {
|
||||||
|
|
||||||
$relative_position = 'after';
|
$relative_position = 'after';
|
||||||
$anchor_block_type = $block['blockName'];
|
$anchor_block_type = $block['blockName'];
|
||||||
$hooked_block_types = array_keys( get_hooked_blocks( $anchor_block_type, $relative_position ) );
|
$hooked_block_types = get_hooked_blocks( $anchor_block_type );
|
||||||
|
$hooked_block_types = isset( $hooked_block_types[ $relative_position ] )
|
||||||
|
? $hooked_block_types[ $relative_position ]
|
||||||
|
: array();
|
||||||
|
|
||||||
/** This filter is documented in wp-includes/blocks.php */
|
/** This filter is documented in wp-includes/blocks.php */
|
||||||
$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
|
$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
|
||||||
foreach ( $hooked_block_types as $hooked_block_type ) {
|
foreach ( $hooked_block_types as $hooked_block_type ) {
|
||||||
|
@ -860,7 +871,11 @@ function make_after_block_visitor( $context ) {
|
||||||
// Candidate for last-child insertion.
|
// Candidate for last-child insertion.
|
||||||
$relative_position = 'last_child';
|
$relative_position = 'last_child';
|
||||||
$anchor_block_type = $parent_block['blockName'];
|
$anchor_block_type = $parent_block['blockName'];
|
||||||
$hooked_block_types = array_keys( get_hooked_blocks( $anchor_block_type, $relative_position ) );
|
$hooked_block_types = get_hooked_blocks( $anchor_block_type );
|
||||||
|
$hooked_block_types = isset( $hooked_block_types[ $relative_position ] )
|
||||||
|
? $hooked_block_types[ $relative_position ]
|
||||||
|
: array();
|
||||||
|
|
||||||
/** This filter is documented in wp-includes/blocks.php */
|
/** This filter is documented in wp-includes/blocks.php */
|
||||||
$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
|
$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
|
||||||
foreach ( $hooked_block_types as $hooked_block_type ) {
|
foreach ( $hooked_block_types as $hooked_block_type ) {
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '6.4-alpha-56703';
|
$wp_version = '6.4-alpha-56704';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
|
Loading…
Reference in New Issue