class-media-library-organizer-notices.php
7.46 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
/**
* Notices class.
*
* @package Media_Library_Organizer
* @author WP Media Library
*/
/**
* Persists success and error messages
* across Admin Screens, primarily for Bulk actions
* on the Media List View.
*
* @since 1.0.0
*/
class Media_Library_Organizer_Notices {
/**
* Holds the base class object.
*
* @since 1.0.0
*
* @var object
*/
public $base;
/**
* Holds success and error notices to be displayed
*
* @since 1.0.0
*
* @var array
*/
public $notices = array(
'success' => array(),
'error' => array(),
);
/**
* Whether to store notices for displaying on the next page load.
*
* Set using enable_store() and disable_store(),
* useful for the Media Library's Grid view
*
* @since 1.0.0
*
* @var bool
*/
private $store = false;
/**
* The key prefix to use for stored notices
*
* @since 1.0.0
*
* @var string
*/
private $key_prefix = '_mlo_notices';
/**
* Constructor
*
* @since 1.0.0
*
* @param object $base Base Plugin Class.
*/
public function __construct( $base ) {
// Store base class.
$this->base = $base;
add_action( 'admin_notices', array( $this, 'output_notices' ) );
}
/**
* Enable persistence on notices
*
* @since 1.0.5
*/
public function enable_store() {
$this->store = true;
}
/**
* Disable persistence on notices
*
* @since 1.0.5
*/
public function disable_store() {
$this->store = false;
}
/**
* Defines the key prefix to use for setting and getting notices
*
* @since 1.0.5
*
* @param string $key_prefix Key Prefix.
*/
public function set_key_prefix( $key_prefix ) {
$this->key_prefix = $key_prefix;
}
/**
* Returns all Success Notices that need to be displayed.
*
* @since 1.0.5
*
* @return array Notices
*/
public function get_success_notices() {
// Get notices from store, if required.
if ( $this->store ) {
$this->notices = $this->get_notices();
}
// Get success notices.
$success_notices = ( isset( $this->notices['success'] ) ? $this->notices['success'] : array() );
/**
* Filters the success notices to return.
*
* @since 1.0.5
*
* @param array $success_notices Success Notices.
* @param object $this->notices Success and Error Notices.
*/
$success_notices = apply_filters( 'media_library_organizer_notices_get_success_notices', $success_notices, $this->notices );
// Return.
return $success_notices;
}
/**
* Add a single Success Notice
*
* @since 1.0.5
*
* @param string $value Message.
* @return bool Success
*/
public function add_success_notice( $value ) {
// Get notices from store, if required.
if ( $this->store ) {
$this->notices = $this->get_notices();
}
// Add success notice.
if ( isset( $this->notices['success'] ) ) {
// Bail if the notice already exists.
if ( in_array( $value, $this->notices['success'], true ) ) {
return true;
}
$this->notices['success'][] = $value;
} else {
$this->notices['success'] = array( $value );
}
// Remove any duplicates.
$this->notices['success'] = array_values( array_unique( $this->notices['success'] ) );
// Store notices, if required.
if ( $this->store ) {
$this->save_notices( $this->notices );
}
return true;
}
/**
* Returns all Error Notices that need to be displayed.
*
* @since 1.0.5
*
* @return array Notices
*/
public function get_error_notices() {
// Get notices from store, if required.
if ( $this->store ) {
$this->notices = $this->get_notices();
}
// Get error notices.
$error_notices = ( isset( $this->notices['error'] ) ? $this->notices['error'] : array() );
/**
* Filters the error notices to return.
*
* @since 1.0.5
*
* @param array $error_notices Error Notices.
* @param object $this->notices Success and Error Notices.
*/
$error_notices = apply_filters( 'media_library_organizer_notices_get_error_notices', $error_notices, $this->notices );
// Return.
return $error_notices;
}
/**
* Add a single Error Notice.
*
* @since 1.0.5
*
* @param string $value Message.
*/
public function add_error_notice( $value ) {
// Get notices from store, if required.
if ( $this->store ) {
$this->notices = $this->get_notices();
}
// Add success notice.
if ( isset( $this->notices['error'] ) ) {
$this->notices['error'][] = $value;
} else {
$this->notices['error'] = array( $value );
}
// Remove any duplicates.
$this->notices['error'] = array_values( array_unique( $this->notices['error'] ) );
// Store notices, if required.
if ( $this->store ) {
$this->save_notices( $this->notices );
}
}
/**
* Returns all Success and Error notices
*
* @since 1.0.5
*
* @return array Notices
*/
private function get_notices() {
// Get notices.
$notices = get_transient( $this->key_prefix );
/**
* Filters the success and error notices to return.
*
* @since 1.0.5
*
* @param array $notices Success and Error Notices.
*/
$notices = apply_filters( 'media_library_organizer_notices_get_notices', $notices );
// If not an array, setup.
if ( ! is_array( $notices ) ) {
$notices = array(
'success' => array(),
'error' => array(),
);
}
// If some keys aren't set, define them now.
if ( ! isset( $notices['success'] ) ) {
$notices['success'] = array();
}
if ( ! isset( $notices['error'] ) ) {
$notices['error'] = array();
}
// Return.
return $notices;
}
/**
* Saves the given notices array.
*
* @since 1.0.5
*
* @param array $notices Notices.
* @return bool Success
*/
private function save_notices( $notices ) {
/**
* Filters the success and error notices to save.
*
* @since 1.0.5
*
* @param array $notices Success and Error Notices.
*/
$notices = apply_filters( 'media_library_organizer_notices_save', $notices );
// Update settings.
set_transient( $this->key_prefix, $notices, 60 );
return true;
}
/**
* Deletes all notices
*
* @since 1.0.5
*/
public function delete_notices() {
// Delete notices.
delete_transient( $this->key_prefix );
/**
* Runs actions immediately after all notices are deleted / cleared from the transients data.
*
* @since 1.0.7
*/
do_action( 'media_library_organizer_notices_delete_notices' );
return true;
}
/**
* Output any success and error notices.
*
* @since 1.0.5
*/
public function output_notices() {
// If no notices exist in the class, check the storage.
if ( count( $this->notices['success'] ) === 0 &&
count( $this->notices['error'] ) === 0 ) {
$this->notices = $this->get_notices();
}
// Success.
if ( count( $this->notices['success'] ) > 0 ) {
?>
<div class="notice notice-success is-dismissible">
<p>
<?php
foreach ( $this->notices['success'] as $notice ) {
echo esc_html( $notice ) . '<br />';
}
?>
</p>
</div>
<?php
}
// Error.
if ( count( $this->notices['error'] ) > 0 ) {
?>
<div class="notice notice-error is-dismissible">
<p>
<?php
foreach ( $this->notices['error'] as $notice ) {
echo esc_html( $notice ) . '<br />';
}
?>
</p>
</div>
<?php
}
// Clear storage if it's not enabled.
// This prevents notices stored in this page request from being immediately destroyed.
if ( ! $this->store ) {
$this->delete_notices();
}
}
}