1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Base class for working with plugins. |
5
|
|
|
*/ |
6
|
|
|
abstract class Jetpack_JSON_API_Plugins_Endpoint extends Jetpack_JSON_API_Endpoint { |
7
|
|
|
|
8
|
|
|
protected $plugins = array(); |
9
|
|
|
|
10
|
|
|
protected $network_wide = false; |
11
|
|
|
|
12
|
|
|
protected $bulk = true; |
13
|
|
|
protected $log; |
14
|
|
|
|
15
|
|
|
static $_response_format = array( |
|
|
|
|
16
|
|
|
'id' => '(safehtml) The plugin\'s ID', |
17
|
|
|
'slug' => '(safehtml) The plugin\'s .org slug', |
18
|
|
|
'active' => '(boolean) The plugin status.', |
19
|
|
|
'update' => '(object) The plugin update info.', |
20
|
|
|
'name' => '(safehtml) The name of the plugin.', |
21
|
|
|
'plugin_url' => '(url) Link to the plugin\'s web site.', |
22
|
|
|
'version' => '(safehtml) The plugin version number.', |
23
|
|
|
'description' => '(safehtml) Description of what the plugin does and/or notes from the author', |
24
|
|
|
'author' => '(safehtml) The author\'s name', |
25
|
|
|
'author_url' => '(url) The authors web site address', |
26
|
|
|
'network' => '(boolean) Whether the plugin can only be activated network wide.', |
27
|
|
|
'autoupdate' => '(boolean) Whether the plugin is automatically updated', |
28
|
|
|
'autoupdate_translation' => '(boolean) Whether the plugin is automatically updating translations', |
29
|
|
|
'next_autoupdate' => '(string) Y-m-d H:i:s for next scheduled update event', |
30
|
|
|
'log' => '(array:safehtml) An array of update log strings.', |
31
|
|
|
'uninstallable' => '(boolean) Whether the plugin is unistallable.', |
32
|
|
|
'action_links' => '(array) An array of action links that the plugin uses.', |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
static $_response_format_v1_2 = array( |
|
|
|
|
36
|
|
|
'slug' => '(safehtml) The plugin\'s .org slug', |
37
|
|
|
'active' => '(boolean) The plugin status.', |
38
|
|
|
'update' => '(object) The plugin update info.', |
39
|
|
|
'name' => '(safehtml) The plugin\'s ID', |
40
|
|
|
'display_name' => '(safehtml) The name of the plugin.', |
41
|
|
|
'version' => '(safehtml) The plugin version number.', |
42
|
|
|
'description' => '(safehtml) Description of what the plugin does and/or notes from the author', |
43
|
|
|
'author' => '(safehtml) The author\'s name', |
44
|
|
|
'author_url' => '(url) The authors web site address', |
45
|
|
|
'plugin_url' => '(url) Link to the plugin\'s web site.', |
46
|
|
|
'network' => '(boolean) Whether the plugin can only be activated network wide.', |
47
|
|
|
'autoupdate' => '(boolean) Whether the plugin is automatically updated', |
48
|
|
|
'autoupdate_translation' => '(boolean) Whether the plugin is automatically updating translations', |
49
|
|
|
'uninstallable' => '(boolean) Whether the plugin is unistallable.', |
50
|
|
|
'action_links' => '(array) An array of action links that the plugin uses.', |
51
|
|
|
'log' => '(array:safehtml) An array of update log strings.', |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
protected function result() { |
55
|
|
|
|
56
|
|
|
$plugins = $this->get_plugins(); |
57
|
|
|
|
58
|
|
|
if ( ! $this->bulk && ! empty( $plugins ) ) { |
59
|
|
|
return array_pop( $plugins ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return array( 'plugins' => $plugins ); |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function validate_input( $plugin ) { |
67
|
|
|
|
68
|
|
|
if ( is_wp_error( $error = parent::validate_input( $plugin ) ) ) { |
69
|
|
|
return $error; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ( is_wp_error( $error = $this->validate_network_wide() ) ) { |
73
|
|
|
return $error; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$args = $this->input(); |
77
|
|
|
// find out what plugin, or plugins we are dealing with |
78
|
|
|
// validate the requested plugins |
79
|
|
|
if ( ! isset( $plugin ) || empty( $plugin ) ) { |
80
|
|
|
if ( ! $args['plugins'] || empty( $args['plugins'] ) ) { |
81
|
|
|
return new WP_Error( 'missing_plugin', __( 'You are required to specify a plugin.', 'jetpack' ), 400 ); |
82
|
|
|
} |
83
|
|
|
if ( is_array( $args['plugins'] ) ) { |
84
|
|
|
$this->plugins = $args['plugins']; |
85
|
|
|
} else { |
86
|
|
|
$this->plugins[] = $args['plugins']; |
87
|
|
|
} |
88
|
|
|
} else { |
89
|
|
|
$this->bulk = false; |
90
|
|
|
$this->plugins[] = urldecode( $plugin ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ( is_wp_error( $error = $this->validate_plugins() ) ) { |
94
|
|
|
return $error; |
95
|
|
|
}; |
96
|
|
|
|
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Walks through submitted plugins to make sure they are valid |
102
|
|
|
* @return bool|WP_Error |
103
|
|
|
*/ |
104
|
|
|
protected function validate_plugins() { |
105
|
|
View Code Duplication |
if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) { |
106
|
|
|
return new WP_Error( 'missing_plugins', __( 'No plugins found.', 'jetpack' )); |
107
|
|
|
} |
108
|
|
|
foreach( $this->plugins as $index => $plugin ) { |
109
|
|
|
if ( ! preg_match( "/\.php$/", $plugin ) ) { |
110
|
|
|
$plugin = $plugin . '.php'; |
111
|
|
|
$this->plugins[ $index ] = $plugin; |
112
|
|
|
} |
113
|
|
|
$valid = $this->validate_plugin( urldecode( $plugin ) ) ; |
114
|
|
|
if ( is_wp_error( $valid ) ) { |
115
|
|
|
return $valid; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected function format_plugin( $plugin_file, $plugin_data ) { |
123
|
|
|
if ( version_compare( $this->min_version, '1.2', '>=' ) ) { |
124
|
|
|
return $this->format_plugin_v1_2( $plugin_file, $plugin_data ); |
125
|
|
|
} |
126
|
|
|
$plugin = array(); |
127
|
|
|
$plugin['id'] = preg_replace("/(.+)\.php$/", "$1", $plugin_file ); |
128
|
|
|
$plugin['slug'] = Jetpack_Autoupdate::get_plugin_slug( $plugin_file ); |
129
|
|
|
$plugin['active'] = Jetpack::is_plugin_active( $plugin_file ); |
130
|
|
|
$plugin['name'] = $plugin_data['Name']; |
131
|
|
|
$plugin['plugin_url'] = $plugin_data['PluginURI']; |
132
|
|
|
$plugin['version'] = $plugin_data['Version']; |
133
|
|
|
$plugin['description'] = $plugin_data['Description']; |
134
|
|
|
$plugin['author'] = $plugin_data['Author']; |
135
|
|
|
$plugin['author_url'] = $plugin_data['AuthorURI']; |
136
|
|
|
$plugin['network'] = $plugin_data['Network']; |
137
|
|
|
$plugin['update'] = $this->get_plugin_updates( $plugin_file ); |
138
|
|
|
$plugin['next_autoupdate'] = date( 'Y-m-d H:i:s', wp_next_scheduled( 'wp_maybe_auto_update' ) ); |
139
|
|
|
$action_link = $this->get_plugin_action_links( $plugin_file ); |
140
|
|
|
if ( ! empty( $action_link ) ) { |
141
|
|
|
$plugin['action_links'] = $action_link; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$autoupdate = in_array( $plugin_file, Jetpack_Options::get_option( 'autoupdate_plugins', array() ) ); |
145
|
|
|
$plugin['autoupdate'] = $autoupdate; |
146
|
|
|
|
147
|
|
|
$autoupdate_translation = in_array( $plugin_file, Jetpack_Options::get_option( 'autoupdate_plugins_translations', array() ) ); |
148
|
|
|
$plugin['autoupdate_translation'] = $autoupdate || $autoupdate_translation || Jetpack_Options::get_option( 'autoupdate_translations', false ); |
149
|
|
|
|
150
|
|
|
$plugin['uninstallable'] = is_uninstallable_plugin( $plugin_file ); |
|
|
|
|
151
|
|
|
|
152
|
|
|
if ( ! empty ( $this->log[ $plugin_file ] ) ) { |
153
|
|
|
$plugin['log'] = $this->log[ $plugin_file ]; |
154
|
|
|
} |
155
|
|
|
return $plugin; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function format_plugin_v1_2( $plugin_file, $plugin_data ) { |
159
|
|
|
$plugin = array(); |
160
|
|
|
$plugin['slug'] = Jetpack_Autoupdate::get_plugin_slug( $plugin_file ); |
161
|
|
|
$plugin['active'] = Jetpack::is_plugin_active( $plugin_file ); |
162
|
|
|
$plugin['name'] = preg_replace("/(.+)\.php$/", "$1", $plugin_file ); |
163
|
|
|
$plugin['display_name'] = $plugin_data['Name']; |
164
|
|
|
$plugin['plugin_url'] = $plugin_data['PluginURI']; |
165
|
|
|
$plugin['version'] = $plugin_data['Version']; |
166
|
|
|
$plugin['description'] = $plugin_data['Description']; |
167
|
|
|
$plugin['author'] = $plugin_data['Author']; |
168
|
|
|
$plugin['author_url'] = $plugin_data['AuthorURI']; |
169
|
|
|
$plugin['network'] = $plugin_data['Network']; |
170
|
|
|
$plugin['update'] = $this->get_plugin_updates( $plugin_file ); |
171
|
|
|
$action_link = $this->get_plugin_action_links( $plugin_file ); |
172
|
|
|
if ( ! empty( $action_link ) ) { |
173
|
|
|
$plugin['action_links'] = $action_link; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$autoupdate = $this->plugin_has_autoupdates_enabled( $plugin_file ); |
177
|
|
|
$plugin['autoupdate'] = $autoupdate; |
178
|
|
|
|
179
|
|
|
$autoupdate_translation = $this->plugin_has_translations_autoupdates_enabled( $plugin_file ); |
180
|
|
|
$plugin['autoupdate_translation'] = $autoupdate || $autoupdate_translation || Jetpack_Options::get_option( 'autoupdate_translations', false ); |
181
|
|
|
$plugin['uninstallable'] = is_uninstallable_plugin( $plugin_file ); |
182
|
|
|
|
183
|
|
|
if ( ! empty ( $this->log[ $plugin_file ] ) ) { |
184
|
|
|
$plugin['log'] = $this->log[ $plugin_file ]; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $plugin; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
protected function plugin_has_autoupdates_enabled( $plugin_file ) { |
191
|
|
|
return (bool) in_array( $plugin_file, Jetpack_Options::get_option( 'autoupdate_plugins', array() ) ); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
protected function plugin_has_translations_autoupdates_enabled( $plugin_file ) { |
195
|
|
|
return (bool) in_array( $plugin_file, Jetpack_Options::get_option( 'autoupdate_plugins_translations', array() ) ); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
protected function get_file_mod_capabilities() { |
200
|
|
|
$reasons_can_not_autoupdate = array(); |
201
|
|
|
$reasons_can_not_modify_files = array(); |
202
|
|
|
|
203
|
|
|
$has_file_system_write_access = Jetpack_Sync_Functions::file_system_write_access(); |
204
|
|
|
if ( ! $has_file_system_write_access ) { |
205
|
|
|
$reasons_can_not_modify_files['has_no_file_system_write_access'] = __( 'The file permissions on this host prevent editing files.', 'jetpack' ); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$disallow_file_mods = Jetpack_Constants::get_constant('DISALLOW_FILE_MODS' ); |
209
|
|
|
if ( $disallow_file_mods ) { |
210
|
|
|
$reasons_can_not_modify_files['disallow_file_mods'] = __( 'File modifications are explicitly disabled by a site administrator.', 'jetpack' ); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$automatic_updater_disabled = Jetpack_Constants::get_constant( 'AUTOMATIC_UPDATER_DISABLED' ); |
214
|
|
|
if ( $automatic_updater_disabled ) { |
215
|
|
|
$reasons_can_not_autoupdate['automatic_updater_disabled'] = __( 'Any autoupdates are explicitly disabled by a site administrator.', 'jetpack' ); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
if ( is_multisite() ) { |
219
|
|
|
// is it the main network ? is really is multi network |
220
|
|
|
if ( Jetpack::is_multi_network() ) { |
221
|
|
|
$reasons_can_not_modify_files['is_multi_network'] = __( 'Multi network install are not supported.', 'jetpack' ); |
222
|
|
|
} |
223
|
|
|
// Is the site the main site here. |
224
|
|
|
if ( ! is_main_site() ) { |
225
|
|
|
$reasons_can_not_modify_files['is_sub_site'] = __( 'The site is not the main network site', 'jetpack' ); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
$file_mod_capabilities = array( |
230
|
|
|
'modify_files' => (bool) empty( $reasons_can_not_modify_files ), // install, remove, update |
231
|
|
|
'autoupdate_files' => (bool) empty( $reasons_can_not_modify_files ) && empty( $reasons_can_not_autoupdate ), // enable autoupdates |
232
|
|
|
); |
233
|
|
|
|
234
|
|
|
if ( ! empty( $reasons_can_not_modify_files ) ) { |
235
|
|
|
$file_mod_capabilities['reasons_modify_files_unavailable'] = $reasons_can_not_modify_files; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
if ( ! $file_mod_capabilities['autoupdate_files'] ) { |
239
|
|
|
$file_mod_capabilities['reasons_autoupdate_unavailable'] = array_merge( $reasons_can_not_autoupdate, $reasons_can_not_modify_files ); |
240
|
|
|
} |
241
|
|
|
return $file_mod_capabilities; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
protected function get_plugins() { |
245
|
|
|
$plugins = array(); |
246
|
|
|
/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ |
247
|
|
|
$installed_plugins = apply_filters( 'all_plugins', get_plugins() ); |
248
|
|
|
foreach( $this->plugins as $plugin ) { |
249
|
|
|
if ( ! isset( $installed_plugins[ $plugin ] ) ) |
250
|
|
|
continue; |
251
|
|
|
$plugins[] = $this->format_plugin( $plugin, $installed_plugins[ $plugin ] ); |
252
|
|
|
} |
253
|
|
|
$args = $this->query_args(); |
254
|
|
|
|
255
|
|
|
if ( isset( $args['offset'] ) ) { |
256
|
|
|
$plugins = array_slice( $plugins, (int) $args['offset'] ); |
257
|
|
|
} |
258
|
|
|
if ( isset( $args['limit'] ) ) { |
259
|
|
|
$plugins = array_slice( $plugins, 0, (int) $args['limit'] ); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return $plugins; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
protected function validate_network_wide() { |
266
|
|
|
$args = $this->input(); |
267
|
|
|
|
268
|
|
|
if ( isset( $args['network_wide'] ) && $args['network_wide'] ) { |
269
|
|
|
$this->network_wide = true; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
if ( $this->network_wide && ! current_user_can( 'manage_network_plugins' ) ) { |
273
|
|
|
return new WP_Error( 'unauthorized', __( 'This user is not authorized to manage plugins network wide.', 'jetpack' ), 403 ); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return true; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
|
280
|
|
|
protected function validate_plugin( $plugin ) { |
281
|
|
|
if ( ! isset( $plugin) || empty( $plugin ) ) { |
282
|
|
|
return new WP_Error( 'missing_plugin', __( 'You are required to specify a plugin to activate.', 'jetpack' ), 400 ); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
if ( is_wp_error( $error = validate_plugin( $plugin ) ) ) { |
286
|
|
|
return new WP_Error( 'unknown_plugin', $error->get_error_messages() , 404 ); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return true; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
protected function get_plugin_updates( $plugin_file ) { |
293
|
|
|
$plugin_updates = get_plugin_updates(); |
294
|
|
|
if ( isset( $plugin_updates[ $plugin_file ] ) ) { |
295
|
|
|
$update = $plugin_updates[ $plugin_file ]->update; |
296
|
|
|
$cleaned_update = array(); |
297
|
|
|
foreach( (array) $update as $update_key => $update_value ) { |
298
|
|
|
switch ( $update_key ) { |
299
|
|
|
case 'id': |
300
|
|
|
case 'slug': |
301
|
|
|
case 'plugin': |
302
|
|
|
case 'new_version': |
303
|
|
|
case 'tested': |
304
|
|
|
$cleaned_update[ $update_key ] = wp_kses( $update_value, array() ); |
305
|
|
|
break; |
306
|
|
|
case 'url': |
307
|
|
|
case 'package': |
308
|
|
|
$cleaned_update[ $update_key ] = esc_url( $update_value ); |
309
|
|
|
break; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
return (object) $cleaned_update; |
313
|
|
|
} |
314
|
|
|
return null; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
protected function get_plugin_action_links( $plugin_file ) { |
318
|
|
|
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-functions.php'; |
319
|
|
|
return Jetpack_Sync_Functions::get_plugins_action_links( $plugin_file ); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.