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
|
|
|
$plugin['action_links'] = $this->get_plugin_action_links( $plugin_file ); |
140
|
|
|
|
141
|
|
|
$autoupdate = in_array( $plugin_file, Jetpack_Options::get_option( 'autoupdate_plugins', array() ) ); |
142
|
|
|
$plugin['autoupdate'] = $autoupdate; |
143
|
|
|
|
144
|
|
|
$autoupdate_translation = in_array( $plugin_file, Jetpack_Options::get_option( 'autoupdate_plugins_translations', array() ) ); |
145
|
|
|
$plugin['autoupdate_translation'] = $autoupdate || $autoupdate_translation || Jetpack_Options::get_option( 'autoupdate_translations', false ); |
146
|
|
|
|
147
|
|
|
$plugin['uninstallable'] = is_uninstallable_plugin( $plugin_file ); |
|
|
|
|
148
|
|
|
|
149
|
|
|
if ( ! empty ( $this->log[ $plugin_file ] ) ) { |
150
|
|
|
$plugin['log'] = $this->log[ $plugin_file ]; |
151
|
|
|
} |
152
|
|
|
return $plugin; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
protected function format_plugin_v1_2( $plugin_file, $plugin_data ) { |
156
|
|
|
$plugin = array(); |
157
|
|
|
$plugin['slug'] = Jetpack_Autoupdate::get_plugin_slug( $plugin_file ); |
158
|
|
|
$plugin['active'] = Jetpack::is_plugin_active( $plugin_file ); |
159
|
|
|
$plugin['name'] = preg_replace("/(.+)\.php$/", "$1", $plugin_file ); |
160
|
|
|
$plugin['display_name'] = $plugin_data['Name']; |
161
|
|
|
$plugin['plugin_url'] = $plugin_data['PluginURI']; |
162
|
|
|
$plugin['version'] = $plugin_data['Version']; |
163
|
|
|
$plugin['description'] = $plugin_data['Description']; |
164
|
|
|
$plugin['author'] = $plugin_data['Author']; |
165
|
|
|
$plugin['author_url'] = $plugin_data['AuthorURI']; |
166
|
|
|
$plugin['network'] = $plugin_data['Network']; |
167
|
|
|
$plugin['update'] = $this->get_plugin_updates( $plugin_file ); |
168
|
|
|
$plugin['action_links'] = $this->get_plugin_action_links( $plugin_file ); |
169
|
|
|
|
170
|
|
|
$autoupdate = $this->plugin_has_autoupdates_enabled( $plugin_file ); |
171
|
|
|
$plugin['autoupdate'] = $autoupdate; |
172
|
|
|
|
173
|
|
|
$autoupdate_translation = $this->plugin_has_translations_autoupdates_enabled( $plugin_file ); |
174
|
|
|
$plugin['autoupdate_translation'] = $autoupdate || $autoupdate_translation || Jetpack_Options::get_option( 'autoupdate_translations', false ); |
175
|
|
|
$plugin['uninstallable'] = is_uninstallable_plugin( $plugin_file ); |
176
|
|
|
|
177
|
|
|
if ( ! empty ( $this->log[ $plugin_file ] ) ) { |
178
|
|
|
$plugin['log'] = $this->log[ $plugin_file ]; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $plugin; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
protected function plugin_has_autoupdates_enabled( $plugin_file ) { |
185
|
|
|
return (bool) in_array( $plugin_file, Jetpack_Options::get_option( 'autoupdate_plugins', array() ) ); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
protected function plugin_has_translations_autoupdates_enabled( $plugin_file ) { |
189
|
|
|
return (bool) in_array( $plugin_file, Jetpack_Options::get_option( 'autoupdate_plugins_translations', array() ) ); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
protected function get_file_mod_capabilities() { |
194
|
|
|
$reasons_can_not_autoupdate = array(); |
195
|
|
|
$reasons_can_not_modify_files = array(); |
196
|
|
|
|
197
|
|
|
$has_file_system_write_access = Jetpack_Sync_Functions::file_system_write_access(); |
198
|
|
|
if ( ! $has_file_system_write_access ) { |
199
|
|
|
$reasons_can_not_modify_files['has_no_file_system_write_access'] = __( 'The file permissions on this host prevent editing files.', 'jetpack' ); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$disallow_file_mods = Jetpack_Constants::get_constant('DISALLOW_FILE_MODS' ); |
203
|
|
|
if ( $disallow_file_mods ) { |
204
|
|
|
$reasons_can_not_modify_files['disallow_file_mods'] = __( 'File modifications are explicitly disabled by a site administrator.', 'jetpack' ); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$automatic_updater_disabled = Jetpack_Constants::get_constant( 'AUTOMATIC_UPDATER_DISABLED' ); |
208
|
|
|
if ( $automatic_updater_disabled ) { |
209
|
|
|
$reasons_can_not_autoupdate['automatic_updater_disabled'] = __( 'Any autoupdates are explicitly disabled by a site administrator.', 'jetpack' ); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if ( is_multisite() ) { |
213
|
|
|
// is it the main network ? is really is multi network |
214
|
|
|
if ( Jetpack::is_multi_network() ) { |
215
|
|
|
$reasons_can_not_modify_files['is_multi_network'] = __( 'Multi network install are not supported.', 'jetpack' ); |
216
|
|
|
} |
217
|
|
|
// Is the site the main site here. |
218
|
|
|
if ( ! is_main_site() ) { |
219
|
|
|
$reasons_can_not_modify_files['is_sub_site'] = __( 'The site is not the main network site', 'jetpack' ); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$file_mod_capabilities = array( |
224
|
|
|
'modify_files' => (bool) empty( $reasons_can_not_modify_files ), // install, remove, update |
225
|
|
|
'autoupdate_files' => (bool) empty( $reasons_can_not_modify_files ) && empty( $reasons_can_not_autoupdate ), // enable autoupdates |
226
|
|
|
); |
227
|
|
|
|
228
|
|
|
if ( ! empty( $reasons_can_not_modify_files ) ) { |
229
|
|
|
$file_mod_capabilities['reasons_modify_files_disabled'] = $reasons_can_not_modify_files; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
if ( ! $file_mod_capabilities['autoupdate_files'] ) { |
233
|
|
|
$file_mod_capabilities['reasons_autoupdate_disabled'] = array_merge( $reasons_can_not_autoupdate, $reasons_can_not_modify_files ); |
234
|
|
|
} |
235
|
|
|
return $file_mod_capabilities; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
protected function get_plugins() { |
239
|
|
|
$plugins = array(); |
240
|
|
|
/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ |
241
|
|
|
$installed_plugins = apply_filters( 'all_plugins', get_plugins() ); |
242
|
|
|
foreach( $this->plugins as $plugin ) { |
243
|
|
|
if ( ! isset( $installed_plugins[ $plugin ] ) ) |
244
|
|
|
continue; |
245
|
|
|
$plugins[] = $this->format_plugin( $plugin, $installed_plugins[ $plugin ] ); |
246
|
|
|
} |
247
|
|
|
$args = $this->query_args(); |
248
|
|
|
|
249
|
|
|
if ( isset( $args['offset'] ) ) { |
250
|
|
|
$plugins = array_slice( $plugins, (int) $args['offset'] ); |
251
|
|
|
} |
252
|
|
|
if ( isset( $args['limit'] ) ) { |
253
|
|
|
$plugins = array_slice( $plugins, 0, (int) $args['limit'] ); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return $plugins; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
protected function validate_network_wide() { |
260
|
|
|
$args = $this->input(); |
261
|
|
|
|
262
|
|
|
if ( isset( $args['network_wide'] ) && $args['network_wide'] ) { |
263
|
|
|
$this->network_wide = true; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
if ( $this->network_wide && ! current_user_can( 'manage_network_plugins' ) ) { |
267
|
|
|
return new WP_Error( 'unauthorized', __( 'This user is not authorized to manage plugins network wide.', 'jetpack' ), 403 ); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
return true; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
|
274
|
|
|
protected function validate_plugin( $plugin ) { |
275
|
|
|
if ( ! isset( $plugin) || empty( $plugin ) ) { |
276
|
|
|
return new WP_Error( 'missing_plugin', __( 'You are required to specify a plugin to activate.', 'jetpack' ), 400 ); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
if ( is_wp_error( $error = validate_plugin( $plugin ) ) ) { |
280
|
|
|
return new WP_Error( 'unknown_plugin', $error->get_error_messages() , 404 ); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
return true; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
protected function get_plugin_updates( $plugin_file ) { |
287
|
|
|
$plugin_updates = get_plugin_updates(); |
288
|
|
|
if ( isset( $plugin_updates[ $plugin_file ] ) ){ |
289
|
|
|
return $plugin_updates[ $plugin_file ]->update; |
290
|
|
|
} |
291
|
|
|
return null; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
protected function get_plugin_action_links( $plugin_file ) { |
295
|
|
|
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-functions.php'; |
296
|
|
|
return Jetpack_Sync_Functions::get_plugins_action_links( $plugin_file ); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
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.