Skip to content
Snippets Groups Projects
Commit ddc4c26e authored by Erick Hitter's avatar Erick Hitter
Browse files

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
parent 8a2ea6d4
Branches
Tags
1 merge request!5Add block-editor support
...@@ -588,77 +588,71 @@ class Automatically_Paginate_Posts { ...@@ -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. // 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( '#<p>(.+?)</p>#i', "$1\r\n\r\n", $content );
$content = preg_replace( '#<br(\s*/)?>#i', "\r\n", $content ); $content = preg_replace( '#<br(\s*/)?>#i', "\r\n", $content );
$content = explode( "\r\n\r\n", $content );
// Count paragraphs. // Count number of paragraphs content was exploded to.
$count = preg_match_all( '#\r\n\r\n#', $content ); $count = count( $content );
// Keep going, if we have something to count. // Nothing to do, goodbye.
if ( is_int( $count ) && 0 < $count ) { if ( $count <= 1 ) {
// Explode content at double (or more) line breaks. return;
$content = explode( "\r\n\r\n", $content ); }
switch ( $paging_type ) { switch ( $paging_type ) {
case 'words': case 'words':
$word_counter = 0; $word_counter = 0;
// Count words per paragraph and break after the paragraph that exceeds the set threshold. // Count words per paragraph and break after the paragraph that exceeds the set threshold.
foreach ( $content as $index => $paragraph ) { foreach ( $content as $index => $paragraph ) {
$word_counter += mb_strlen( $word_counter += mb_strlen(
wp_strip_all_tags( wp_strip_all_tags(
$paragraph $paragraph
) )
); );
if ( $word_counter >= $num_words ) { if ( $word_counter >= $num_words ) {
$content[ $index ] .= '<!--nextpage-->'; $content[ $index ] .= '<!--nextpage-->';
$word_counter = 0; $word_counter = 0;
}
} }
}
break; break;
case 'pages':
default:
// Count number of paragraphs content was exploded to.
$count = count( $content );
$frequency = $this->get_insertion_frequency_by_pages( case 'pages':
$count, default:
$num_pages $frequency = $this->get_insertion_frequency_by_pages(
); $count,
$num_pages
);
$i = $this->get_initial_counter_for_pages( $i = 1;
$count,
$num_pages
);
// Loop through content pieces and append Quicktag as is appropriate. // Loop through content pieces and append Quicktag as is appropriate.
foreach ( $content as $key => $value ) { foreach ( $content as $key => $value ) {
if ( $this->is_at_end_for_pages( $key, $count ) ) { if ( $this->is_at_end_for_pages( $key, $count ) ) {
break; break;
} }
if ( if (
$this->is_insertion_point_for_pages( $this->is_insertion_point_for_pages(
$key, $key,
$i, $i,
$frequency $frequency
) )
) { ) {
$content[ $key ] .= '<!--nextpage-->'; $content[ $key ] .= '<!--nextpage-->';
$i++; $i++;
}
} }
}
break; break;
} }
// Reunite content. // Reunite content.
$content = implode( "\r\n\r\n", $content ); $content = implode( "\r\n\r\n", $content );
// And, overwrite the original content. // And, overwrite the original content.
$the_post->post_content = $content; $the_post->post_content = $content;
}
} }
/** /**
...@@ -719,7 +713,7 @@ class Automatically_Paginate_Posts { ...@@ -719,7 +713,7 @@ class Automatically_Paginate_Posts {
$num_pages $num_pages
); );
$i = $this->get_initial_counter_for_pages( $count, $num_pages ); $i = 1;
foreach ( $blocks as $key => $block ) { foreach ( $blocks as $key => $block ) {
$new_blocks[] = $block; $new_blocks[] = $block;
...@@ -763,17 +757,6 @@ class Automatically_Paginate_Posts { ...@@ -763,17 +757,6 @@ class Automatically_Paginate_Posts {
return $frequency; 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. * Determine if more page breaks should be inserted.
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment