-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPlugin.php
225 lines (185 loc) · 6.1 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php namespace Vdomah\BlogViews;
use Db;
use Cookie;
use Event;
use Session;
use System\Classes\PluginBase;
use Rainlab\Blog\Components\Post as PostComponent;
use Rainlab\Blog\Models\Post as PostModel;
use Cms\Classes\Controller;
use DeviceDetector\Parser\Bot as BotParser;
use Vdomah\BlogViews\Components\Popular;
use Vdomah\BlogViews\Components\Views;
use Vdomah\BlogViews\Models\Settings;
/**
* BlogViews Plugin Information File
*/
class Plugin extends PluginBase
{
const POST_VIEWED = 'vdomah-blog-post-viewed';
const EVENT_BEFORE_TRACK = 'blogviews.before.track';
const PARAM_TRACK_PREVENT = 'trackPrevent';
const PARAM_TRACK_BOT = 'trackBot';
/**
* @var array Require the RainLab.Blog plugin
*/
public $require = ['RainLab.Blog'];
/**
* @var string Table to store post views count
*/
public $table_views = 'vdomah_blogviews_views';
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'vdomah.blogviews::lang.plugin.name',
'description' => 'vdomah.blogviews::lang.plugin.description',
'author' => 'Art Gek',
'icon' => 'icon-signal',
'homepage' => 'https://github.com/vdomah/blogviews'
];
}
/**
* Registers any front-end components implemented in this plugin.
*
* @return array
*/
public function registerComponents()
{
return [
Views::class => 'views',
Popular::class => 'popularPosts'
];
}
public function boot()
{
PostComponent::extend(function($component) {
if ($this->app->runningInBackend() || !Controller::getController()) {
return;
}
$result = Event::fire(self::EVENT_BEFORE_TRACK, [$this, $component]);
$preventTrack = isset($result[0][self::PARAM_TRACK_PREVENT]) && $result[0][self::PARAM_TRACK_PREVENT];
if ($preventTrack)
return;
$trackBot = isset($result[0][self::PARAM_TRACK_BOT]) && $result[0][self::PARAM_TRACK_BOT];
if ($this->isBot() && !$trackBot) {
// do not do anything if a bot is detected
return;
}
$post = $this->getPost($component);
if (!is_null($post)) {
$this->track($post);
}
return true;
});
PostModel::extend(function($model) {
$model->addDynamicMethod('getViewsAttribute', function() use ($model) {
$obj = Db::table('vdomah_blogviews_views')
->where('post_id', $model->getKey())->first();
$out = is_null($obj) ? 0 : $obj->views;
return $out;
});
});
}
private function setViews($post, $views = null)
{
$obj = Db::table($this->table_views)
->where('post_id', $post->getKey());
if ($obj->count() > 0) {
$row = $obj->first();
if (!$views) {
$views = ++$row->views;
}
$obj->update(['views' => $views]);
} else {
if (!$views) {
$views = 1;
}
Db::table($this->table_views)->insert([
'post_id' => $post->getKey(),
'views' => $views
]);
}
}
private function isBot()
{
$is_bot = false;
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$botParser = new BotParser();
$botParser->setUserAgent($_SERVER['HTTP_USER_AGENT']);
$botParser->discardDetails();
$is_bot = $botParser->parse();
}
return $is_bot;
}
/*
* Getting slug value using logic from Cms\Classes\Controller setComponentPropertiesFromParams
*
* @return PostModel
*/
private function getPost($component)
{
$slugValue = $component->property('slug');
$routerParameters = Controller::getController()->getRouter()->getParameters();
$slugValueFromUrl = null;
if (preg_match('/^\{\{([^\}]+)\}\}$/', $slugValue, $matches)) {
$paramName = trim($matches[1]);
if (substr($paramName, 0, 1) == ':') {
$routeParamName = substr($paramName, 1);
$slugValueFromUrl = array_key_exists($routeParamName, $routerParameters)
? $routerParameters[$routeParamName]
: null;
}
}
if (!$slugValueFromUrl)
return null;
$post = PostModel::whereSlug($slugValueFromUrl)->first();
return $post;
}
private function track($post)
{
if (Settings::get('double_tracking_prevent_method', 'cookie') == 'session') {
$this->setViewsSession($post);
} else {
$this->setViewsCookie($post);
}
}
private function setViewsSession($post)
{
if (!Session::has(self::POST_VIEWED)) {
Session::put(self::POST_VIEWED, []);
}
if (!in_array($post->getKey(), Session::get(self::POST_VIEWED))) {
$this->setViews($post);
Session::push(self::POST_VIEWED, $post->getKey());
}
}
private function setViewsCookie($post)
{
$cookName = self::POST_VIEWED . '-' . $post->getKey();
if (Cookie::get( $cookName, 0 ) == 0) {
$this->setViews($post);
Cookie::queue( $cookName, '1', 525000 );
}
}
public function registerSettings()
{
return [
'config' => [
'label' => 'vdomah.blogviews::lang.plugin.name',
'description' => 'vdomah.blogviews::lang.plugin.description_settings',
'category' => 'rainlab.blog::lang.blog.menu_label',
'icon' => 'icon-signal',
'class' => Settings::class,
'order' => 501,
'permissions' => [
'blogviews-menu-settings',
],
],
];
}
}