-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from lukeyouell/develop
Develop
- Loading branch information
Showing
11 changed files
with
334 additions
and
3 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,77 @@ | ||
# craft-readingtime | ||
<img src="src/icon.svg" alt="icon" width="100" height="100"> | ||
|
||
# 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. | ||
|
||
## 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)` | ||
|
||
#### Examples | ||
|
||
```twig | ||
{{ string|readingTime }} | ||
Returns: 30 second | ||
``` | ||
|
||
```twig | ||
{{ richTextField|readingTime }} | ||
Returns: 2 minutes, 40 seconds | ||
``` | ||
|
||
```twig | ||
{{ richTextField|readingTime(false) }} | ||
Returns: 3 minutes | ||
``` | ||
|
||
## 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 | ||
<?php | ||
|
||
return [ | ||
'wordsPerMinute' => 200 | ||
]; | ||
``` | ||
|
||
Brought to you by [Luke Youell](https://github.com/lukeyouell) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "Reading Time", | ||
"handle": "reading-time", | ||
"hasCpSettings": true, | ||
"hasCpSection": false, | ||
"changelogUrl": "https://raw.githubusercontent.com/lukeyouell/craft-readingtime/master/CHANGELOG.md", | ||
"class": "lukeyouell\\readingtime\\ReadingTime" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
/** | ||
* Reading Time plugin for Craft CMS 3.x | ||
* | ||
* Calculate the estimated reading time for content. | ||
* | ||
* @link https://github.com/lukeyouell | ||
* @copyright Copyright (c) 2018 Luke Youell | ||
*/ | ||
|
||
namespace lukeyouell\readingtime; | ||
|
||
use lukeyouell\readingtime\models\Settings; | ||
use lukeyouell\readingtime\twigextensions\ReadingTimeTwigExtension; | ||
|
||
use Craft; | ||
use craft\base\Plugin; | ||
use craft\services\Plugins; | ||
use craft\events\PluginEvent; | ||
|
||
use yii\base\Event; | ||
|
||
class ReadingTime extends Plugin | ||
{ | ||
// Static Properties | ||
// ========================================================================= | ||
|
||
public static $plugin; | ||
|
||
// Public Properties | ||
// ========================================================================= | ||
|
||
public $schemaVersion = '1.0.0'; | ||
|
||
// Public Methods | ||
// ========================================================================= | ||
|
||
public function init() | ||
{ | ||
parent::init(); | ||
self::$plugin = $this; | ||
|
||
Craft::$app->view->registerTwigExtension(new ReadingTimeTwigExtension()); | ||
|
||
Craft::info( | ||
Craft::t( | ||
'reading-time', | ||
'{name} plugin loaded', | ||
['name' => $this->name] | ||
), | ||
__METHOD__ | ||
); | ||
} | ||
|
||
// Protected Methods | ||
// ========================================================================= | ||
|
||
protected function createSettingsModel() | ||
{ | ||
return new Settings(); | ||
} | ||
|
||
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' => $settings, | ||
'overrides' => array_keys($overrides) | ||
] | ||
); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
/** | ||
* Reading Time plugin for Craft CMS 3.x | ||
* | ||
* Calculate the estimated reading time for content. | ||
* | ||
* @link https://github.com/lukeyouell | ||
* @copyright Copyright (c) 2018 Luke Youell | ||
*/ | ||
|
||
namespace lukeyouell\readingtime\models; | ||
|
||
use lukeyouell\readingtime\ReadingTime; | ||
|
||
use Craft; | ||
use craft\base\Model; | ||
|
||
class Settings extends Model | ||
{ | ||
// Public Properties | ||
// ========================================================================= | ||
|
||
public $wordsPerMinute = 200; | ||
|
||
// Public Methods | ||
// ========================================================================= | ||
|
||
public function rules() | ||
{ | ||
return [ | ||
[['wordsPerMinute'], 'required'], | ||
[['wordsPerMinute'], 'number', 'integerOnly' => true] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{% import "_includes/forms" as forms %} | ||
|
||
{% macro configWarning(setting) -%} | ||
{% set setting = '<code>' ~ setting ~ '</code>' %} | ||
{{ '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, | ||
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, | ||
disabled: 'wordsPerMinute' in overrides, | ||
warning: 'wordsPerMinute' in overrides ? configWarning('wordsPerMinute'), | ||
}) }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
/** | ||
* Reading Time plugin for Craft CMS 3.x | ||
* | ||
* Calculate the estimated reading time for content. | ||
* | ||
* @link https://github.com/lukeyouell | ||
* @copyright Copyright (c) 2018 Luke Youell | ||
*/ | ||
|
||
namespace lukeyouell\readingtime\twigextensions; | ||
|
||
use lukeyouell\readingtime\ReadingTime; | ||
|
||
use Craft; | ||
use craft\helpers\DateTimeHelper; | ||
|
||
class ReadingTimeTwigExtension extends \Twig_Extension | ||
{ | ||
// Public Methods | ||
// ========================================================================= | ||
|
||
public function getName() | ||
{ | ||
return 'readingTime'; | ||
} | ||
|
||
public function getFilters() | ||
{ | ||
return [ | ||
new \Twig_SimpleFilter('readingTime', [$this, 'readingTime']), | ||
]; | ||
} | ||
|
||
public function readingTime($value = null, $showSeconds = true) | ||
{ | ||
$settings = ReadingTime::$plugin->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; | ||
} | ||
} |