Skip to content

Commit

Permalink
Merge branch 'release/2.0.0-beta.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
jalendport committed Mar 8, 2023
2 parents 2ed1d2c + 2b07e85 commit 18e988e
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 74 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 2.0.0-beta.1 - 2023-03-07

### Added
- Initial Craft 4 release

## 1.6.0 - 2019-11-16

Transfer of ownership 👀
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Calculate the estimated read time for content.

#### Requirements

This plugin requires Craft CMS 3.0.0, or later.
This plugin requires Craft CMS 4.0.0, or later.

#### Plugin Store

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "jalendport/craft-readtime",
"description": "Calculate the estimated read time for content.",
"type": "craft-plugin",
"version": "1.6.0",
"version": "2.0.0-beta.1",
"keywords": [
"craft",
"cms",
Expand All @@ -26,7 +26,7 @@
}
],
"require": {
"craftcms/cms": "^3.0.0"
"craftcms/cms": "^4.0.0"
},
"autoload": {
"psr-4": {
Expand Down
34 changes: 17 additions & 17 deletions src/ReadTime.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Read Time plugin for Craft CMS 3.x
* Read Time plugin for Craft CMS 4.x
*
* Calculate the estimated read time for content.
*
Expand All @@ -10,35 +10,29 @@

namespace jalendport\readtime;

use jalendport\readtime\models\Settings;
use jalendport\readtime\twigextensions\ReadTimeTwigExtension;

use Craft;
use craft\base\Model;
use craft\base\Plugin;
use craft\services\Plugins;
use craft\events\PluginEvent;

use yii\base\Event;
use jalendport\readtime\models\Settings;
use jalendport\readtime\twigextensions\ReadTimeTwigExtension;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use yii\base\Exception;

class ReadTime extends Plugin
{
// Static Properties
// =========================================================================

public static $plugin;

// Public Properties
// =========================================================================

public $schemaVersion = '1.0.0';
public string $schemaVersion = '1.0.0';

// Public Methods
// =========================================================================

public function init()
{
parent::init();
self::$plugin = $this;

Craft::$app->view->registerTwigExtension(new ReadTimeTwigExtension());

Expand All @@ -55,12 +49,18 @@ public function init()
// Protected Methods
// =========================================================================

protected function createSettingsModel()
protected function createSettingsModel(): ?Model
{
return new Settings();
}

protected function settingsHtml(): string
/**
* @throws SyntaxError
* @throws RuntimeError
* @throws Exception
* @throws LoaderError
*/
protected function settingsHtml(): ?string
{
// Get and pre-validate the settings
$settings = $this->getSettings();
Expand Down
9 changes: 3 additions & 6 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Read Time plugin for Craft CMS 3.x
* Read Time plugin for Craft CMS 4.x
*
* Calculate the estimated read time for content.
*
Expand All @@ -10,22 +10,19 @@

namespace jalendport\readtime\models;

use jalendport\readtime\ReadTime;

use Craft;
use craft\base\Model;

class Settings extends Model
{
// Public Properties
// =========================================================================

public $wordsPerMinute = 200;
public int $wordsPerMinute = 200;

// Public Methods
// =========================================================================

public function rules()
public function rules(): array
{
return [
[['wordsPerMinute'], 'required'],
Expand Down
37 changes: 19 additions & 18 deletions src/models/TimeModel.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Read Time plugin for Craft CMS 3.x
* Read Time plugin for Craft CMS 4.x
*
* Calculate the estimated read time for content.
*
Expand All @@ -10,36 +10,37 @@

namespace jalendport\readtime\models;

use jalendport\readtime\ReadTime;

use Craft;
use craft\base\Model;
use craft\helpers\DateTimeHelper;
use Exception;

class TimeModel extends Model
{
// Public Properties
// =========================================================================

public $seconds = 0;
public int $seconds = 0;

public $showSeconds = true;
public bool $showSeconds = true;

// Public Methods
// =========================================================================

public function __toString()
{
return (string) $this->human();
return $this->human();
}

public function human()
{
return DateTimeHelper::secondsToHumanTimeDuration($this->seconds, $this->showSeconds);
public function human(): string
{
return DateTimeHelper::humanDuration($this->seconds, $this->showSeconds);
}

public function interval($format = '%h hours, %i minutes, %s seconds')
{
/**
* @throws Exception
*/
public function interval($format = '%h hours, %i minutes, %s seconds'): string
{
$currentTimeStamp = DateTimeHelper::currentTimeStamp();
$datetimeStart = DateTimeHelper::toDateTime($currentTimeStamp);
$datetimeEnd = DateTimeHelper::toDateTime(DateTimeHelper::currentTimeStamp() + $this->seconds);
Expand All @@ -49,18 +50,18 @@ public function interval($format = '%h hours, %i minutes, %s seconds')
return $interval->format($format);
}

public function seconds()
{
public function seconds(): int
{
return $this->seconds;
}

public function minutes()
{
public function minutes(): float
{
return floor($this->seconds / 60);
}

public function hours()
{
public function hours(): float
{
return floor(($this->seconds / 60) / 60);
}
}
69 changes: 39 additions & 30 deletions src/twigextensions/ReadTimeTwigExtension.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Read Time plugin for Craft CMS 3.x
* Read Time plugin for Craft CMS 4.x
*
* Calculate the estimated read time for content.
*
Expand All @@ -10,51 +10,59 @@

namespace jalendport\readtime\twigextensions;

use jalendport\readtime\ReadTime;
use jalendport\readtime\models\TimeModel;

use Craft;
use craft\helpers\DateTimeHelper;
use craft\elements\Entry;
use craft\elements\MatrixBlock;
use craft\errors\InvalidFieldException;
use craft\fields\Matrix;
use craft\helpers\StringHelper;

use jalendport\readtime\models\Settings;
use jalendport\readtime\ReadTime;
use jalendport\readtime\models\TimeModel;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use verbb\supertable\fields\SuperTableField;
use yii\base\ErrorException;

class ReadTimeTwigExtension extends \Twig_Extension
class ReadTimeTwigExtension extends AbstractExtension
{
// Public Methods
// =========================================================================

public function getName()
{
public function getName(): string
{
return 'readTime';
}

public function getFunctions()
{
public function getFunctions(): array
{
return [
new \Twig_SimpleFunction('readTime', [$this, 'readTimeFunction']),
new TwigFunction('readTime', [$this, 'readTimeFunction']),
];
}

public function getFilters()
{
public function getFilters(): array
{
return [
new \Twig_SimpleFilter('readTime', [$this, 'readTimeFilter']),
new TwigFilter('readTime', [$this, 'readTimeFilter']),
];
}

public function readTimeFunction($element, $showSeconds = true)
{
/**
* @throws InvalidFieldException
*/
public function readTimeFunction($element, $showSeconds = true): TimeModel
{
$totalSeconds = 0;
$vals = '';

if ($element instanceof \craft\elements\Entry) {
if ($element instanceof Entry) {
// Provided value is an entry

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

Expand All @@ -64,12 +72,12 @@ public function readTimeFunction($element, $showSeconds = true)
$totalSeconds = $totalSeconds + $seconds;
}
}
} elseif($field instanceof \verbb\supertable\fields\SuperTableField) {
} elseif($field instanceof SuperTableField) {
foreach($element->getFieldValue($field->handle)->all() as $block) {
$blockFields = $block->getFieldLayout()->getFields();

foreach ($blockFields as $blockField) {
if ($blockField instanceof \craft\fields\Matrix) {
if ($blockField instanceof Matrix) {
foreach($block->getFieldValue($blockField->handle)->all() as $matrix) {
$matrixFields = $matrix->getFieldLayout()->getFields();

Expand Down Expand Up @@ -100,8 +108,8 @@ public function readTimeFunction($element, $showSeconds = true)
Craft::info('matrix field provided', 'readtime');

foreach ($element as $block) {
if ($block instanceof \craft\elements\MatrixBlock) {
$blockFields = $block->getFieldLayout()->getFields();
if ($block instanceof MatrixBlock) {
$blockFields = $block->getFieldLayout()->getCustomFields();

foreach ($blockFields as $blockField) {
$value = $block->getFieldValue($blockField->handle);
Expand All @@ -120,8 +128,8 @@ public function readTimeFunction($element, $showSeconds = true)
return new TimeModel($data);
}

public function readTimeFilter($value = null, $showSeconds = true)
{
public function readTimeFilter($value = null, $showSeconds = true): TimeModel
{
$seconds = $this->valToSeconds($value);

$data = [
Expand All @@ -135,9 +143,10 @@ public function readTimeFilter($value = null, $showSeconds = true)
// Private Methods
// =========================================================================

private function valToSeconds($value)
{
$settings = ReadTime::$plugin->getSettings();
private function valToSeconds($value): float
{
/** @var Settings $settings */
$settings = ReadTime::getInstance()->getSettings();
$wpm = $settings->wordsPerMinute;

$string = StringHelper::toString($value);
Expand Down

0 comments on commit 18e988e

Please sign in to comment.