-
Notifications
You must be signed in to change notification settings - Fork 1
/
class-ratchet-plugin-notices.php
349 lines (280 loc) · 8.65 KB
/
class-ratchet-plugin-notices.php
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
<?php
/**
* Plugin Manager Notices
*
* @version 1.0.2
* @author Jason Bobich, Theme Blvd
* @copyright 2009-2017 Theme Blvd
* @link http://mypluginmanager.com
* @link http://themeblvd.com
* @package ratchet
* @subpackage My_Plugin_Manager
* @license GPL-2.0+
*/
if ( ! class_exists( 'ratchet_Plugin_Notices' ) ) {
/**
* Handles important user notices across the
* entire admin about suggested plugins.
*
* @since 1.0.0
*/
class ratchet_Plugin_Notices {
/**
* Any notices that need to be displayed across the
* entire admin.
*
* This is an array of notice slugs like `install`
* and `update`.
*
* @since 1.0.0
* @var array
*/
private $notices = array();
/**
* Arguments needed for object.
*
* @since 1.0.0
* @var array
*/
private $args = array();
/**
* Object containing plugin manager.
*
* @since 1.0.0
* @var My_Plugin_Manager
*/
private $manager;
/**
* Object containing all plugin data.
*
* @since 1.0.0
* @var My_Plugins
*/
private $plugins;
/**
* Class constructor.
*
* @param array $args {
* Arguments required for object.
*
* @type string $admin_url URL to plugin manager.
* @type string $package_url URL to drop-in package.
* @type string $nag_action Text to lead user to manage suggested plugins.
* @type string $nag_dismiss Text to dismiss admin notice.
* @type string $nag_update Text to tell user there are incompatible plugins that need updating.
* @type string $nag_install_single Text to tell user there is a suggested plugins to install.
* @type string $nag_install_multiple Text to tell user there are suggested plugins to install.
* }
* @param My_Plugin_Manager $manager Plugin manager object.
* @param My_Plugins $plugins Plugins object.
*/
public function __construct( $args, $manager, $plugins ) {
$this->args = wp_parse_args( $args, array(
'package_name' => '',
'package_url' => '',
'admin_url' => '',
'nag_action' => '',
'nag_dismiss' => '',
'nag_update' => '',
'nag_install_single' => '',
'nag_install_multiple' => '',
));
$this->manager = $manager;
$this->plugins = $plugins;
// Determine admin-wide notices.
add_action( 'admin_enqueue_scripts', array( $this, 'set_notices' ) );
// Print necessary scripts for admin-wide notices.
add_action( 'admin_enqueue_scripts', array( $this, 'add_assets' ) );
// Display admin-wide notices.
add_action( 'admin_notices', array( $this, 'add_notices' ) );
// Handle notice dismissals through Ajax.
add_action( 'wp_ajax_plugin-manager-dismiss-notice', array( $this, 'dismiss' ) );
}
/**
* Sets admin plugin notices, which are seen
* throughout the entire admin.
*
* @since 1.0.0
*/
public function set_notices() {
// DEBUG:
// delete_user_meta( get_current_user_id(), 'ratchet-dismiss-plugin-notice_update' );
// delete_user_meta( get_current_user_id(), 'ratchet-dismiss-plugin-notice_install' );
$this->notices = array();
/*
* Because our notices are intended to lead the user to
* our admin screen, the notices should show everywhere
* except our admin screen.
*/
if ( $this->manager->is_admin_screen() ) {
return;
}
$plugins = $this->plugins->get();
/*
* If necessary, add notice to install plugins.
*
* If the message hasn't been dismissed, this will tell the
* user how many suggested plugins have the `not-installed`
* status.
*/
$to_install = array();
foreach ( $plugins as $plugin ) {
if ( 'not-installed' === $plugin['status'] ) {
$to_install[] = $plugin['slug'];
}
}
if ( $to_install ) {
/*
* For the install notice, we want to use all suggested
* plugins, and not just the count of plugins still to
* be installed.
*
* This is so if the user dimisses the notice, they won't
* see it again by installing and uninstalling plugins.
* However, they will see it if the theme author adds
* more suggested plugins.
*/
$check = implode( ',', array_keys( $plugins ) ); // All suggested plugins, not just those in $to_install.
if ( get_user_meta( get_current_user_id(), 'ratchet-dismiss-plugin-notice_install', true ) !== $check ) {
$count = count( $to_install );
if ( 0 === $count || $count >= 2 ) {
$plural = 'multiple';
} else {
$plural = 'single';
}
$this->notices['install']['message'] = sprintf(
$this->args[ 'nag_install_' . $plural ],
$this->args['package_name'],
$count
);
$this->notices['install']['value'] = $check;
}
}
/*
* If necessary, add notice to update plugins.
*
* If the message hasn't been dismissed, this will tell the
* user how many plugins have status `incompatible`.
*
* For a plugin to be marked incompatible, it must be active
* and it's suggested version must be higher than its
* installed version.
*/
$to_update = array();
foreach ( $plugins as $plugin ) {
if ( 'incompatible' === $plugin['status'] ) {
$to_update[] = $plugin['slug'];
}
}
if ( $to_update ) {
/*
* For the update notice, we want the notice to appear
* any time the string of incompatible plugins changes.
*/
$check = implode( ',', $to_update );
if ( get_user_meta( get_current_user_id(), 'ratchet-dismiss-plugin-notice_update', true ) !== $check ) {
$this->notices['update']['message'] = sprintf(
$this->args['nag_update'],
$this->args['package_name']
);
$this->notices['update']['value'] = $check;
}
}
}
/**
* Add any assets needed for plugin notices.
*
* @since 1.0.0
*/
public function add_assets() {
if ( ! $this->notices ) {
return;
}
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_enqueue_script(
'ratchet-plugin-notices',
esc_url( $this->args['package_url'] . "/assets/js/plugin-notices$suffix.js" ),
array( 'jquery' )
);
}
/**
* Handles admin plugin notices, which are seen
* throughout the entire admin.
*
* @since 1.0.0
*/
public function add_notices() {
if ( $this->notices ) {
foreach ( $this->notices as $key => $data ) {
$this->display( $key, $data['value'], $data['message'] );
}
}
}
/**
* Display custom admin notice.
*
* @since 1.0.0
*/
public function display( $key, $value, $message ) {
$class = 'plugin-manager-notice notice is-dismissible';
if ( 'update' === $key ) {
$class .= ' notice-warning';
} else {
$class .= ' notice-info';
}
$nonce = wp_create_nonce( 'plugin-manager-dismiss-notice_' . $key );
$action_link = sprintf(
'<a href="%1$s">%2$s</a>',
esc_url( $this->args['admin_url'] ),
esc_html( $this->args['nag_action'] )
);
$dismiss_link = sprintf(
'<a href="%1$s">%2$s</a>',
esc_url( $this->args['admin_url'] ),
esc_html( $this->args['nag_dismiss'] )
);
?>
<div id="ratchet-plugin-<?php echo esc_attr( $key ); ?>-notice" class="<?php echo $class; ?>" data-notice-key="<?php echo esc_attr( $key ); ?>" data-notice-value="<?php echo esc_attr( $value ); ?>" data-security="<?php echo esc_attr( $nonce ); ?>" data-namespace="ratchet" data-dismiss="<?php echo esc_attr( $this->args['nag_dismiss'] ); ?>">
<p><?php echo esc_html( $message ); ?></p>
<p><?php echo $action_link; ?></a></p>
</div>
<?php
}
/**
* Dimiss admin notices via Ajax.
*
* @since 1.0.0
*/
public function dismiss() {
/*
* Check to make sure this action belongs to current
* object.
*
* This may seem a bit weird. - Because of how we generate
* the package files for authors, we're keeping the ajax action
* generically named `plugin-manager-dismiss-notice`.
*
* So, in the event multiple instances of our generated objects
* have hooked to `wp_ajax_plugin-manager-dismiss-notice`,
* we'll double check the namespace to make sure the right
* message gets dismissed.
*/
if ( empty( $_POST['namespace'] ) || 'ratchet' !== $_POST['namespace'] ) {
return;
}
check_ajax_referer( 'plugin-manager-dismiss-notice_' . $_POST['notice_key'] );
if ( ! current_user_can( 'install_plugins' ) ) {
wp_die();
}
if ( ! current_user_can( 'update_plugins' ) ) {
wp_die();
}
update_user_meta(
get_current_user_id(),
'ratchet-dismiss-plugin-notice_' . $_POST['notice_key'],
$_POST['notice_value']
);
wp_die();
}
}
}