函数文档

insert_hooked_blocks()

💡 云策文档标注

概述

insert_hooked_blocks() 函数用于返回钩子块相对于锚块特定位置的标记。它处理钩子块列表,应用过滤器,并序列化块以生成输出。

关键要点

  • 参数包括锚块数组、相对位置(如 'before'、'after'、'first_child'、'last_child')、钩子块数组和上下文(如 WP_Block_Template)。
  • 使用 apply_filters 钩子(如 hooked_block_types、hooked_block)允许开发者过滤钩子块类型和解析块数组。
  • 函数会检查 ignoredHookedBlocks 元数据,以决定是否忽略特定钩子块,确保输出正确。

代码示例

function insert_hooked_blocks( &$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();

    $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );

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

        $parsed_hooked_block = apply_filters( 'hooked_block', $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );
        $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 ) {
            continue;
        }

        if (
            ! isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ||
            ! in_array( $hooked_block_type, $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'], true )
        ) {
            $markup .= serialize_block( $parsed_hooked_block );
        }
    }

    return $markup;
}

注意事项

  • 相对位置参数必须是 'before'、'after'、'first_child' 或 'last_child' 之一,否则可能导致错误。
  • 钩子块数组的结构应基于锚块类型和相对位置进行分组,以确保正确检索。
  • 使用 hooked_block 和 hooked_block_{$hooked_block_type} 过滤器时,返回 null 可以抑制特定钩子块的输出。

📄 原文内容

Returns the markup for blocks hooked to the given anchor block in a specific relative position.

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

Source

function insert_hooked_blocks( &$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();

	/**
	 * Filters the list of hooked block types for a given anchor block type and relative position.
	 *
	 * @since 6.4.0
	 *
	 * @param string[]                        $hooked_block_types The list of hooked block types.
	 * @param string                          $relative_position  The relative position of the hooked blocks.
	 *                                                            Can be one of 'before', 'after', 'first_child', or 'last_child'.
	 * @param string                          $anchor_block_type  The anchor block type.
	 * @param WP_Block_Template|WP_Post|array $context            The block template, template part, post object,
	 *                                                            or pattern that the anchor block belongs to.
	 */
	$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );

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

		/**
		 * Filters the parsed block array for a given hooked block.
		 *
		 * @since 6.5.0
		 *
		 * @param array|null                      $parsed_hooked_block The parsed block array for the given hooked block type, or null to suppress the block.
		 * @param string                          $hooked_block_type   The hooked block type name.
		 * @param string                          $relative_position   The relative position of the hooked block.
		 * @param array                           $parsed_anchor_block The anchor block, in parsed block array format.
		 * @param WP_Block_Template|WP_Post|array $context             The block template, template part, post object,
		 *                                                             or pattern that the anchor block belongs to.
		 */
		$parsed_hooked_block = apply_filters( 'hooked_block', $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );

		/**
		 * Filters the parsed block array for a given hooked block.
		 *
		 * The dynamic portion of the hook name, `$hooked_block_type`, refers to the block type name of the specific hooked block.
		 *
		 * @since 6.5.0
		 *
		 * @param array|null                      $parsed_hooked_block The parsed block array for the given hooked block type, or null to suppress the block.
		 * @param string                          $hooked_block_type   The hooked block type name.
		 * @param string                          $relative_position   The relative position of the hooked block.
		 * @param array                           $parsed_anchor_block The anchor block, in parsed block array format.
		 * @param WP_Block_Template|WP_Post|array $context             The block template, template part, post object,
		 *                                                             or pattern that the anchor block belongs to.
		 */
		$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 ) {
			continue;
		}

		// It's possible that the filter returned a block of a different type, so we explicitly
		// look for the original `$hooked_block_type` in the `ignoredHookedBlocks` metadata.
		if (
			! isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ||
			! in_array( $hooked_block_type, $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'], true )
		) {
			$markup .= serialize_block( $parsed_hooked_block );
		}
	}

	return $markup;
}

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.