Skip to content

Commit

Permalink
Blocks: Fix incorrect placement for hooked blocks in the parent conta…
Browse files Browse the repository at this point in the history
…iner
  • Loading branch information
gziolo committed Sep 11, 2023
1 parent bb4ed88 commit ff1c019
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/experimental/block-hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,24 @@ function gutenberg_insert_hooked_block( $inserted_block, $relative_position, $an
array_unshift( $block['innerBlocks'], $inserted_block );
// Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`)
// when rendering blocks, we also need to prepend a value (`null`, to mark a block
// location) to that array.
array_unshift( $block['innerContent'], null );
// location) to that array after HTML content for the inner blocks wrapper.
for ( $chunk_index = 0; $chunk_index < count( $block['innerContent'] ); $chunk_index++ ) {
if ( is_null( $block['innerContent'][ $chunk_index ] ) ) {
break;
}
}
array_splice( $block['innerContent'], $chunk_index, 0, array( null ) );
} elseif ( 'last_child' === $relative_position ) {
array_push( $block['innerBlocks'], $inserted_block );
// Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`)
// when rendering blocks, we also need to prepend a value (`null`, to mark a block
// location) to that array.
array_push( $block['innerContent'], null );
// when rendering blocks, we also need to correctly prepend a value (`null`, to mark a block
// location) to that array after HTML content for the inner blocks wrapper.
for ( $chunk_index = count( $block['innerContent'] ) - 1; $chunk_index >= 0; $chunk_index-- ) {
if ( is_null( $block['innerContent'][ $chunk_index ] ) ) {
break;
}
}
array_splice( $block['innerContent'], $chunk_index, 0, array( null ) );
}
return $block;
}
Expand Down

0 comments on commit ff1c019

Please sign in to comment.