Blocks: Introduce a variation of serialize blocks helper with traversing

Introduces two new functions `traverse_and_serialize_blocks` and `traverse_and_serialize_block` with the additional `$callback` argument. It is possible to pass parent block, block index, chunk index to the callback argument.

Reverts changes applied to `serialize_blocks` and `serialize_block` in #59327 with [56557].

Props ockham, mukesh27.
See #59313.



Built from https://develop.svn.wordpress.org/trunk@56620


git-svn-id: http://core.svn.wordpress.org/trunk@56132 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
gziolo 2023-09-19 12:50:19 +00:00
parent 8fbd793f33
commit d3a8869891
3 changed files with 77 additions and 16 deletions

View File

@ -609,7 +609,7 @@ function _build_block_template_result_from_file( $template_file, $template_type
}
$blocks = parse_blocks( $template_content );
$template->content = serialize_blocks( $blocks, '_inject_theme_attribute_in_template_part_block' );
$template->content = traverse_and_serialize_blocks( $blocks, '_inject_theme_attribute_in_template_part_block' );
return $template;
}

View File

@ -927,22 +927,16 @@ function get_comment_delimited_block_content( $block_name, $block_attributes, $b
* instead preserve the markup as parsed.
*
* @since 5.3.1
* @since 6.4.0 The `$callback` parameter was added.
*
* @param array $block A representative array of a single parsed block object. See WP_Block_Parser_Block.
* @param callable|null $callback Optional. Callback to run on each block in the tree before serialization. Default null.
* @param array $block A representative array of a single parsed block object. See WP_Block_Parser_Block.
* @return string String of rendered HTML.
*/
function serialize_block( $block, $callback = null ) {
if ( is_callable( $callback ) ) {
$block = call_user_func( $callback, $block );
}
function serialize_block( $block ) {
$block_content = '';
$index = 0;
foreach ( $block['innerContent'] as $chunk ) {
$block_content .= is_string( $chunk ) ? $chunk : serialize_block( $block['innerBlocks'][ $index++ ], $callback );
$block_content .= is_string( $chunk ) ? $chunk : serialize_block( $block['innerBlocks'][ $index++ ] );
}
if ( ! is_array( $block['attrs'] ) ) {
@ -961,16 +955,83 @@ function serialize_block( $block, $callback = null ) {
* parsed blocks.
*
* @since 5.3.1
* @since 6.4.0 The `$callback` parameter was added.
*
* @param array[] $blocks An array of representative arrays of parsed block objects. See serialize_block().
* @param callable|null $callback Optional. Callback to run on each block in the tree before serialization. Default null.
* @param array[] $blocks An array of representative arrays of parsed block objects. See serialize_block().
* @return string String of rendered HTML.
*/
function serialize_blocks( $blocks, $callback = null ) {
function serialize_blocks( $blocks ) {
return implode( '', array_map( 'serialize_block', $blocks ) );
}
/**
* Traverses the block applying transformations using the callback provided and returns the content of a block,
* including comment delimiters, serializing all attributes from the given parsed block.
*
* This should be used when there is a need to modify the saved block.
* Prefer `serialize_block` when preparing a block to be saved to post content.
*
* @since 6.4.0
*
* @see serialize_block()
*
* @param array $block A representative array of a single parsed block object. See WP_Block_Parser_Block.
* @param callable $callback Callback to run on each block in the tree before serialization.
* It is called with the following arguments: $block, $parent_block, $block_index, $chunk_index.
* @return string String of rendered HTML.
*/
function traverse_and_serialize_block( $block, $callback ) {
$block_content = '';
$block_index = 0;
foreach ( $block['innerContent'] as $chunk_index => $chunk ) {
if ( is_string( $chunk ) ) {
$block_content .= $chunk;
} else {
$inner_block = call_user_func(
$callback,
$block['innerBlocks'][ $block_index ],
$block,
$block_index,
$chunk_index
);
$block_index++;
$block_content .= traverse_and_serialize_block( $inner_block, $callback );
}
}
if ( ! is_array( $block['attrs'] ) ) {
$block['attrs'] = array();
}
return get_comment_delimited_block_content(
$block['blockName'],
$block['attrs'],
$block_content
);
}
/**
* Traverses the blocks applying transformations using the callback provided,
* and returns a joined string of the aggregate serialization of the given parsed blocks.
*
* This should be used when there is a need to modify the saved blocks.
* Prefer `serialize_blocks` when preparing blocks to be saved to post content.
*
* @since 6.4.0
*
* @see serialize_blocks()
*
* @param array[] $blocks An array of representative arrays of parsed block objects. See serialize_block().
* @param callable $callback Callback to run on each block in the tree before serialization.
* It is called with the following arguments: $block, $parent_block, $block_index, $chunk_index.
* @return string String of rendered HTML.
*/
function traverse_and_serialize_blocks( $blocks, $callback ) {
$result = '';
foreach ( $blocks as $block ) {
$result .= serialize_block( $block, $callback );
// At the top level, there is no parent block, block index, or chunk index to pass to the callback.
$block = call_user_func( $callback, $block );
$result .= traverse_and_serialize_block( $block, $callback );
}
return $result;
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.4-alpha-56619';
$wp_version = '6.4-alpha-56620';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.