-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPlugin.php
131 lines (117 loc) · 3.14 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace GinoPane\BlogTimeToRead;
use System\Classes\PluginBase;
use RainLab\Blog\Models\Post as PostModel;
use GinoPane\BlogTimeToRead\Models\Settings;
use GinoPane\BlogTimeToRead\Helpers\TimeToRead;
use GinoPane\BlogTimeToRead\Components\TimeToRead as TimeToReadComponent;
/**
* Class Plugin
*
* @package GinoPane\BlogTimeToRead
*/
class Plugin extends PluginBase
{
const DEFAULT_ICON = 'icon-clock-o';
const LOCALIZATION_KEY = 'ginopane.blogtimetoread::lang.';
/**
* @var array Require some plugins
*/
public $require = [
'RainLab.Blog',
'RainLab.Translate'
];
/** @var TimeToRead */
private $helper;
/**
* Returns information about this plugin
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => self::LOCALIZATION_KEY . 'plugin.name',
'description' => self::LOCALIZATION_KEY . 'plugin.description',
'author' => 'Siarhei <Gino Pane> Karavai',
'icon' => self::DEFAULT_ICON,
'homepage' => 'https://github.com/GinoPane/oc-blogtimetoread-plugin'
];
}
/**
* Boot method, called right before the request route
*/
public function boot()
{
$this->helper = new TimeToRead(Settings::instance());
// extend the post model
$this->extendModel();
}
/**
* Register components
*
* @return array
*/
public function registerComponents()
{
return [
TimeToReadComponent::class => TimeToReadComponent::NAME,
];
}
/**
* Register plugin settings
*
* @return array
*/
public function registerSettings(){
return [
'settings' => [
'label' => self::LOCALIZATION_KEY . 'settings.name',
'description' => self::LOCALIZATION_KEY . 'settings.description',
'icon' => self::DEFAULT_ICON,
'class' => Settings::class,
'order' => 800,
'category' => 'rainlab.blog::lang.blog.menu_label'
]
];
}
/**
* @return array
*/
public function registerMarkupTags()
{
return [
'filters' => [
'timeToRead' => [$this, 'getTimeToRead']
]
];
}
/**
* @param $text
* @param null $speed
* @param null $roundingEnabled
*
* @return int
*/
public function getTimeToRead($text, $speed = null, $roundingEnabled = null)
{
return $this->helper->calculate(
$text,
array_filter([
Settings::READING_SPEED_KEY => $speed,
Settings::ROUNDING_UP_ENABLED_KEY => $roundingEnabled
])
);
}
/**
* Extend RainLab Post model
*/
private function extendModel()
{
PostModel::extend(function ($model) {
$model->addDynamicMethod('getTimeToReadAttribute', function() use ($model) {
return $this->helper->calculate($model->content);
});
});
}
}