← All changes
|
app/Features/AdminNotices/AdminNoticesController.php
+67
-0
2.0.0
→
2.1.1
View file →
| @@ -1,0 +1,67 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace Metricool\Features\AdminNotices; | |
| 6 | + | |
| 7 | +if (!defined('ABSPATH')) { | |
| 8 | + exit; | |
| 9 | +} | |
| 10 | + | |
| 11 | +use Metricool\Interfaces\FeatureInterface; | |
| 12 | +use Metricool\Support\Helpers\Storages\EnvironmentConfig; | |
| 13 | + | |
| 14 | +class AdminNoticesController implements FeatureInterface | |
| 15 | +{ | |
| 16 | + private EnvironmentConfig $env; | |
| 17 | + private AdminNoticesEndpoints $endpoints; | |
| 18 | + private AdminNoticesRepository $repository; | |
| 19 | + | |
| 20 | + public function __construct(EnvironmentConfig $env, AdminNoticesEndpoints $endpoints, AdminNoticesRepository $repository) | |
| 21 | + { | |
| 22 | + $this->env = $env; | |
| 23 | + $this->endpoints = $endpoints; | |
| 24 | + $this->repository = $repository; | |
| 25 | + } | |
| 26 | + | |
| 27 | + public function register(): void | |
| 28 | + { | |
| 29 | + add_action('admin_init', [$this, 'renderNotices']); | |
| 30 | + | |
| 31 | + $this->endpoints->register(); | |
| 32 | + } | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * Render all admin notices that should be displayed | |
| 36 | + */ | |
| 37 | + public function renderNotices(): void | |
| 38 | + { | |
| 39 | + $notices = $this->repository->getShouldBeDisplayed(); | |
| 40 | + | |
| 41 | + if (count($notices) === 0) { | |
| 42 | + return; | |
| 43 | + } | |
| 44 | + | |
| 45 | + add_action('admin_enqueue_scripts', [$this, 'enqueueScripts']); | |
| 46 | + | |
| 47 | + add_action('admin_notices', static function () use ($notices) { | |
| 48 | + foreach ($notices as $notice) { | |
| 49 | + $notice->render(); | |
| 50 | + } | |
| 51 | + }); | |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * Enqueue the javascript for the admin notices | |
| 56 | + */ | |
| 57 | + public function enqueueScripts(): void | |
| 58 | + { | |
| 59 | + wp_enqueue_script( | |
| 60 | + 'metricool-admin-notice', | |
| 61 | + $this->env->getUrl('plugin.assets_url') . 'js/admin-notice.js', | |
| 62 | + [], | |
| 63 | + $this->env->getString('plugin.version'), | |
| 64 | + true | |
| 65 | + ); | |
| 66 | + } | |
| 67 | +} | |