General: Add optional callback argument to serialize_block(s).

Allow passing a function callback to serialize_block(s) that is invoked on each node in the tree of parsed blocks as it is traversed for serialization.

A function argument was chosen for passing the callback function as it reflects a pattern familiar from other algorithms that apply a given callback function while traversing a data structure -- most notably PHP's own `array_map()`.

Introducing a filter was considered as an alternative but ultimately dismissed. For a fairly low-level and general-purpose function such as `serialize_block()`, using a filter to pass the callback seemed risky, as it also requires ''removing'' that filter after usage in order to prevent the callback from being accidentally applied when `serialize_block()` is called in an entirely different context.

Introducing a separate function for applying a given operation during tree traversal (i.e. independently of serialization) was also considered but dismissed, as it would unnecessarily duplicate tree traversal.

Props dlh, gziolo.
Fixes #59327. See #59313.

git-svn-id: https://develop.svn.wordpress.org/trunk@56557 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Bernie Reiter 2023-09-12 13:00:32 +00:00
parent cc775b9985
commit 44e71c8e70
2 changed files with 42 additions and 6 deletions

View File

@ -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'] ) ) {
@ -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;
}
/**

View File

@ -54,4 +54,28 @@ class Tests_Blocks_Serialize extends WP_UnitTestCase {
$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() {
$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;
}
}