PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / trunk
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution vtrunk
2.0.1 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.9.0 2.0.0
solid-performance / src / Performance / Notices / Notice.php

Notice.php in Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution trunk, at src/Performance/Notices/Notice.php

182 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A notice to display messages to users in the admin.
4 *
5 * @since 0.1.0
6 *
7 * @package SolidWP\Performance
8 */
9
10 declare( strict_types=1 );
11
12 namespace SolidWP\Performance\Notices;
13
14 use InvalidArgumentException;
15
16 /**
17 * A Notice to display in the admin.
18 *
19 * @since 0.1.0
20 *
21 * @package SolidWP\Performance
22 */
23 class Notice {
24
25 public const INFO = 'info';
26 public const SUCCESS = 'success';
27 public const WARNING = 'warning';
28 public const ERROR = 'error';
29
30 public const ALLOWED_TYPES = [
31 self::INFO,
32 self::SUCCESS,
33 self::WARNING,
34 self::ERROR,
35 ];
36
37 /**
38 * The notice type, one of the above constants.
39 *
40 * @since 0.1.0
41 *
42 * @var string
43 */
44 private string $type;
45
46 /**
47 * The already translated message to display.
48 *
49 * @since 0.1.0
50 *
51 * @see __()
52 *
53 * @var string
54 */
55 private string $message;
56
57 /**
58 * Whether this notice is dismissible.
59 *
60 * @since 0.1.0
61 *
62 * @var bool
63 */
64 private bool $dismissible;
65
66 /**
67 * The ID of the notice.
68 *
69 * @since 0.1.0
70 *
71 * @var string
72 */
73 private string $id;
74
75 /**
76 * Any additional classes that should be added to the notice.
77 *
78 * @since 0.1.0
79 *
80 * @var array<int,string>
81 */
82 private array $additional_classes;
83
84 /**
85 * Attributes (key value pairs) that should be added to the notice div.
86 *
87 * @since 0.1.0
88 *
89 * @var array<string,string>
90 */
91 private array $attributes;
92
93 /**
94 * Whether to wrap the message in paragraph tags.
95 *
96 * @since 0.1.0
97 *
98 * @var bool
99 */
100 private bool $paragraph_wrap;
101
102 /**
103 * Whether this is an alt-notice.
104 *
105 * @since 0.1.0
106 *
107 * @var bool
108 */
109 private bool $alt;
110
111 /**
112 * Whether this should be a large notice.
113 *
114 * @since 0.1.0
115 *
116 * @var bool
117 */
118 private bool $large;
119
120 /**
121 * The class constructor.
122 *
123 * @param string $type The notice type, one of the above constants.
124 * @param string $message The already translated message to display.
125 * @param bool $dismissible Whether this notice is dismissible.
126 * @param string $id The ID of the admin notice.
127 * @param array $additional_classes Any additional classes that should be added to the notice.
128 * @param array $attributes An associative array of attributes and values added to the notice div.
129 * @param bool $paragraph_wrap If the message should be wrapped in paragraph tags.
130 * @param bool $alt Whether this is an alt-notice.
131 * @param bool $large Whether this should be a large notice.
132 *
133 * @throws InvalidArgumentException When an invalid type or message is used.
134 */
135 public function __construct(
136 string $type,
137 string $message,
138 bool $dismissible = false,
139 string $id = '',
140 array $additional_classes = [],
141 array $attributes = [],
142 bool $paragraph_wrap = true,
143 bool $alt = false,
144 bool $large = false
145 ) {
146 if ( ! in_array( $type, self::ALLOWED_TYPES, true ) ) {
147 throw new InvalidArgumentException(
148 sprintf(
149 // Translators: a list of allowed values: info, success, warning, error.
150 __( 'Notice $type must be one of: %s', 'solid-performance' ),
151 implode( ', ', self::ALLOWED_TYPES )
152 )
153 );
154 }
155
156 if ( empty( $message ) ) {
157 throw new InvalidArgumentException( __( 'The $message cannot be empty', 'solid-performance' ) );
158 }
159
160 $this->type = $type;
161 $this->message = $message;
162 $this->dismissible = $dismissible;
163 $this->id = $id;
164 $this->additional_classes = $additional_classes;
165 $this->attributes = $attributes;
166 $this->paragraph_wrap = $paragraph_wrap;
167 $this->alt = $alt;
168 $this->large = $large;
169 }
170
171 /**
172 * Returns an array of the Notice's properties.
173 *
174 * @since 0.1.0
175 *
176 * @return array{type: string, message: string, dismissible: bool, alt: bool, large: bool}
177 */
178 public function to_array(): array {
179 return get_object_vars( $this );
180 }
181 }
182