函数文档

set_ignored_hooked_blocks_metadata()

💡 云策文档标注

概述

set_ignored_hooked_blocks_metadata() 函数用于向锚块(anchor block)的忽略钩块(ignored hooked blocks)元数据中添加钩块类型列表。此函数仅供内部使用,返回空字符串。

关键要点

  • 函数作用:将钩块类型列表添加到锚块的 ignoredHookedBlocks 元数据中,以标记已处理的钩块。
  • 参数说明:接受四个参数:$parsed_anchor_block(锚块的解析数组)、$relative_position(相对位置,如 'before'、'after' 等)、$hooked_blocks(钩块类型数组)、$context(上下文,如 WP_Block_Template 或 WP_Post)。
  • 内部逻辑:通过过滤器 hooked_block_types 和 hooked_block 处理钩块类型和解析块,合并并去重忽略钩块列表。
  • 返回值:始终返回空字符串,因为钩块标记已在 insert_hooked_blocks 中创建。
  • 相关钩子:包括 hooked_block_types、hooked_block 和 hooked_block_{$hooked_block_type} 过滤器,用于自定义钩块行为。

代码示例

function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) {
    $anchor_block_type  = $parsed_anchor_block['blockName'];
    $hooked_block_types = isset( $anchor_block_type, $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
        ? $hooked_blocks[ $anchor_block_type ][ $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 );
    if ( empty( $hooked_block_types ) ) {
        return '';
    }

    foreach ( $hooked_block_types as $index => $hooked_block_type ) {
        $parsed_hooked_block = array(
            'blockName'    => $hooked_block_type,
            'attrs'        => array(),
            'innerBlocks'  => array(),
            'innerContent' => array(),
        );

        /** This filter is documented in wp-includes/blocks.php */
        $parsed_hooked_block = apply_filters( 'hooked_block', $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );

        /** This filter is documented in wp-includes/blocks.php */
        $parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );

        if ( null === $parsed_hooked_block ) {
            unset( $hooked_block_types[ $index ] );
        }
    }

    $previously_ignored_hooked_blocks = isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] )
        ? $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks']
        : array();

    $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array_unique(
        array_merge(
            $previously_ignored_hooked_blocks,
            $hooked_block_types
        )
    );

    // Markup for the hooked blocks has already been created (in `insert_hooked_blocks`).
    return '';
}

注意事项

  • 此函数为内部函数,不建议在插件或主题中直接调用。
  • 相对位置 $relative_position 必须是 'before'、'after'、'first_child' 或 'last_child' 之一。
  • 钩块处理依赖于 hooked_block_types 和 hooked_block 过滤器,开发者可通过这些过滤器自定义钩块行为。

📄 原文内容

Adds a list of hooked block types to an anchor block’s ignored hooked block types.

Description

This function is meant for internal use only.

Parameters

$parsed_anchor_blockarrayrequired
The anchor block, in parsed block array format.
$relative_positionstringrequired
The relative position of the hooked blocks.
Can be one of 'before', 'after', 'first_child', or 'last_child'.
$hooked_blocksarrayrequired
An array of hooked block types, grouped by anchor block and relative position.
$contextWP_Block_Template|WP_Post|arrayrequired
The block template, template part, or pattern that the anchor block belongs to.

Return

string Empty string.

Source

function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) {
	$anchor_block_type  = $parsed_anchor_block['blockName'];
	$hooked_block_types = isset( $anchor_block_type, $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
		? $hooked_blocks[ $anchor_block_type ][ $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 );
	if ( empty( $hooked_block_types ) ) {
		return '';
	}

	foreach ( $hooked_block_types as $index => $hooked_block_type ) {
		$parsed_hooked_block = array(
			'blockName'    => $hooked_block_type,
			'attrs'        => array(),
			'innerBlocks'  => array(),
			'innerContent' => array(),
		);

		/** This filter is documented in wp-includes/blocks.php */
		$parsed_hooked_block = apply_filters( 'hooked_block', $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );

		/** This filter is documented in wp-includes/blocks.php */
		$parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );

		if ( null === $parsed_hooked_block ) {
			unset( $hooked_block_types[ $index ] );
		}
	}

	$previously_ignored_hooked_blocks = isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] )
		? $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks']
		: array();

	$parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array_unique(
		array_merge(
			$previously_ignored_hooked_blocks,
			$hooked_block_types
		)
	);

	// Markup for the hooked blocks has already been created (in `insert_hooked_blocks`).
	return '';
}

Hooks

apply_filters( ‘hooked_block’, array|null $parsed_hooked_block, string $hooked_block_type, string $relative_position, array $parsed_anchor_block, WP_Block_Template|WP_Post|array $context )

Filters the parsed block array for a given hooked block.

apply_filters( ‘hooked_block_types’, string[] $hooked_block_types, string $relative_position, string $anchor_block_type, WP_Block_Template|WP_Post|array $context )

Filters the list of hooked block types for a given anchor block type and relative position.

apply_filters( “hooked_block_{$hooked_block_type}”, array|null $parsed_hooked_block, string $hooked_block_type, string $relative_position, array $parsed_anchor_block, WP_Block_Template|WP_Post|array $context )

Filters the parsed block array for a given hooked block.

Changelog

Version Description
6.5.0 Introduced.