|
| 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