Blocks: Introduce helper function to retrieve hooked blocks

In order to implement Block Hooks (see #59313), we added block_hooks field to the WP_Block_Type class, as well as to block registration related functions. In this follow-up, new helper function gets introduced that is going to compute the list of hooked blocks by other registered blocks for a given block type.

Extracted from https://github.com/WordPress/wordpress-develop/pull/5158 and covered with unit tests.

Props ockham.
Fixes #59383.


Built from https://develop.svn.wordpress.org/trunk@56610


git-svn-id: http://core.svn.wordpress.org/trunk@56122 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
gziolo 2023-09-18 12:43:13 +00:00
parent 323d8cb19d
commit 7520c3fb46
2 changed files with 22 additions and 1 deletions

View File

@ -739,6 +739,27 @@ function get_dynamic_block_names() {
return $dynamic_block_names;
}
/**
* Retrieves block types (and positions) hooked into the given block.
*
* @since 6.4.0
*
* @param string $name Block type name including namespace.
* @return array Associative array of `$block_type_name => $position` pairs.
*/
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 ) {
foreach ( $block_type->block_hooks as $anchor_block_type => $relative_position ) {
if ( $anchor_block_type === $name ) {
$hooked_blocks[ $block_type->name ] = $relative_position;
}
}
}
return $hooked_blocks;
}
/**
* Given an array of attributes, returns a string in the serialized attributes
* format prepared for post content.

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.4-alpha-56609';
$wp_version = '6.4-alpha-56610';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.