-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtecart-cookie-manager.php
252 lines (213 loc) · 8.26 KB
/
tecart-cookie-manager.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
* @package Grav\Plugin\TecartCookieManager
*
* @copyright Copyright (C) 2020 TecArt
* @license MIT License; see LICENSE file for details.
*/
namespace Grav\Plugin;
use Grav\Common\Grav;
use Grav\Common\Utils;
use Grav\Common\Plugin;
use Grav\Common\Data\Data;
use Grav\Common\Data\Blueprints;
use Grav\Common\File\CompiledYamlFile;
use RocketTheme\Toolbox\Event\Event;
use Grav\Plugin\TecartCookieManager\Classes\CookieManager\CookieManager;
use Grav\Plugin\TecartCookieManager\Classes\CookieConsent\CookieConsent;
require_once __DIR__ . '/classes/CookieManager/CookieManager.php';
require_once __DIR__ . '/classes/CookieConsent/CookieConsent.php';
/**
* Class TecartCookieManagerPlugin
* @package Grav\Plugin
*/
class TecartCookieManagerPlugin extends Plugin {
protected $routes = [
'cookie-manager',
'cookie-manager-categories',
'cookie-manager-scripts'
];
/**
* Base assets path.
* @type string
*/
private $assetsPath = 'plugin://tecart-cookie-manager/assets/';
/**
* Init cookie banner template.
* @type string
*/
private $initTemplate = 'partials/init-tecart-cookie-banner.html.twig';
/**
* custom stylesheet and javascript
* @type string
*/
//private $customCSS = 'css/tecart-cookie-manager.css';
private $customCSS = 'css/tecart-cookie-manager.min.css';
//private $customJS = 'js/tecart-cookie-manager.js';
private $customJS = 'js/tecart-cookie-manager.min.js';
/**
* cookie consent files
*
* // Source: https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.css
* // Source: https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js
*
* @type string
*/
private $cookieConsentCSS = 'vendor/cookieconsent/cookieconsent.min.css';
private $cookieConsentJS = 'vendor/cookieconsent/cookieconsent.min.js';
/**
* admin controller
* @type string
*/
private $adminController;
/**
* admin controller
* @type string
*/
private $dataStorageDefault = 'user://data';
/**
* @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 [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
'onAdminControllerInit' => ['onAdminControllerInit', 0],
];
}
/**
* Initialize the plugin
*/
public function onPluginsInitialized() {
$this->grav['locator']->addPath('blueprints', '', __DIR__ . DS . 'blueprints');
// proceed if we are in the admin plugin
if ($this->isAdmin()) {
// Enable the main event we are interested in
$this->enable([
'onAdminTwigTemplatePaths' => ['onAdminTwigTemplatePaths', 0],
'onGetPageTemplates' => ['onGetPageTemplates', 0],
'onAdminMenu' => ['onAdminMenu', 0],
'onAdminData' => ['onAdminData', 0],
]);
}
// don't proceed if we are in the admin plugin
else{
// Enable the main event we are interested in
$this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
]);
}
}
/**
* Push plugin templates to twig paths array.
* FRONTEND
*/
public function onTwigTemplatePaths() {
// Push own templates to twig paths
array_push($this->grav['twig']->twig_paths,__DIR__ . '/templates');
}
/**
* Add plugin CSS and JS files to the grav assets.
* FRONTEND
*/
public function onTwigSiteVariables() {
$twig = $this->grav['twig'];
$type = CookieManager::getCurrentCookieManagerPath();
if ($this->isAdmin()) {
if(in_array($type, $this->routes)){
$vars = CookieManager::getCookieManagerDataTwigVars();
$twig->twig_vars = array_merge($twig->twig_vars, $vars);
}
}
else {
$assets = $this->grav['assets'];
//array for twig variables of tecart-cookie-manager/blueprints.yaml//
$pluginConfig = $this->config->toArray();
$cookieBannerData = CookieConsent::getYamlDataByType('cookie-manager');
$cookieBannerScripts = CookieConsent::getYamlDataByType('cookie-manager-scripts');
$cookieBannerCategories = CookieConsent::getYamlDataByType('cookie-manager-categories');
// Add plugin CSS files to the grav assets.
$assets->addCss($this->assetsPath . $this->cookieConsentCSS, array('rel' => 'stylesheet'));
$assets->addCss($this->assetsPath . $this->customCSS, array('rel' => 'stylesheet'));
// Add plugin JS files to the grav assets.
$assets->addJs($this->assetsPath . $this->cookieConsentJS, array('loading' => 'async'));
$assets->addJs($this->assetsPath . $this->customJS, array('loading' => 'async'));
// Add inline JS to bottom of page to initialize cookie banner
$assets->addInlineJs(
$twig->twig->render(
$this->initTemplate,
[
'config' => $pluginConfig,
'cookieBannerData' => $cookieBannerData,
'cookieBannerScripts' => $cookieBannerScripts,
'cookieBannerCategories' => $cookieBannerCategories,
])
);
}
}
/**
* Add Routes to the custom data pages when the admin menu is being loaded
*/
public function onAdminMenu() {
if ($this->isAdmin()) {
$this->grav['twig']->plugins_hooked_nav['PLUGIN_TECART_COOKIE_MANGER.COOKIE_MANAGER'] = ['route' => $this->routes[0], 'icon' => 'fa-shield'];
}
}
/**
* Get admin page template
*/
public function onAdminTwigTemplatePaths(Event $event) {
$paths = $event['paths'];
$paths[] = __DIR__ . DS . 'admin/templates';
$event['paths'] = $paths;
}
/**
* Add blueprint directory.
*/
public function onGetPageTemplates(Event $event) {
$types = $event->types;
$types->scanBlueprints('plugin://' . $this->name . '/blueprints');
}
/**
* Add additional blueprints data
*/
public function onAdminControllerInit(Event $event) {
$controller = $event['controller'];
$this->adminController = $controller;
}
public function onAdminData(Event $event) {
$type = $event['type']; //current route
// Check if current context is a custom page
if(in_array($type, $this->routes)) {
$locator = Grav::instance()['locator'];
$blueprint = CookieManager::getCurrentCookieManagerBlueprint();
$obj = new Data(CookieManager::getCookieManagerData(), $blueprint);
$post = $this->adminController->data;
//location of yaml files
$dataStorage = $this->dataStorageDefault;
if($this->config['plugins']['tecart-cookie-manager']['data_storage'] ){
if($this->config['plugins']['tecart-cookie-manager']['data_storage'] == "pages") {
$dataStorage = "page://assets";
// If the directory doesn't exist yet, create the directory
if (!is_dir($locator->findResource($dataStorage))) {
mkdir($locator->findResource($dataStorage), 0755, true);
}
}
}
if($post){
$obj->merge($post);
$event['data_type'] = $obj;
$file = CompiledYamlFile::instance($locator->findResource($dataStorage) . DS .$type. ".yaml");
$obj->file($file);
$obj->save();
}
}
}
}