From ddc4c26e7cb7d8849edb86206bf8d05bf782bbfa Mon Sep 17 00:00:00 2001 From: Erick Hitter <git-contrib@ethitter.com> Date: Sun, 26 Jun 2022 19:49:20 -0700 Subject: [PATCH] Fix splitting when number of paragraphs is one greater than number of pages Previously, no splitting would occur if the paragraph count exceeded the desired number of pages by one. Fixes #1 --- automatically-paginate-posts.php | 119 +++++++++++++------------------ 1 file changed, 51 insertions(+), 68 deletions(-) diff --git a/automatically-paginate-posts.php b/automatically-paginate-posts.php index cac8517..88b95d2 100644 --- a/automatically-paginate-posts.php +++ b/automatically-paginate-posts.php @@ -588,77 +588,71 @@ class Automatically_Paginate_Posts { // Normalize post content to simplify paragraph counting and automatic paging. Accounts for content that hasn't been cleaned up by TinyMCE. $content = preg_replace( '#<p>(.+?)</p>#i', "$1\r\n\r\n", $content ); $content = preg_replace( '#<br(\s*/)?>#i', "\r\n", $content ); + $content = explode( "\r\n\r\n", $content ); - // Count paragraphs. - $count = preg_match_all( '#\r\n\r\n#', $content ); + // Count number of paragraphs content was exploded to. + $count = count( $content ); - // Keep going, if we have something to count. - if ( is_int( $count ) && 0 < $count ) { - // Explode content at double (or more) line breaks. - $content = explode( "\r\n\r\n", $content ); + // Nothing to do, goodbye. + if ( $count <= 1 ) { + return; + } - switch ( $paging_type ) { - case 'words': - $word_counter = 0; + switch ( $paging_type ) { + case 'words': + $word_counter = 0; - // Count words per paragraph and break after the paragraph that exceeds the set threshold. - foreach ( $content as $index => $paragraph ) { - $word_counter += mb_strlen( - wp_strip_all_tags( - $paragraph - ) - ); + // Count words per paragraph and break after the paragraph that exceeds the set threshold. + foreach ( $content as $index => $paragraph ) { + $word_counter += mb_strlen( + wp_strip_all_tags( + $paragraph + ) + ); - if ( $word_counter >= $num_words ) { - $content[ $index ] .= '<!--nextpage-->'; - $word_counter = 0; - } + if ( $word_counter >= $num_words ) { + $content[ $index ] .= '<!--nextpage-->'; + $word_counter = 0; } + } - break; - - case 'pages': - default: - // Count number of paragraphs content was exploded to. - $count = count( $content ); + break; - $frequency = $this->get_insertion_frequency_by_pages( - $count, - $num_pages - ); + case 'pages': + default: + $frequency = $this->get_insertion_frequency_by_pages( + $count, + $num_pages + ); - $i = $this->get_initial_counter_for_pages( - $count, - $num_pages - ); + $i = 1; - // Loop through content pieces and append Quicktag as is appropriate. - foreach ( $content as $key => $value ) { - if ( $this->is_at_end_for_pages( $key, $count ) ) { - break; - } + // Loop through content pieces and append Quicktag as is appropriate. + foreach ( $content as $key => $value ) { + if ( $this->is_at_end_for_pages( $key, $count ) ) { + break; + } - if ( - $this->is_insertion_point_for_pages( - $key, - $i, - $frequency - ) - ) { - $content[ $key ] .= '<!--nextpage-->'; - $i++; - } + if ( + $this->is_insertion_point_for_pages( + $key, + $i, + $frequency + ) + ) { + $content[ $key ] .= '<!--nextpage-->'; + $i++; } + } - break; - } + break; + } - // Reunite content. - $content = implode( "\r\n\r\n", $content ); + // Reunite content. + $content = implode( "\r\n\r\n", $content ); - // And, overwrite the original content. - $the_post->post_content = $content; - } + // And, overwrite the original content. + $the_post->post_content = $content; } /** @@ -719,7 +713,7 @@ class Automatically_Paginate_Posts { $num_pages ); - $i = $this->get_initial_counter_for_pages( $count, $num_pages ); + $i = 1; foreach ( $blocks as $key => $block ) { $new_blocks[] = $block; @@ -763,17 +757,6 @@ class Automatically_Paginate_Posts { return $frequency; } - /** - * Get counter starting value for use when splitting by pages. - * - * @param int $count Total number of paragraphs. - * @param int $num_pages Desired number of pages. - * @return int - */ - protected function get_initial_counter_for_pages( $count, $num_pages ) { - return $count - 1 === $num_pages ? 2 : 1; - } - /** * Determine if more page breaks should be inserted. * -- GitLab