From 7be32fd91135c21707eae93fbc66cc9870dd8c61 Mon Sep 17 00:00:00 2001 From: lukeyouell <> Date: Wed, 6 Jun 2018 14:20:09 +0100 Subject: [PATCH 1/6] Initial commit --- .gitattributes | 2 - .gitignore | 1 + CHANGELOG.md | 7 ++ LICENSE.md | 9 +++ README.md | 34 ++++++++- composer.json | 40 +++++++++++ src/ReadingTime.php | 72 +++++++++++++++++++ src/icon.svg | 12 ++++ src/models/Settings.php | 35 +++++++++ src/templates/settings.twig | 12 ++++ .../ReadingTimeTwigExtension.php | 49 +++++++++++++ 11 files changed, 270 insertions(+), 3 deletions(-) delete mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 LICENSE.md create mode 100644 composer.json create mode 100644 src/ReadingTime.php create mode 100644 src/icon.svg create mode 100644 src/models/Settings.php create mode 100644 src/templates/settings.twig create mode 100644 src/twigextensions/ReadingTimeTwigExtension.php diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index dfe0770..0000000 --- a/.gitattributes +++ /dev/null @@ -1,2 +0,0 @@ -# Auto detect text files and perform LF normalization -* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..5d65b27 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,7 @@ +# Reading Time Changelog + +All notable changes to this project will be documented in this file. + +## 1.0.0 - 2018-06-06 +### Added +- Initial release diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..6f534f3 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright (c) 2018 Luke Youell + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index 90a0a4d..6b5a3ec 100644 --- a/README.md +++ b/README.md @@ -1 +1,33 @@ -# craft-readingtime +icon + +# Reading Time plugin for Craft CMS 3 + +Calculate the estimated reading time for content. + +## Installation + +### Requirements + +This plugin requires Craft CMS 3.0.0, or later. + +### Plugin Store + +Log into your control panel and click on 'Plugin Store'. Search for 'Reading Time'. + +### Composer + +1. Open your terminal and go to your Craft project: + +```bash +cd /path/to/project +``` + +2. Then tell Composer to load the plugin: + +```bash +composer require lukeyouell/craft-readingtime +``` + +3. In the Control Panel, go to Settings → Plugins and click the “Install” button for Reading Time. + +Brought to you by [Luke Youell](https://github.com/lukeyouell) diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..cf0830a --- /dev/null +++ b/composer.json @@ -0,0 +1,40 @@ +{ + "name": "lukeyouell/craft-readingtime", + "description": "Calculate the estimated reading time for content.", + "type": "craft-plugin", + "version": "1.0.0", + "keywords": [ + "craft", + "cms", + "craftcms", + "craft-plugin", + "average reading time" + ], + "support": { + "docs": "https://github.com/lukeyouell/craft-readingtime/blob/master/README.md", + "issues": "https://github.com/lukeyouell/craft-readingtime/issues" + }, + "license": "MIT", + "authors": [ + { + "name": "Luke Youell", + "homepage": "https://github.com/lukeyouell" + } + ], + "require": { + "craftcms/cms": "^3.0.0" + }, + "autoload": { + "psr-4": { + "lukeyouell\\readingtime\\": "src/" + } + }, + "extra": { + "name": "Average Reading Time", + "handle": "average-reading-time", + "hasCpSettings": true, + "hasCpSection": false, + "changelogUrl": "https://raw.githubusercontent.com/lukeyouell/craft-readingtime/master/CHANGELOG.md", + "class": "lukeyouell\\readingtime\\ReadingTime" + } +} diff --git a/src/ReadingTime.php b/src/ReadingTime.php new file mode 100644 index 0000000..a2e885b --- /dev/null +++ b/src/ReadingTime.php @@ -0,0 +1,72 @@ +view->registerTwigExtension(new ReadingTimeTwigExtension()); + + Craft::info( + Craft::t( + 'average-reading-time', + '{name} plugin loaded', + ['name' => $this->name] + ), + __METHOD__ + ); + } + + // Protected Methods + // ========================================================================= + + protected function createSettingsModel() + { + return new Settings(); + } + + protected function settingsHtml(): string + { + return Craft::$app->view->renderTemplate( + 'average-reading-time/settings', + [ + 'settings' => $this->getSettings() + ] + ); + } +} diff --git a/src/icon.svg b/src/icon.svg new file mode 100644 index 0000000..dd16151 --- /dev/null +++ b/src/icon.svg @@ -0,0 +1,12 @@ + + + + + + average-reading-time-icon + + + diff --git a/src/models/Settings.php b/src/models/Settings.php new file mode 100644 index 0000000..1a47422 --- /dev/null +++ b/src/models/Settings.php @@ -0,0 +1,35 @@ + true] + ]; + } +} diff --git a/src/templates/settings.twig b/src/templates/settings.twig new file mode 100644 index 0000000..2925693 --- /dev/null +++ b/src/templates/settings.twig @@ -0,0 +1,12 @@ +{% import "_includes/forms" as forms %} + +{{ forms.textField({ + first: true, + required: true, + label: 'Words per Minute', + instructions: 'This is used to calculate the average reading time. Average readers reach around 200 wpm.', + placeholder: '200', + id: 'wordsPerMinute', + name: 'wordsPerMinute', + value: settings.wordsPerMinute, +}) }} diff --git a/src/twigextensions/ReadingTimeTwigExtension.php b/src/twigextensions/ReadingTimeTwigExtension.php new file mode 100644 index 0000000..e923b6c --- /dev/null +++ b/src/twigextensions/ReadingTimeTwigExtension.php @@ -0,0 +1,49 @@ +getSettings(); + + $value = is_array($value) ? implode(' ', $value) : (string)$value; + $wpm = $settings->wordsPerMinute; + + $words = str_word_count(strip_tags($value)); + $seconds = floor($words / $wpm * 60); + + $duration = DateTimeHelper::secondsToHumanTimeDuration($seconds, $showSeconds); + + return $duration; + } +} From ba5ad74aa62f83812b20d972b3c90fe81620d42c Mon Sep 17 00:00:00 2001 From: lukeyouell <> Date: Wed, 6 Jun 2018 14:25:22 +0100 Subject: [PATCH 2/6] Corrected name --- composer.json | 4 ++-- src/ReadingTime.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index cf0830a..74dcd68 100644 --- a/composer.json +++ b/composer.json @@ -30,8 +30,8 @@ } }, "extra": { - "name": "Average Reading Time", - "handle": "average-reading-time", + "name": "Reading Time", + "handle": "reading-time", "hasCpSettings": true, "hasCpSection": false, "changelogUrl": "https://raw.githubusercontent.com/lukeyouell/craft-readingtime/master/CHANGELOG.md", diff --git a/src/ReadingTime.php b/src/ReadingTime.php index a2e885b..c1940d3 100644 --- a/src/ReadingTime.php +++ b/src/ReadingTime.php @@ -44,7 +44,7 @@ public function init() Craft::info( Craft::t( - 'average-reading-time', + 'reading-time', '{name} plugin loaded', ['name' => $this->name] ), @@ -63,7 +63,7 @@ protected function createSettingsModel() protected function settingsHtml(): string { return Craft::$app->view->renderTemplate( - 'average-reading-time/settings', + 'reading-time/settings', [ 'settings' => $this->getSettings() ] From f4c02672f88411f94a2dd5ba0850c8650acce66e Mon Sep 17 00:00:00 2001 From: lukeyouell <> Date: Wed, 6 Jun 2018 14:27:17 +0100 Subject: [PATCH 3/6] Smaller headings --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6b5a3ec..793880a 100644 --- a/README.md +++ b/README.md @@ -6,15 +6,15 @@ Calculate the estimated reading time for content. ## Installation -### Requirements +#### Requirements This plugin requires Craft CMS 3.0.0, or later. -### Plugin Store +#### Plugin Store Log into your control panel and click on 'Plugin Store'. Search for 'Reading Time'. -### Composer +#### Composer 1. Open your terminal and go to your Craft project: From 33df6d836f0505e3b1c56e0d47e576f40b616b85 Mon Sep 17 00:00:00 2001 From: lukeyouell <> Date: Wed, 6 Jun 2018 14:33:51 +0100 Subject: [PATCH 4/6] Documentation --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 793880a..7dad17b 100644 --- a/README.md +++ b/README.md @@ -30,4 +30,22 @@ composer require lukeyouell/craft-readingtime 3. In the Control Panel, go to Settings → Plugins and click the “Install” button for Reading Time. +## Configuration + +The average user reading speed is set at 200 words per minute by default, this can be changed in the plugin settings. + +## Using the Filter + +The `|readingTime` 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. + +Seconds are included by default, but can be disabled by using `|readingTime(false)` + +#### Example + +```twig +{{ 'Sed scelerisque tellus vel faucibus luctus.'|readingTime }} + +Returns: 1 second +``` + Brought to you by [Luke Youell](https://github.com/lukeyouell) From 28dcecf91f6c0a3d3b21803a82cf6bdaf36ac244 Mon Sep 17 00:00:00 2001 From: lukeyouell <> Date: Wed, 6 Jun 2018 14:41:26 +0100 Subject: [PATCH 5/6] Overriding plugin settings --- README.md | 14 ++++++++++++++ src/ReadingTime.php | 10 +++++++++- src/templates/settings.twig | 12 ++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7dad17b..1f72f4a 100644 --- a/README.md +++ b/README.md @@ -48,4 +48,18 @@ Seconds are included by default, but can be disabled by using `|readingTime(fals Returns: 1 second ``` +## Overriding Plugin Settings + +If you create a [config file](https://docs.craftcms.com/v3/configuration.html) in your `config` folder called `reading-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. + +Here’s what that config file might look like along with a list of all of the possible values you can override. + +```php + 200 +]; +``` + Brought to you by [Luke Youell](https://github.com/lukeyouell) diff --git a/src/ReadingTime.php b/src/ReadingTime.php index c1940d3..ca98dab 100644 --- a/src/ReadingTime.php +++ b/src/ReadingTime.php @@ -62,10 +62,18 @@ protected function createSettingsModel() protected function settingsHtml(): string { + // Get and pre-validate the settings + $settings = $this->getSettings(); + $settings->validate(); + + // Get the settings that are being defined by the config file + $overrides = Craft::$app->getConfig()->getConfigFromFile(strtolower($this->handle)); + return Craft::$app->view->renderTemplate( 'reading-time/settings', [ - 'settings' => $this->getSettings() + 'settings' => $settings, + 'overrides' => array_keys($overrides) ] ); } diff --git a/src/templates/settings.twig b/src/templates/settings.twig index 2925693..25434b1 100644 --- a/src/templates/settings.twig +++ b/src/templates/settings.twig @@ -1,5 +1,15 @@ {% import "_includes/forms" as forms %} +{% macro configWarning(setting) -%} + {% set setting = '' ~ setting ~ '' %} + {{ 'This is being overridden by the {setting} config setting in your {file} config file.'|t('reading-time', { + setting: setting, + file: 'reading-time.php' + })|raw }} +{%- endmacro %} + +{% from _self import configWarning %} + {{ forms.textField({ first: true, required: true, @@ -9,4 +19,6 @@ id: 'wordsPerMinute', name: 'wordsPerMinute', value: settings.wordsPerMinute, + disabled: 'wordsPerMinute' in overrides, + warning: 'wordsPerMinute' in overrides ? configWarning('wordsPerMinute'), }) }} From 35fc80dba1d582a12444943ed11a1b9abc6d0c00 Mon Sep 17 00:00:00 2001 From: lukeyouell <> Date: Wed, 6 Jun 2018 14:44:21 +0100 Subject: [PATCH 6/6] Better examples --- README.md | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1f72f4a..216fc81 100644 --- a/README.md +++ b/README.md @@ -40,12 +40,24 @@ The `|readingTime` filter returns a human time duration of how long it takes the Seconds are included by default, but can be disabled by using `|readingTime(false)` -#### Example +#### Examples ```twig -{{ 'Sed scelerisque tellus vel faucibus luctus.'|readingTime }} +{{ string|readingTime }} -Returns: 1 second +Returns: 30 second +``` + +```twig +{{ richTextField|readingTime }} + +Returns: 2 minutes, 40 seconds +``` + +```twig +{{ richTextField|readingTime(false) }} + +Returns: 3 minutes ``` ## Overriding Plugin Settings