Make WordPress Core


Ignore:
Timestamp:
09/19/2023 12:48:41 PM (10 months ago)
Author:
gziolo
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r56618 r56620  
    928928 *
    929929 * @since 5.3.1
    930  * @since 6.4.0 The `$callback` parameter was added.
    931  *
    932  * @param array         $block    A representative array of a single parsed block object. See WP_Block_Parser_Block.
    933  * @param callable|null $callback Optional. Callback to run on each block in the tree before serialization. Default null.
     930 *
     931 * @param array $block A representative array of a single parsed block object. See WP_Block_Parser_Block.
    934932 * @return string String of rendered HTML.
    935933 */
    936 function serialize_block( $block, $callback = null ) {
    937     if ( is_callable( $callback ) ) {
    938         $block = call_user_func( $callback, $block );
    939     }
    940 
     934function serialize_block( $block ) {
    941935    $block_content = '';
    942936
    943937    $index = 0;
    944938    foreach ( $block['innerContent'] as $chunk ) {
    945         $block_content .= is_string( $chunk ) ? $chunk : serialize_block( $block['innerBlocks'][ $index++ ], $callback );
     939        $block_content .= is_string( $chunk ) ? $chunk : serialize_block( $block['innerBlocks'][ $index++ ] );
    946940    }
    947941
     
    962956 *
    963957 * @since 5.3.1
    964  * @since 6.4.0 The `$callback` parameter was added.
    965  *
    966  * @param array[]       $blocks   An array of representative arrays of parsed block objects. See serialize_block().
    967  * @param callable|null $callback Optional. Callback to run on each block in the tree before serialization. Default null.
     958 *
     959 * @param array[] $blocks An array of representative arrays of parsed block objects. See serialize_block().
    968960 * @return string String of rendered HTML.
    969961 */
    970 function serialize_blocks( $blocks, $callback = null ) {
     962function serialize_blocks( $blocks ) {
     963    return implode( '', array_map( 'serialize_block', $blocks ) );
     964}
     965
     966/**
     967 * Traverses the block applying transformations using the callback provided and returns the content of a block,
     968 * including comment delimiters, serializing all attributes from the given parsed block.
     969 *
     970 * This should be used when there is a need to modify the saved block.
     971 * Prefer `serialize_block` when preparing a block to be saved to post content.
     972 *
     973 * @since 6.4.0
     974 *
     975 * @see serialize_block()
     976 *
     977 * @param array    $block    A representative array of a single parsed block object. See WP_Block_Parser_Block.
     978 * @param callable $callback Callback to run on each block in the tree before serialization.
     979 *                           It is called with the following arguments: $block, $parent_block, $block_index, $chunk_index.
     980 * @return string String of rendered HTML.
     981 */
     982function traverse_and_serialize_block( $block, $callback ) {
     983    $block_content = '';
     984    $block_index   = 0;
     985
     986    foreach ( $block['innerContent'] as $chunk_index => $chunk ) {
     987        if ( is_string( $chunk ) ) {
     988            $block_content .= $chunk;
     989        } else {
     990            $inner_block = call_user_func(
     991                $callback,
     992                $block['innerBlocks'][ $block_index ],
     993                $block,
     994                $block_index,
     995                $chunk_index
     996            );
     997            $block_index++;
     998            $block_content .= traverse_and_serialize_block( $inner_block, $callback );
     999        }
     1000    }
     1001
     1002    if ( ! is_array( $block['attrs'] ) ) {
     1003        $block['attrs'] = array();
     1004    }
     1005
     1006    return get_comment_delimited_block_content(
     1007        $block['blockName'],
     1008        $block['attrs'],
     1009        $block_content
     1010    );
     1011}
     1012
     1013/**
     1014 * Traverses the blocks applying transformations using the callback provided,
     1015 * and returns a joined string of the aggregate serialization of the given parsed blocks.
     1016 *
     1017 * This should be used when there is a need to modify the saved blocks.
     1018 * Prefer `serialize_blocks` when preparing blocks to be saved to post content.
     1019 *
     1020 * @since 6.4.0
     1021 *
     1022 * @see serialize_blocks()
     1023 *
     1024 * @param array[]  $blocks   An array of representative arrays of parsed block objects. See serialize_block().
     1025 * @param callable $callback Callback to run on each block in the tree before serialization.
     1026 *                           It is called with the following arguments: $block, $parent_block, $block_index, $chunk_index.
     1027 * @return string String of rendered HTML.
     1028 */
     1029function traverse_and_serialize_blocks( $blocks, $callback ) {
    9711030    $result = '';
    9721031    foreach ( $blocks as $block ) {
    973         $result .= serialize_block( $block, $callback );
     1032        // At the top level, there is no parent block, block index, or chunk index to pass to the callback.
     1033        $block = call_user_func( $callback, $block );
     1034        $result .= traverse_and_serialize_block( $block, $callback );
    9741035    }
    9751036    return $result;
Note: See TracChangeset for help on using the changeset viewer.