mirror of
git://develop.git.wordpress.org/
synced 2025-03-23 13:29:56 +01:00
Blocks: During traversal, allow post callback to modify block.
Both the `$pre_callback` and `$post_callback` functions that are given as arguments to `traverse_and_serialize_block(s)` receive a reference to the current block as their first argument. However, while any changes that the "pre" callback makes to the block are reflected by the serialized markup, the same wasn't true for the "post" callback: Any changes that it made were only applied ''after'' the block had already been serialized. This commit changes the behavior such that `$post_callback`'s changes to the current block are also reflected in the serialized markup. Reviewed by hellofromTonya. Merges [56970] to the 6.4 branch. See #59646. Props gziolo. Fixes #59669. git-svn-id: https://develop.svn.wordpress.org/branches/6.4@57043 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
ce100be06a
commit
65e39deeec
@ -1063,18 +1063,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;
|
||||
}
|
||||
}
|
||||
@ -1138,18 +1140,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, &$parent_block, $next )
|
||||
);
|
||||
}
|
||||
|
||||
$result .= traverse_and_serialize_block( $block, $pre_callback, $post_callback );
|
||||
$result .= isset( $post_markup ) ? $post_markup : '';
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
@ -74,6 +74,23 @@ class Tests_Blocks_Serialize extends WP_UnitTestCase {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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';
|
||||
|
Loading…
x
Reference in New Issue
Block a user