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

Add optional callback argument to serialize_block(s) #5187

Closed
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
24 changes: 18 additions & 6 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -794,16 +794,22 @@ function get_comment_delimited_block_content( $block_name, $block_attributes, $b
* instead preserve the markup as parsed.
*
* @since 5.3.1
* @since 6.4.0 The `$callback` parameter was added.
*
* @param array $block A representative array of a single parsed block object. See WP_Block_Parser_Block.
* @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.
* @return string String of rendered HTML.
*/
function serialize_block( $block ) {
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++ ] );
$block_content .= is_string( $chunk ) ? $chunk : serialize_block( $block['innerBlocks'][ $index++ ], $callback );
}

if ( ! is_array( $block['attrs'] ) ) {
Expand All @@ -822,12 +828,18 @@ function serialize_block( $block ) {
* parsed blocks.
*
* @since 5.3.1
* @since 6.4.0 The `$callback` parameter was added.
*
* @param array[] $blocks An array of representative arrays of parsed block objects. See serialize_block().
* @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.
* @return string String of rendered HTML.
*/
function serialize_blocks( $blocks ) {
return implode( '', array_map( 'serialize_block', $blocks ) );
function serialize_blocks( $blocks, $callback = null ) {
$result = '';
foreach ( $blocks as $block ) {
$result .= serialize_block( $block, $callback );
};
return $result;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/phpunit/tests/blocks/serialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,28 @@ public function test_serialized_block_name() {
$this->assertSame( 'example', strip_core_block_namespace( 'core/example' ) );
$this->assertSame( 'plugin/example', strip_core_block_namespace( 'plugin/example' ) );
}

/**
* @ticket 59327
*
* @covers ::serialize_blocks
*/
public function test_callback_argument() {
ockham marked this conversation as resolved.
Show resolved Hide resolved
$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' ) );

$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';
}
return $block;
}
}