-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCookieConsentPlugin.php
211 lines (190 loc) · 6.14 KB
/
CookieConsentPlugin.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
<?php
declare(strict_types=1);
namespace Plugin\CookieConsent;
use App\Infrastructure\Services\Plugin;
use App\Shared\Services\Registry;
use App\Shared\Services\Utils;
use Codefy\CommandBus\Exceptions\CommandPropertyNotFoundException;
use Codefy\QueryBus\UnresolvableQueryHandlerException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\SimpleCache\InvalidArgumentException;
use Qubus\EventDispatcher\ActionFilter\Action;
use Qubus\EventDispatcher\ActionFilter\Filter;
use Qubus\Exception\Data\TypeException;
use Qubus\Exception\Exception;
use ReflectionException;
use function App\Shared\Helpers\add_plugins_submenu;
use function App\Shared\Helpers\cms_enqueue_css;
use function App\Shared\Helpers\cms_enqueue_js;
use function App\Shared\Helpers\plugin_basename;
use function App\Shared\Helpers\plugin_dir_path;
use function App\Shared\Helpers\plugin_url;
use function dirname;
use function Qubus\Security\Helpers\esc_html__;
use function strpos;
final class CookieConsentPlugin extends Plugin
{
/**
* @throws ReflectionException
* @throws Exception
*/
public function meta(): array
{
$plugin = [
'name' => esc_html__(string: 'Cookie Consent', domain: 'cookie-consent'),
'id' => 'cookie-consent',
'author' => 'Joshua Parker',
'version' => '2.0.0',
'description' => 'Cookie Consent helps you comply with the EU regulations
regarding the usage of website cookies.',
'basename' => plugin_basename(dirname(__FILE__)),
'path' => plugin_dir_path(dirname(__FILE__)),
'url' => plugin_url('', __CLASS__),
'pluginUri' => 'https://github.com/getdevflow/cookie-consent',
'authorUri' => 'https://nomadicjosh.com/',
'className' => get_class($this),
];
Registry::getInstance()->set('cc-plugin', $plugin);
return $plugin;
}
/**
* @throws ReflectionException
*/
public function handle(): void
{
Action::getInstance()->addAction('cms_head', [$this, 'enqueueFrontEndCss']);
Action::getInstance()->addAction('cms_footer', [$this, 'enqueueFrontEndJs']);
Action::getInstance()->addAction('cms_admin_head', [$this, 'enqueueBackEndCss']);
Action::getInstance()->addAction('cms_admin_footer', [$this, 'enqueueBackEndJs']);
Action::getInstance()->addAction('plugins_submenu', [$this, 'registerSubmenu']);
Action::getInstance()->addAction('plugins_loaded', [$this, 'render'], 2);
}
/**
* @return void
* @throws CommandPropertyNotFoundException
* @throws ContainerExceptionInterface
* @throws Exception
* @throws NotFoundExceptionInterface
* @throws ReflectionException
* @throws TypeException
* @throws UnresolvableQueryHandlerException
*/
public function registerSubmenu(): void
{
echo add_plugins_submenu(
menuTitle: $this->meta()['name'],
menuRoute: 'plugin/' . $this->meta()['id'],
screen: $this->meta()['id'],
permission: 'manage:plugins'
);
}
/**
* @return void
* @throws Exception
* @throws InvalidArgumentException
* @throws ReflectionException
*/
public function enqueueFrontEndCss(): void
{
if (Utils::isAdmin()) {
return;
}
if ($this->option->read('icc_popup_enabled') === 1) {
cms_enqueue_css(
config: 'plugin',
asset: $this->url() . '/assets/css/cookieconsent.min.css',
slug: $this->id()
);
}
}
/**
* @return void
* @throws Exception
* @throws InvalidArgumentException
* @throws ReflectionException
*/
public function enqueueFrontEndJs(): void
{
if (Utils::isAdmin()) {
return;
}
if ($this->option->read('icc_popup_enabled') === 1) {
cms_enqueue_js(
config: 'plugin',
asset: $this->url() . '/assets/js/cookieconsent.min.js',
slug: $this->id()
);
if ($this->option->read('icc_popup_enabled') === 1 && $this->option->read('icc_popup_options')) {
if (Utils::isAdmin()) {
return;
}
$config = $this->option->read('icc_popup_options');
echo '<script>window.cookieconsent.initialise(' . $config . ');</script>';
}
}
}
/**
* @return void
* @throws Exception
* @throws ReflectionException
*/
public function enqueueBackEndCss(): void
{
if (!Utils::isAdmin()) {
return;
}
if (
strpos(
Utils::getPathInfo(
'/admin/plugin/' . $this->id() . '/'
),
'/admin/plugin/' . $this->id() . '/'
) !== 0
) {
return;
}
cms_enqueue_css(
config: 'plugin',
asset: $this->url() . '/assets/css/admin.css',
slug: $this->id()
);
}
/**
* @return void
* @throws Exception
* @throws ReflectionException
*/
public function enqueueBackEndJs(): void
{
if (!Utils::isAdmin()) {
return;
}
if (
strpos(
Utils::getPathInfo(
'/admin/plugin/' . $this->id() . '/'
),
'/admin/plugin/' . $this->id() . '/'
) !== 0
) {
return;
}
cms_enqueue_js(
config: 'plugin',
asset: $this->url() . '/assets/js/scripts.js',
slug: $this->id()
);
}
/**
* @return void
* @throws ReflectionException
*/
public function render(): void
{
Filter::getInstance()->addFilter('plugin_route', function ($router) {
$router->setDefaultNamespace('\\Plugin\\CookieConsent\\Controllers');
$router->map(['GET', 'POST'], '/admin/plugin/cookie-consent/', 'CookieConsentController@index');
}, 5);
}
}