traverse_and_serialize_blocks()
概述
traverse_and_serialize_blocks() 是一个 WordPress 内部函数,用于递归遍历解析后的块数组,并在序列化前后应用回调函数,最终返回拼接的序列化标记。它主要用于修改已保存的块或向返回值注入标记。
关键要点
- 函数接收解析后的块数组和两个可选回调:$pre_callback(序列化前调用)和$post_callback(序列化后调用)。
- 回调函数可以接收当前块的引用、父块以及相邻块(前一个或后一个)作为参数,并允许修改块。
- 如果回调返回字符串值,该字符串将分别前置或附加到序列化块的标记中。
- 序列化块包括注释分隔符和所有序列化属性,适用于修改保存的块或注入标记场景。
- 此函数仅供内部使用,在准备保存到文章内容时,应优先使用 serialize_blocks()。
代码示例
function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_callback = null ) {
$result = '';
$parent_block = null;
$pre_callback_is_callable = is_callable( $pre_callback );
$post_callback_is_callable = is_callable( $post_callback );
foreach ( $blocks as $index => $block ) {
if ( $pre_callback_is_callable ) {
$prev = 0 === $index
? null
: $blocks[ $index - 1 ];
$result .= call_user_func_array(
$pre_callback,
array( &$block, &$parent_block, $prev )
);
}
if ( $post_callback_is_callable ) {
$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;
}注意事项
- 此函数是内部函数,不建议在插件或主题中直接使用,以避免兼容性问题。
- 在需要修改块内容或添加额外标记时使用,但保存到文章内容时应改用 serialize_blocks()。
- 回调函数需正确处理块引用和参数,以避免意外副作用。
Given an array of parsed block trees, applies callbacks before and after serializing them and returns their concatenated output.
Description
Recursively traverses the blocks and their inner blocks and applies the two callbacks provided as arguments, the first one before serializing a block, and the second one after serializing.
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 blocks, or to inject markup into the return value. Prefer serialize_blocks when preparing blocks to be saved to post content.
This function is meant for internal use only.
See also
Parameters
$blocksarray[]required-
An array of parsed blocks. 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
Source
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.
$pre_callback_is_callable = is_callable( $pre_callback );
$post_callback_is_callable = is_callable( $post_callback );
foreach ( $blocks as $index => $block ) {
if ( $pre_callback_is_callable ) {
$prev = 0 === $index
? null
: $blocks[ $index - 1 ];
$result .= call_user_func_array(
$pre_callback,
array( &$block, &$parent_block, $prev )
);
}
if ( $post_callback_is_callable ) {
$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;
}
Changelog
| Version | Description |
|---|---|
| 6.4.0 | Introduced. |