函数文档

make_before_block_visitor()

💡 云策文档标注

概述

make_before_block_visitor() 函数返回一个回调函数,用于在遍历和序列化块时,向指定块注入主题属性,并在其前添加钩子块。该函数主要用于内部处理,如作为 traverse_and_serialize_block(s) 的 $pre_callback 参数。

关键要点

  • 返回一个函数,用于注入主题属性到 Template Part 块,并在指定块前添加钩子块。
  • 参数包括 $hooked_blocks(钩子块数组)、$context(上下文对象)和 $callback(回调函数,默认为 'insert_hooked_blocks')。
  • 返回的函数接受 $block、$parent_block 和 $prev 参数,返回序列化标记,包含钩子块的标记。
  • 函数内部调用 _inject_theme_attribute_in_template_part_block() 处理主题属性,并根据条件调用 $callback 添加钩子块。
  • 此函数为内部使用,不建议在外部直接调用。

代码示例

function make_before_block_visitor( $hooked_blocks, $context, $callback = 'insert_hooked_blocks' ) {
    return function ( &$block, &$parent_block = null, $prev = null ) use ( $hooked_blocks, $context, $callback ) {
        _inject_theme_attribute_in_template_part_block( $block );

        $markup = '';

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

        $markup .= call_user_func_array(
            $callback,
            array( &$block, 'before', $hooked_blocks, $context )
        );

        return $markup;
    };
}

注意事项

  • 此函数为内部使用,主要用于 apply_block_hooks_to_content() 等内部函数,不建议在插件或主题中直接调用。
  • 参数 $callback 默认为 'insert_hooked_blocks',可自定义以控制钩子块的插入逻辑。
  • 函数返回的闭包会修改传入的 $block 和 $parent_block 引用,需注意副作用。

📄 原文内容

Returns a function that injects the theme attribute into, and hooked blocks before, a given block.

Description

The returned function can be used as $pre_callback argument to traverse_and_serialize_block(s), where it will inject the theme attribute into all Template Part blocks, and prepend the markup for any blocks hooked before the given block and as its parent’s first_child, respectively.

This function is meant for internal use only.

Parameters

$hooked_blocksarrayrequired
An array of blocks hooked to another given 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 before it.

Source

function make_before_block_visitor( $hooked_blocks, $context, $callback = 'insert_hooked_blocks' ) {
	/**
	 * Injects hooked blocks before the given block, injects the `theme` attribute into Template Part blocks, and returns the serialized markup.
	 *
	 * If the current block is a Template Part block, inject the `theme` attribute.
	 * Furthermore, prepend the markup for any blocks hooked `before` the given block and as its parent's
	 * `first_child`, respectively, to the serialized markup for the given block.
	 *
	 * @param array $block        The block to inject the theme attribute into, and hooked blocks before. Passed by reference.
	 * @param array $parent_block The parent block of the given block. Passed by reference. Default null.
	 * @param array $prev         The previous sibling block of the given block. Default null.
	 * @return string The serialized markup for the given block, with the markup for any hooked blocks prepended to it.
	 */
	return function ( &$block, &$parent_block = null, $prev = null ) use ( $hooked_blocks, $context, $callback ) {
		_inject_theme_attribute_in_template_part_block( $block );

		$markup = '';

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

		$markup .= call_user_func_array(
			$callback,
			array( &$block, 'before', $hooked_blocks, $context )
		);

		return $markup;
	};
}

Changelog

Version Description
6.5.0 Added $callback argument.
6.4.0 Introduced.