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
檢視更多 >