From 3c7e1bc64789094c22c1ad2e11fb32f534be54e6 Mon Sep 17 00:00:00 2001 From: Derrick Koo Date: Tue, 19 Apr 2022 15:47:43 -0600 Subject: [PATCH 1/2] fix: insertion of prompts around Group blocks --- includes/class-newspack-popups-inserter.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/class-newspack-popups-inserter.php b/includes/class-newspack-popups-inserter.php index 9f3478d9f..9b8641da7 100755 --- a/includes/class-newspack-popups-inserter.php +++ b/includes/class-newspack-popups-inserter.php @@ -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 ) ); } } From fcdd11143a7669200d81c076a75eb2f379f8610a Mon Sep 17 00:00:00 2001 From: Derrick Koo Date: Tue, 19 Apr 2022 16:00:10 -0600 Subject: [PATCH 2/2] test: fix single group unit test; add new test for multiple groups --- tests/test-content-insertion.php | 80 +++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/tests/test-content-insertion.php b/tests/test-content-insertion.php index 085d8f626..7a14b8b50 100644 --- a/tests/test-content-insertion.php +++ b/tests/test-content-insertion.php @@ -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( @@ -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 = ' + +
+

Paragraph 1

+ + + +

Paragraph 2

+
+ + + +
+ +

Paragraph 3

+ + + +

Paragraph 4

+
+ + + +
+ +

Paragraph 5

+ + + +

Paragraph 6

+
+ + + +
+ +

Paragraph 7

+ + + +

Paragraph 8

+
+ + + +
+ +

Paragraph 9

+ + + +

Paragraph 10

+
+ '; + + 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.' + ); + } }