|
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_notices' ), 999 ); |
|
57
|
|
|
add_action( 'give_dismiss_notices', array( $this, 'dismiss_notices' ) ); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Register notice. |
|
62
|
|
|
* |
|
63
|
|
|
* @since 1.8.9 |
|
64
|
|
|
* @access public |
|
65
|
|
|
* |
|
66
|
|
|
* @param $notice_args |
|
67
|
|
|
* |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
|
|
public function register_notice( $notice_args ) { |
|
71
|
|
|
$notice_args = wp_parse_args( |
|
72
|
|
|
$notice_args, |
|
73
|
|
|
array( |
|
74
|
|
|
'id' => '', |
|
75
|
|
|
'description' => '', |
|
76
|
|
|
'auto_dismissible' => false, |
|
77
|
|
|
|
|
78
|
|
|
// Value: error/updated |
|
79
|
|
|
'type' => 'error', |
|
80
|
|
|
|
|
81
|
|
|
// Value: null/user/all |
|
82
|
|
|
'dismissible_type' => null, |
|
83
|
|
|
|
|
84
|
|
|
// Value: shortly/permanent/null/custom |
|
85
|
|
|
'dismiss_interval' => null, |
|
86
|
|
|
|
|
87
|
|
|
// Only set it when custom is defined. |
|
88
|
|
|
'dismiss_interval_time' => null, |
|
89
|
|
|
|
|
90
|
|
|
) |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
// Bailout. |
|
94
|
|
|
if ( empty( $notice_args['id'] ) ) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
self::$notices[ $notice_args['id'] ] = $notice_args; |
|
99
|
|
|
|
|
100
|
|
|
// Auto set show param if not already set. |
|
101
|
|
|
if ( ! isset( self::$notices[ $notice_args['id'] ]['show'] ) ) { |
|
102
|
|
|
self::$notices[ $notice_args['id'] ]['show'] = $this->is_notice_dismissed( $notice_args ) ? false : true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// Auto set time interval for shortly. |
|
106
|
|
|
if ( 'shortly' === self::$notices[ $notice_args['id'] ]['dismiss_interval'] ) { |
|
107
|
|
|
self::$notices[ $notice_args['id'] ]['dismiss_interval_time'] = DAY_IN_SECONDS; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return true; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get give style admin notice. |
|
115
|
|
|
* |
|
116
|
|
|
* @since 1.8 |
|
117
|
|
|
* @access public |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $message |
|
120
|
|
|
* @param string $type |
|
121
|
|
|
* |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
public static function notice_html( $message, $type = 'updated' ) { |
|
125
|
|
|
ob_start(); |
|
126
|
|
|
?> |
|
127
|
|
|
<div class="<?php echo $type; ?> notice"> |
|
128
|
|
|
<p><?php echo $message; ?></p> |
|
129
|
|
|
</div> |
|
130
|
|
|
<?php |
|
131
|
|
|
|
|
132
|
|
|
return ob_get_clean(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Display notice. |
|
137
|
|
|
* |
|
138
|
|
|
* @since 1.8.9 |
|
139
|
|
|
* |
|
140
|
|
|
*/ |
|
141
|
|
|
public function render_notices() { |
|
142
|
|
|
// Bailout. |
|
143
|
|
|
if ( empty( self::$notices ) ) { |
|
144
|
|
|
return; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$output = ''; |
|
148
|
|
|
|
|
149
|
|
|
foreach ( self::$notices as $notice_id => $notice ) { |
|
150
|
|
|
// Check flag set to true to show notice. |
|
151
|
|
|
if ( ! $notice['show'] ) { |
|
152
|
|
|
continue; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
// Check if notice dismissible or not. |
|
156
|
|
|
if ( ! self::$has_auto_dismissible_notice ) { |
|
157
|
|
|
self::$has_auto_dismissible_notice = $notice['auto_dismissible']; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
// Check if notice dismissible or not. |
|
161
|
|
|
if ( ! self::$has_dismiss_interval_notice ) { |
|
162
|
|
|
self::$has_dismiss_interval_notice = $notice['dismiss_interval']; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$css_id = ( false === strpos( $notice['id'], 'give' ) ? "give-{$notice['id']}" : $notice['id'] ); |
|
166
|
|
|
|
|
167
|
|
|
$css_class = $notice['type'] . ' give-notice notice is-dismissible'; |
|
168
|
|
|
$output .= sprintf( |
|
169
|
|
|
'<div id="%1$s" class="%2$s" data-auto-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">' . " \n", |
|
170
|
|
|
$css_id, |
|
171
|
|
|
$css_class, |
|
172
|
|
|
$notice['auto_dismissible'], |
|
173
|
|
|
$notice['dismissible_type'], |
|
174
|
|
|
$notice['dismiss_interval'], |
|
175
|
|
|
$notice['id'], |
|
176
|
|
|
wp_create_nonce( "give_edit_{$notice_id}_notice" ), |
|
177
|
|
|
$notice['dismiss_interval_time'] |
|
178
|
|
|
); |
|
179
|
|
|
$output .= "<p>{$notice['description']}</p>"; |
|
180
|
|
|
$output .= "</div> \n"; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
echo $output; |
|
184
|
|
|
|
|
185
|
|
|
$this->print_js(); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Print notice js. |
|
190
|
|
|
* |
|
191
|
|
|
* @since 1.8.9 |
|
192
|
|
|
* @access private |
|
193
|
|
|
*/ |
|
194
|
|
|
private function print_js() { |
|
195
|
|
|
if ( self::$has_auto_dismissible_notice ) : |
|
196
|
|
|
?> |
|
197
|
|
|
<script> |
|
198
|
|
|
jQuery(document).ready(function () { |
|
199
|
|
|
// auto hide setting message in 5 seconds. |
|
200
|
|
|
window.setTimeout( |
|
201
|
|
|
function () { |
|
202
|
|
|
jQuery('.give-notice[data-auto-dismissible="1"]').slideUp(); |
|
203
|
|
|
}, |
|
204
|
|
|
5000 |
|
205
|
|
|
); |
|
206
|
|
|
}) |
|
207
|
|
|
</script> |
|
208
|
|
|
<?php |
|
209
|
|
|
endif; |
|
210
|
|
|
|
|
211
|
|
|
if ( self::$has_dismiss_interval_notice ) : |
|
212
|
|
|
?> |
|
213
|
|
|
<script> |
|
214
|
|
|
jQuery(document).ready(function () { |
|
215
|
|
|
var $body = jQuery('body'); |
|
216
|
|
|
|
|
217
|
|
|
$body.on('click', '.give_dismiss_notice', function (e) { |
|
218
|
|
|
var $parent = jQuery(this).parents('.give-notice'); |
|
219
|
|
|
$parent.find('button.notice-dismiss').trigger('click'); |
|
220
|
|
|
|
|
221
|
|
|
return false; |
|
222
|
|
|
}); |
|
223
|
|
|
|
|
224
|
|
|
$body.on('click', 'button.notice-dismiss', function (e) { |
|
225
|
|
|
var $parent = jQuery(this).parents('.give-notice'); |
|
226
|
|
|
e.preventDefault(); |
|
227
|
|
|
|
|
228
|
|
|
var data = { |
|
229
|
|
|
'give-action' : 'dismiss_notices', |
|
230
|
|
|
'notice_id' : $parent.data('notice-id'), |
|
231
|
|
|
'dismissible_type' : $parent.data('dismissible-type'), |
|
232
|
|
|
'dismiss_interval' : $parent.data('dismiss-interval'), |
|
233
|
|
|
'dismiss_interval_time': $parent.data('dismiss-interval-time'), |
|
234
|
|
|
'_wpnonce' : $parent.data('security') |
|
235
|
|
|
}; |
|
236
|
|
|
|
|
237
|
|
|
// Bailout. |
|
238
|
|
|
if ( |
|
239
|
|
|
! data.dismiss_interval || |
|
240
|
|
|
! data.dismissible_type |
|
241
|
|
|
) { |
|
242
|
|
|
return false; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
jQuery.post( |
|
246
|
|
|
'<?php echo admin_url(); ?>admin-ajax.php', |
|
247
|
|
|
data, |
|
248
|
|
|
function (response) { |
|
249
|
|
|
|
|
250
|
|
|
}) |
|
251
|
|
|
}) |
|
252
|
|
|
}); |
|
253
|
|
|
</script> |
|
254
|
|
|
<?php |
|
255
|
|
|
endif; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Hide notice. |
|
261
|
|
|
* |
|
262
|
|
|
* @since 1.8.9 |
|
263
|
|
|
* @access public |
|
264
|
|
|
*/ |
|
265
|
|
|
public function dismiss_notices() { |
|
266
|
|
|
$_post = give_clean( $_POST ); |
|
267
|
|
|
$notice_id = esc_attr( $_post['notice_id'] ); |
|
268
|
|
|
|
|
269
|
|
|
// Bailout. |
|
270
|
|
|
if ( |
|
271
|
|
|
empty( $notice_id ) || |
|
272
|
|
|
empty( $_post['dismissible_type'] ) || |
|
273
|
|
|
empty( $_post['dismiss_interval'] ) || |
|
274
|
|
|
! check_ajax_referer( "give_edit_{$notice_id}_notice", '_wpnonce' ) |
|
275
|
|
|
) { |
|
276
|
|
|
wp_send_json_error(); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
$notice_key = Give()->notices->get_notice_key( $notice_id, $_post['dismiss_interval'] ); |
|
280
|
|
|
if ( 'user' === $_post['dismissible_type'] ) { |
|
281
|
|
|
$current_user = wp_get_current_user(); |
|
282
|
|
|
$notice_key = Give()->notices->get_notice_key( $notice_id, $_post['dismiss_interval'], $current_user->ID ); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
$notice_dismiss_time = ! empty( $_post['dismiss_interval_time'] ) ? $_post['dismiss_interval_time'] : null; |
|
286
|
|
|
|
|
287
|
|
|
// Save option to hide notice. |
|
288
|
|
|
Give_Cache::set( $notice_key, true, $notice_dismiss_time, true ); |
|
289
|
|
|
|
|
290
|
|
|
wp_send_json_success(); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Get notice key. |
|
296
|
|
|
* |
|
297
|
|
|
* @since 1.8.9 |
|
298
|
|
|
* @access public |
|
299
|
|
|
* |
|
300
|
|
|
* @param string $notice_id |
|
301
|
|
|
* @param string $dismiss_interval |
|
302
|
|
|
* @param int $user_id |
|
303
|
|
|
* |
|
304
|
|
|
* @return string |
|
305
|
|
|
*/ |
|
306
|
|
|
public function get_notice_key( $notice_id, $dismiss_interval = null, $user_id = 0 ) { |
|
307
|
|
|
$notice_key = "_give_notice_{$notice_id}"; |
|
308
|
|
|
|
|
309
|
|
|
if( ! empty( $dismiss_interval ) ) { |
|
310
|
|
|
$notice_key .= "_{$dismiss_interval}"; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
if ( $user_id ) { |
|
314
|
|
|
$notice_key .= "_{$user_id}"; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
$notice_key = sanitize_key( $notice_key ); |
|
318
|
|
|
|
|
319
|
|
|
return $notice_key; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* Check if notice dismissed or not |
|
325
|
|
|
* |
|
326
|
|
|
* @since 1.8.9 |
|
327
|
|
|
* @access public |
|
328
|
|
|
* |
|
329
|
|
|
* @param array $notice |
|
330
|
|
|
* |
|
331
|
|
|
* @return bool|null |
|
332
|
|
|
*/ |
|
333
|
|
|
public function is_notice_dismissed( $notice ) { |
|
334
|
|
|
$notice_key = $this->get_notice_key( $notice['id'], $notice['dismiss_interval'] ); |
|
335
|
|
|
|
|
336
|
|
|
if ( 'user' === $notice['dismissible_type'] ) { |
|
337
|
|
|
$current_user = wp_get_current_user(); |
|
338
|
|
|
$notice_key = Give()->notices->get_notice_key( $notice['id'], $notice['dismiss_interval'], $current_user->ID ); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
$notice_data = Give_Cache::get( $notice_key, true ); |
|
342
|
|
|
|
|
343
|
|
|
return ! empty( $notice_data ) && ! is_wp_error( $notice_data ); |
|
344
|
|
|
} |
|
345
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.