get_hooked_blocks()
云策文档标注
概述
get_hooked_blocks() 函数用于检索钩入指定块的块类型,按锚块类型和相对位置分组。它遍历所有已注册的块类型,收集其 block_hooks 属性,并返回一个结构化数组。
关键要点
- 函数返回一个数组,其中键为锚块类型,值为按相对位置分组的钩入块类型名称列表。
- 内部使用 WP_Block_Type_Registry::get_instance()->get_all_registered() 获取所有已注册块类型。
- 仅处理 WP_Block_Type 实例且 block_hooks 属性为数组的块类型。
- 该函数在 WordPress 6.4.0 版本中引入。
代码示例
function get_hooked_blocks() {
$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 => $relative_position ) {
if ( ! isset( $hooked_blocks[ $anchor_block_type ] ) ) {
$hooked_blocks[ $anchor_block_type ] = array();
}
if ( ! isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] ) ) {
$hooked_blocks[ $anchor_block_type ][ $relative_position ] = array();
}
$hooked_blocks[ $anchor_block_type ][ $relative_position ][] = $block_type->name;
}
}
return $hooked_blocks;
}
原文内容
Retrieves block types hooked into the given block, grouped by anchor block type and the relative position.
Source
function get_hooked_blocks() {
$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 => $relative_position ) {
if ( ! isset( $hooked_blocks[ $anchor_block_type ] ) ) {
$hooked_blocks[ $anchor_block_type ] = array();
}
if ( ! isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] ) ) {
$hooked_blocks[ $anchor_block_type ][ $relative_position ] = array();
}
$hooked_blocks[ $anchor_block_type ][ $relative_position ][] = $block_type->name;
}
}
return $hooked_blocks;
}
Changelog
| Version | Description |
|---|---|
| 6.4.0 | Introduced. |