-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-ratchet-plugins.php
More file actions
360 lines (290 loc) · 8.99 KB
/
class-ratchet-plugins.php
File metadata and controls
360 lines (290 loc) · 8.99 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
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
350
351
352
353
354
355
356
357
358
359
360
<?php
/**
* Plugin Formatting and Storage
*
* @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_Plugins' ) ) {
/**
* Handles formatting and storing suggested
* plugins.
*
* @since 1.0.0
*/
class ratchet_Plugins {
/**
* All initial, unformatted plugins.
*
* @since 1.0.0
* @var array
*/
private $pre_plugins = array();
/**
* All formatted suggested plugins.
*
* @since 1.0.0
* @var array
*/
private $plugins = array();
/**
* Plugin manager object.
*
* @since 1.0.0
* @var My_Plugin_Manager
*/
private $manager;
/**
* Class constructor.
*
* @param array $pre_plugins Suggested plugins to format.
* @param My_Plugin_Manager $manager Plugin manager object.
*/
public function __construct( $pre_plugins, $manager ) {
$this->pre_plugins = $pre_plugins;
$this->manager = $manager;
/*
* Setup plugins. The formatting for this must get
* hooked later in the WordPress loading process to
* make use of /wp-admin/includes/plugin.php.
*/
add_action( 'current_screen', array( $this, 'set' ) );
}
/**
* Consistently format plugins and add some
* helpful info.
*
* @since 1.0.0
*/
public function set() {
/*
* If we're loading our admin screen, we can refresh
* the WordPress plugin update cache to show the
* latest results.
*/
if ( $this->manager->is_admin_screen() ) {
wp_update_plugins();
}
/*
* Get all currently installed plugins, that exist
* from suggested plugin list, including which have
* updates ready to install.
*/
$installed = $this->get_installed();
/*
* Loop through passed in plugins, looking for matches
* with installed plugins, and store results to $plugins.
*/
foreach ( $this->pre_plugins as $plugin ) {
$info = array();
if ( array_key_exists( $plugin['slug'], $installed ) ) {
$info = $installed[ $plugin['slug'] ];
}
$this->add( $plugin, $info );
}
// Alphabatize plugins by slug.
ksort( $this->plugins );
}
/**
* Check if plugins have been setup and formatted.
*
* @since 1.0.0
*
* @return bool If $plugins has been properly setup.
*/
public function is_set() {
if ( $this->plugins ) {
return true;
}
return false;
}
/**
* Format and store plugin to $this->plugins.
*
* The "status" of a plugin can be one of the following:
*
* 1. `not-installed` It's simply not installed.
* 2. `inactive` Installed, but not activated yet.
* 3. `incompatible` Activated, but installed version is less than suggested version.
* 4. `active` Installed, activated and compatible.
*
* Note: The `incompatible` status will only be applied
* if the current version is less than the suggsted version;
* it will NOT be applied simply because WordPress says
* the plugin has an update available.
*
* The final stored plugin data will be an array formatted,
* as follows.
*
* $plugin {
* @type string $name Name of plugin, like `My Plugin`.
* @type string $slug Slug of plugin, like `my-plugin`.
* @type string $url URL to plugin website, ONLY if not on wordpress.org.
* @type string $version Suggested plugin version, like `2.0+`.
* @type string $current_version Current installed version of plugin.
* @type string $new_version Latest available version of plugin.
* @type string $file Plugin file, like `my-plugin/my-plugin.php`.
* @type string $status Plugin status key (see above).
* @type bool $update Whether WordPress says the plugin has an update available.
* @type array $notice {
* Optional. Notice for plugin, if needed.
*
* @type string $message Message for notice.
* @type string $class CSS class(es) for notice.
* }
* }
*
* @since 1.0.0
*
* @param array $plugin {
* Plugin info from initial object creation.
*
* @type string $name Name of plugin, like `My Plugin`.
* @type string $slug Slug of plugin, like `my-plugin`.
* @type string $url URL to plugin website, ONLY if not on wordpress.org.
* @type string $version Suggested plugin version, like `2.0+`.
* }
* @param array $info {
* Optional. Plugin info from WP, if installed.
*
* @type string $file Location of plugin file.
* @type string $current_version Current installed version.
* @type string $new_version Newest version available.
* @type bool $is_active Whether plugin is active.
* }
*/
public function add( $plugin, $info = null ) {
// Merge initial plugin data with data abstract.
$plugin = array_merge( array(
'name' => null,
'slug' => null,
'url' => null,
'version' => null,
'current_version' => null,
'new_version' => null,
'file' => null,
'status' => 'not-installed',
'update' => false,
'notice' => '',
), $plugin );
/*
* If installed data is null, it means it hasn't been
* attempted to be retrieved. If it has been retrieved
* but was empty (i.e plugin isn't' installed), then
* an empty array should be passed for $info.
*/
if ( null === $info ) {
$installed = $this->get_installed();
if ( array_key_exists( $plugin['slug'], $installed ) ) {
$info = $installed[ $plugin['slug'] ];
}
}
/*
* At this point, if $info is empty the plugin status
* shall remain as 'not-installed' and the following
* setup will be skipped.
*/
if ( $info ) {
// Add plugin file location.
if ( ! empty( $info['file'] ) ) {
$plugin['file'] = $info['file'];
}
// Add currently installed version.
if ( ! empty( $info['current_version'] ) ) {
$plugin['current_version'] = $info['current_version'];
}
// Add latest available version to check against current.
if ( ! empty( $info['new_version'] ) ) {
$plugin['new_version'] = $info['new_version'];
}
// Set plugin status.
if ( ! empty( $info['is_active'] ) ) {
if ( version_compare( $plugin['version'], $plugin['current_version'], '>' ) ) {
$plugin['status'] = 'incompatible';
} else {
$plugin['status'] = 'active';
}
} else {
$plugin['status'] = 'inactive';
}
// Add plugin notice for available update.
if ( version_compare( $plugin['new_version'], $plugin['current_version'], '>' ) ) {
$plugin['update'] = true;
}
}
if ( ! $plugin['url'] ) {
$plugin['url'] = 'https://wordpress.org/plugins/' . $plugin['slug'];
}
// Store final, formatted plugin.
$this->plugins[ $plugin['slug'] ] = $plugin;
}
/**
* Get data for a plugin, or data for all plugins.
*
* @since 1.0.0
*
* @param string $slug Optional. Slug of plugin to retrieve data for. Leave empty for all plugins.
* @return array|bool Data for all plugins, data for single plugin, or `false` if single plugin doesn't exist.
*/
public function get( $slug = '' ) {
if ( ! $slug ) {
return $this->plugins;
}
if ( isset( $this->plugins[ $slug ] ) ) {
return $this->plugins[ $slug ];
}
return false;
}
/**
* Match installed plugins from WordPress against
* our suggested plugins, to return information
* for our suggested plugins, wnich are currently
* installed.
*
* @since 1.0.0
*
* @return array $installed Installed plugin that are suggested.
*/
public function get_installed() {
// Create array of our suggested plugin slugs.
$slugs = array();
foreach ( $this->pre_plugins as $plugin ) {
$slugs[] = $plugin['slug'];
}
// Gather installed plugins from our suggested plugins.
$installed = array();
$plugins = get_plugins();
foreach ( $plugins as $key => $plugin ) {
$data = get_plugin_data( WP_PLUGIN_DIR . '/' . $key );
$slug = dirname( plugin_basename( $key ) );
if ( ! in_array( $slug, $slugs ) ) {
continue;
}
$installed[ $slug ] = array(
'slug' => $slug,
'file' => $key,
'current_version' => $data['Version'],
'new_version' => $data['Version'],
'is_active' => is_plugin_active( $key ),
);
}
// Determine which of those installed plugins have an update.
$plugin_updates = get_site_transient( 'update_plugins' );
if ( isset( $plugin_updates->response ) ) {
foreach ( $plugin_updates->response as $plugin ) {
if ( ! in_array( $plugin->slug, $slugs ) ) {
continue;
}
$installed[ $plugin->slug ]['new_version'] = $plugin->new_version;
}
}
return $installed;
}
}
}