diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d6d6e6..643bd81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to this project will be documented in this file. +## 1.2.0 - 2018-06-18 + +### Added +- Calculate the read time of the whole entry based on it's field layout + ## 1.1.0 - 2018-06-06 ### Changed diff --git a/README.md b/README.md index 67d5be6..50f1982 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ The average user read speed is set at 200 words per minute by default, this can ## Using the Filter -The `|readTime` filter returns a human time duration of how long it takes the average user to read the provided content. The value provided can be a string or an array of values. +The `|readTime` filter returns the human time duration of how long it takes the average user to read the provided content. The value provided can be a string or an array of values. Seconds are included by default, but can be disabled by using `|readTime(false)` @@ -60,6 +60,26 @@ Returns: 2 minutes, 40 seconds Returns: 3 minutes ``` +## Using the Function + +The `readTime()` function returns the average read time for the whole entry based on it's field layout. + +Seconds are included by default, but can be disabled by adding a second parameter of `false` + +#### Examples + +```twig +{{ readTime(entry) }} + +Returns: 9 minutes, 40 seconds +``` + +```twig +{{ readTime(entry, false) }} + +Returns: 10 minutes +``` + ## Overriding Plugin Settings If you create a [config file](https://docs.craftcms.com/v3/configuration.html) in your `config` folder called `read-time.php`, you can override the plugin’s settings in the Control Panel. Since that config file is fully [multi-environment](https://docs.craftcms.com/v3/configuration.html) aware, this is a handy way to have different settings across multiple environments. @@ -78,6 +98,4 @@ return [ Some things to do, and ideas for potential features: -- Twig extension that calculates the read time for all of the fields that exist within a given entry `{{ readTime(entry) }}` for example - Brought to you by [Luke Youell](https://github.com/lukeyouell) diff --git a/composer.json b/composer.json index b7efa1a..8d5dde8 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "lukeyouell/craft-readtime", "description": "Calculate the estimated read time for content.", "type": "craft-plugin", - "version": "1.1.0", + "version": "1.2.0", "keywords": [ "craft", "cms", diff --git a/src/twigextensions/ReadTimeTwigExtension.php b/src/twigextensions/ReadTimeTwigExtension.php index aba683e..f35872a 100644 --- a/src/twigextensions/ReadTimeTwigExtension.php +++ b/src/twigextensions/ReadTimeTwigExtension.php @@ -15,6 +15,8 @@ use Craft; use craft\helpers\DateTimeHelper; +use yii\base\ErrorException; + class ReadTimeTwigExtension extends \Twig_Extension { // Public Methods @@ -25,14 +27,46 @@ public function getName() return 'readTime'; } + public function getFunctions() + { + return [ + new \Twig_SimpleFunction('readTime', [$this, 'readTimeFunction']), + ]; + } + public function getFilters() { return [ - new \Twig_SimpleFilter('readTime', [$this, 'readTime']), + new \Twig_SimpleFilter('readTime', [$this, 'readTimeFilter']), ]; } - public function readTime($value = null, $showSeconds = true) + public function readTimeFunction($element, $showSeconds = true) + { + $settings = ReadTime::$plugin->getSettings(); + $wpm = $settings->wordsPerMinute; + $totalSeconds = 0; + + foreach ($element->getFieldLayout()->getFields() as $field) { + try { + $fieldVal = $element->getFieldValue($field->handle); + + $value = is_array($fieldVal) ? implode(' ', $fieldVal) : (string)$fieldVal; + $words = str_word_count(strip_tags($value)); + $seconds = floor($words / $wpm * 60); + + $totalSeconds = $totalSeconds + $seconds; + } catch (ErrorException $e) { + continue; + } + } + + $duration = DateTimeHelper::secondsToHumanTimeDuration($seconds, $showSeconds); + + return $duration; + } + + public function readTimeFilter($value = null, $showSeconds = true) { $settings = ReadTime::$plugin->getSettings();