Skip to content

Commit 60b6fde

Browse files
committed
Initial commit.
Signed-off-by: Joshua Parker <joshua@joshuaparker.dev>
0 parents  commit 60b6fde

16 files changed

+1587
-0
lines changed

.gitattributes

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
assets/* linguist-vendored
2+
*.html linguist-detectable=false
3+
*.js linguist-detectable=false
4+
*.css linguist-detectable=false
5+
*.less linguist-detectable=false
6+
7+
# Configure diff output for .php and .phar files.
8+
*.php diff=php
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Plugin\CookieConsent\Controllers;
6+
7+
use App\Application\Devflow;
8+
use App\Infrastructure\Persistence\Database;
9+
use App\Infrastructure\Services\Options;
10+
use App\Infrastructure\Services\UserAuth;
11+
use Codefy\Framework\Http\BaseController;
12+
use Psr\Container\ContainerExceptionInterface;
13+
use Psr\Container\NotFoundExceptionInterface;
14+
use Psr\Http\Message\ResponseInterface;
15+
use Psr\SimpleCache\InvalidArgumentException;
16+
use Qubus\Exception\Data\TypeException;
17+
use Qubus\Exception\Exception;
18+
use Qubus\Http\ServerRequest;
19+
use Qubus\Http\Session\SessionException;
20+
use Qubus\Http\Session\SessionService;
21+
use Qubus\Routing\Router;
22+
use Qubus\View\Renderer;
23+
use ReflectionException;
24+
25+
use function App\Shared\Helpers\admin_url;
26+
use function Qubus\Security\Helpers\t__;
27+
28+
class CookieConsentController extends BaseController
29+
{
30+
public function __construct(
31+
SessionService $sessionService,
32+
Router $router,
33+
protected UserAuth $user,
34+
protected Database $dfdb,
35+
?Renderer $view = null
36+
) {
37+
parent::__construct($sessionService, $router, $view);
38+
}
39+
40+
/**
41+
* @param ServerRequest $request
42+
* @return string|ResponseInterface
43+
* @throws ContainerExceptionInterface
44+
* @throws Exception
45+
* @throws InvalidArgumentException
46+
* @throws NotFoundExceptionInterface
47+
* @throws ReflectionException
48+
* @throws SessionException
49+
* @throws TypeException
50+
*/
51+
public function index(ServerRequest $request): string|ResponseInterface
52+
{
53+
if (false === $this->user->can(permissionName: 'manage:plugins', request: $request)) {
54+
Devflow::inst()::$APP->flash->error(
55+
message: t__(msgid: 'Access denied.', domain: 'cookie-consent')
56+
);
57+
58+
return $this->redirect(admin_url());
59+
}
60+
61+
if ($request->getMethod() === 'POST') {
62+
$update = Options::factory()->massUpdate($request->getParsedBody());
63+
64+
if ($update === false) {
65+
Devflow::inst()::$APP->flash->error(
66+
message: t__(msgid: 'Update error.', domain: 'cookie-consent')
67+
);
68+
} else {
69+
Devflow::inst()::$APP->flash->success(
70+
message: t__(msgid: 'Updated successfully.', domain: 'cookie-consent')
71+
);
72+
}
73+
74+
return $this->redirect($request->getServerParams()['HTTP_REFERER']);
75+
}
76+
77+
return $this->view->render('plugin::CookieConsent/view/index');
78+
}
79+
}

CookieConsentPlugin.php

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Plugin\CookieConsent;
6+
7+
use App\Infrastructure\Services\Plugin;
8+
use App\Shared\Services\Registry;
9+
use App\Shared\Services\Utils;
10+
use Codefy\CommandBus\Exceptions\CommandPropertyNotFoundException;
11+
use Codefy\Framework\Codefy;
12+
use Codefy\QueryBus\UnresolvableQueryHandlerException;
13+
use Psr\Container\ContainerExceptionInterface;
14+
use Psr\Container\NotFoundExceptionInterface;
15+
use Psr\SimpleCache\InvalidArgumentException;
16+
use Qubus\EventDispatcher\ActionFilter\Action;
17+
use Qubus\EventDispatcher\ActionFilter\Filter;
18+
use Qubus\Exception\Data\TypeException;
19+
use Qubus\Exception\Exception;
20+
use ReflectionException;
21+
22+
use function App\Shared\Helpers\add_plugins_submenu;
23+
use function App\Shared\Helpers\cms_enqueue_css;
24+
use function App\Shared\Helpers\cms_enqueue_js;
25+
use function App\Shared\Helpers\load_plugin_textdomain;
26+
use function App\Shared\Helpers\plugin_basename;
27+
use function App\Shared\Helpers\plugin_dir_path;
28+
use function App\Shared\Helpers\plugin_url;
29+
use function dirname;
30+
use function Qubus\Security\Helpers\esc_html__;
31+
use function strpos;
32+
33+
final class CookieConsentPlugin extends Plugin
34+
{
35+
/**
36+
* @throws ReflectionException
37+
* @throws Exception
38+
*/
39+
public function meta(): array
40+
{
41+
$plugin = [
42+
'name' => esc_html__(string: 'Cookie Consent', domain: 'cookie-consent'),
43+
'id' => 'cookie-consent',
44+
'author' => 'Joshua Parker',
45+
'version' => '1.0.0',
46+
'description' => 'Cookie Consent helps you comply with the EU regulations
47+
regarding the usage of website cookies.',
48+
'basename' => plugin_basename(dirname(__FILE__)),
49+
'path' => plugin_dir_path(__FILE__),
50+
'url' => plugin_url('', __CLASS__),
51+
'pluginUri' => 'https://github.com/getdevflow/cookie-consent',
52+
'authorUri' => 'https://nomadicjosh.com/',
53+
'className' => get_class($this),
54+
];
55+
56+
Registry::getInstance()->set('cc-plugin', $plugin);
57+
58+
return $plugin;
59+
}
60+
61+
/**
62+
* @throws ReflectionException
63+
*/
64+
public function handle(): void
65+
{
66+
Action::getInstance()->addAction('plugins_loaded', [$this, 'setLocale']);
67+
Action::getInstance()->addAction('cms_head', [$this, 'enqueueFrontEndCss']);
68+
Action::getInstance()->addAction('cms_footer', [$this, 'enqueueFrontEndJs']);
69+
Action::getInstance()->addAction('cms_admin_head', [$this, 'enqueueBackEndCss']);
70+
Action::getInstance()->addAction('cms_admin_footer', [$this, 'enqueueBackEndJs']);
71+
Action::getInstance()->addAction('plugins_submenu', [$this, 'registerSubmenu']);
72+
Action::getInstance()->addAction('plugins_loaded', [$this, 'render']);
73+
}
74+
75+
/**
76+
* @return void
77+
* @throws ContainerExceptionInterface
78+
* @throws Exception
79+
* @throws InvalidArgumentException
80+
* @throws NotFoundExceptionInterface
81+
* @throws ReflectionException
82+
*/
83+
public function setLocale(): void
84+
{
85+
load_plugin_textdomain($this->id(), $this->path() . 'locale');
86+
}
87+
88+
/**
89+
* @return void
90+
* @throws CommandPropertyNotFoundException
91+
* @throws ContainerExceptionInterface
92+
* @throws Exception
93+
* @throws NotFoundExceptionInterface
94+
* @throws ReflectionException
95+
* @throws TypeException
96+
* @throws UnresolvableQueryHandlerException
97+
*/
98+
public function registerSubmenu(): void
99+
{
100+
echo add_plugins_submenu(
101+
menuTitle: $this->meta()['name'],
102+
menuRoute: 'plugin/' . $this->meta()['id'],
103+
screen: $this->meta()['id'],
104+
permission: 'manage:plugins'
105+
);
106+
}
107+
108+
/**
109+
* @return void
110+
* @throws Exception
111+
* @throws InvalidArgumentException
112+
* @throws ReflectionException
113+
*/
114+
public function enqueueFrontEndCss(): void
115+
{
116+
if (Utils::isAdmin()) {
117+
return;
118+
}
119+
120+
if ($this->option->read('icc_popup_enabled') === 1) {
121+
cms_enqueue_css(
122+
config: 'plugin',
123+
asset: $this->url() . '/assets/css/cookieconsent.min.css',
124+
minify: false,
125+
pluginSlug: $this->id()
126+
);
127+
}
128+
}
129+
130+
/**
131+
* @return void
132+
* @throws Exception
133+
* @throws InvalidArgumentException
134+
* @throws ReflectionException
135+
*/
136+
public function enqueueFrontEndJs(): void
137+
{
138+
if (Utils::isAdmin()) {
139+
return;
140+
}
141+
142+
if ($this->option->read('icc_popup_enabled') === 1) {
143+
cms_enqueue_js(
144+
config: 'plugin',
145+
asset: $this->url() . '/assets/js/cookieconsent.min.js',
146+
minify: false,
147+
pluginSlug: $this->id()
148+
);
149+
150+
if ($this->option->read('icc_popup_enabled') === 1 && $this->option->read('icc_popup_options')) {
151+
if (Utils::isAdmin()) {
152+
return;
153+
}
154+
$config = $this->option->read('icc_popup_options');
155+
echo '<script>window.cookieconsent.initialise(' . $config . ');</script>';
156+
}
157+
}
158+
}
159+
160+
/**
161+
* @return void
162+
* @throws Exception
163+
* @throws ReflectionException
164+
*/
165+
public function enqueueBackEndCss(): void
166+
{
167+
if (!Utils::isAdmin()) {
168+
return;
169+
}
170+
171+
if (
172+
strpos(
173+
Utils::getPathInfo(
174+
'/admin/plugin/' . $this->id() . '/'
175+
),
176+
'/admin/plugin/' . $this->id() . '/'
177+
) !== 0
178+
) {
179+
return;
180+
}
181+
182+
cms_enqueue_css(
183+
config: 'plugin',
184+
asset: $this->url() . '/assets/css/admin.css',
185+
minify: false,
186+
pluginSlug: $this->id()
187+
);
188+
}
189+
190+
/**
191+
* @return void
192+
* @throws Exception
193+
* @throws ReflectionException
194+
*/
195+
public function enqueueBackEndJs(): void
196+
{
197+
if (!Utils::isAdmin()) {
198+
return;
199+
}
200+
201+
if (
202+
strpos(
203+
Utils::getPathInfo(
204+
'/admin/plugin/' . $this->id() . '/'
205+
),
206+
'/admin/plugin/' . $this->id() . '/'
207+
) !== 0
208+
) {
209+
return;
210+
}
211+
212+
cms_enqueue_js(
213+
config: 'plugin',
214+
asset: $this->url() . '/assets/js/scripts.js',
215+
minify: false,
216+
pluginSlug: $this->id()
217+
);
218+
}
219+
220+
/**
221+
* @return void
222+
* @throws ReflectionException
223+
*/
224+
public function render(): void
225+
{
226+
Filter::getInstance()->addFilter('plugin_route', function ($router) {
227+
$router->setDefaultNamespace('\\Plugin\\CookieConsent\\Controllers');
228+
$router->map(['GET', 'POST'], '/admin/plugin/cookie-consent/', 'CookieConsentController@index');
229+
}, 5);
230+
}
231+
}

0 commit comments

Comments
 (0)