class-shortcodes-ultimate-notice.php
6.15 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
<?php
/**
* The abstract class for creating admin notices.
*
* @since 5.0.0
*
* @package Shortcodes_Ultimate
* @subpackage Shortcodes_Ultimate/admin
*/
abstract class Shortcodes_Ultimate_Notice {
/**
* The ID of the notice.
*
* @since 5.0.0
* @access protected
* @var string $notice_id The ID of the notice.
*/
protected $notice_id;
/**
* The full path to the notice template file.
*
* @since 5.0.0
* @access protected
* @var string $template_file The full path to the notice template file.
*/
protected $template_file;
/**
* The delay before displaying the notice at the first time (in seconds).
*
* @since 5.0.0
* @access protected
* @var string $first_time_delay The delay before displaying the notice at the first time (in seconds).
*/
protected $first_time_delay;
/**
* The delay for deferring the notice (in seconds).
*
* @since 5.0.0
* @access protected
* @var string $defer_delay The delay for deferring the notice (in seconds).
*/
protected $defer_delay;
/**
* The required user capability to view the notice.
*
* @since 5.0.0
* @access protected
* @var string $capability The required user capability to view the notice.
*/
protected $capability;
/**
* The name of the option to store dismissed notices.
*
* @since 5.0.0
* @access private
* @var string $option_name The name of the option to store dismissed notices.
*/
private $option_name;
/**
* Initialize the class and set its properties.
*
* @since 5.0.0
* @access protected
* @param string $notice_id The ID of the notice.
* @param string $template_file The full path to the notice template file.
*/
public function __construct( $notice_id, $template_file ) {
$this->notice_id = $notice_id;
$this->template_file = $template_file;
$this->first_time_delay = 0;
$this->defer_delay = 3 * DAY_IN_SECONDS;
$this->capability = 'manage_options';
$this->option_name = 'su_option_dismissed_notices';
}
/**
* This method should be implemented by childs.
*
* @since 5.0.0
*/
abstract function display_notice();
/**
* Include template file.
*
* @since 5.0.0
* @access protected
* @param mixed $data The data to pass to the template.
*/
protected function include_template( $data = null ) {
if ( file_exists( $this->template_file ) ) {
include $this->template_file;
}
}
/**
* Set new status for the notice.
*
* @since 5.0.0
* @access protected
* @param string $status New status. Can be 'dismissed' or 'deferred'
*/
public function update_notice_status( $status ) {
$dismissed = $this->get_dismissed_notices();
$id = $this->notice_id;
if ( $status === 'dismissed' ) {
$dismissed[ $id ] = true;
} elseif ( $status === 'deferred' ) {
$dismissed[ $id ] = time() + (int) $this->defer_delay;
} elseif ( is_numeric( $status ) ) {
$dismissed[ $id ] = time() + (int) $status;
}
update_option( $this->option_name, $dismissed );
}
/**
* Dismiss the notice.
*
* @since 5.0.0
*/
public function dismiss_notice() {
if ( ! $this->current_user_can_view() ) {
return;
}
if ( ! isset( $_GET['nonce'], $_GET['id'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_GET['nonce'], 'su_dismiss_notice' ) ) {
return;
}
if ( $_GET['id'] !== $this->notice_id ) {
return;
}
$status = empty( $_GET['defer'] ) ? 'dismissed' : 'deferred';
$this->update_notice_status( $status );
if ( isset( $_GET['redirect_to'] ) ) {
wp_safe_redirect( $_GET['redirect_to'] );
exit;
}
if ( ! wp_get_referer() ) {
return;
}
wp_safe_redirect( wp_get_referer() );
exit;
}
/**
* Retrieve the link to dismiss the notice.
*
* @since 5.0.0
* @access protected
* @param bool $defer Defer the notice instead of dismissing.
* @param string $redirect Custom redirect URL.
* @return string The admin url.
*/
public function get_dismiss_link( $defer = false, $redirect = '' ) {
$link = admin_url(
sprintf(
'admin-post.php?action=%s&nonce=%s&id=%s',
'su_dismiss_notice',
wp_create_nonce( 'su_dismiss_notice' ),
$this->notice_id
)
);
if ( $defer ) {
$link = add_query_arg( 'defer', 1, $link );
}
if ( $redirect ) {
$link = add_query_arg( 'redirect_to', esc_url( $redirect ), $link );
}
return $link;
}
/**
* This conditional tag checks if the notice has been dismissed.
*
* @since 5.0.0
* @access protected
* @return boolean True if the notice has been dismissed, false if not.
*/
protected function is_dismissed() {
$dismissed = $this->get_dismissed_notices();
$id = $this->notice_id;
// No data about the notice (not dismissed/deferred)
if ( ! isset( $dismissed[ $id ] ) ) {
return false;
}
// Notice deferred
if ( is_numeric( $dismissed[ $id ] ) && time() < $dismissed[ $id ] ) {
return true;
}
// Notice dismissed
if ( $dismissed[ $id ] === true ) {
return true;
}
// Default behavior
return false;
}
/**
* Defer the notice at the first time it should be displayed.
*
* @since 5.0.0
*/
public function defer_first_time() {
$dismissed = $this->get_dismissed_notices();
$id = $this->notice_id;
if ( ! isset( $dismissed[ $id ] ) ) {
$this->update_notice_status( $this->first_time_delay );
}
}
/**
* Helper function to retrieve dismissed notices.
*
* @since 5.0.0
* @access protected
* @return mixed Dismissed notices.
*/
protected function get_dismissed_notices() {
return get_option( $this->option_name, array() );
}
/**
* Helper function to retrieve the ID of the current screen.
*
* @since 5.0.0
* @access protected
* @return string The ID of the current screen.
*/
protected function get_current_screen_id() {
$screen = get_current_screen();
return $screen->id;
}
/**
* Check if current user can view the notice.
*
* @since 5.0.0
* @access protected
* @return bool True if current user can view the notice, False otherwise.
*/
protected function current_user_can_view() {
return current_user_can( $this->capability );
}
}