diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index b739e1244d..fe9c56a551 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -760,84 +760,6 @@ function get_hooked_blocks( $name ) { return $hooked_blocks; } -/** - * Insert a parsed block into a parent block's inner blocks. - * - * Given a parsed block, a block index, and a chunk index, insert another parsed block - * into the parent block at the given indices. - * - * Note that the this mutates the parent block by inserting into the parent's `innerBlocks` - * array, and by updating the parent's `innerContent` array accordingly. - * - * @since 6.4.0 - * - * @param array $parent_block The parent block. - * @param int $block_index The index specifying the insertion position among the parent block's inner blocks. - * @param int $chunk_index The index specifying the insertion position among the parent block's inner content chunks. - * @param array $inserted_block The block to insert. - * @return void - */ -function insert_inner_block( &$parent_block, $block_index, $chunk_index, $inserted_block ) { - array_splice( $parent_block['innerBlocks'], $block_index, 0, array( $inserted_block ) ); - - /* - * Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`) - * when rendering blocks, we also need to insert a value (`null`, to mark a block - * location) into that array. - */ - array_splice( $parent_block['innerContent'], $chunk_index, 0, array( null ) ); -} - -/** - * Prepend a parsed block to a parent block's inner blocks. - * - * Given a parsed block, prepend another parsed block to the parent block's inner blocks. - * - * Note that the this mutates the parent block by inserting into the parent's `innerBlocks` - * array, and by updating the parent's `innerContent` array accordingly. - * - * @since 6.4.0 - * - * @param array $parent_block The parent block. - * @param array $inserted_block The block to insert. - * @return void - */ -function prepend_inner_block( &$parent_block, $inserted_block ) { - $chunk_index = 0; - for ( $index = 0; $index < count( $parent_block['innerContent'] ); $index++ ) { - if ( is_null( $parent_block['innerContent'][ $index ] ) ) { - $chunk_index = $index; - break; - } - } - insert_inner_block( $parent_block, 0, $chunk_index, $inserted_block ); -} - -/** - * Append a parsed block to a parent block's inner blocks. - * - * Given a parsed block, append another parsed block to the parent block's inner blocks. - * - * Note that the this mutates the parent block by inserting into the parent's `innerBlocks` - * array, and by updating the parent's `innerContent` array accordingly. - * - * @since 6.4.0 - * - * @param array $parent_block The parent block. - * @param array $inserted_block The block to insert. - * @return void - */ -function append_inner_block( &$parent_block, $inserted_block ) { - $chunk_index = count( $parent_block['innerContent'] ); - for ( $index = count( $parent_block['innerContent'] ); $index > 0; $index-- ) { - if ( is_null( $parent_block['innerContent'][ $index - 1 ] ) ) { - $chunk_index = $index; - break; - } - } - insert_inner_block( $parent_block, count( $parent_block['innerBlocks'] ), $chunk_index, $inserted_block ); -} - /** * Given an array of attributes, returns a string in the serialized attributes * format prepared for post content. diff --git a/tests/phpunit/tests/blocks/blockHooks.php b/tests/phpunit/tests/blocks/blockHooks.php index e22e611860..2d5fbb6e9b 100644 --- a/tests/phpunit/tests/blocks/blockHooks.php +++ b/tests/phpunit/tests/blocks/blockHooks.php @@ -17,12 +17,9 @@ class Tests_Blocks_BlockHooks extends WP_UnitTestCase { * @since 6.4.0 */ public function tear_down() { - $registry = WP_Block_Type_Registry::get_instance(); - $block_names = array( - 'tests/injected-one', - 'tests/injected-two', - ); - foreach ( $block_names as $block_name ) { + $registry = WP_Block_Type_Registry::get_instance(); + + foreach ( array( 'tests/my-block', 'tests/my-container-block' ) as $block_name ) { if ( $registry->is_registered( $block_name ) ) { $registry->unregister( $block_name ); } @@ -100,157 +97,4 @@ class Tests_Blocks_BlockHooks extends WP_UnitTestCase { 'block hooked at the last child position' ); } - - /** - * @ticket 59385 - * - * @covers ::insert_inner_block - * - * @dataProvider data_insert_inner_block - * - * @param string $block_index Block index to insert the block at. - * @param string $expected_markup Expected markup after the block is inserted. - */ - public function test_insert_inner_block( $block_index, $expected_markup ) { - $original_markup = << -
- -

Foo

- -
- -HTML; - - $inserted_block = array( - 'blockName' => 'tests/hooked-block', - 'attrs' => array(), - 'innerBlocks' => array(), - 'innerHTML' => '', - 'innerContent' => array(), - ); - - $expected = parse_blocks( $expected_markup )[0]; - $block = parse_blocks( $original_markup )[0]; - insert_inner_block( $block, $block_index, 1, $inserted_block ); - $this->assertSame( $expected, $block ); - } - - /** - * Data provider. - * - * @return array[] - */ - public function data_insert_inner_block() { - $expected_before_markup = << -
- -

Foo

- -
- -HTML; - - $expected_after_markup = << -
- -

Foo

- -
- -HTML; - - return array( - 'insert before given block' => array( - 'block_index' => 0, - 'expected_markup' => $expected_before_markup, - ), - 'insert after given block' => array( - 'block_index' => 1, - 'expected_markup' => $expected_after_markup, - ), - ); - } - - /** - * @ticket 59385 - * - * @covers ::prepend_inner_block - */ - public function test_prepend_inner_block() { - $original_markup = << -
- -

Foo

- -
- -HTML; - - $inserted_block = array( - 'blockName' => 'tests/hooked-block', - 'attrs' => array(), - 'innerBlocks' => array(), - 'innerHTML' => '', - 'innerContent' => array(), - ); - - $expected_markup = << -
- -

Foo

- -
- -HTML; - - $expected = parse_blocks( $expected_markup )[0]; - $block = parse_blocks( $original_markup )[0]; - prepend_inner_block( $block, $inserted_block ); - $this->assertSame( $expected, $block ); - } - - /** - * @ticket 59385 - * - * @covers ::append_inner_block - */ - public function test_append_inner_block() { - $original_markup = << -
- -

Foo

- -
- -HTML; - - $inserted_block = array( - 'blockName' => 'tests/hooked-block', - 'attrs' => array(), - 'innerBlocks' => array(), - 'innerHTML' => '', - 'innerContent' => array(), - ); - - $expected_markup = << -
- -

Foo

- -
- -HTML; - - $expected = parse_blocks( $expected_markup )[0]; - $block = parse_blocks( $original_markup )[0]; - append_inner_block( $block, $inserted_block ); - $this->assertSame( $expected, $block ); - } }