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.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// Exit if accessed directly. |
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
14
|
|
|
exit; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Give_Notices Class |
19
|
|
|
* |
20
|
|
|
* @since 1.0 |
21
|
|
|
*/ |
22
|
|
|
class Give_Notices { |
23
|
|
|
/** |
24
|
|
|
* List of notices |
25
|
|
|
* @var array |
26
|
|
|
* @since 1.8 |
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.0 |
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 New Parameter and remove the auto_dismissible parameter. |
125
|
|
|
* Value: auto/true/false |
126
|
|
|
* |
127
|
|
|
* @since 1.8.14 |
128
|
|
|
*/ |
129
|
|
|
'dismissible' => true, |
130
|
|
|
|
131
|
|
|
// Value: error/warning/success/info/updated |
132
|
|
|
'type' => 'error', |
133
|
|
|
|
134
|
|
|
// Value: null/user/all |
135
|
|
|
'dismissible_type' => null, |
136
|
|
|
|
137
|
|
|
// Value: shortly/permanent/null/custom |
138
|
|
|
'dismiss_interval' => null, |
139
|
|
|
|
140
|
|
|
// Only set it when custom is defined. |
141
|
|
|
'dismiss_interval_time' => null, |
142
|
|
|
|
143
|
|
|
) |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Filter to modify Notice args before it get add |
148
|
|
|
* |
149
|
|
|
* @since 1.8.14 |
150
|
|
|
*/ |
151
|
|
|
$notice_args = apply_filters( 'give_register_notice_args', $notice_args ); |
152
|
|
|
|
153
|
|
|
// Set extra dismiss links if any. |
154
|
|
|
if ( false !== strpos( $notice_args['description'], 'data-dismiss-interval' ) ) { |
155
|
|
|
|
156
|
|
|
preg_match_all( "/data-([^\"]*)=\"([^\"]*)\"/", $notice_args['description'], $extra_notice_dismiss_link ); |
|
|
|
|
157
|
|
|
|
158
|
|
|
if ( ! empty( $extra_notice_dismiss_link ) ) { |
159
|
|
|
$extra_notice_dismiss_links = array_chunk( current( $extra_notice_dismiss_link ), 3 ); |
160
|
|
|
foreach ( $extra_notice_dismiss_links as $extra_notice_dismiss_link ) { |
161
|
|
|
// Create array og key ==> value by parsing query string created after renaming data attributes. |
162
|
|
|
$data_attribute_query_str = str_replace( array( 'data-', '-', '"' ), array( |
163
|
|
|
'', |
164
|
|
|
'_', |
165
|
|
|
'', |
166
|
|
|
), implode( '&', $extra_notice_dismiss_link ) ); |
167
|
|
|
|
168
|
|
|
$notice_args['extra_links'][] = wp_parse_args( $data_attribute_query_str ); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
174
|
|
|
self::$notices[ $notice_args['id'] ] = $notice_args; |
175
|
|
|
|
176
|
|
|
// Auto set show param if not already set. |
177
|
|
|
if ( ! isset( self::$notices[ $notice_args['id'] ]['show'] ) ) { |
178
|
|
|
self::$notices[ $notice_args['id'] ]['show'] = $this->is_notice_dismissed( $notice_args ) ? false : true; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// Auto set time interval for shortly. |
182
|
|
|
if ( 'shortly' === self::$notices[ $notice_args['id'] ]['dismiss_interval'] ) { |
183
|
|
|
self::$notices[ $notice_args['id'] ]['dismiss_interval_time'] = DAY_IN_SECONDS; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return true; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Display notice. |
191
|
|
|
* |
192
|
|
|
* @since 1.8.9 |
193
|
|
|
* |
194
|
|
|
*/ |
195
|
|
|
public function render_admin_notices() { |
196
|
|
|
// Bailout. |
197
|
|
|
if ( empty( self::$notices ) ) { |
198
|
|
|
return; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$output = ''; |
202
|
|
|
|
203
|
|
|
foreach ( self::$notices as $notice_id => $notice ) { |
204
|
|
|
// Check flag set to true to show notice. |
205
|
|
|
if ( ! $notice['show'] ) { |
206
|
|
|
continue; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
// Check if notice dismissible or not. |
210
|
|
|
if ( ! self::$has_auto_dismissible_notice ) { |
211
|
|
|
self::$has_auto_dismissible_notice = ( 'auto' === $notice['dismissible'] ); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// Check if notice dismissible or not. |
215
|
|
|
if ( ! self::$has_dismiss_interval_notice ) { |
216
|
|
|
self::$has_dismiss_interval_notice = $notice['dismiss_interval']; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$css_id = ( false === strpos( $notice['id'], 'give' ) ? "give-{$notice['id']}" : $notice['id'] ); |
220
|
|
|
|
221
|
|
|
$css_class = 'give-notice notice ' . ( empty( $notice['dismissible'] ) ? 'non' : 'is' ) . "-dismissible {$notice['type']} notice-{$notice['type']}"; |
222
|
|
|
$output .= sprintf( |
223
|
|
|
'<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", |
224
|
|
|
$css_id, |
225
|
|
|
$css_class, |
226
|
|
|
give_clean( $notice['dismissible'] ), |
227
|
|
|
$notice['dismissible_type'], |
228
|
|
|
$notice['dismiss_interval'], |
229
|
|
|
$notice['id'], |
230
|
|
|
empty( $notice['dismissible_type'] ) ? '' : wp_create_nonce( "give_edit_{$notice_id}_notice" ), |
231
|
|
|
$notice['dismiss_interval_time'] |
232
|
|
|
); |
233
|
|
|
|
234
|
|
|
$output .= ( 0 === strpos( $notice['description'], '<div' ) || 0 === strpos( $notice['description'], '<p' ) ? $notice['description'] : "<p>{$notice['description']}</p>" ); |
235
|
|
|
$output .= "</div> \n"; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
echo $output; |
|
|
|
|
239
|
|
|
|
240
|
|
|
$this->print_js(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Render give frontend notices. |
246
|
|
|
* |
247
|
|
|
* @since 1.8.9 |
248
|
|
|
* @access public |
249
|
|
|
* |
250
|
|
|
* @param int $form_id |
251
|
|
|
*/ |
252
|
|
|
public function render_frontend_notices( $form_id = 0 ) { |
253
|
|
|
$errors = give_get_errors(); |
254
|
|
|
|
255
|
|
|
$request_form_id = isset( $_REQUEST['form-id'] ) ? absint( $_REQUEST['form-id'] ) : 0; |
|
|
|
|
256
|
|
|
|
257
|
|
|
// Sanity checks first: Ensure that gateway returned errors display on the appropriate form. |
258
|
|
|
if ( ! isset( $_POST['give_ajax'] ) && $request_form_id !== $form_id ) { |
|
|
|
|
259
|
|
|
return; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
if ( $errors ) { |
263
|
|
|
self::print_frontend_errors( $errors ); |
|
|
|
|
264
|
|
|
|
265
|
|
|
give_clear_errors(); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Print notice js. |
271
|
|
|
* |
272
|
|
|
* @since 1.8.9 |
273
|
|
|
* @access private |
274
|
|
|
*/ |
275
|
|
|
private function print_js() { |
276
|
|
|
if ( self::$has_auto_dismissible_notice ) : |
277
|
|
|
?> |
278
|
|
|
<script> |
279
|
|
|
jQuery(document).ready(function () { |
280
|
|
|
// auto hide setting message in 5 seconds. |
281
|
|
|
window.setTimeout( |
282
|
|
|
function () { |
283
|
|
|
jQuery('.give-notice[data-dismissible="auto"]').slideUp(); |
284
|
|
|
}, |
285
|
|
|
5000 |
286
|
|
|
); |
287
|
|
|
}) |
288
|
|
|
</script> |
289
|
|
|
<?php |
290
|
|
|
endif; |
291
|
|
|
|
292
|
|
|
if ( self::$has_dismiss_interval_notice ) : |
293
|
|
|
?> |
294
|
|
|
<script> |
295
|
|
|
jQuery(document).ready(function () { |
296
|
|
|
var $body = jQuery('body'); |
297
|
|
|
|
298
|
|
|
$body.on('click', '.give_dismiss_notice', function (e) { |
299
|
|
|
var $parent = jQuery(this).parents('.give-notice'), |
300
|
|
|
custom_notice_data = { |
301
|
|
|
'dismissible_type': jQuery(this).data('dismissible-type'), |
302
|
|
|
'dismiss_interval': jQuery(this).data('dismiss-interval'), |
303
|
|
|
'dismiss_interval_time': jQuery(this).data('dismiss-interval-time') |
304
|
|
|
}; |
305
|
|
|
|
306
|
|
|
$parent.find('button.notice-dismiss').trigger('click', [custom_notice_data]); |
307
|
|
|
return false; |
308
|
|
|
}); |
309
|
|
|
|
310
|
|
|
$body.on('click', 'button.notice-dismiss', function (e, custom_notice_data) { |
311
|
|
|
var $parent = jQuery(this).parents('.give-notice'), |
312
|
|
|
custom_notice_data = custom_notice_data || {}; |
313
|
|
|
|
314
|
|
|
e.preventDefault(); |
315
|
|
|
|
316
|
|
|
var data = { |
317
|
|
|
'give-action': 'dismiss_notices', |
318
|
|
|
'notice_id': $parent.data('notice-id'), |
319
|
|
|
'dismissible_type': $parent.data('dismissible-type'), |
320
|
|
|
'dismiss_interval': $parent.data('dismiss-interval'), |
321
|
|
|
'dismiss_interval_time': $parent.data('dismiss-interval-time'), |
322
|
|
|
'_wpnonce': $parent.data('security') |
323
|
|
|
}; |
324
|
|
|
|
325
|
|
|
if (Object.keys(custom_notice_data).length) { |
326
|
|
|
jQuery.extend(data, custom_notice_data); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
// Bailout. |
330
|
|
|
if ( |
331
|
|
|
!data.dismiss_interval || |
332
|
|
|
!data.dismissible_type |
333
|
|
|
) { |
334
|
|
|
return false; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
jQuery.post( |
338
|
|
|
'<?php echo admin_url(); ?>admin-ajax.php', |
|
|
|
|
339
|
|
|
data, |
340
|
|
|
function (response) { |
341
|
|
|
|
342
|
|
|
}) |
343
|
|
|
}) |
344
|
|
|
}); |
345
|
|
|
</script> |
346
|
|
|
<?php |
347
|
|
|
endif; |
348
|
|
|
?> |
349
|
|
|
<script> |
350
|
|
|
jQuery(document).ready(function($){ |
351
|
|
|
// Fix notice appearance issue. |
352
|
|
|
window.setTimeout( |
353
|
|
|
function(){ |
354
|
|
|
$('.give-notice').slideDown(); |
355
|
|
|
}, |
356
|
|
|
1000 |
357
|
|
|
); |
358
|
|
|
}); |
359
|
|
|
</script> |
360
|
|
|
<?php |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Hide notice. |
366
|
|
|
* |
367
|
|
|
* @since 1.8.9 |
368
|
|
|
* @access public |
369
|
|
|
*/ |
370
|
|
|
public function dismiss_notices() { |
371
|
|
|
$_post = give_clean( $_POST ); |
|
|
|
|
372
|
|
|
$notice_id = esc_attr( $_post['notice_id'] ); |
373
|
|
|
|
374
|
|
|
// Bailout. |
375
|
|
|
if ( |
376
|
|
|
empty( $notice_id ) || |
377
|
|
|
empty( $_post['dismissible_type'] ) || |
378
|
|
|
empty( $_post['dismiss_interval'] ) || |
379
|
|
|
! check_ajax_referer( "give_edit_{$notice_id}_notice", '_wpnonce' ) |
380
|
|
|
) { |
381
|
|
|
wp_send_json_error(); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
$notice_key = Give()->notices->get_notice_key( $notice_id, $_post['dismiss_interval'] ); |
385
|
|
View Code Duplication |
if ( 'user' === $_post['dismissible_type'] ) { |
|
|
|
|
386
|
|
|
$current_user = wp_get_current_user(); |
387
|
|
|
$notice_key = Give()->notices->get_notice_key( $notice_id, $_post['dismiss_interval'], $current_user->ID ); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
$notice_dismiss_time = ! empty( $_post['dismiss_interval_time'] ) ? $_post['dismiss_interval_time'] : null; |
391
|
|
|
|
392
|
|
|
// Save option to hide notice. |
393
|
|
|
Give_Cache::set( $notice_key, true, $notice_dismiss_time, true ); |
394
|
|
|
|
395
|
|
|
wp_send_json_success(); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Get notice key. |
401
|
|
|
* |
402
|
|
|
* @since 1.8.9 |
403
|
|
|
* @access public |
404
|
|
|
* |
405
|
|
|
* @param string $notice_id |
406
|
|
|
* @param string $dismiss_interval |
407
|
|
|
* @param int $user_id |
408
|
|
|
* |
409
|
|
|
* @return string |
410
|
|
|
*/ |
411
|
|
|
public function get_notice_key( $notice_id, $dismiss_interval = null, $user_id = 0 ) { |
412
|
|
|
$notice_key = "_give_notice_{$notice_id}"; |
413
|
|
|
|
414
|
|
|
if ( ! empty( $dismiss_interval ) ) { |
415
|
|
|
$notice_key .= "_{$dismiss_interval}"; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
if ( $user_id ) { |
419
|
|
|
$notice_key .= "_{$user_id}"; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
$notice_key = sanitize_key( $notice_key ); |
423
|
|
|
|
424
|
|
|
return $notice_key; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Get notice dismiss link. |
430
|
|
|
* |
431
|
|
|
* @param $notice_args |
432
|
|
|
* |
433
|
|
|
* @return string |
434
|
|
|
*/ |
435
|
|
|
public function get_dismiss_link( $notice_args ) { |
436
|
|
|
$notice_args = wp_parse_args( |
437
|
|
|
$notice_args, |
438
|
|
|
array( |
439
|
|
|
'title' => __( 'Click here', 'give' ), |
440
|
|
|
'dismissible_type' => '', |
441
|
|
|
'dismiss_interval' => '', |
442
|
|
|
'dismiss_interval_time' => null, |
443
|
|
|
) |
444
|
|
|
); |
445
|
|
|
|
446
|
|
|
return sprintf( |
447
|
|
|
'<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>', |
448
|
|
|
$notice_args['dismissible_type'], |
449
|
|
|
$notice_args['dismiss_interval'], |
450
|
|
|
$notice_args['dismiss_interval_time'], |
451
|
|
|
$notice_args['title'] |
452
|
|
|
); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* Check if notice dismissed or not |
458
|
|
|
* |
459
|
|
|
* @since 1.8.9 |
460
|
|
|
* @access public |
461
|
|
|
* |
462
|
|
|
* @param array $notice |
463
|
|
|
* |
464
|
|
|
* @return bool|null |
465
|
|
|
*/ |
466
|
|
|
public function is_notice_dismissed( $notice ) { |
467
|
|
|
$notice_key = $this->get_notice_key( $notice['id'], $notice['dismiss_interval'] ); |
468
|
|
|
$is_notice_dismissed = false; |
|
|
|
|
469
|
|
|
|
470
|
|
View Code Duplication |
if ( 'user' === $notice['dismissible_type'] ) { |
|
|
|
|
471
|
|
|
$current_user = wp_get_current_user(); |
472
|
|
|
$notice_key = Give()->notices->get_notice_key( $notice['id'], $notice['dismiss_interval'], $current_user->ID ); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
$notice_data = Give_Cache::get( $notice_key, true ); |
476
|
|
|
|
477
|
|
|
// Find notice dismiss link status if notice has extra dismissible links. |
478
|
|
|
if ( ( empty( $notice_data ) || is_wp_error( $notice_data ) ) && ! empty( $notice['extra_links'] ) ) { |
479
|
|
|
|
480
|
|
|
foreach ( $notice['extra_links'] as $extra_link ) { |
481
|
|
|
$new_notice_data = wp_parse_args( $extra_link, $notice ); |
482
|
|
|
unset( $new_notice_data['extra_links'] ); |
483
|
|
|
|
484
|
|
|
if ( $is_notice_dismissed = $this->is_notice_dismissed( $new_notice_data ) ) { |
485
|
|
|
return $is_notice_dismissed; |
486
|
|
|
} |
487
|
|
|
} |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
$is_notice_dismissed = ! empty( $notice_data ) && ! is_wp_error( $notice_data ); |
491
|
|
|
|
492
|
|
|
return $is_notice_dismissed; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* Print frontend errors. |
498
|
|
|
* |
499
|
|
|
* @since 1.8.9 |
500
|
|
|
* @access public |
501
|
|
|
* |
502
|
|
|
* @param array $errors |
503
|
|
|
*/ |
504
|
|
|
public static function print_frontend_errors( $errors ) { |
505
|
|
|
// Bailout. |
506
|
|
|
if ( ! $errors ) { |
|
|
|
|
507
|
|
|
return; |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* Change auto_dismissible to dismissible and set the value to true |
512
|
|
|
* |
513
|
|
|
* @since 1.8.14 |
514
|
|
|
*/ |
515
|
|
|
$default_notice_args = array( |
516
|
|
|
'dismissible' => true, |
517
|
|
|
'dismiss_interval' => 5000, |
518
|
|
|
); |
519
|
|
|
|
520
|
|
|
// Note: we will remove give_errors class in future. |
521
|
|
|
$classes = apply_filters( 'give_error_class', array( 'give_notices', 'give_errors' ) ); |
522
|
|
|
|
523
|
|
|
echo sprintf( '<div class="%s">', implode( ' ', $classes ) ); |
|
|
|
|
524
|
|
|
|
525
|
|
|
// Loop error codes and display errors. |
526
|
|
|
foreach ( $errors as $error_id => $error ) { |
527
|
|
|
// Backward compatibility v<1.8.11 |
528
|
|
|
if ( is_string( $error ) ) { |
529
|
|
|
$error = array( |
530
|
|
|
'message' => $error, |
531
|
|
|
'notice_args' => array(), |
532
|
|
|
); |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
$notice_args = wp_parse_args( $error['notice_args'], $default_notice_args ); |
536
|
|
|
|
537
|
|
|
/** |
538
|
|
|
* Filter to modify Frontend Errors args before errors is display. |
539
|
|
|
* |
540
|
|
|
* @since 1.8.14 |
541
|
|
|
*/ |
542
|
|
|
$notice_args = apply_filters( 'give_frontend_errors_args', $notice_args ); |
543
|
|
|
|
544
|
|
|
echo sprintf( |
|
|
|
|
545
|
|
|
'<div class="give_error give_notice" id="give_error_%1$s" data-dismissible="%2$s" data-dismiss-interval="%3$d"> |
546
|
|
|
<p><strong>%4$s</strong>: %5$s</p> |
547
|
|
|
</div>', |
548
|
|
|
$error_id, |
549
|
|
|
give_clean( $notice_args['dismissible'] ), |
550
|
|
|
absint( $notice_args['dismiss_interval'] ), |
551
|
|
|
esc_html__( 'Error', 'give' ), |
552
|
|
|
$error['message'] |
553
|
|
|
); |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
echo '</div>'; |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
/** |
560
|
|
|
* Print frontend notice. |
561
|
|
|
* Notice: notice type can be success/error/warning |
562
|
|
|
* |
563
|
|
|
* @since 1.8.9 |
564
|
|
|
* @access public |
565
|
|
|
* |
566
|
|
|
* @param string $message |
567
|
|
|
* @param bool $echo |
568
|
|
|
* @param string $notice_type |
569
|
|
|
* @param array $notice_args |
570
|
|
|
* |
571
|
|
|
* @return string |
572
|
|
|
*/ |
573
|
|
|
public static function print_frontend_notice( $message, $echo = true, $notice_type = 'warning', $notice_args = array() ) { |
574
|
|
|
if ( empty( $message ) ) { |
575
|
|
|
return ''; |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* Change auto_dismissible to dismissible and set the value to true |
580
|
|
|
* |
581
|
|
|
* @since 1.8.14 |
582
|
|
|
*/ |
583
|
|
|
$default_notice_args = array( |
584
|
|
|
'dismissible' => true, |
585
|
|
|
'dismiss_interval' => 5000, |
586
|
|
|
); |
587
|
|
|
|
588
|
|
|
$notice_args = wp_parse_args( $notice_args, $default_notice_args ); |
589
|
|
|
|
590
|
|
|
/** |
591
|
|
|
* Filter to modify Frontend notice args before notices is display. |
592
|
|
|
* |
593
|
|
|
* @since 1.8.14 |
594
|
|
|
*/ |
595
|
|
|
$notice_args = apply_filters( 'give_frontend_notice_args', $notice_args ); |
596
|
|
|
|
597
|
|
|
// Note: we will remove give_errors class in future. |
598
|
|
|
$error = sprintf( |
599
|
|
|
'<div class="give_notices give_errors" id="give_error_%1$s"> |
600
|
|
|
<p class="give_error give_notice give_%1$s" data-dismissible="%2$s" data-dismiss-interval="%3$d"> |
601
|
|
|
%4$s |
602
|
|
|
</p> |
603
|
|
|
</div>', |
604
|
|
|
$notice_type, |
605
|
|
|
give_clean( $notice_args['dismissible'] ), |
606
|
|
|
absint( $notice_args['dismiss_interval'] ), |
607
|
|
|
$message |
608
|
|
|
); |
609
|
|
|
|
610
|
|
|
if ( ! $echo ) { |
611
|
|
|
return $error; |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
echo $error; |
|
|
|
|
615
|
|
|
} |
616
|
|
|
} |
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.