Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blocks: Pass parent block, block index, chunk index to serialize_block(s) callback argument #5242

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -853,18 +853,24 @@ function get_comment_delimited_block_content( $block_name, $block_attributes, $b
*
* @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.
* It is called with the following arguments: $block, $parent_block, $block_index, $chunk_index.
* @return string String of rendered HTML.
*/
function serialize_block( $block, $callback = null ) {
if ( is_callable( $callback ) ) {
$block = call_user_func( $callback, $block );
}

$block_content = '';

$index = 0;
foreach ( $block['innerContent'] as $chunk ) {
$block_content .= is_string( $chunk ) ? $chunk : serialize_block( $block['innerBlocks'][ $index++ ], $callback );
$block_index = 0;
foreach ( $block['innerContent'] as $chunk_index => $chunk ) {
if ( is_string( $chunk ) ) {
$block_content .= $chunk;
} else {
$inner_block = $block['innerBlocks'][ $block_index ];
if ( is_callable( $callback ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a fan of your suggestion to split the loop, but without that, we could at least examine the impact of trapping $is_callable = is_callable( $callback ) outside of the loop. it appears like is_callable() is a function call and not a PHP operand, so it could be notable in its cost, particularly on long posts.

in any case, here it seems a little odd to embed that check since $callback shouldn't change between iterations and there's no need to recompute it every time.

$inner_block = call_user_func( $callback, $inner_block, $block, $block_index, $chunk_index );
}
$block_index++;
$block_content .= serialize_block( $inner_block, $callback );
}
}

if ( ! is_array( $block['attrs'] ) ) {
Expand All @@ -887,11 +893,16 @@ function serialize_block( $block, $callback = null ) {
*
* @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.
* It is called with the following arguments: $block, $parent_block, $block_index, $chunk_index.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment appears to directly contradict the code below it

* @return string String of rendered HTML.
*/
function serialize_blocks( $blocks, $callback = null ) {
$result = '';
foreach ( $blocks as $block ) {
if ( is_callable( $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 .= serialize_block( $block, $callback );
}
return $result;
Expand Down
Loading