From 4563b9c9e35eb19536bdadfa670ab2ae2c4b6679 Mon Sep 17 00:00:00 2001 From: Erick Hitter <git-contrib@ethitter.com> Date: Sun, 26 Jun 2022 15:43:58 -0700 Subject: [PATCH] Basic Gutenberg support --- README.md | 1 + automatically-paginate-posts.php | 45 +++++++++++++++++++++- languages/automatically-paginate-posts.pot | 18 ++++----- readme.txt | 1 + 4 files changed, 54 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 372a24d..8032a97 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ You can also use the filter `autopaging_post_types` to add support by appending * `autopaging_max_num_words` - override the minimum number of words allowed per page page when the paging type is "words". * `autopaging_num_pages` - change the number of pages content is displayed on at runtime. Filter provides access to the full post object in addition to the number of pages. * `autopaging_num_words` - change the number of words displayed per page at runtime. Filter provides access to the full post object in addition to the number of words. +* `autopaging_supported_block_types_for_word_counts` - specify which block types are considered when splitting a block-editor post by word count. ## Changelog ## diff --git a/automatically-paginate-posts.php b/automatically-paginate-posts.php index 8abe8dc..401e8f0 100644 --- a/automatically-paginate-posts.php +++ b/automatically-paginate-posts.php @@ -79,6 +79,16 @@ class Automatically_Paginate_Posts { */ private $num_words_default = ''; + /** + * When splitting by word counts, these blocks are considered. Tags are + * stripped and remaining content is counted. + * + * @var array + */ + private $supported_block_types_for_word_counts = array( + 'core/paragraph', + ); + /** * Allowed split types. * @@ -86,8 +96,6 @@ class Automatically_Paginate_Posts { */ private $paging_types_allowed = array( 'pages', 'words' ); - // Ensure option names match values in `uninstall()` method. - /** * Supported-post-types option name. * @@ -180,6 +188,12 @@ class Automatically_Paginate_Posts { if ( 0 == $this->num_words ) { $this->num_words = $this->num_words_default; } + + // Supported blocks for splitting by words. + $this->supported_block_types_for_word_counts = apply_filters( + 'autopaging_supported_block_types_for_word_counts', + $this->supported_block_types_for_word_counts + ); } /** @@ -666,6 +680,33 @@ class Automatically_Paginate_Posts { switch ( $paging_type ) { case 'words': + $word_count = 0; + + foreach ( $blocks as $block ) { + $new_blocks[] = $block; + + if ( + in_array( + $block['blockName'], + $this->supported_block_types_for_word_counts, + true + ) + ) { + $word_count += mb_strlen( + trim( + wp_strip_all_tags( + $block['innerHTML'] + ) + ) + ); + + if ( $word_count >= $num_words ) { + $new_blocks[] = $this->get_parsed_nextpage_block(); + + $word_count = 0; + } + } + } break; case 'pages': diff --git a/languages/automatically-paginate-posts.pot b/languages/automatically-paginate-posts.pot index 521dbec..61a53bf 100644 --- a/languages/automatically-paginate-posts.pot +++ b/languages/automatically-paginate-posts.pot @@ -5,7 +5,7 @@ msgstr "" "Project-Id-Version: Automatically Paginate Posts 0.3\n" "Report-Msgid-Bugs-To: " "https://wordpress.org/support/plugin/automatically-paginate-posts\n" -"POT-Creation-Date: 2022-06-26 21:05:45+00:00\n" +"POT-Creation-Date: 2022-06-26 22:43:32+00:00\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" @@ -29,39 +29,39 @@ msgstr "" msgid "Automatically Paginate Posts" msgstr "" -#: automatically-paginate-posts.php:223 +#: automatically-paginate-posts.php:244 msgid "Supported post types:" msgstr "" -#: automatically-paginate-posts.php:224 +#: automatically-paginate-posts.php:245 msgid "Split post by:" msgstr "" -#: automatically-paginate-posts.php:302 +#: automatically-paginate-posts.php:323 msgid "Total number of pages: " msgstr "" -#: automatically-paginate-posts.php:303 +#: automatically-paginate-posts.php:324 msgid "Approximate words per page: " msgstr "" -#: automatically-paginate-posts.php:370 +#: automatically-paginate-posts.php:391 msgid "" "If chosen, each page will contain approximately this many words, depending " "on paragraph lengths." msgstr "" -#: automatically-paginate-posts.php:400 +#: automatically-paginate-posts.php:421 msgid "Post Autopaging" msgstr "" -#: automatically-paginate-posts.php:416 +#: automatically-paginate-posts.php:437 msgid "" "Check the box above to prevent this post from automatically being split " "over multiple pages." msgstr "" -#: automatically-paginate-posts.php:421 +#: automatically-paginate-posts.php:442 #. translators: 1. Quicktag code example. msgid "" "Note that if the %1$s Quicktag is used to manually page this post, " diff --git a/readme.txt b/readme.txt index ff6901f..2070969 100644 --- a/readme.txt +++ b/readme.txt @@ -44,6 +44,7 @@ You can also use the filter `autopaging_post_types` to add support by appending * `autopaging_max_num_words` - override the minimum number of words allowed per page page when the paging type is "words". * `autopaging_num_pages` - change the number of pages content is displayed on at runtime. Filter provides access to the full post object in addition to the number of pages. * `autopaging_num_words` - change the number of words displayed per page at runtime. Filter provides access to the full post object in addition to the number of words. +* `autopaging_supported_block_types_for_word_counts` - specify which block types are considered when splitting a block-editor post by word count. == Changelog == -- GitLab