From 8a2ea6d44553f62f9eaa05d5b2b630b4f5334b9f Mon Sep 17 00:00:00 2001
From: Erick Hitter <git-contrib@ethitter.com>
Date: Sun, 26 Jun 2022 15:47:32 -0700
Subject: [PATCH] Fix word splitting for Classic Editor

---
 automatically-paginate-posts.php | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/automatically-paginate-posts.php b/automatically-paginate-posts.php
index 401e8f0..cac8517 100644
--- a/automatically-paginate-posts.php
+++ b/automatically-paginate-posts.php
@@ -603,14 +603,15 @@ class Automatically_Paginate_Posts {
 
 					// Count words per paragraph and break after the paragraph that exceeds the set threshold.
 					foreach ( $content as $index => $paragraph ) {
-						$paragraph_words = count( preg_split( '/\s+/', strip_tags( $paragraph ) ) );
-						$word_counter += $paragraph_words;
+						$word_counter += mb_strlen(
+							wp_strip_all_tags(
+								$paragraph
+							)
+						);
 
 						if ( $word_counter >= $num_words ) {
 							$content[ $index ] .= '<!--nextpage-->';
 							$word_counter = 0;
-						} else {
-							break;
 						}
 					}
 
@@ -676,7 +677,7 @@ class Automatically_Paginate_Posts {
 		$num_pages
 	) {
 		$blocks     = parse_blocks( $the_post->post_content );
-		$new_blocks = [];
+		$new_blocks = array();
 
 		switch ( $paging_type ) {
 			case 'words':
@@ -785,6 +786,8 @@ class Automatically_Paginate_Posts {
 	}
 
 	/**
+	 * Determine if current loop iteration is where a page break is expected.
+	 *
 	 * @param int $loop_key            Current position in array of blocks.
 	 * @param int $insertion_iterator  Current number of page breaks inserted.
 	 * @param int $insertion_frequency After this many blocks a should break be
-- 
GitLab