Changeset 56634
- Timestamp:
- 09/20/2023 04:47:44 PM (13 months ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks.php
r56620 r56634 762 762 763 763 /** 764 * Insert a parsed block into a parent block's inner blocks.765 *766 * Given a parsed block, a block index, and a chunk index, insert another parsed block767 * into the parent block at the given indices.768 *769 * Note that the this mutates the parent block by inserting into the parent's `innerBlocks`770 * array, and by updating the parent's `innerContent` array accordingly.771 *772 * @since 6.4.0773 *774 * @param array $parent_block The parent block.775 * @param int $block_index The index specifying the insertion position among the parent block's inner blocks.776 * @param int $chunk_index The index specifying the insertion position among the parent block's inner content chunks.777 * @param array $inserted_block The block to insert.778 * @return void779 */780 function insert_inner_block( &$parent_block, $block_index, $chunk_index, $inserted_block ) {781 array_splice( $parent_block['innerBlocks'], $block_index, 0, array( $inserted_block ) );782 783 /*784 * Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`)785 * when rendering blocks, we also need to insert a value (`null`, to mark a block786 * location) into that array.787 */788 array_splice( $parent_block['innerContent'], $chunk_index, 0, array( null ) );789 }790 791 /**792 * Prepend a parsed block to a parent block's inner blocks.793 *794 * Given a parsed block, prepend another parsed block to the parent block's inner blocks.795 *796 * Note that the this mutates the parent block by inserting into the parent's `innerBlocks`797 * array, and by updating the parent's `innerContent` array accordingly.798 *799 * @since 6.4.0800 *801 * @param array $parent_block The parent block.802 * @param array $inserted_block The block to insert.803 * @return void804 */805 function prepend_inner_block( &$parent_block, $inserted_block ) {806 $chunk_index = 0;807 for ( $index = 0; $index < count( $parent_block['innerContent'] ); $index++ ) {808 if ( is_null( $parent_block['innerContent'][ $index ] ) ) {809 $chunk_index = $index;810 break;811 }812 }813 insert_inner_block( $parent_block, 0, $chunk_index, $inserted_block );814 }815 816 /**817 * Append a parsed block to a parent block's inner blocks.818 *819 * Given a parsed block, append another parsed block to the parent block's inner blocks.820 *821 * Note that the this mutates the parent block by inserting into the parent's `innerBlocks`822 * array, and by updating the parent's `innerContent` array accordingly.823 *824 * @since 6.4.0825 *826 * @param array $parent_block The parent block.827 * @param array $inserted_block The block to insert.828 * @return void829 */830 function append_inner_block( &$parent_block, $inserted_block ) {831 $chunk_index = count( $parent_block['innerContent'] );832 for ( $index = count( $parent_block['innerContent'] ); $index > 0; $index-- ) {833 if ( is_null( $parent_block['innerContent'][ $index - 1 ] ) ) {834 $chunk_index = $index;835 break;836 }837 }838 insert_inner_block( $parent_block, count( $parent_block['innerBlocks'] ), $chunk_index, $inserted_block );839 }840 841 /**842 764 * Given an array of attributes, returns a string in the serialized attributes 843 765 * format prepared for post content. -
trunk/tests/phpunit/tests/blocks/blockHooks.php
r56618 r56634 18 18 */ 19 19 public function tear_down() { 20 $registry = WP_Block_Type_Registry::get_instance(); 21 $block_names = array( 22 'tests/injected-one', 23 'tests/injected-two', 24 ); 25 foreach ( $block_names as $block_name ) { 20 $registry = WP_Block_Type_Registry::get_instance(); 21 22 foreach ( array( 'tests/my-block', 'tests/my-container-block' ) as $block_name ) { 26 23 if ( $registry->is_registered( $block_name ) ) { 27 24 $registry->unregister( $block_name ); … … 101 98 ); 102 99 } 103 104 /**105 * @ticket 59385106 *107 * @covers ::insert_inner_block108 *109 * @dataProvider data_insert_inner_block110 *111 * @param string $block_index Block index to insert the block at.112 * @param string $expected_markup Expected markup after the block is inserted.113 */114 public function test_insert_inner_block( $block_index, $expected_markup ) {115 $original_markup = <<<HTML116 <!-- wp:tests/group {"layout":{"type":"constrained"}} -->117 <div class="wp-block-group">118 <!-- wp:paragraph -->119 <p>Foo</p>120 <!-- /wp:paragraph -->121 </div>122 <!-- /wp:tests/group -->123 HTML;124 125 $inserted_block = array(126 'blockName' => 'tests/hooked-block',127 'attrs' => array(),128 'innerBlocks' => array(),129 'innerHTML' => '',130 'innerContent' => array(),131 );132 133 $expected = parse_blocks( $expected_markup )[0];134 $block = parse_blocks( $original_markup )[0];135 insert_inner_block( $block, $block_index, 1, $inserted_block );136 $this->assertSame( $expected, $block );137 }138 139 /**140 * Data provider.141 *142 * @return array[]143 */144 public function data_insert_inner_block() {145 $expected_before_markup = <<<HTML146 <!-- wp:tests/group {"layout":{"type":"constrained"}} -->147 <div class="wp-block-group">148 <!-- wp:tests/hooked-block /--><!-- wp:paragraph -->149 <p>Foo</p>150 <!-- /wp:paragraph -->151 </div>152 <!-- /wp:tests/group -->153 HTML;154 155 $expected_after_markup = <<<HTML156 <!-- wp:tests/group {"layout":{"type":"constrained"}} -->157 <div class="wp-block-group">158 <!-- wp:paragraph -->159 <p>Foo</p>160 <!-- /wp:paragraph --><!-- wp:tests/hooked-block /-->161 </div>162 <!-- /wp:tests/group -->163 HTML;164 165 return array(166 'insert before given block' => array(167 'block_index' => 0,168 'expected_markup' => $expected_before_markup,169 ),170 'insert after given block' => array(171 'block_index' => 1,172 'expected_markup' => $expected_after_markup,173 ),174 );175 }176 177 /**178 * @ticket 59385179 *180 * @covers ::prepend_inner_block181 */182 public function test_prepend_inner_block() {183 $original_markup = <<<HTML184 <!-- wp:tests/group {"layout":{"type":"constrained"}} -->185 <div class="wp-block-group">186 <!-- wp:paragraph -->187 <p>Foo</p>188 <!-- /wp:paragraph -->189 </div>190 <!-- /wp:tests/group -->191 HTML;192 193 $inserted_block = array(194 'blockName' => 'tests/hooked-block',195 'attrs' => array(),196 'innerBlocks' => array(),197 'innerHTML' => '',198 'innerContent' => array(),199 );200 201 $expected_markup = <<<HTML202 <!-- wp:tests/group {"layout":{"type":"constrained"}} -->203 <div class="wp-block-group">204 <!-- wp:tests/hooked-block /--><!-- wp:paragraph -->205 <p>Foo</p>206 <!-- /wp:paragraph -->207 </div>208 <!-- /wp:tests/group -->209 HTML;210 211 $expected = parse_blocks( $expected_markup )[0];212 $block = parse_blocks( $original_markup )[0];213 prepend_inner_block( $block, $inserted_block );214 $this->assertSame( $expected, $block );215 }216 217 /**218 * @ticket 59385219 *220 * @covers ::append_inner_block221 */222 public function test_append_inner_block() {223 $original_markup = <<<HTML224 <!-- wp:tests/group {"layout":{"type":"constrained"}} -->225 <div class="wp-block-group">226 <!-- wp:paragraph -->227 <p>Foo</p>228 <!-- /wp:paragraph -->229 </div>230 <!-- /wp:tests/group -->231 HTML;232 233 $inserted_block = array(234 'blockName' => 'tests/hooked-block',235 'attrs' => array(),236 'innerBlocks' => array(),237 'innerHTML' => '',238 'innerContent' => array(),239 );240 241 $expected_markup = <<<HTML242 <!-- wp:tests/group {"layout":{"type":"constrained"}} -->243 <div class="wp-block-group">244 <!-- wp:paragraph -->245 <p>Foo</p>246 <!-- /wp:paragraph --><!-- wp:tests/hooked-block /-->247 </div>248 <!-- /wp:tests/group -->249 HTML;250 251 $expected = parse_blocks( $expected_markup )[0];252 $block = parse_blocks( $original_markup )[0];253 append_inner_block( $block, $inserted_block );254 $this->assertSame( $expected, $block );255 }256 100 }
Note: See TracChangeset
for help on using the changeset viewer.