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..dec4ef7 100644 --- a/classes/TwigReadingTimeFilters.php +++ b/classes/TwigReadingTimeFilters.php @@ -51,10 +51,15 @@ 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)); + $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"); } @@ -80,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; @@ -91,7 +105,24 @@ 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_short_range = $minutes_short_count; + $minutes_long_range = $minutes_long_count; + } elseif ($minutes_low_range == 0) { + $minutes_short_range = $minutes_short_count; + $minutes_long_range = $minutes_long_count; + } else { + $minutes_short_range = ( + $minutes_low_range . $range_str . $minutes_high_range + ); + $minutes_long_range = ( + $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']; } elseif (array_key_exists('minutes_label', $options) and $minutes_short_count > 1) { @@ -109,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_short_range' => $minutes_short_range, + 'seconds_short_count' => $seconds_short_count, + 'minutes_long_count' => $minutes_long_count, + 'minutes_long_range' => $minutes_long_range, + 'seconds_long_count' => $seconds_long_count, + 'minutes_text' => $minutes_text, + 'seconds_text' => $seconds_text ]; $result = $options['format']; 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