Changeset 56620 for trunk/src/wp-includes/blocks.php
- Timestamp:
- 09/19/2023 12:48:41 PM (17 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks.php
r56618 r56620 928 928 * 929 929 * @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. 934 932 * @return string String of rendered HTML. 935 933 */ 936 function serialize_block( $block, $callback = null ) { 937 if ( is_callable( $callback ) ) { 938 $block = call_user_func( $callback, $block ); 939 } 940 934 function serialize_block( $block ) { 941 935 $block_content = ''; 942 936 943 937 $index = 0; 944 938 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++ ] ); 946 940 } 947 941 … … 962 956 * 963 957 * @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(). 968 960 * @return string String of rendered HTML. 969 961 */ 970 function serialize_blocks( $blocks, $callback = null ) { 962 function 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 */ 982 function 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 */ 1029 function traverse_and_serialize_blocks( $blocks, $callback ) { 971 1030 $result = ''; 972 1031 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 ); 974 1035 } 975 1036 return $result;
Note: See TracChangeset
for help on using the changeset viewer.