From 7520c3fb46e03f16561de045c343b14eb991c2e6 Mon Sep 17 00:00:00 2001 From: gziolo Date: Mon, 18 Sep 2023 12:43:13 +0000 Subject: [PATCH] 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 --- wp-includes/blocks.php | 21 +++++++++++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index 07bbd5ab01..bef4ea3bce 100644 --- a/wp-includes/blocks.php +++ b/wp-includes/blocks.php @@ -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. diff --git a/wp-includes/version.php b/wp-includes/version.php index 52d3ab0262..4c710c849b 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.