Skip to content
Merged
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/class-newspack-popups-inserter.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ function ( $block_groups, $block ) use ( &$block_index, $parsed_blocks, $max_ind
// Give length-ignored blocks a length of 1 so that prompts at 0% can still be inserted before them.
$pos++;
} else {
$pos += strlen( wp_strip_all_tags( $block['innerHTML'] ) );
$block_content = self::get_block_content( $block );
$pos += strlen( wp_strip_all_tags( $block_content ) );
}
}

Expand Down
80 changes: 79 additions & 1 deletion tests/test-content-insertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,8 @@ public function test_prompt_insertion_zero_group() {
self::assertEqualBlockNames(
[
'core/shortcode', // Popup 1 - inserted before the group block.
'core/shortcode', // Popup 2 - inserted before the group block.
'core/group',
'core/shortcode', // Popup 2 - inserted after the group block.
'core/shortcode', // Popup 3.
],
Newspack_Popups_Inserter::insert_popups_in_post_content(
Expand All @@ -446,4 +446,82 @@ public function test_prompt_insertion_zero_group() {
'The popups are inserted into the content at expected positions.'
);
}

/**
* Test prompt insertion at 0% with multiple top-level Group blocks.
*/
public function test_prompt_insertion_multiple_groups() {
$post_content = '
<!-- wp:group -->
<div class="wp-block-group"><!-- wp:paragraph -->
<p>Paragraph 1</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Paragraph 2</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->

<!-- wp:group -->
<div class="wp-block-group">
<!-- wp:paragraph -->
<p>Paragraph 3</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Paragraph 4</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->

<!-- wp:group -->
<div class="wp-block-group">
<!-- wp:paragraph -->
<p>Paragraph 5</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Paragraph 6</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->

<!-- wp:group -->
<div class="wp-block-group">
<!-- wp:paragraph -->
<p>Paragraph 7</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Paragraph 8</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->

<!-- wp:group -->
<div class="wp-block-group">
<!-- wp:paragraph -->
<p>Paragraph 9</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Paragraph 10</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->';

self::assertEqualBlockNames(
[
'core/shortcode', // Popup 1 - 0%.
'core/group',
'core/group',
'core/group',
'core/shortcode', // Popup 2 - 70%.
'core/group',
'core/group',
'core/shortcode', // Popup 3 - 100%.
],
Newspack_Popups_Inserter::insert_popups_in_post_content(
$post_content,
self::$popups
),
'The popups are inserted into the content at expected positions.'
);
}
}