Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion includes/collections/class-content-inserter.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ function ( $block ) use ( $blocks_to_skip_empty ) {
// Insert after the nth block.
$rendered_content = '';
foreach ( $parsed_blocks as $index => $block ) {
$rendered_content .= $block['innerHTML'];
// Use render_block() so inner blocks (e.g. core/list-item children of core/list) and dynamic blocks are emitted.
$rendered_content .= render_block( $block );

// Insert after the nth block (index is 0-based).
if ( $index === $nth_block - 1 ) {
Expand Down
23 changes: 23 additions & 0 deletions tests/unit-tests/collections/class-test-content-inserter.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,29 @@ public function test_insert_after_nth_block() {
$this->assertLessThan( $third_pos, $insert_pos, 'Inserted content should come before third paragraph.' );
}

/**
* Test insert_after_nth_block preserves inner blocks (e.g., list items).
*
* Regression: a `core/list` block stores its `<li>` items as `core/list-item`
* inner blocks, not in `innerHTML`. The same applies to columns, groups,
* buttons, etc. Reassembling parsed blocks must use `render_block()` so
* inner-block content (and dynamic blocks) are emitted.
*
* @covers \Newspack\Collections\Content_Inserter::insert_after_nth_block
*/
public function test_insert_after_nth_block_preserves_inner_blocks() {
$insert_html = '<div>Inserted content</div>';

$block_content = "<!-- wp:paragraph -->\n<p>First paragraph.</p>\n<!-- /wp:paragraph -->\n\n"
. "<!-- wp:paragraph -->\n<p>Second paragraph.</p>\n<!-- /wp:paragraph -->\n\n"
. "<!-- wp:list -->\n<ul class=\"wp-block-list\"><!-- wp:list-item -->\n<li>Item one</li>\n<!-- /wp:list-item -->\n\n<!-- wp:list-item -->\n<li>Item two</li>\n<!-- /wp:list-item --></ul>\n<!-- /wp:list -->";

$result = Content_Inserter::insert_after_nth_block( $block_content, $insert_html, 2 );

$this->assertStringContainsString( '<li>Item one</li>', $result, 'First list item should be preserved.' );
$this->assertStringContainsString( '<li>Item two</li>', $result, 'Second list item should be preserved.' );
}

/**
* Test check_if_post_is_in_collection excludes draft collections.
*
Expand Down
Loading