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