1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Admin Notices Class. |
4
|
|
|
* |
5
|
|
|
* @package Give |
6
|
|
|
* @subpackage Admin/Notices |
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
8
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
9
|
|
|
* @since 1.8.9 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// Exit if accessed directly. |
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
14
|
|
|
exit; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Give_Notices Class |
19
|
|
|
* |
20
|
|
|
* @since 1.8.9 |
21
|
|
|
*/ |
22
|
|
|
class Give_Notices { |
23
|
|
|
/** |
24
|
|
|
* List of notices |
25
|
|
|
* @var array |
26
|
|
|
* @since 1.8.9 |
27
|
|
|
* @access private |
28
|
|
|
*/ |
29
|
|
|
private static $notices = array(); |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Flag to check if any notice auto dismissible among all notices |
34
|
|
|
* |
35
|
|
|
* @since 1.8.9 |
36
|
|
|
* @access private |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
private static $has_auto_dismissible_notice = false; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Flag to check if any notice has dismiss interval among all notices |
43
|
|
|
* |
44
|
|
|
* @since 1.8.9 |
45
|
|
|
* @access private |
46
|
|
|
* @var bool |
47
|
|
|
*/ |
48
|
|
|
private static $has_dismiss_interval_notice = false; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get things started. |
52
|
|
|
* |
53
|
|
|
* @since 1.8.9 |
54
|
|
|
*/ |
55
|
|
|
public function __construct() { |
56
|
|
|
add_action( 'admin_notices', array( $this, 'render_admin_notices' ), 999 ); |
57
|
|
|
add_action( 'give_dismiss_notices', array( $this, 'dismiss_notices' ) ); |
58
|
|
|
|
59
|
|
|
add_action( 'give_frontend_notices', array( $this, 'render_frontend_notices' ), 999 ); |
60
|
|
|
add_action( 'give_donation_form_before_personal_info', array( $this, 'render_frontend_notices' ) ); |
61
|
|
|
add_action( 'give_ajax_donation_errors', array( $this, 'render_frontend_notices' ) ); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Backward compatibility for deprecated params. |
65
|
|
|
* |
66
|
|
|
* @since 1.8.14 |
67
|
|
|
*/ |
68
|
|
|
add_filter( 'give_register_notice_args', array( $this, 'bc_deprecated_params' ) ); |
69
|
|
|
add_filter( 'give_frontend_errors_args', array( $this, 'bc_deprecated_params' ) ); |
70
|
|
|
add_filter( 'give_frontend_notice_args', array( $this, 'bc_deprecated_params' ) ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Add backward compatibility to deprecated params. |
75
|
|
|
* |
76
|
|
|
* @since 1.8.14 |
77
|
|
|
* @access public |
78
|
|
|
* |
79
|
|
|
* @param array $args Array of notice params |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function bc_deprecated_params( $args ) { |
84
|
|
|
/** |
85
|
|
|
* Param: auto_dismissible |
86
|
|
|
* deprecated in 1.8.14 |
87
|
|
|
* |
88
|
|
|
* Check if auto_dismissible is set and it true then unset and change dismissible parameter value to auto |
89
|
|
|
*/ |
90
|
|
|
if ( isset( $args['auto_dismissible'] ) ) { |
91
|
|
|
if ( ! empty( $args['auto_dismissible'] ) ) { |
92
|
|
|
$args['dismissible'] = 'auto'; |
93
|
|
|
} |
94
|
|
|
// unset auto_dismissible as it has been deprecated. |
95
|
|
|
unset( $args['auto_dismissible'] ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $args; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Register notice. |
103
|
|
|
* |
104
|
|
|
* @since 1.8.9 |
105
|
|
|
* @access public |
106
|
|
|
* |
107
|
|
|
* @param $notice_args |
108
|
|
|
* |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
public function register_notice( $notice_args ) { |
112
|
|
|
// Bailout. |
113
|
|
|
if ( empty( $notice_args['id'] ) || array_key_exists( $notice_args['id'], self::$notices ) ) { |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$notice_args = wp_parse_args( |
118
|
|
|
$notice_args, |
119
|
|
|
array( |
120
|
|
|
'id' => '', |
121
|
|
|
'description' => '', |
122
|
|
|
|
123
|
|
|
/* |
124
|
|
|
* Add custom notice html |
125
|
|
|
* Note: This param has more priority then description, so if you have both param then this one will be use |
126
|
|
|
* for generating notice html. Most of feature of notice attach to core generated html, so if you set |
127
|
|
|
* custom html then please add required classes and data attribute which help to apply feature on notice. |
128
|
|
|
* |
129
|
|
|
* @since 1.8.16 |
130
|
|
|
*/ |
131
|
|
|
'description_html' => '', |
132
|
|
|
|
133
|
|
|
/* |
134
|
|
|
* Add New Parameter and remove the auto_dismissible parameter. |
135
|
|
|
* Value: auto/true/false |
136
|
|
|
* |
137
|
|
|
* @since 1.8.14 |
138
|
|
|
*/ |
139
|
|
|
'dismissible' => true, |
140
|
|
|
|
141
|
|
|
// Value: error/warning/success/info/updated |
142
|
|
|
'type' => 'error', |
143
|
|
|
|
144
|
|
|
// Value: null/user/all |
145
|
|
|
'dismissible_type' => null, |
146
|
|
|
|
147
|
|
|
// Value: shortly/permanent/null/custom |
148
|
|
|
'dismiss_interval' => null, |
149
|
|
|
|
150
|
|
|
// Only set it when custom is defined. |
151
|
|
|
'dismiss_interval_time' => null, |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
154
|
|
|
) |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Filter to modify Notice args before it get add |
159
|
|
|
* |
160
|
|
|
* @since 1.8.14 |
161
|
|
|
*/ |
162
|
|
|
$notice_args = apply_filters( 'give_register_notice_args', $notice_args ); |
163
|
|
|
|
164
|
|
|
// Set extra dismiss links if any. |
165
|
|
|
if ( false !== strpos( $notice_args['description'], 'data-dismiss-interval' ) ) { |
166
|
|
|
|
167
|
|
|
preg_match_all( "/data-([^\"]*)=\"([^\"]*)\"/", $notice_args['description'], $extra_notice_dismiss_link ); |
|
|
|
|
168
|
|
|
|
169
|
|
|
if ( ! empty( $extra_notice_dismiss_link ) ) { |
170
|
|
|
$extra_notice_dismiss_links = array_chunk( current( $extra_notice_dismiss_link ), 3 ); |
171
|
|
|
foreach ( $extra_notice_dismiss_links as $extra_notice_dismiss_link ) { |
172
|
|
|
// Create array og key ==> value by parsing query string created after renaming data attributes. |
173
|
|
|
$data_attribute_query_str = str_replace( array( 'data-', '-', '"' ), array( |
174
|
|
|
'', |
175
|
|
|
'_', |
176
|
|
|
'', |
177
|
|
|
), implode( '&', $extra_notice_dismiss_link ) ); |
178
|
|
|
|
179
|
|
|
$notice_args['extra_links'][] = wp_parse_args( $data_attribute_query_str ); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
185
|
|
|
self::$notices[ $notice_args['id'] ] = $notice_args; |
186
|
|
|
|
187
|
|
|
// Auto set show param if not already set. |
188
|
|
|
if ( ! isset( self::$notices[ $notice_args['id'] ]['show'] ) ) { |
189
|
|
|
self::$notices[ $notice_args['id'] ]['show'] = $this->is_notice_dismissed( $notice_args ) ? false : true; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// Auto set time interval for shortly. |
193
|
|
|
if ( 'shortly' === self::$notices[ $notice_args['id'] ]['dismiss_interval'] ) { |
194
|
|
|
self::$notices[ $notice_args['id'] ]['dismiss_interval_time'] = DAY_IN_SECONDS; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return true; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Display notice. |
202
|
|
|
* |
203
|
|
|
* @since 1.8.9 |
204
|
|
|
* |
205
|
|
|
*/ |
206
|
|
|
public function render_admin_notices() { |
207
|
|
|
// Bailout. |
208
|
|
|
if ( empty( self::$notices ) ) { |
209
|
|
|
return; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$output = ''; |
213
|
|
|
|
214
|
|
|
foreach ( self::$notices as $notice_id => $notice ) { |
215
|
|
|
// Check flag set to true to show notice. |
216
|
|
|
if ( ! $notice['show'] ) { |
217
|
|
|
continue; |
218
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
221
|
|
|
// Render custom html. |
222
|
|
|
if( ! empty( $notice['description_html'] ) ) { |
|
|
|
|
223
|
|
|
$output .= "{$notice['description_html']} \n"; |
224
|
|
|
continue; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
// Check if notice dismissible or not. |
228
|
|
|
if ( ! self::$has_auto_dismissible_notice ) { |
229
|
|
|
self::$has_auto_dismissible_notice = ( 'auto' === $notice['dismissible'] ); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
// Check if notice dismissible or not. |
233
|
|
|
if ( ! self::$has_dismiss_interval_notice ) { |
234
|
|
|
self::$has_dismiss_interval_notice = $notice['dismiss_interval']; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$css_id = ( false === strpos( $notice['id'], 'give' ) ? "give-{$notice['id']}" : $notice['id'] ); |
238
|
|
|
|
239
|
|
|
$css_class = 'give-notice notice ' . ( empty( $notice['dismissible'] ) ? 'non' : 'is' ) . "-dismissible {$notice['type']} notice-{$notice['type']}"; |
240
|
|
|
$output .= sprintf( |
241
|
|
|
'<div id="%1$s" class="%2$s" data-dismissible="%3$s" data-dismissible-type="%4$s" data-dismiss-interval="%5$s" data-notice-id="%6$s" data-security="%7$s" data-dismiss-interval-time="%8$s" style="display: none">' . " \n", |
242
|
|
|
$css_id, |
243
|
|
|
$css_class, |
244
|
|
|
give_clean( $notice['dismissible'] ), |
245
|
|
|
$notice['dismissible_type'], |
246
|
|
|
$notice['dismiss_interval'], |
247
|
|
|
$notice['id'], |
248
|
|
|
empty( $notice['dismissible_type'] ) ? '' : wp_create_nonce( "give_edit_{$notice_id}_notice" ), |
249
|
|
|
$notice['dismiss_interval_time'] |
250
|
|
|
); |
251
|
|
|
|
252
|
|
|
$output .= ( 0 === strpos( $notice['description'], '<div' ) || 0 === strpos( $notice['description'], '<p' ) ? $notice['description'] : "<p>{$notice['description']}</p>" ); |
253
|
|
|
$output .= "</div> \n"; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
echo $output; |
|
|
|
|
257
|
|
|
|
258
|
|
|
$this->print_js(); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Render give frontend notices. |
264
|
|
|
* |
265
|
|
|
* @since 1.8.9 |
266
|
|
|
* @access public |
267
|
|
|
* |
268
|
|
|
* @param int $form_id |
269
|
|
|
*/ |
270
|
|
|
public function render_frontend_notices( $form_id = 0 ) { |
271
|
|
|
$errors = give_get_errors(); |
272
|
|
|
|
273
|
|
|
$request_form_id = isset( $_REQUEST['form-id'] ) ? absint( $_REQUEST['form-id'] ) : 0; |
|
|
|
|
274
|
|
|
|
275
|
|
|
// Sanity checks first: Ensure that gateway returned errors display on the appropriate form. |
276
|
|
|
if ( ! isset( $_POST['give_ajax'] ) && $request_form_id !== $form_id ) { |
|
|
|
|
277
|
|
|
return; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
if ( $errors ) { |
281
|
|
|
self::print_frontend_errors( $errors ); |
|
|
|
|
282
|
|
|
|
283
|
|
|
give_clear_errors(); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Print notice js. |
289
|
|
|
* |
290
|
|
|
* @since 1.8.9 |
291
|
|
|
* @access private |
292
|
|
|
*/ |
293
|
|
|
private function print_js() { |
294
|
|
|
if ( self::$has_auto_dismissible_notice ) : |
295
|
|
|
?> |
296
|
|
|
<script> |
297
|
|
|
jQuery(document).ready(function () { |
298
|
|
|
// auto hide setting message in 5 seconds. |
299
|
|
|
window.setTimeout( |
300
|
|
|
function () { |
301
|
|
|
jQuery('.give-notice[data-dismissible="auto"]').slideUp(); |
302
|
|
|
}, |
303
|
|
|
5000 |
304
|
|
|
); |
305
|
|
|
}) |
306
|
|
|
</script> |
307
|
|
|
<?php |
308
|
|
|
endif; |
309
|
|
|
|
310
|
|
|
if ( self::$has_dismiss_interval_notice ) : |
311
|
|
|
?> |
312
|
|
|
<script> |
313
|
|
|
jQuery(document).ready(function () { |
314
|
|
|
var $body = jQuery('body'); |
315
|
|
|
|
316
|
|
|
$body.on('click', '.give_dismiss_notice', function (e) { |
317
|
|
|
var $parent = jQuery(this).parents('.give-notice'), |
318
|
|
|
custom_notice_data = { |
319
|
|
|
'dismissible_type': jQuery(this).data('dismissible-type'), |
320
|
|
|
'dismiss_interval': jQuery(this).data('dismiss-interval'), |
321
|
|
|
'dismiss_interval_time': jQuery(this).data('dismiss-interval-time') |
322
|
|
|
}; |
323
|
|
|
|
324
|
|
|
$parent.find('button.notice-dismiss').trigger('click', [custom_notice_data]); |
325
|
|
|
return false; |
326
|
|
|
}); |
327
|
|
|
|
328
|
|
|
$body.on('click', 'button.notice-dismiss', function (e, custom_notice_data) { |
329
|
|
|
var $parent = jQuery(this).parents('.give-notice'), |
330
|
|
|
custom_notice_data = custom_notice_data || {}; |
331
|
|
|
|
332
|
|
|
e.preventDefault(); |
333
|
|
|
|
334
|
|
|
var data = { |
335
|
|
|
'give-action': 'dismiss_notices', |
336
|
|
|
'notice_id': $parent.data('notice-id'), |
337
|
|
|
'dismissible_type': $parent.data('dismissible-type'), |
338
|
|
|
'dismiss_interval': $parent.data('dismiss-interval'), |
339
|
|
|
'dismiss_interval_time': $parent.data('dismiss-interval-time'), |
340
|
|
|
'_wpnonce': $parent.data('security') |
341
|
|
|
}; |
342
|
|
|
|
343
|
|
|
if (Object.keys(custom_notice_data).length) { |
344
|
|
|
jQuery.extend(data, custom_notice_data); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
// Bailout. |
348
|
|
|
if ( |
349
|
|
|
!data.dismiss_interval || |
350
|
|
|
!data.dismissible_type |
351
|
|
|
) { |
352
|
|
|
return false; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
jQuery.post( |
356
|
|
|
'<?php echo admin_url(); ?>admin-ajax.php', |
|
|
|
|
357
|
|
|
data, |
358
|
|
|
function (response) { |
359
|
|
|
|
360
|
|
|
}) |
361
|
|
|
}) |
362
|
|
|
}); |
363
|
|
|
</script> |
364
|
|
|
<?php |
365
|
|
|
endif; |
366
|
|
|
?> |
367
|
|
|
<script> |
368
|
|
|
jQuery(document).ready(function($){ |
369
|
|
|
// Fix notice appearance issue. |
370
|
|
|
window.setTimeout( |
371
|
|
|
function(){ |
372
|
|
|
$('.give-notice').slideDown(); |
373
|
|
|
}, |
374
|
|
|
1000 |
375
|
|
|
); |
376
|
|
|
}); |
377
|
|
|
</script> |
378
|
|
|
<?php |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Hide notice. |
384
|
|
|
* |
385
|
|
|
* @since 1.8.9 |
386
|
|
|
* @access public |
387
|
|
|
*/ |
388
|
|
|
public function dismiss_notices() { |
389
|
|
|
$_post = give_clean( $_POST ); |
|
|
|
|
390
|
|
|
$notice_id = esc_attr( $_post['notice_id'] ); |
391
|
|
|
|
392
|
|
|
// Bailout. |
393
|
|
|
if ( |
394
|
|
|
empty( $notice_id ) || |
395
|
|
|
empty( $_post['dismissible_type'] ) || |
396
|
|
|
empty( $_post['dismiss_interval'] ) || |
397
|
|
|
! check_ajax_referer( "give_edit_{$notice_id}_notice", '_wpnonce' ) |
398
|
|
|
) { |
399
|
|
|
wp_send_json_error(); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
$notice_key = Give()->notices->get_notice_key( $notice_id, $_post['dismiss_interval'] ); |
403
|
|
View Code Duplication |
if ( 'user' === $_post['dismissible_type'] ) { |
|
|
|
|
404
|
|
|
$current_user = wp_get_current_user(); |
405
|
|
|
$notice_key = Give()->notices->get_notice_key( $notice_id, $_post['dismiss_interval'], $current_user->ID ); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
$notice_dismiss_time = ! empty( $_post['dismiss_interval_time'] ) ? $_post['dismiss_interval_time'] : null; |
409
|
|
|
|
410
|
|
|
// Save option to hide notice. |
411
|
|
|
Give_Cache::set( $notice_key, true, $notice_dismiss_time, true ); |
412
|
|
|
|
413
|
|
|
wp_send_json_success(); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Get notice key. |
419
|
|
|
* |
420
|
|
|
* @since 1.8.9 |
421
|
|
|
* @access public |
422
|
|
|
* |
423
|
|
|
* @param string $notice_id |
424
|
|
|
* @param string $dismiss_interval |
425
|
|
|
* @param int $user_id |
426
|
|
|
* |
427
|
|
|
* @return string |
428
|
|
|
*/ |
429
|
|
|
public function get_notice_key( $notice_id, $dismiss_interval = null, $user_id = 0 ) { |
430
|
|
|
$notice_key = "_give_notice_{$notice_id}"; |
431
|
|
|
|
432
|
|
|
if ( ! empty( $dismiss_interval ) ) { |
433
|
|
|
$notice_key .= "_{$dismiss_interval}"; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
if ( $user_id ) { |
437
|
|
|
$notice_key .= "_{$user_id}"; |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
$notice_key = sanitize_key( $notice_key ); |
441
|
|
|
|
442
|
|
|
return $notice_key; |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* Get notice dismiss link. |
448
|
|
|
* |
449
|
|
|
* @param $notice_args |
450
|
|
|
* |
451
|
|
|
* @return string |
452
|
|
|
*/ |
453
|
|
|
public function get_dismiss_link( $notice_args ) { |
454
|
|
|
$notice_args = wp_parse_args( |
455
|
|
|
$notice_args, |
456
|
|
|
array( |
457
|
|
|
'title' => __( 'Click here', 'give' ), |
458
|
|
|
'dismissible_type' => '', |
459
|
|
|
'dismiss_interval' => '', |
460
|
|
|
'dismiss_interval_time' => null, |
461
|
|
|
) |
462
|
|
|
); |
463
|
|
|
|
464
|
|
|
return sprintf( |
465
|
|
|
'<a href="#" class="give_dismiss_notice" data-dismissible-type="%1$s" data-dismiss-interval="%2$s" data-dismiss-interval-time="%3$s">%4$s</a>', |
466
|
|
|
$notice_args['dismissible_type'], |
467
|
|
|
$notice_args['dismiss_interval'], |
468
|
|
|
$notice_args['dismiss_interval_time'], |
469
|
|
|
$notice_args['title'] |
470
|
|
|
); |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* Check if notice dismissed or not |
476
|
|
|
* |
477
|
|
|
* @since 1.8.9 |
478
|
|
|
* @access public |
479
|
|
|
* |
480
|
|
|
* @param array $notice |
481
|
|
|
* |
482
|
|
|
* @return bool|null |
483
|
|
|
*/ |
484
|
|
|
public function is_notice_dismissed( $notice ) { |
485
|
|
|
$notice_key = $this->get_notice_key( $notice['id'], $notice['dismiss_interval'] ); |
486
|
|
|
$is_notice_dismissed = false; |
|
|
|
|
487
|
|
|
|
488
|
|
View Code Duplication |
if ( 'user' === $notice['dismissible_type'] ) { |
|
|
|
|
489
|
|
|
$current_user = wp_get_current_user(); |
490
|
|
|
$notice_key = Give()->notices->get_notice_key( $notice['id'], $notice['dismiss_interval'], $current_user->ID ); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
$notice_data = Give_Cache::get( $notice_key, true ); |
494
|
|
|
|
495
|
|
|
// Find notice dismiss link status if notice has extra dismissible links. |
496
|
|
|
if ( ( empty( $notice_data ) || is_wp_error( $notice_data ) ) && ! empty( $notice['extra_links'] ) ) { |
497
|
|
|
|
498
|
|
|
foreach ( $notice['extra_links'] as $extra_link ) { |
499
|
|
|
$new_notice_data = wp_parse_args( $extra_link, $notice ); |
500
|
|
|
unset( $new_notice_data['extra_links'] ); |
501
|
|
|
|
502
|
|
|
if ( $is_notice_dismissed = $this->is_notice_dismissed( $new_notice_data ) ) { |
503
|
|
|
return $is_notice_dismissed; |
504
|
|
|
} |
505
|
|
|
} |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
$is_notice_dismissed = ! empty( $notice_data ) && ! is_wp_error( $notice_data ); |
509
|
|
|
|
510
|
|
|
return $is_notice_dismissed; |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
|
514
|
|
|
/** |
515
|
|
|
* Print frontend errors. |
516
|
|
|
* |
517
|
|
|
* @since 1.8.9 |
518
|
|
|
* @access public |
519
|
|
|
* |
520
|
|
|
* @param array $errors |
521
|
|
|
*/ |
522
|
|
|
public static function print_frontend_errors( $errors ) { |
523
|
|
|
// Bailout. |
524
|
|
|
if ( ! $errors ) { |
|
|
|
|
525
|
|
|
return; |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* Change auto_dismissible to dismissible and set the value to true |
530
|
|
|
* |
531
|
|
|
* @since 1.8.14 |
532
|
|
|
*/ |
533
|
|
|
$default_notice_args = array( |
534
|
|
|
'dismissible' => true, |
535
|
|
|
'dismiss_interval' => 5000, |
536
|
|
|
); |
537
|
|
|
|
538
|
|
|
// Note: we will remove give_errors class in future. |
539
|
|
|
$classes = apply_filters( 'give_error_class', array( 'give_notices', 'give_errors' ) ); |
540
|
|
|
|
541
|
|
|
echo sprintf( '<div class="%s">', implode( ' ', $classes ) ); |
|
|
|
|
542
|
|
|
|
543
|
|
|
// Loop error codes and display errors. |
544
|
|
|
foreach ( $errors as $error_id => $error ) { |
545
|
|
|
// Backward compatibility v<1.8.11 |
546
|
|
|
if ( is_string( $error ) ) { |
547
|
|
|
$error = array( |
548
|
|
|
'message' => $error, |
549
|
|
|
'notice_args' => array(), |
550
|
|
|
); |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
$notice_args = wp_parse_args( $error['notice_args'], $default_notice_args ); |
554
|
|
|
|
555
|
|
|
/** |
556
|
|
|
* Filter to modify Frontend Errors args before errors is display. |
557
|
|
|
* |
558
|
|
|
* @since 1.8.14 |
559
|
|
|
*/ |
560
|
|
|
$notice_args = apply_filters( 'give_frontend_errors_args', $notice_args ); |
561
|
|
|
|
562
|
|
|
echo sprintf( |
|
|
|
|
563
|
|
|
'<div class="give_error give_notice" id="give_error_%1$s" data-dismissible="%2$s" data-dismiss-interval="%3$d"> |
564
|
|
|
<p><strong>%4$s</strong>: %5$s</p> |
565
|
|
|
</div>', |
566
|
|
|
$error_id, |
567
|
|
|
give_clean( $notice_args['dismissible'] ), |
568
|
|
|
absint( $notice_args['dismiss_interval'] ), |
569
|
|
|
esc_html__( 'Error', 'give' ), |
570
|
|
|
$error['message'] |
571
|
|
|
); |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
echo '</div>'; |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
/** |
578
|
|
|
* Print frontend notice. |
579
|
|
|
* Notice: notice type can be success/error/warning |
580
|
|
|
* |
581
|
|
|
* @since 1.8.9 |
582
|
|
|
* @access public |
583
|
|
|
* |
584
|
|
|
* @param string $message |
585
|
|
|
* @param bool $echo |
586
|
|
|
* @param string $notice_type |
587
|
|
|
* @param array $notice_args |
588
|
|
|
* |
589
|
|
|
* @return string |
590
|
|
|
*/ |
591
|
|
|
public static function print_frontend_notice( $message, $echo = true, $notice_type = 'warning', $notice_args = array() ) { |
592
|
|
|
if ( empty( $message ) ) { |
593
|
|
|
return ''; |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
/** |
597
|
|
|
* Change auto_dismissible to dismissible and set the value to true |
598
|
|
|
* |
599
|
|
|
* @since 1.8.14 |
600
|
|
|
*/ |
601
|
|
|
$default_notice_args = array( |
602
|
|
|
'dismissible' => true, |
603
|
|
|
'dismiss_interval' => 5000, |
604
|
|
|
); |
605
|
|
|
|
606
|
|
|
$notice_args = wp_parse_args( $notice_args, $default_notice_args ); |
607
|
|
|
|
608
|
|
|
/** |
609
|
|
|
* Filter to modify Frontend notice args before notices is display. |
610
|
|
|
* |
611
|
|
|
* @since 1.8.14 |
612
|
|
|
*/ |
613
|
|
|
$notice_args = apply_filters( 'give_frontend_notice_args', $notice_args ); |
614
|
|
|
|
615
|
|
|
// Note: we will remove give_errors class in future. |
616
|
|
|
$error = sprintf( |
617
|
|
|
'<div class="give_notices give_errors" id="give_error_%1$s"> |
618
|
|
|
<p class="give_error give_notice give_%1$s" data-dismissible="%2$s" data-dismiss-interval="%3$d"> |
619
|
|
|
%4$s |
620
|
|
|
</p> |
621
|
|
|
</div>', |
622
|
|
|
$notice_type, |
623
|
|
|
give_clean( $notice_args['dismissible'] ), |
624
|
|
|
absint( $notice_args['dismiss_interval'] ), |
625
|
|
|
$message |
626
|
|
|
); |
627
|
|
|
|
628
|
|
|
if ( ! $echo ) { |
629
|
|
|
return $error; |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
echo $error; |
|
|
|
|
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
/** |
636
|
|
|
* Print Inline Notice. |
637
|
|
|
* Note: dismissible feature will note work if notice will add to dom by javascript after document load. |
638
|
|
|
* |
639
|
|
|
* @param array $notice_args An array of notice arguments. |
640
|
|
|
* |
641
|
|
|
* @todo Implement render_admin_notices function within this function in future. |
642
|
|
|
* |
643
|
|
|
* @access public |
644
|
|
|
* @since 1.8.17 |
645
|
|
|
* |
646
|
|
|
* @return string |
647
|
|
|
*/ |
648
|
|
|
public function print_admin_notices( $notice_args = array() ) { |
649
|
|
|
// Bailout. |
650
|
|
|
if ( empty( $notice_args['description'] ) ) { |
651
|
|
|
return ''; |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
$defaults = array( |
655
|
|
|
'id' => '', |
656
|
|
|
'echo' => true, |
657
|
|
|
'notice_type' => 'warning', |
658
|
|
|
'dismissible' => true, |
659
|
|
|
); |
660
|
|
|
$notice_args = wp_parse_args( $notice_args, $defaults ); |
661
|
|
|
|
662
|
|
|
$output = ''; |
663
|
|
|
$css_id = ! empty( $notice_args['id'] ) ? $notice_args['id'] : uniqid( 'give-inline-notice-' ); |
664
|
|
|
$css_class = "notice-{$notice_args['notice_type']} give-notice notice inline"; |
665
|
|
|
$css_class .= ( $notice_args['dismissible'] ) ? ' is-dismissible' : ''; |
666
|
|
|
$output .= sprintf( |
667
|
|
|
'<div id="%1$s" class="%2$s"><p>%3$s</p></div>', |
668
|
|
|
$css_id, |
669
|
|
|
$css_class, |
670
|
|
|
$notice_args['description'] |
671
|
|
|
); |
672
|
|
|
|
673
|
|
|
if ( ! $notice_args['echo'] ) { |
674
|
|
|
return $output; |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
echo $output; |
|
|
|
|
678
|
|
|
} |
679
|
|
|
} |