Changeset 56644
- Timestamp:
- 09/21/2023 08:32:52 AM (17 months ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/block-template-utils.php
r56621 r56644 516 516 * 517 517 * @param array $block a parsed block. 518 * @return array Updated block.519 */ 520 function _inject_theme_attribute_in_template_part_block( $block ) {518 * @return void 519 */ 520 function _inject_theme_attribute_in_template_part_block( &$block ) { 521 521 if ( 522 522 'core/template-part' === $block['blockName'] && … … 525 525 $block['attrs']['theme'] = get_stylesheet(); 526 526 } 527 return $block;528 527 } 529 528 -
trunk/src/wp-includes/blocks.php
r56634 r56644 887 887 888 888 /** 889 * Traverses the block applying transformations using the callback provided and returns the content of a block, 890 * including comment delimiters, serializing all attributes from the given parsed block. 891 * 892 * This should be used when there is a need to modify the saved block. 893 * Prefer `serialize_block` when preparing a block to be saved to post content. 889 * Traverses a parsed block tree and applies callbacks before and after serializing it. 890 * 891 * Recursively traverses the block and its inner blocks and applies the two callbacks provided as 892 * arguments, the first one before serializing the block, and the second one after serializing it. 893 * If either callback returns a string value, it will be prepended and appended to the serialized 894 * block markup, respectively. 895 * 896 * The callbacks will receive a reference to the current block as their first argument, so that they 897 * can also modify it, and the current block's parent block as second argument. Finally, the 898 * `$pre_callback` receives the previous block, whereas the `$post_callback` receives 899 * the next block as third argument. 900 * 901 * Serialized blocks are returned including comment delimiters, and with all attributes serialized. 902 * 903 * This function should be used when there is a need to modify the saved block, or to inject markup 904 * into the return value. Prefer `serialize_block` when preparing a block to be saved to post content. 894 905 * 895 906 * @since 6.4.0 … … 897 908 * @see serialize_block() 898 909 * 899 * @param array $block A representative array of a single parsed block object. See WP_Block_Parser_Block. 900 * @param callable $callback Callback to run on each block in the tree before serialization. 901 * It is called with the following arguments: $block, $parent_block, $block_index, $chunk_index. 902 * @return string String of rendered HTML. 903 */ 904 function traverse_and_serialize_block( $block, $callback ) { 910 * @param array $block A representative array of a single parsed block object. See WP_Block_Parser_Block. 911 * @param callable $pre_callback Callback to run on each block in the tree before it is traversed and serialized. 912 * It is called with the following arguments: &$block, $parent_block, $previous_block. 913 * Its string return value will be prepended to the serialized block markup. 914 * @param callable $post_callback Callback to run on each block in the tree after it is traversed and serialized. 915 * It is called with the following arguments: &$block, $parent_block, $next_block. 916 * Its string return value will be appended to the serialized block markup. 917 * @return string Serialized block markup. 918 */ 919 function traverse_and_serialize_block( $block, $pre_callback = null, $post_callback = null ) { 905 920 $block_content = ''; 906 921 $block_index = 0; 907 922 908 foreach ( $block['innerContent'] as $chunk _index => $chunk) {923 foreach ( $block['innerContent'] as $chunk ) { 909 924 if ( is_string( $chunk ) ) { 910 925 $block_content .= $chunk; 911 926 } else { 912 $inner_block = call_user_func( 913 $callback, 914 $block['innerBlocks'][ $block_index ], 915 $block, 916 $block_index, 917 $chunk_index 918 ); 927 $inner_block = $block['innerBlocks'][ $block_index ]; 928 929 if ( is_callable( $pre_callback ) ) { 930 $prev = 0 === $block_index 931 ? null 932 : $block['innerBlocks'][ $block_index - 1 ]; 933 $block_content .= call_user_func_array( 934 $pre_callback, 935 array( &$inner_block, $block, $prev ) 936 ); 937 } 938 939 $block_content .= traverse_and_serialize_block( $inner_block, $pre_callback, $post_callback ); 940 941 if ( is_callable( $post_callback ) ) { 942 $next = count( $block['innerBlocks'] ) - 1 === $block_index 943 ? null 944 : $block['innerBlocks'][ $block_index + 1 ]; 945 $block_content .= call_user_func_array( 946 $post_callback, 947 array( &$inner_block, $block, $next ) 948 ); 949 } 919 950 $block_index++; 920 $block_content .= traverse_and_serialize_block( $inner_block, $callback );921 951 } 922 952 } … … 934 964 935 965 /** 936 * Traverses the blocks applying transformations using the callback provided, 937 * and returns a joined string of the aggregate serialization of the given parsed blocks. 938 * 939 * This should be used when there is a need to modify the saved blocks. 940 * Prefer `serialize_blocks` when preparing blocks to be saved to post content. 966 * Given an array of parsed block trees, applies callbacks before and after serializing them and 967 * returns their concatenated output. 968 * 969 * Recursively traverses the blocks and their inner blocks and applies the two callbacks provided as 970 * arguments, the first one before serializing a block, and the second one after serializing. 971 * If either callback returns a string value, it will be prepended and appended to the serialized 972 * block markup, respectively. 973 * 974 * The callbacks will receive a reference to the current block as their first argument, so that they 975 * can also modify it, and the current block's parent block as second argument. Finally, the 976 * `$pre_callback` receives the previous block, whereas the `$post_callback` receives 977 * the next block as third argument. 978 * 979 * Serialized blocks are returned including comment delimiters, and with all attributes serialized. 980 * 981 * This function should be used when there is a need to modify the saved blocks, or to inject markup 982 * into the return value. Prefer `serialize_blocks` when preparing blocks to be saved to post content. 941 983 * 942 984 * @since 6.4.0 … … 944 986 * @see serialize_blocks() 945 987 * 946 * @param array[] $blocks An array of representative arrays of parsed block objects. See serialize_block(). 947 * @param callable $callback Callback to run on each block in the tree before serialization. 948 * It is called with the following arguments: $block, $parent_block, $block_index, $chunk_index. 949 * @return string String of rendered HTML. 950 */ 951 function traverse_and_serialize_blocks( $blocks, $callback ) { 988 * @param array[] $blocks An array of parsed blocks. See WP_Block_Parser_Block. 989 * @param callable $pre_callback Callback to run on each block in the tree before it is traversed and serialized. 990 * It is called with the following arguments: &$block, $parent_block, $previous_block. 991 * Its string return value will be prepended to the serialized block markup. 992 * @param callable $post_callback Callback to run on each block in the tree after it is traversed and serialized. 993 * It is called with the following arguments: &$block, $parent_block, $next_block. 994 * Its string return value will be appended to the serialized block markup. 995 * @return string Serialized block markup. 996 */ 997 function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_callback = null ) { 952 998 $result = ''; 953 foreach ( $blocks as $block ) { 954 // At the top level, there is no parent block, block index, or chunk index to pass to the callback. 955 $block = call_user_func( $callback, $block ); 956 $result .= traverse_and_serialize_block( $block, $callback ); 999 foreach ( $blocks as $index => $block ) { 1000 if ( is_callable( $pre_callback ) ) { 1001 $prev = 0 === $index 1002 ? null 1003 : $blocks[ $index - 1 ]; 1004 $result .= call_user_func_array( 1005 $pre_callback, 1006 array( &$block, null, $prev ) // At the top level, there is no parent block to pass to the callback. 1007 ); 1008 } 1009 $result .= traverse_and_serialize_block( $block, $pre_callback, $post_callback ); 1010 if ( is_callable( $post_callback ) ) { 1011 $next = count( $blocks ) - 1 === $index 1012 ? null 1013 : $blocks[ $index + 1 ]; 1014 $result .= call_user_func_array( 1015 $post_callback, 1016 array( &$block, null, $next ) // At the top level, there is no parent block to pass to the callback. 1017 ); 1018 } 957 1019 } 958 1020 return $result; -
trunk/tests/phpunit/tests/block-template-utils.php
r56584 r56644 226 226 */ 227 227 public function test_inject_theme_attribute_in_template_part_block() { 228 $template_part_block _without_theme_attribute= array(228 $template_part_block = array( 229 229 'blockName' => 'core/template-part', 230 230 'attrs' => array( … … 239 239 ); 240 240 241 $actual = _inject_theme_attribute_in_template_part_block( $template_part_block_without_theme_attribute);241 _inject_theme_attribute_in_template_part_block( $template_part_block ); 242 242 $expected = array( 243 243 'blockName' => 'core/template-part', … … 255 255 $this->assertSame( 256 256 $expected, 257 $ actual,257 $template_part_block, 258 258 '`theme` attribute was not correctly injected in template part block.' 259 259 ); … … 266 266 */ 267 267 public function test_not_inject_theme_attribute_in_template_part_block_theme_attribute_exists() { 268 $template_part_block _with_existing_theme_attribute= array(268 $template_part_block = array( 269 269 'blockName' => 'core/template-part', 270 270 'attrs' => array( … … 280 280 ); 281 281 282 $actual = _inject_theme_attribute_in_template_part_block( $template_part_block_with_existing_theme_attribute ); 282 $expected = $template_part_block; 283 _inject_theme_attribute_in_template_part_block( $template_part_block ); 283 284 $this->assertSame( 284 $ template_part_block_with_existing_theme_attribute,285 $ actual,285 $expected, 286 $template_part_block, 286 287 'Existing `theme` attribute in template part block was not respected by attribute injection.' 287 288 ); … … 302 303 ); 303 304 304 $actual = _inject_theme_attribute_in_template_part_block( $non_template_part_block ); 305 $expected = $non_template_part_block; 306 _inject_theme_attribute_in_template_part_block( $non_template_part_block ); 305 307 $this->assertSame( 308 $expected, 306 309 $non_template_part_block, 307 $actual,308 310 '`theme` attribute injection modified non-template-part block.' 309 311 ); -
trunk/tests/phpunit/tests/blocks/serialize.php
r56620 r56644 59 59 /** 60 60 * @ticket 59327 61 * @ticket 59412 61 62 * 62 63 * @covers ::traverse_and_serialize_blocks … … 74 75 } 75 76 76 public static function add_attribute_to_inner_block( $block ) {77 public static function add_attribute_to_inner_block( &$block ) { 77 78 if ( 'core/inner' === $block['blockName'] ) { 78 79 $block['attrs']['myattr'] = 'myvalue'; 79 80 } 80 return $block;81 81 } 82 82 83 83 /** 84 84 * @ticket 59327 85 * @ticket 59412 85 86 * 86 87 * @covers ::traverse_and_serialize_blocks … … 93 94 $blocks = parse_blocks( $original ); 94 95 95 $actual = traverse_and_serialize_blocks( 96 $blocks, 97 function ( $block ) { 98 return $block; 99 } 100 ); 96 $actual = traverse_and_serialize_blocks( $blocks ); 101 97 102 98 $this->assertSame( $original, $actual );
Note: See TracChangeset
for help on using the changeset viewer.