函数文档

traverse_and_serialize_block()

💡 云策文档标注

概述

traverse_and_serialize_block() 函数用于递归遍历解析后的块树,并在序列化前后应用回调函数。它主要用于修改保存的块或向返回值注入标记,但仅限于内部使用。

关键要点

  • 递归遍历块及其内部块,在序列化前应用 $pre_callback,序列化后应用 $post_callback。
  • 回调函数接收当前块引用、父块和相邻块作为参数,并可修改块内容。
  • 如果回调返回字符串值,将分别前置或追加到序列化块标记中。
  • 序列化块包括注释分隔符和所有序列化属性。
  • 此函数适用于需要修改保存块或注入标记的场景,但应优先使用 serialize_block() 准备保存到文章内容的块。

代码示例

function traverse_and_serialize_block( $block, $pre_callback = null, $post_callback = null ) {
    $block_content = '';
    $block_index   = 0;

    foreach ( $block['innerContent'] as $chunk ) {
        if ( is_string( $chunk ) ) {
            $block_content .= $chunk;
        } else {
            $inner_block = $block['innerBlocks'][ $block_index ];

            if ( is_callable( $pre_callback ) ) {
                $prev = 0 === $block_index
                    ? null
                    : $block['innerBlocks'][ $block_index - 1 ];

                $block_content .= call_user_func_array(
                    $pre_callback,
                    array( &$inner_block, &$block, $prev )
                );
            }

            if ( is_callable( $post_callback ) ) {
                $next = count( $block['innerBlocks'] ) - 1 === $block_index
                    ? null
                    : $block['innerBlocks'][ $block_index + 1 ];

                $post_markup = call_user_func_array(
                    $post_callback,
                    array( &$inner_block, &$block, $next )
                );
            }

            $block_content .= traverse_and_serialize_block( $inner_block, $pre_callback, $post_callback );
            $block_content .= isset( $post_markup ) ? $post_markup : '';

            ++$block_index;
        }
    }

    if ( ! is_array( $block['attrs'] ) ) {
        $block['attrs'] = array();
    }

    return get_comment_delimited_block_content(
        $block['blockName'],
        $block['attrs'],
        $block_content
    );
}

注意事项

  • 此函数仅用于内部使用,开发者应谨慎调用。
  • 参数 $block 必须是关联数组形式的解析块对象,参考 WP_Block_Parser_Block。
  • 回调函数 $pre_callback 和 $post_callback 是可选的,默认值为 null。
  • 函数返回序列化块标记字符串。

📄 原文内容

Traverses a parsed block tree and applies callbacks before and after serializing it.

Description

Recursively traverses the block and its inner blocks and applies the two callbacks provided as arguments, the first one before serializing the block, and the second one after serializing it.
If either callback returns a string value, it will be prepended and appended to the serialized block markup, respectively.

The callbacks will receive a reference to the current block as their first argument, so that they can also modify it, and the current block’s parent block as second argument. Finally, the $pre_callback receives the previous block, whereas the $post_callback receives the next block as third argument.

Serialized blocks are returned including comment delimiters, and with all attributes serialized.

This function should be used when there is a need to modify the saved block, or to inject markup into the return value. Prefer serialize_block when preparing a block to be saved to post content.

This function is meant for internal use only.

See also

Parameters

$blockarrayrequired
An associative array of a single parsed block object. See WP_Block_Parser_Block.
$pre_callbackcallableoptional
Callback to run on each block in the tree before it is traversed and serialized.
It is called with the following arguments: &$block, $parent_block, $previous_block.
Its string return value will be prepended to the serialized block markup.

Default:null

$post_callbackcallableoptional
Callback to run on each block in the tree after it is traversed and serialized.
It is called with the following arguments: &$block, $parent_block, $next_block.
Its string return value will be appended to the serialized block markup.

Default:null

Return

string Serialized block markup.

Source

function traverse_and_serialize_block( $block, $pre_callback = null, $post_callback = null ) {
	$block_content = '';
	$block_index   = 0;

	foreach ( $block['innerContent'] as $chunk ) {
		if ( is_string( $chunk ) ) {
			$block_content .= $chunk;
		} else {
			$inner_block = $block['innerBlocks'][ $block_index ];

			if ( is_callable( $pre_callback ) ) {
				$prev = 0 === $block_index
					? null
					: $block['innerBlocks'][ $block_index - 1 ];

				$block_content .= call_user_func_array(
					$pre_callback,
					array( &$inner_block, &$block, $prev )
				);
			}

			if ( is_callable( $post_callback ) ) {
				$next = count( $block['innerBlocks'] ) - 1 === $block_index
					? null
					: $block['innerBlocks'][ $block_index + 1 ];

				$post_markup = call_user_func_array(
					$post_callback,
					array( &$inner_block, &$block, $next )
				);
			}

			$block_content .= traverse_and_serialize_block( $inner_block, $pre_callback, $post_callback );
			$block_content .= isset( $post_markup ) ? $post_markup : '';

			++$block_index;
		}
	}

	if ( ! is_array( $block['attrs'] ) ) {
		$block['attrs'] = array();
	}

	return get_comment_delimited_block_content(
		$block['blockName'],
		$block['attrs'],
		$block_content
	);
}

Changelog

Version Description
6.4.0 Introduced.