Skip to content

Commit

Permalink
Merge pull request #6 from lukeyouell/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
lukeyouell authored Jun 19, 2018
2 parents 18d1b1f + d5c4e90 commit 893ce6c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

All notable changes to this project will be documented in this file.

## 1.2.1 - 2018-06-19

### Fixed
- `readTime()` function now includes matrix fields when calculating the read time

## 1.2.0 - 2018-06-18

### Added
- Calculate the read time of the whole entry based on it's field layout
- Calculate the read time of the whole entry based on it's field layout

## 1.1.0 - 2018-06-06

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "lukeyouell/craft-readtime",
"description": "Calculate the estimated read time for content.",
"type": "craft-plugin",
"version": "1.2.0",
"version": "1.2.1",
"keywords": [
"craft",
"cms",
Expand Down
51 changes: 34 additions & 17 deletions src/twigextensions/ReadTimeTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Craft;
use craft\helpers\DateTimeHelper;
use craft\helpers\StringHelper;

use yii\base\ErrorException;

Expand Down Expand Up @@ -43,41 +44,57 @@ public function getFilters()

public function readTimeFunction($element, $showSeconds = true)
{
$settings = ReadTime::$plugin->getSettings();
$wpm = $settings->wordsPerMinute;
$totalSeconds = 0;
$vals = '';

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;
// If field is a matrix then loop through fields in block
if ($field instanceof craft\fields\Matrix) {
foreach($element->getFieldValue($field->handle)->all() as $block) {
$blockFields = $block->getFieldLayout()->getFields();

foreach($blockFields as $blockField){
$value = $block->getFieldValue($blockField->handle);
$seconds = $this->valToSeconds($value);
$totalSeconds = $totalSeconds + $seconds;
}
}
} else {
$value = $element->getFieldValue($field->handle);
$seconds = $this->valToSeconds($value);
$totalSeconds = $totalSeconds + $seconds;
}
} catch (ErrorException $e) {
continue;
}
}

$duration = DateTimeHelper::secondsToHumanTimeDuration($seconds, $showSeconds);
$duration = DateTimeHelper::secondsToHumanTimeDuration($totalSeconds, $showSeconds);

return $duration;
}

public function readTimeFilter($value = null, $showSeconds = true)
{
$settings = ReadTime::$plugin->getSettings();
$seconds = $this->valToSeconds($value);
$duration = DateTimeHelper::secondsToHumanTimeDuration($seconds, $showSeconds);

$value = is_array($value) ? implode(' ', $value) : (string)$value;
$wpm = $settings->wordsPerMinute;
return $duration;
}

$words = str_word_count(strip_tags($value));
$seconds = floor($words / $wpm * 60);
// Private Methods
// =========================================================================

$duration = DateTimeHelper::secondsToHumanTimeDuration($seconds, $showSeconds);
private function valToSeconds($value)
{
$settings = ReadTime::$plugin->getSettings();
$wpm = $settings->wordsPerMinute;

return $duration;
$string = StringHelper::toString($value);
$wordCount = StringHelper::countWords($string);
$seconds = floor($wordCount / $wpm * 60);

return $seconds;
}
}

0 comments on commit 893ce6c

Please sign in to comment.