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.




git-svn-id: https://develop.svn.wordpress.org/trunk@56620 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Greg Ziółkowski
2023-09-19 12:48:41 +00:00
parent 3916624c13
commit 48b9b6cbab
3 changed files with 105 additions and 21 deletions

View File

@@ -13,14 +13,15 @@ class Tests_Blocks_Serialize extends WP_UnitTestCase {
/**
* @dataProvider data_serialize_identity_from_parsed
*
* @param string $original Original block markup.
*/
public function test_serialize_identity_from_parsed( $original ) {
$blocks = parse_blocks( $original );
$actual = serialize_blocks( $blocks );
$expected = $original;
$actual = serialize_blocks( $blocks );
$this->assertSame( $expected, $actual );
$this->assertSame( $original, $actual );
}
public function data_serialize_identity_from_parsed() {
@@ -58,13 +59,13 @@ class Tests_Blocks_Serialize extends WP_UnitTestCase {
/**
* @ticket 59327
*
* @covers ::serialize_blocks
* @covers ::traverse_and_serialize_blocks
*/
public function test_callback_argument() {
public function test_traverse_and_serialize_blocks() {
$markup = "<!-- wp:outer --><!-- wp:inner {\"key\":\"value\"} -->Example.<!-- /wp:inner -->\n\nExample.\n\n<!-- wp:void /--><!-- /wp:outer -->";
$blocks = parse_blocks( $markup );
$actual = serialize_blocks( $blocks, array( __CLASS__, 'add_attribute_to_inner_block' ) );
$actual = traverse_and_serialize_blocks( $blocks, 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 -->",
@@ -78,4 +79,26 @@ class Tests_Blocks_Serialize extends WP_UnitTestCase {
}
return $block;
}
/**
* @ticket 59327
*
* @covers ::traverse_and_serialize_blocks
*
* @dataProvider data_serialize_identity_from_parsed
*
* @param string $original Original block markup.
*/
public function test_traverse_and_serialize_identity_from_parsed( $original ) {
$blocks = parse_blocks( $original );
$actual = traverse_and_serialize_blocks(
$blocks,
function ( $block ) {
return $block;
}
);
$this->assertSame( $original, $actual );
}
}