PluginProbe
WindPress – Tailwind CSS integration for WordPress / trunk
WindPress – Tailwind CSS integration for WordPress vtrunk
3.2.88 3.2.87 3.2.86 3.2.85 3.2.84 3.2.83 3.2.82 3.2.81 trunk 3.0.0 3.0.1 3.0.10 3.0.11 3.0.12 3.0.13 3.0.14 3.0.15 3.0.16 3.0.17 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 All 142 releases
windpress / src / Utils / Notice.php

Notice.php in WindPress – Tailwind CSS integration for WordPress trunk, at src/Utils/Notice.php

120 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the WindPress package.
5 *
6 * (c) Joshua Gugun Siagian <suabahasa@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 declare (strict_types=1);
12 namespace WindPress\WindPress\Utils;
13
14 use WIND_PRESS;
15 /**
16 * Manage the plugin's notices for the wp-admin page.
17 *
18 * @author Joshua Gugun Siagian <suabahasa@gmail.com>
19 */
20 class Notice
21 {
22 /**
23 * @var string
24 */
25 public const ERROR = 'error';
26 /**
27 * @var string
28 */
29 public const SUCCESS = 'success';
30 /**
31 * @var string
32 */
33 public const WARNING = 'warning';
34 /**
35 * @var string
36 */
37 public const INFO = 'info';
38 /**
39 * @var string
40 */
41 public const OPTION_NAME = WIND_PRESS::WP_OPTION . '_notices';
42 /**
43 * Get lists of notices.
44 */
45 public static function get_lists(?bool $purge = \true): array
46 {
47 $notices = get_option(self::OPTION_NAME, []);
48 if ($purge) {
49 update_option(self::OPTION_NAME, []);
50 }
51 return $notices;
52 }
53 /**
54 * Callback for the admin_notices action.
55 * Prints the notices in the admin page.
56 */
57 public static function admin_notices(): void
58 {
59 $messages = self::get_lists();
60 if ($messages && is_array($messages)) {
61 foreach ($messages as $message) {
62 echo sprintf('<div class="notice notice-%s is-dismissible %s">%s</div>', esc_attr($message['status']), esc_attr(self::OPTION_NAME), wp_kses_post($message['message']));
63 }
64 }
65 }
66 public static function add(string $status, string $message, ?string $key = null, bool $unique = \false): void
67 {
68 $notices = get_option(self::OPTION_NAME, []);
69 $payload = ['status' => $status, 'message' => $message];
70 if ($unique) {
71 if ($key && isset($notices[$key])) {
72 return;
73 }
74 if (in_array(['status' => $status, 'message' => $message], $notices, \true)) {
75 return;
76 }
77 }
78 if ($key) {
79 $notices[$key] = $payload;
80 } else {
81 $notices[] = $payload;
82 }
83 update_option(self::OPTION_NAME, $notices);
84 }
85 /**
86 * Add bulk notices.
87 *
88 * @param string|array $messages a message or an array of messages to add.
89 */
90 public static function adds(string $status, $messages): void
91 {
92 if (!is_array($messages)) {
93 $messages = [$messages];
94 }
95 foreach ($messages as $message) {
96 if (!is_array($message)) {
97 self::add($status, $message);
98 } else {
99 self::add($status, ...$message);
100 }
101 }
102 }
103 public static function success(string $message, ?string $key = null, bool $unique = \false): void
104 {
105 self::add(self::SUCCESS, ...func_get_args());
106 }
107 public static function warning(string $message, ?string $key = null, bool $unique = \false): void
108 {
109 self::add(self::WARNING, ...func_get_args());
110 }
111 public static function info(string $message, ?string $key = null, bool $unique = \false): void
112 {
113 self::add(self::INFO, ...func_get_args());
114 }
115 public static function error(string $message, ?string $key = null, bool $unique = \false): void
116 {
117 self::add(self::ERROR, ...func_get_args());
118 }
119 }
120