-
Notifications
You must be signed in to change notification settings - Fork 1
/
views.php
157 lines (135 loc) · 3.79 KB
/
views.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
<?php
/**
* @package Grav\Plugin\Views
*
* @copyright Copyright (C) 2014 - 2019 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/
namespace Grav\Plugin;
use Composer\Autoload\ClassLoader;
use Grav\Common\Config\Config;
use Grav\Common\Plugin;
use Grav\Common\Uri;
use Grav\Plugin\Views\Views;
use RocketTheme\Toolbox\Event\Event;
/**
* Class ViewsPlugin
* @package Grav\Plugin
*/
class ViewsPlugin extends Plugin
{
/**
* @return array
*
* The getSubscribedEvents() gives the core a list of events
* that the plugin wants to listen to. The key of each
* array section is the event that the plugin listens to
* and the value (in the form of an array) contains the
* callable (or function) as well as the priority. The
* higher the number the higher the priority.
*/
public static function getSubscribedEvents()
{
return [
'onCliInitialize' => [
['autoload', 100000],
['register', 1000]
],
'onPluginsInitialized' => [
['autoload', 100000],
['register', 1000],
['onPluginsInitialized', 1000]
]
];
}
/**
* [onPluginsInitialized:100000] Composer autoload.
*
* @return ClassLoader
*/
public function autoload()
{
return require __DIR__ . '/vendor/autoload.php';
}
/**
* Register the service
*/
public function register()
{
$this->grav['views'] = function ($c) {
/** @var Config $config */
$config = $c['config'];
return new Views($config->get('plugins.views'));
};
}
/**
* Initialize the plugin
*/
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->enable([
'onAdminGenerateReports' => [
['onAdminGenerateReports', 10]
],
'onTwigTemplatePaths' => [
['onTwigTemplatePaths', 0]
],
]);
return;
}
$this->enable([
'onTwigInitialized' => [
['onTwigInitialized', 0]
],
]);
if ($this->grav['config']->get('plugins.views.autotrack')) {
$this->enable([
'onPageInitialized' => [
['onPageInitialized', 0]
],
]);
}
}
public function onAdminGenerateReports(Event $e)
{
$reports = $e['reports'];
/** @var Uri $uri */
$uri = $this->grav['uri'];
$data = [
'views' => $this->grav['views']->getAll(null, 20, 'desc'),
'base_url' => $baseUrlRelative = $uri->rootUrl(false),
];
$reports['Grav Views'] = $this->grav['twig']->processTemplate('reports/views-report.html.twig', $data);
}
/**
* Add current directory to twig lookup paths.
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
public function onTwigInitialized()
{
$this->grav['twig']->twig()->addFunction(
new \Twig_SimpleFunction('track_views', [$this, 'trackViewsFunc'], ['is_safe' => ['html']])
);
}
public function onPageInitialized(Event $event)
{
$page = $event['page'];
$this->grav['views']->track($page->route(), 'pages');
}
/**
* @param mixed|null $id
*/
public function trackViewsFunc($id = null, $type = 'pages')
{
if (null === $id) {
return;
}
// Convert objects to string
$id = (string)$id;
$this->grav['views']->track($id, $type);
}
}