traverse_and_serialize_blocks

函数
traverse_and_serialize_blocks ( $blocks, $pre_callback = null, $post_callback = null )
参数
  • (array[]) $blocks An array of parsed blocks. See WP_Block_Parser_Block.
    Required:
  • (callable) $pre_callback 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.
    Required:
    Default: null
  • (callable) $post_callback 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.
    Required:
    Default: null
返回值
  • (string) Serialized block markup.
相关
  • serialize_blocks()
定义位置
相关方法
traverse_and_serialize_blockserialize_blocksserialize_blockrender_blockserialize_block_attributes
引入
6.4.0
弃用
-

给定一个已解析的区块树数组,在序列化区块树之前和之后应用回调函数,并返回其串联输出。

递归遍历区块及其内部区块,并应用作为参数提供的两个回调函数:第一个回调函数在区块序列化之前,第二个回调函数在序列化之后。如果任一回调函数返回字符串值,则该字符串值将分别被添加到序列化后的区块标记的前面和后面。

回调函数将接收当前区块的引用作为其第一个参数,以便它们也可以修改该区块;并将当前区块的父区块作为第二个参数。最后,“$pre_callback”接收前一个区块,而“$post_callback”接收下一个区块作为第三个参数。

返回的序列化区块包含注释分隔符,并且所有属性都已序列化。

当需要修改已保存的区块或将标记注入返回值时,应使用此函数。在准备要保存到帖子内容的区块时,建议使用“serialize_blocks”。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_callback = null ) {
$result = '';
$parent_block = null; // At the top level, there is no parent block to pass to the callbacks; yet the callbacks expect a reference.
foreach ( $blocks as $index => $block ) {
if ( is_callable( $pre_callback ) ) {
$prev = 0 === $index
? null
: $blocks[ $index - 1 ];
$result .= call_user_func_array(
$pre_callback,
array( &$block, &$parent_block, $prev )
);
}
if ( is_callable( $post_callback ) ) {
$next = count( $blocks ) - 1 === $index
? null
: $blocks[ $index + 1 ];
$post_markup = call_user_func_array(
$post_callback,
array( &$block, &$parent_block, $next )
);
}
$result .= traverse_and_serialize_block( $block, $pre_callback, $post_callback );
$result .= isset( $post_markup ) ? $post_markup : '';
}
return $result;
}
function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_callback = null ) { $result = ''; $parent_block = null; // At the top level, there is no parent block to pass to the callbacks; yet the callbacks expect a reference. foreach ( $blocks as $index => $block ) { if ( is_callable( $pre_callback ) ) { $prev = 0 === $index ? null : $blocks[ $index - 1 ]; $result .= call_user_func_array( $pre_callback, array( &$block, &$parent_block, $prev ) ); } if ( is_callable( $post_callback ) ) { $next = count( $blocks ) - 1 === $index ? null : $blocks[ $index + 1 ]; $post_markup = call_user_func_array( $post_callback, array( &$block, &$parent_block, $next ) ); } $result .= traverse_and_serialize_block( $block, $pre_callback, $post_callback ); $result .= isset( $post_markup ) ? $post_markup : ''; } return $result; }
function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_callback = null ) {
	$result       = '';
	$parent_block = null; // At the top level, there is no parent block to pass to the callbacks; yet the callbacks expect a reference.

	foreach ( $blocks as $index => $block ) {
		if ( is_callable( $pre_callback ) ) {
			$prev = 0 === $index
				? null
				: $blocks[ $index - 1 ];

			$result .= call_user_func_array(
				$pre_callback,
				array( &$block, &$parent_block, $prev )
			);
		}

		if ( is_callable( $post_callback ) ) {
			$next = count( $blocks ) - 1 === $index
				? null
				: $blocks[ $index + 1 ];

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

		$result .= traverse_and_serialize_block( $block, $pre_callback, $post_callback );
		$result .= isset( $post_markup ) ? $post_markup : '';
	}

	return $result;
}

常见问题

FAQs
查看更多 >