From 96cf3a6e32291a937d2fb6289e29995945479bc1 Mon Sep 17 00:00:00 2001 From: Ryan Abel Date: Fri, 4 Dec 2020 00:04:04 -0600 Subject: [PATCH 1/5] Add estimate range settings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds range settings for reading time estimate ranges. estimate_range to control the range percentage ± and range_str for the string to combine the low and high estimates. --- blueprints.yaml | 17 +++++++++++++++++ classes/TwigReadingTimeFilters.php | 4 +++- readingtime.yaml | 2 ++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/blueprints.yaml b/blueprints.yaml index 3f22d21..9efd81e 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -36,6 +36,23 @@ form: min: 1 max: 1000 + estimate_range: + type: text + size: x-small + append: ±% + help: 'Percentage to add and remove from the reading time estimate to get a reading time range. 10% on a 10-minute read would give 9–11 minutes.' + label: Reading time estimate range + validate: + type: int + min: 0 + max: 100 + + range_str: + type: text + label: Estimate range string + help: 'String to use between the low and high estimate range numbers (e.g., "—", "-", " to ", etc.).' + size: x-small + format: type: text label: Format diff --git a/classes/TwigReadingTimeFilters.php b/classes/TwigReadingTimeFilters.php index d5545e3..62cc342 100644 --- a/classes/TwigReadingTimeFilters.php +++ b/classes/TwigReadingTimeFilters.php @@ -51,6 +51,8 @@ public function getReadingTime( $content, $params = array() ) $words = count(preg_split('/\s+/', strip_tags($content)) ?: []); $wpm = $options['words_per_minute']; + $estimate_range = ($options['estimate_range'] / 100); + $range_str = $options['range_str']; $minutes_short_count = floor($words / $wpm); $seconds_short_count = floor($words % $wpm / ($wpm / 60)); @@ -91,7 +93,7 @@ public function getReadingTime( $content, $params = array() ) $minutes_long_count = number_format($minutes_short_count, 2); $seconds_long_count = number_format($seconds_short_count, 2); - + if (array_key_exists('minute_label', $options) and $minutes_short_count == 1) { $minutes_text = $options['minute_label']; } elseif (array_key_exists('minutes_label', $options) and $minutes_short_count > 1) { diff --git a/readingtime.yaml b/readingtime.yaml index 9247030..44a309b 100644 --- a/readingtime.yaml +++ b/readingtime.yaml @@ -1,5 +1,7 @@ enabled: true words_per_minute: 200 +estimate_range: 15 +range_str: "–" format: "{minutes_short_count} {minutes_text}, {seconds_short_count} {seconds_text}" round: seconds include_image_views: false From 8536bc854c17d0f41d5d1c2189d6b26766305b14 Mon Sep 17 00:00:00 2001 From: Ryan Abel Date: Fri, 4 Dec 2020 00:06:58 -0600 Subject: [PATCH 2/5] Add estimate range calculation --- classes/TwigReadingTimeFilters.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/classes/TwigReadingTimeFilters.php b/classes/TwigReadingTimeFilters.php index 62cc342..5691586 100644 --- a/classes/TwigReadingTimeFilters.php +++ b/classes/TwigReadingTimeFilters.php @@ -57,6 +57,9 @@ public function getReadingTime( $content, $params = array() ) $minutes_short_count = floor($words / $wpm); $seconds_short_count = floor($words % $wpm / ($wpm / 60)); + $minutes_low_range = floor(($words * (1 - $estimate_range)) / $wpm); + $minutes_high_range = floor(($words * (1 + $estimate_range)) / $wpm); + if ($options['include_image_views']) { $stripped = strip_tags($content, ""); $images_in_content = substr_count($stripped, "grav['log']->error("Plugin 'readingtime' - seconds_per_image failed regex vadation"); } @@ -82,10 +88,16 @@ public function getReadingTime( $content, $params = array() ) $round = $options['round']; if ($round == 'minutes') { - $minutes_short_count = round(($minutes_short_count*60 + $seconds_short_count) / 60); + $minutes_short_count = round(($minutes_short_count * 60 + $seconds_short_count) / 60); + + $minutes_low_range = round(($minutes_low_range * 60 + $seconds_low_range) / 60); + $minutes_high_range = round(($minutes_high_range * 60 + $seconds_high_range) / 60); if ( $minutes_short_count < 1 ) { $minutes_short_count = 1; + + $minutes_low_range = 0; + $minutes_high_range = 1; } $seconds_short_count = 0; From e5f9a85a96fdb870858e6b59c9787d3d4da14618 Mon Sep 17 00:00:00 2001 From: Ryan Abel Date: Fri, 4 Dec 2020 00:07:20 -0600 Subject: [PATCH 3/5] Add estimate range formatting Depending on the reading time estimate and the estimate range percentage, we may end up with identical high and low estimates or flooring out at zero minutes. Test for these cases and return a range that makes sense. --- classes/TwigReadingTimeFilters.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/classes/TwigReadingTimeFilters.php b/classes/TwigReadingTimeFilters.php index 5691586..4159bc2 100644 --- a/classes/TwigReadingTimeFilters.php +++ b/classes/TwigReadingTimeFilters.php @@ -105,6 +105,23 @@ public function getReadingTime( $content, $params = array() ) $minutes_long_count = number_format($minutes_short_count, 2); $seconds_long_count = number_format($seconds_short_count, 2); + $minutes_low_range_long = number_format($minutes_low_range, 2); + $minutes_high_range_long = number_format($minutes_high_range, 2); + + if ($minutes_low_range == $minutes_high_range or $minutes_low_range == 0) { + $minutes_range_short = $minutes_short_count; + $minutes_range_long = $minutes_long_count; + } elseif ($minutes_low_range == 0) { + $minutes_range_short = $minutes_short_count; + $minutes_range_long = $minutes_long_count; + } else { + $minutes_range_short = ( + $minutes_low_range . $range_str . $minutes_high_range + ); + $minutes_range_long = ( + $minutes_low_range_long . $range_str . $minutes_high_range_long + ); + } if (array_key_exists('minute_label', $options) and $minutes_short_count == 1) { $minutes_text = $options['minute_label']; From 638fb96b07eb573cd541c823c8323221da652893 Mon Sep 17 00:00:00 2001 From: Ryan Abel Date: Fri, 4 Dec 2020 00:08:49 -0600 Subject: [PATCH 4/5] Add estimate range to return list --- classes/TwigReadingTimeFilters.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/classes/TwigReadingTimeFilters.php b/classes/TwigReadingTimeFilters.php index 4159bc2..714ff0f 100644 --- a/classes/TwigReadingTimeFilters.php +++ b/classes/TwigReadingTimeFilters.php @@ -140,12 +140,14 @@ public function getReadingTime( $content, $params = array() ) } $replace = [ - 'minutes_short_count' => $minutes_short_count, - 'seconds_short_count' => $seconds_short_count, - 'minutes_long_count' => $minutes_long_count, - 'seconds_long_count' => $seconds_long_count, - 'minutes_text' => $minutes_text, - 'seconds_text' => $seconds_text + 'minutes_short_count' => $minutes_short_count, + 'minutes_range_short' => $minutes_range_short, + 'seconds_short_count' => $seconds_short_count, + 'minutes_long_count' => $minutes_long_count, + 'minutes_range_long' => $minutes_range_long, + 'seconds_long_count' => $seconds_long_count, + 'minutes_text' => $minutes_text, + 'seconds_text' => $seconds_text ]; $result = $options['format']; From afca3a272d3401d785cf4d04c761f401ac1d2100 Mon Sep 17 00:00:00 2001 From: Ryan Abel Date: Fri, 4 Dec 2020 00:26:57 -0600 Subject: [PATCH 5/5] Flip range and short/long order in variable names --- classes/TwigReadingTimeFilters.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/classes/TwigReadingTimeFilters.php b/classes/TwigReadingTimeFilters.php index 714ff0f..dec4ef7 100644 --- a/classes/TwigReadingTimeFilters.php +++ b/classes/TwigReadingTimeFilters.php @@ -109,16 +109,16 @@ public function getReadingTime( $content, $params = array() ) $minutes_high_range_long = number_format($minutes_high_range, 2); if ($minutes_low_range == $minutes_high_range or $minutes_low_range == 0) { - $minutes_range_short = $minutes_short_count; - $minutes_range_long = $minutes_long_count; + $minutes_short_range = $minutes_short_count; + $minutes_long_range = $minutes_long_count; } elseif ($minutes_low_range == 0) { - $minutes_range_short = $minutes_short_count; - $minutes_range_long = $minutes_long_count; + $minutes_short_range = $minutes_short_count; + $minutes_long_range = $minutes_long_count; } else { - $minutes_range_short = ( + $minutes_short_range = ( $minutes_low_range . $range_str . $minutes_high_range ); - $minutes_range_long = ( + $minutes_long_range = ( $minutes_low_range_long . $range_str . $minutes_high_range_long ); } @@ -141,10 +141,10 @@ public function getReadingTime( $content, $params = array() ) $replace = [ 'minutes_short_count' => $minutes_short_count, - 'minutes_range_short' => $minutes_range_short, + 'minutes_short_range' => $minutes_short_range, 'seconds_short_count' => $seconds_short_count, 'minutes_long_count' => $minutes_long_count, - 'minutes_range_long' => $minutes_range_long, + 'minutes_long_range' => $minutes_long_range, 'seconds_long_count' => $seconds_long_count, 'minutes_text' => $minutes_text, 'seconds_text' => $seconds_text