Skip to content

Commit 02a4039

Browse files
laurelfulfordclaude
andcommitted
fix(collections): restore list items in posts with card indicator
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 964c01c commit 02a4039

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

includes/collections/class-content-inserter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ function ( $block ) use ( $blocks_to_skip_empty ) {
264264
// Insert after the nth block.
265265
$rendered_content = '';
266266
foreach ( $parsed_blocks as $index => $block ) {
267-
$rendered_content .= $block['innerHTML'];
267+
// Use render_block() so inner blocks (e.g. core/list-item children of core/list) and dynamic blocks are emitted.
268+
$rendered_content .= render_block( $block );
268269

269270
// Insert after the nth block (index is 0-based).
270271
if ( $index === $nth_block - 1 ) {

tests/unit-tests/collections/class-test-content-inserter.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,29 @@ public function test_insert_after_nth_block() {
278278
$this->assertLessThan( $third_pos, $insert_pos, 'Inserted content should come before third paragraph.' );
279279
}
280280

281+
/**
282+
* Test insert_after_nth_block preserves inner blocks (e.g., list items).
283+
*
284+
* Regression: a `core/list` block stores its `<li>` items as `core/list-item`
285+
* inner blocks, not in `innerHTML`. The same applies to columns, groups,
286+
* buttons, etc. Reassembling parsed blocks must use `render_block()` so
287+
* inner-block content (and dynamic blocks) are emitted.
288+
*
289+
* @covers \Newspack\Collections\Content_Inserter::insert_after_nth_block
290+
*/
291+
public function test_insert_after_nth_block_preserves_inner_blocks() {
292+
$insert_html = '<div>Inserted content</div>';
293+
294+
$block_content = "<!-- wp:paragraph -->\n<p>First paragraph.</p>\n<!-- /wp:paragraph -->\n\n"
295+
. "<!-- wp:paragraph -->\n<p>Second paragraph.</p>\n<!-- /wp:paragraph -->\n\n"
296+
. "<!-- 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 -->";
297+
298+
$result = Content_Inserter::insert_after_nth_block( $block_content, $insert_html, 2 );
299+
300+
$this->assertStringContainsString( '<li>Item one</li>', $result, 'First list item should be preserved.' );
301+
$this->assertStringContainsString( '<li>Item two</li>', $result, 'Second list item should be preserved.' );
302+
}
303+
281304
/**
282305
* Test check_if_post_is_in_collection excludes draft collections.
283306
*

0 commit comments

Comments
 (0)