-
Notifications
You must be signed in to change notification settings - Fork 7
/
cookiespolicy.php
64 lines (54 loc) · 2.16 KB
/
cookiespolicy.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
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
class CookiesPolicyPlugin extends Plugin
{
public static function getSubscribedEvents() {
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
];
}
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->active = false;
return;
}
$this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
]);
}
/**
* Add current directory to twig lookup paths.
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* if enabled on this page, load the JS + CSS theme.
*/
public function onTwigSiteVariables()
{
$type = strtolower($this->config->get('plugins.cookiespolicy.type'));
if (empty($type)){
throw new \InvalidArgumentException('The Cookie Policy type variable value must be defined. At the moment it is empty. If you are overriding the default configuration, please define the type variable too with a valid value.');
}
if (!preg_grep("/" . $type . "/i", array(
"bar",
"dialog",
))){
throw new \InvalidArgumentException(sprintf('The Cookie Policy type variable value must be one of "bar" or "dialog". You gave "%s"', $type));
}
$this->grav['assets']->addJs('plugin://cookiespolicy/assets/js/cookiechoices.js');
$this->grav['assets']->addCss('plugin://cookiespolicy/assets/css/cookiechoices_' . $type . '.css', -999);
$twig = $this->grav['twig'];
$twig->twig_vars['cookiespolicy_cookie_type'] = $type;
$twig->twig_vars['cookiespolicy_url'] = $this->config->get('plugins.cookiespolicy.url');
$twig->twig_vars['cookiespolicy_markup'] = $twig->twig->render('partials/cookiespolicy.html.twig', array(
'cookiespolicy_cookie_type' => $twig->twig_vars['cookiespolicy_cookie_type'],
'cookiespolicy_url' => $twig->twig_vars['cookiespolicy_url']
));
}
}