mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
ockham
wants to merge
9
commits into
WordPress:trunk
from
ockham:update/serialize-block-pass-parent-to-callback
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ef30398
serialize_blocks: Move callback invocation around
ockham 7d457ac
Pass parent to callback
ockham 1314cbd
Ugh, fix
ockham 2c6309c
Increment later
ockham 5b0c8e1
Pass block index to callback
ockham 681613d
Rename index to block_index
ockham bf9ee4c
Pass chunk_index to callback
ockham 1098ef2
Add PHPDoc explanation
ockham 04a8d1a
Add explanatory comment
ockham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ) ) { | ||
$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'] ) ) { | ||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 likeis_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.