函数文档

make_after_block_visitor()

💡 云策文档标注

概述

make_after_block_visitor() 函数返回一个用于在指定块后注入钩子块的回调函数。该函数主要用于内部调用,作为 traverse_and_serialize_block(s) 的 $post_callback 参数,以序列化块标记并附加钩子块。

关键要点

  • 返回一个可调用函数,用于在给定块后注入钩子块,并作为其父块的 last_child。
  • 参数包括 $hooked_blocks(钩子块数组)、$context(块所属的上下文对象)和 $callback(生成标记的回调函数,默认为 'insert_hooked_blocks')。
  • 函数内部使用 $callback 处理 'after' 和 'last_child' 钩子,返回序列化标记。
  • 此函数仅供内部使用,不应在外部代码中直接调用。

代码示例

function make_after_block_visitor( $hooked_blocks, $context, $callback = 'insert_hooked_blocks' ) {
    return function ( &$block, &$parent_block = null, $next = null ) use ( $hooked_blocks, $context, $callback ) {
        $markup = call_user_func_array(
            $callback,
            array( &$block, 'after', $hooked_blocks, $context )
        );

        if ( $parent_block && ! $next ) {
            $markup .= call_user_func_array(
                $callback,
                array( &$parent_block, 'last_child', $hooked_blocks, $context )
            );
        }

        return $markup;
    };
}

注意事项

  • 此函数从 WordPress 6.4.0 版本引入,6.5.0 版本添加了 $callback 参数。
  • 相关函数包括 apply_block_hooks_to_content(),用于在内容上运行钩子块算法。

📄 原文内容

Returns a function that injects the hooked blocks after a given block.

Description

The returned function can be used as $post_callback argument to traverse_and_serialize_block(s), where it will append the markup for any blocks hooked after the given block and as its parent’s last_child, respectively.

This function is meant for internal use only.

Parameters

$hooked_blocksarrayrequired
An array of blocks hooked to another block.
$contextWP_Block_Template|WP_Post|arrayrequired
A block template, template part, post object, or pattern that the blocks belong to.
$callbackcallablerequired
A function that will be called for each block to generate the markup for a given list of blocks that are hooked to it.
Default: 'insert_hooked_blocks'.

Return

callable A function that returns the serialized markup for the given block, including the markup for any hooked blocks after it.

Source

function make_after_block_visitor( $hooked_blocks, $context, $callback = 'insert_hooked_blocks' ) {
	/**
	 * Injects hooked blocks after the given block, and returns the serialized markup.
	 *
	 * Append the markup for any blocks hooked `after` the given block and as its parent's
	 * `last_child`, respectively, to the serialized markup for the given block.
	 *
	 * @param array $block        The block to inject the hooked blocks after. Passed by reference.
	 * @param array $parent_block The parent block of the given block. Passed by reference. Default null.
	 * @param array $next         The next sibling block of the given block. Default null.
	 * @return string The serialized markup for the given block, with the markup for any hooked blocks appended to it.
	 */
	return function ( &$block, &$parent_block = null, $next = null ) use ( $hooked_blocks, $context, $callback ) {
		$markup = call_user_func_array(
			$callback,
			array( &$block, 'after', $hooked_blocks, $context )
		);

		if ( $parent_block && ! $next ) {
			// Candidate for last-child insertion.
			$markup .= call_user_func_array(
				$callback,
				array( &$parent_block, 'last_child', $hooked_blocks, $context )
			);
		}

		return $markup;
	};
}

Changelog

Version Description
6.5.0 Added $callback argument.
6.4.0 Introduced.