Skip to content

Commit

Permalink
Blocks: Introduce helper function to retrieve hooked blocks
Browse files Browse the repository at this point in the history
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 WordPress/wordpress-develop#5158 and covered with unit tests.

Props ockham.
Fixes #59383.


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


git-svn-id: https://core.svn.wordpress.org/trunk@56122 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
gziolo committed Sep 18, 2023
1 parent a404448 commit 06ba518
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 21 additions & 0 deletions wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 06ba518

Please sign in to comment.