From eeefd48208f6d19dbb354991b9917c5e1ece49ad Mon Sep 17 00:00:00 2001 From: Bernhard Reiter Date: Tue, 26 Sep 2023 11:49:21 +0000 Subject: [PATCH] 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 --- wp-includes/blocks.php | 41 ++++++++++++++++++++++++++++------------- wp-includes/version.php | 2 +- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index 81c7d250fc..0c09a91f36 100644 --- a/wp-includes/blocks.php +++ b/wp-includes/blocks.php @@ -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 * - * @param string $name Block type name including namespace. - * @param string $relative_position Optional. Relative position of the hooked block. Default empty string. - * @return array Associative array of `$block_type_name => $position` pairs. + * @param string $name Block type name including namespace. + * @return array[] Array of block types grouped by their relative position. */ -function get_hooked_blocks( $name, $relative_position = '' ) { +function get_hooked_blocks( $name ) { $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered(); $hooked_blocks = array(); foreach ( $block_types as $block_type ) { if ( ! ( $block_type instanceof WP_Block_Type ) || ! is_array( $block_type->block_hooks ) ) { 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 ) { continue; } - if ( $relative_position && $relative_position !== $position ) { - continue; + if ( ! isset( $hooked_blocks[ $relative_position ] ) ) { + $hooked_blocks[ $relative_position ] = array(); } - $hooked_blocks[ $block_type->name ] = $position; + $hooked_blocks[ $relative_position ][] = $block_type->name; } } return $hooked_blocks; @@ -787,7 +786,11 @@ function make_before_block_visitor( $context ) { // Candidate for first-child insertion. $relative_position = 'first_child'; $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. * @@ -807,7 +810,11 @@ function make_before_block_visitor( $context ) { $relative_position = 'before'; $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 */ $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 ) { @@ -849,7 +856,11 @@ function make_after_block_visitor( $context ) { $relative_position = 'after'; $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 */ $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 ) { @@ -860,7 +871,11 @@ function make_after_block_visitor( $context ) { // Candidate for last-child insertion. $relative_position = 'last_child'; $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 */ $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 ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index a25b87afd3..0d958f95bd 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @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.