diff --git a/automatically-paginate-posts.php b/automatically-paginate-posts.php index 4984fab991f212f4f5637d0e1625422a2e2ca41b..1eb0bfdba2c6a3352d6e94dfd5228eaa2705f436 100644 --- a/automatically-paginate-posts.php +++ b/automatically-paginate-posts.php @@ -34,6 +34,16 @@ require_once dirname( __FILE__ ) . '/inc/class-block-editor.php'; * Class Automatically_Paginate_Posts. */ class Automatically_Paginate_Posts { + /** + * WordPress Quicktag that creates pagination. + */ + protected const QUICKTAG = '<!--nextpage-->'; + + /** + * String length of nextpage Quicktag. + */ + protected const QUICKTAG_LENGTH = 15; + /** * Supported post types. * @@ -711,11 +721,29 @@ class Automatically_Paginate_Posts { ); if ( $word_counter >= $num_words ) { - $content[ $index ] .= '<!--nextpage-->'; + $content[ $index ] .= static::QUICKTAG; $word_counter = 0; } } + // Prevent the last page from being empty. + $last_page = array_pop( $content ); + if ( + static::QUICKTAG === + substr( + $last_page, + - static::QUICKTAG_LENGTH + ) + ) { + $content[] = substr( + $last_page, + 0, + strlen( $last_page ) - static::QUICKTAG_LENGTH + ); + } else { + $content[] = $last_page; + } + break; case 'pages': diff --git a/tests/test-automatically-paginate-posts.php b/tests/test-automatically-paginate-posts.php index cc876466bd1a6d55ca4c47bb0ee3715d1edca793..ff1085d0e0203c12c1c16e934fbfe1104f1b54f0 100755 --- a/tests/test-automatically-paginate-posts.php +++ b/tests/test-automatically-paginate-posts.php @@ -111,7 +111,12 @@ class Test_Automatically_Paginate_Posts extends WP_UnitTestCase { public function test_filter_the_posts( $expected, $input ) { $post = $this->factory->post->create_and_get( $input['post_args'] ); - update_option( 'autopaging_paging_type', $input['type'] ); + add_filter( + 'pre_option_pages', + static function() use( $input ) { + return $input['type']; + } + ); add_filter( 'autopaging_num_pages', static function() use( $input ) { @@ -162,7 +167,7 @@ class Test_Automatically_Paginate_Posts extends WP_UnitTestCase { 'num_words' => 2, ), ), - 'Classic post, two pages' => array( + 'Classic post, two pages, split to pages' => array( "1\r\n\r\n2<!--nextpage-->\r\n\r\n3", array( 'post_args' => array( @@ -174,7 +179,7 @@ class Test_Automatically_Paginate_Posts extends WP_UnitTestCase { 'num_words' => 2, ), ), - 'Classic post, three pages' => array( + 'Classic post, three pages, split to pages' => array( "1<!--nextpage-->\r\n\r\n2<!--nextpage-->\r\n\r\n3", array( 'post_args' => array( @@ -186,6 +191,42 @@ class Test_Automatically_Paginate_Posts extends WP_UnitTestCase { 'num_words' => 2, ), ), + 'Classic post, one page, split on words' => array( + "1\r\n\r\n2\r\n\r\n3\r\n\r\n4", + array( + 'post_args' => array( + 'post_type' => 'post', + 'post_content' => "1\r\n\r\n2\r\n\r\n3\r\n\r\n4", + ), + 'type' => 'words', + 'num_pages' => 2, + 'num_words' => 5, + ), + ), + 'Classic post, two pages, split on words' => array( + "1\r\n\r\n2<!--nextpage-->\r\n\r\n3\r\n\r\n4", + array( + 'post_args' => array( + 'post_type' => 'post', + 'post_content' => "1\r\n\r\n2\r\n\r\n3\r\n\r\n4", + ), + 'type' => 'words', + 'num_pages' => 2, + 'num_words' => 2, + ), + ), + 'Classic post, three pages, split on words' => array( + "1<!--nextpage-->\r\n\r\n2<!--nextpage-->\r\n\r\n3<!--nextpage-->\r\n\r\n4", + array( + 'post_args' => array( + 'post_type' => 'post', + 'post_content' => "1\r\n\r\n2\r\n\r\n3\r\n\r\n4", + ), + 'type' => 'words', + 'num_pages' => 2, + 'num_words' => 1, + ), + ), ); } }