Notice.php
3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
* SearchWP Notice.
*
* @package SearchWP
* @author Jon Christopher
*/
namespace SearchWP;
/**
* Class Notice is responsible for storing a notice to be shown in the UI.
*
* @since 4.0
*/
class Notice implements \JsonSerializable {
/**
* The message for this Notice.
*
* @since 4.0
* @var string
*/
private $message = '';
/**
* The tooltip for this Notice, shown after the message.
*
* @since 4.0
* @var string
*/
private $tooltip = '';
/**
* The type of this Notice. Accepted values include:
* - ''
* - 'success'
* - 'error'
* - 'warning'
* - 'info'
* - 'notice'
*
* @since 4.0
* @var string
*/
private $type = '';
/**
* The placement of this Notice in the UI. Accepted values include:
* - ''
* - 'details'
*
* @since 4.1
* @var string
*/
public $placement = '';
/**
* The icon of this Notice (i.e. HTML class).
*
* @since 4.0
* @var string
*/
private $icon;
/**
* Whether this notice can be dismissed. Can be FALSE or a callback to be fired when dismissed.
*
* @since 4.0
* @var mixed
*/
private $dismissable = false;
/**
* "More Info" link to find out more information about this Notice.
*
* @since 4.0
* @var array
*/
private $more;
/**
* Option constructor.
*
* @since 4.0
* @param mixed $value The value to store.
* @param string $label The label to use.
*/
function __construct( string $message = '', array $args = [] ) {
$this->message = sanitize_text_field( $message );
$defaults = array(
'type' => $this->type,
'placement' => $this->placement,
'icon' => $this->icon,
'tooltip' => $this->tooltip,
'dismissable' => $this->dismissable,
'more' => $this->more,
);
$args = wp_parse_args( $args, $defaults );
if ( ! empty( $args['type'] ) && in_array( $args['type'], [ 'success', 'error', 'warning', 'info', 'notice' ] ) ) {
$this->type = $args['type'];
}
if ( ! empty( $args['placement'] ) && in_array( $args['placement'], [ 'details' ] ) ) {
$this->placement = $args['placement'];
}
$this->icon = sanitize_text_field( $args['icon'] );
$this->tooltip = sanitize_text_field( $args['tooltip'] );
if ( false !== $args['dismissable'] && is_callable( $args['dismissable'] ) ) {
// FUTURE: Add AJAX endpoint that calls this function when a dismissable Notice is needed.
$this->dismissable = $args['dismissable'];
}
if (
! empty( $args['more'] )
&& is_array( $args['more'] )
&& isset( $args['more']['target'] )
&& isset( $args['more']['text'] )
) {
$this->more = [
'target' => $args['more']['target'],
'text' => $args['more']['text'],
];
}
}
/**
* Getter for message.
*
* @since 4.0
* @return string The message.
*/
public function get_message() {
return $this->message;
}
/**
* Getter for type.
*
* @since 4.0
* @return mixed The type.
*/
public function get_type() {
return $this->type;
}
/**
* Getter for icon.
*
* @since 4.0
* @return string The icon.
*/
public function get_icon() {
return $this->icon;
}
/**
* Getter for tooltip.
*
* @since 4.0
* @return string The tooltip.
*/
public function get_tooltip() {
return $this->tooltip;
}
/**
* Getter for more info array.
*
* @since 4.0
* @return string The more info array.
*/
public function get_more() {
return $this->more;
}
/**
* Provides the model to use when representing this Notice as JSON.
*
* @since 4.0
* @return array
*/
public function jsonSerialize(): array {
return [
'message' => $this->get_message(),
'type' => $this->get_type(),
'placement' => $this->placement,
'icon' => $this->get_icon(),
'tooltip' => $this->get_tooltip(),
'more' => $this->get_more(),
];
}
}