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

Block: Give traverse_and_serialize_block(s)' post-callback access to block instance #5525

Closed
Show file tree
Hide file tree
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
15 changes: 9 additions & 6 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -1062,18 +1062,20 @@ function traverse_and_serialize_block( $block, $pre_callback = null, $post_callb
);
}

$block_content .= traverse_and_serialize_block( $inner_block, $pre_callback, $post_callback );

if ( is_callable( $post_callback ) ) {
$next = count( $block['innerBlocks'] ) - 1 === $block_index
? null
: $block['innerBlocks'][ $block_index + 1 ];

$block_content .= call_user_func_array(
$post_markup = call_user_func_array(
$post_callback,
array( &$inner_block, $block, $next )
);
}

$block_content .= traverse_and_serialize_block( $inner_block, $pre_callback, $post_callback );
$block_content .= isset( $post_markup ) ? $post_markup : '';

++$block_index;
}
}
Expand Down Expand Up @@ -1135,18 +1137,19 @@ function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_cal
);
}

$result .= traverse_and_serialize_block( $block, $pre_callback, $post_callback );

if ( is_callable( $post_callback ) ) {
$next = count( $blocks ) - 1 === $index
? null
: $blocks[ $index + 1 ];

$result .= call_user_func_array(
$post_markup = call_user_func_array(
$post_callback,
array( &$block, null, $next ) // At the top level, there is no parent block to pass to the callback.
);
}

$result .= traverse_and_serialize_block( $block, $pre_callback, $post_callback );
$result .= isset( $post_markup ) ? $post_markup : '';
}

return $result;
Expand Down
17 changes: 17 additions & 0 deletions tests/phpunit/tests/blocks/serialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ public function test_traverse_and_serialize_blocks_pre_callback_modifies_current
);
}

/**
* @ticket 59669
*
* @covers ::traverse_and_serialize_blocks
*/
public function test_traverse_and_serialize_blocks_post_callback_modifies_current_block() {
$markup = "<!-- wp:outer --><!-- wp:inner {\"key\":\"value\"} -->Example.<!-- /wp:inner -->\n\nExample.\n\n<!-- wp:void /--><!-- /wp:outer -->";
$blocks = parse_blocks( $markup );

$actual = traverse_and_serialize_blocks( $blocks, null, array( __CLASS__, 'add_attribute_to_inner_block' ) );

$this->assertSame(
"<!-- wp:outer --><!-- wp:inner {\"key\":\"value\",\"myattr\":\"myvalue\"} -->Example.<!-- /wp:inner -->\n\nExample.\n\n<!-- wp:void /--><!-- /wp:outer -->",
$actual
);
}

public static function add_attribute_to_inner_block( &$block ) {
if ( 'core/inner' === $block['blockName'] ) {
$block['attrs']['myattr'] = 'myvalue';
Expand Down
Loading