Changeset 56620
- Timestamp:
- 09/19/2023 12:48:41 PM (12 months ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/block-template-utils.php
r56578 r56620 610 610 611 611 $blocks = parse_blocks( $template_content ); 612 $template->content = serialize_blocks( $blocks, '_inject_theme_attribute_in_template_part_block' );612 $template->content = traverse_and_serialize_blocks( $blocks, '_inject_theme_attribute_in_template_part_block' ); 613 613 614 614 return $template; -
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; -
trunk/tests/phpunit/tests/blocks/serialize.php
r56557 r56620 14 14 /** 15 15 * @dataProvider data_serialize_identity_from_parsed 16 * 17 * @param string $original Original block markup. 16 18 */ 17 19 public function test_serialize_identity_from_parsed( $original ) { 18 20 $blocks = parse_blocks( $original ); 19 21 20 $actual = serialize_blocks( $blocks ); 21 $expected = $original; 22 $actual = serialize_blocks( $blocks ); 22 23 23 $this->assertSame( $ expected, $actual );24 $this->assertSame( $original, $actual ); 24 25 } 25 26 … … 59 60 * @ticket 59327 60 61 * 61 * @covers :: serialize_blocks62 * @covers ::traverse_and_serialize_blocks 62 63 */ 63 public function test_ callback_argument() {64 public function test_traverse_and_serialize_blocks() { 64 65 $markup = "<!-- wp:outer --><!-- wp:inner {\"key\":\"value\"} -->Example.<!-- /wp:inner -->\n\nExample.\n\n<!-- wp:void /--><!-- /wp:outer -->"; 65 66 $blocks = parse_blocks( $markup ); 66 67 67 $actual = serialize_blocks( $blocks, array( __CLASS__, 'add_attribute_to_inner_block' ) );68 $actual = traverse_and_serialize_blocks( $blocks, array( __CLASS__, 'add_attribute_to_inner_block' ) ); 68 69 69 70 $this->assertSame( … … 79 80 return $block; 80 81 } 82 83 /** 84 * @ticket 59327 85 * 86 * @covers ::traverse_and_serialize_blocks 87 * 88 * @dataProvider data_serialize_identity_from_parsed 89 * 90 * @param string $original Original block markup. 91 */ 92 public function test_traverse_and_serialize_identity_from_parsed( $original ) { 93 $blocks = parse_blocks( $original ); 94 95 $actual = traverse_and_serialize_blocks( 96 $blocks, 97 function ( $block ) { 98 return $block; 99 } 100 ); 101 102 $this->assertSame( $original, $actual ); 103 } 81 104 }
Note: See TracChangeset
for help on using the changeset viewer.