PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.6.4
Jetpack – WP Security, Backup, Speed, & Growth v12.6.4
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / json-endpoints / jetpack / class.jetpack-json-api-plugins-modify-endpoint.php

class.jetpack-json-api-plugins-modify-endpoint.php in Jetpack – WP Security, Backup, Speed, & Growth 12.6.4, at json-endpoints/jetpack/class.jetpack-json-api-plugins-modify-endpoint.php

551 lines 17.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3 use Automattic\Jetpack\Constants;
4
5 new Jetpack_JSON_API_Plugins_Modify_Endpoint(
6 array(
7 'description' => 'Activate/Deactivate a Plugin on your Jetpack Site, or set automatic updates',
8 'min_version' => '1',
9 'max_version' => '1.1',
10 'method' => 'POST',
11 'path' => '/sites/%s/plugins/%s',
12 'stat' => 'plugins:1:modify',
13 'path_labels' => array(
14 '$site' => '(int|string) The site ID, The site domain',
15 '$plugin' => '(string) The plugin ID',
16 ),
17 'allow_jetpack_site_auth' => true,
18 'request_format' => array(
19 'action' => '(string) Possible values are \'update\'',
20 'autoupdate' => '(bool) Whether or not to automatically update the plugin',
21 'active' => '(bool) Activate or deactivate the plugin',
22 'network_wide' => '(bool) Do action network wide (default value: false)',
23 ),
24 'query_parameters' => array(
25 'autoupdate' => '(bool=false) If the update is happening as a result of autoupdate event',
26 ),
27 'response_format' => Jetpack_JSON_API_Plugins_Endpoint::$_response_format,
28 'example_request_data' => array(
29 'headers' => array(
30 'authorization' => 'Bearer YOUR_API_TOKEN',
31 ),
32 'body' => array(
33 'action' => 'update',
34 ),
35 ),
36 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/example.wordpress.org/plugins/hello-dolly%20hello',
37 )
38 );
39
40 new Jetpack_JSON_API_Plugins_Modify_Endpoint(
41 array(
42 'description' => 'Activate/Deactivate a list of plugins on your Jetpack Site, or set automatic updates',
43 'min_version' => '1',
44 'max_version' => '1.1',
45 'method' => 'POST',
46 'path' => '/sites/%s/plugins',
47 'stat' => 'plugins:modify',
48 'path_labels' => array(
49 '$site' => '(int|string) The site ID, The site domain',
50 ),
51 'request_format' => array(
52 'action' => '(string) Possible values are \'update\'',
53 'autoupdate' => '(bool) Whether or not to automatically update the plugin',
54 'active' => '(bool) Activate or deactivate the plugin',
55 'network_wide' => '(bool) Do action network wide (default value: false)',
56 'plugins' => '(array) A list of plugin ids to modify',
57 ),
58 'allow_jetpack_site_auth' => true,
59 'query_parameters' => array(
60 'autoupdate' => '(bool=false) If the update is happening as a result of autoupdate event',
61 ),
62 'response_format' => array(
63 'plugins' => '(array:plugin) An array of plugin objects.',
64 'updated' => '(array) A list of plugin ids that were updated. Only present if action is update.',
65 'not_updated' => '(array) A list of plugin ids that were not updated. Only present if action is update.',
66 'log' => '(array) Update log. Only present if action is update.',
67 ),
68 'example_request_data' => array(
69 'headers' => array(
70 'authorization' => 'Bearer YOUR_API_TOKEN',
71 ),
72 'body' => array(
73 'active' => true,
74 'plugins' => array(
75 'jetpack/jetpack',
76 'akismet/akismet',
77 ),
78 ),
79 ),
80 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/example.wordpress.org/plugins',
81 )
82 );
83
84 new Jetpack_JSON_API_Plugins_Modify_Endpoint(
85 array(
86 'description' => 'Update a Plugin on your Jetpack Site',
87 'min_version' => '1',
88 'max_version' => '1.1',
89 'method' => 'POST',
90 'path' => '/sites/%s/plugins/%s/update/',
91 'stat' => 'plugins:1:update',
92 'path_labels' => array(
93 '$site' => '(int|string) The site ID, The site domain',
94 '$plugin' => '(string) The plugin ID',
95 ),
96 'allow_jetpack_site_auth' => true,
97 'query_parameters' => array(
98 'autoupdate' => '(bool=false) If the update is happening as a result of autoupdate event',
99 ),
100 'response_format' => Jetpack_JSON_API_Plugins_Endpoint::$_response_format,
101 'example_request_data' => array(
102 'headers' => array(
103 'authorization' => 'Bearer YOUR_API_TOKEN',
104 ),
105 ),
106 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/example.wordpress.org/plugins/hello-dolly%20hello/update',
107 )
108 );
109
110 /**
111 * Plugins modify endpoint class.
112 *
113 * POST /sites/%s/plugins/%s
114 * POST /sites/%s/plugins
115 */
116 class Jetpack_JSON_API_Plugins_Modify_Endpoint extends Jetpack_JSON_API_Plugins_Endpoint {
117
118 /**
119 * The slug.
120 *
121 * @var string
122 */
123 protected $slug = null;
124
125 /**
126 * Needed capabilities.
127 *
128 * @var string
129 */
130 protected $needed_capabilities = 'activate_plugins';
131
132 /**
133 * Action.
134 *
135 * @var string
136 */
137 protected $action = 'default_action';
138
139 /**
140 * Expected actions.
141 *
142 * @var array
143 */
144 protected $expected_actions = array( 'update', 'install', 'delete', 'update_translations' );
145
146 /**
147 * Callback.
148 *
149 * @param string $path - the path.
150 * @param int $blog_id - the blog ID.
151 * @param object $object - the object.
152 *
153 * @return bool|WP_Error
154 */
155 public function callback( $path = '', $blog_id = 0, $object = null ) {
156
157 Jetpack_JSON_API_Endpoint::validate_input( $object );
158 switch ( $this->action ) {
159 case 'delete':
160 $this->needed_capabilities = 'delete_plugins';
161 break;
162 case 'update_translations':
163 case 'update':
164 $this->needed_capabilities = 'update_plugins';
165 break;
166 case 'install':
167 $this->needed_capabilities = 'install_plugins';
168 break;
169 }
170
171 $args = $this->input();
172
173 if ( is_array( $args ) && ( isset( $args['autoupdate'] ) || isset( $args['autoupdate_translations'] ) ) ) {
174 $this->needed_capabilities = 'update_plugins';
175 }
176
177 return parent::callback( $path, $blog_id, $object );
178 }
179
180 /**
181 * The default action.
182 *
183 * @return bool|WP_Error
184 */
185 public function default_action() {
186 $args = $this->input();
187
188 if ( isset( $args['autoupdate'] ) && is_bool( $args['autoupdate'] ) ) {
189 if ( $args['autoupdate'] ) {
190 $this->autoupdate_on();
191 } else {
192 $this->autoupdate_off();
193 }
194 }
195
196 if ( isset( $args['active'] ) && is_bool( $args['active'] ) ) {
197 if ( $args['active'] ) {
198 // We don't have to check for activate_plugins permissions since we assume that the user has those
199 // Since we set them via $needed_capabilities.
200 return $this->activate();
201 } elseif ( $this->current_user_can( 'deactivate_plugins' ) ) {
202 return $this->deactivate();
203 } else {
204 return new WP_Error( 'unauthorized_error', __( 'Plugin deactivation is not allowed', 'jetpack' ), '403' );
205 }
206 }
207
208 if ( isset( $args['autoupdate_translations'] ) && is_bool( $args['autoupdate_translations'] ) ) {
209 if ( $args['autoupdate_translations'] ) {
210 $this->autoupdate_translations_on();
211 } else {
212 $this->autoupdate_translations_off();
213 }
214 }
215
216 return true;
217 }
218
219 /**
220 * Turn on autoupdate.
221 */
222 protected function autoupdate_on() {
223 $autoupdate_plugins = (array) get_site_option( 'auto_update_plugins', array() );
224 $autoupdate_plugins = array_unique( array_merge( $autoupdate_plugins, $this->plugins ) );
225 update_site_option( 'auto_update_plugins', $autoupdate_plugins );
226 }
227
228 /**
229 * Turn off autoupdate.
230 */
231 protected function autoupdate_off() {
232 $autoupdate_plugins = (array) get_site_option( 'auto_update_plugins', array() );
233 $autoupdate_plugins = array_diff( $autoupdate_plugins, $this->plugins );
234 update_site_option( 'auto_update_plugins', $autoupdate_plugins );
235 }
236
237 /**
238 * Turn autoupdate translations on.
239 */
240 protected function autoupdate_translations_on() {
241 $autoupdate_plugins = Jetpack_Options::get_option( 'autoupdate_plugins_translations', array() );
242 $autoupdate_plugins = array_unique( array_merge( $autoupdate_plugins, $this->plugins ) );
243 Jetpack_Options::update_option( 'autoupdate_plugins_translations', $autoupdate_plugins );
244 }
245
246 /**
247 * Turn autoupdate translations off.
248 */
249 protected function autoupdate_translations_off() {
250 $autoupdate_plugins = Jetpack_Options::get_option( 'autoupdate_plugins_translations', array() );
251 $autoupdate_plugins = array_diff( $autoupdate_plugins, $this->plugins );
252 Jetpack_Options::update_option( 'autoupdate_plugins_translations', $autoupdate_plugins );
253 }
254
255 /**
256 * Activate the plugin.
257 *
258 * @return null|WP_Error null if the activation was successful.
259 */
260 protected function activate() {
261 $permission_error = false;
262 foreach ( $this->plugins as $plugin ) {
263
264 if ( ! $this->current_user_can( 'activate_plugin', $plugin ) ) {
265 $this->log[ $plugin ]['error'] = __( 'Sorry, you are not allowed to activate this plugin.', 'jetpack' );
266 $has_errors = true;
267 $permission_error = true;
268 continue;
269 }
270
271 if ( ( ! $this->network_wide && Jetpack::is_plugin_active( $plugin ) ) || is_plugin_active_for_network( $plugin ) ) {
272 $this->log[ $plugin ]['error'] = __( 'The Plugin is already active.', 'jetpack' );
273 $has_errors = true;
274 continue;
275 }
276
277 if ( ! $this->network_wide && is_network_only_plugin( $plugin ) && is_multisite() ) {
278 $this->log[ $plugin ]['error'] = __( 'Plugin can only be Network Activated', 'jetpack' );
279 $has_errors = true;
280 continue;
281 }
282
283 $result = activate_plugin( $plugin, '', $this->network_wide );
284
285 if ( is_wp_error( $result ) ) {
286 $this->log[ $plugin ]['error'] = $result->get_error_messages();
287 $has_errors = true;
288 continue;
289 }
290
291 $success = Jetpack::is_plugin_active( $plugin );
292 if ( $success && $this->network_wide ) {
293 $success &= is_plugin_active_for_network( $plugin );
294 }
295
296 if ( ! $success ) {
297 $this->log[ $plugin ]['error'] = $result->get_error_messages;
298 $has_errors = true;
299 continue;
300 }
301 $this->log[ $plugin ][] = __( 'Plugin activated.', 'jetpack' );
302 }
303
304 if ( ! $this->bulk && isset( $has_errors ) ) {
305 $plugin = $this->plugins[0];
306 if ( $permission_error ) {
307 return new WP_Error( 'unauthorized_error', $this->log[ $plugin ]['error'], 403 );
308 }
309
310 return new WP_Error( 'activation_error', $this->log[ $plugin ]['error'] );
311 }
312 }
313
314 /**
315 * Check if the current user has capabilities.
316 *
317 * @param string $capability - the capability we're checking.
318 * @param string $plugin - the plugin we're checking.
319 *
320 * @return bool
321 */
322 protected function current_user_can( $capability, $plugin = null ) {
323 // If this endpoint accepts site based authentication and a blog token is used, skip capabilities check.
324 if ( $this->accepts_site_based_authentication() ) {
325 return true;
326 }
327 if ( $plugin ) {
328 return current_user_can( $capability, $plugin );
329 }
330
331 return current_user_can( $capability );
332 }
333
334 /**
335 * Deactivate the plugin.
336 *
337 * @return null|WP_Error null if the deactivation was successful
338 */
339 protected function deactivate() {
340 $permission_error = false;
341 foreach ( $this->plugins as $plugin ) {
342 if ( ! $this->current_user_can( 'deactivate_plugin', $plugin ) ) {
343 $error = __( 'Sorry, you are not allowed to deactivate this plugin.', 'jetpack' );
344 $this->log[ $plugin ]['error'] = $error;
345 $permission_error = true;
346 continue;
347 }
348
349 if ( ! Jetpack::is_plugin_active( $plugin ) ) {
350 $error = __( 'The Plugin is already deactivated.', 'jetpack' );
351 $this->log[ $plugin ]['error'] = $error;
352 continue;
353 }
354
355 deactivate_plugins( $plugin, false, $this->network_wide );
356
357 $success = ! Jetpack::is_plugin_active( $plugin );
358 if ( $success && $this->network_wide ) {
359 $success &= ! is_plugin_active_for_network( $plugin );
360 }
361
362 if ( ! $success ) {
363 $error = __( 'There was an error deactivating your plugin', 'jetpack' );
364 $this->log[ $plugin ]['error'] = $error;
365 continue;
366 }
367 $this->log[ $plugin ][] = __( 'Plugin deactivated.', 'jetpack' );
368 }
369 if ( ! $this->bulk && isset( $error ) ) {
370 if ( $permission_error ) {
371 return new WP_Error( 'unauthorized_error', $error, 403 );
372 }
373
374 return new WP_Error( 'deactivation_error', $error );
375 }
376 }
377
378 /**
379 * Update the plugin.
380 *
381 * @return bool|WP_Error
382 */
383 protected function update() {
384
385 $query_args = $this->query_args();
386 if ( isset( $query_args['autoupdate'] ) && $query_args['autoupdate'] ) {
387 Constants::set_constant( 'JETPACK_PLUGIN_AUTOUPDATE', true );
388 }
389 wp_clean_plugins_cache( false );
390 ob_start();
391 wp_update_plugins(); // Check for Plugin updates
392 ob_end_clean();
393
394 $update_plugins = get_site_transient( 'update_plugins' );
395
396 if ( isset( $update_plugins->response ) ) {
397 $plugin_updates_needed = array_keys( $update_plugins->response );
398 } else {
399 $plugin_updates_needed = array();
400 }
401
402 $update_attempted = false;
403
404 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
405
406 // unhook this functions that output things before we send our response header.
407 remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
408 remove_action( 'upgrader_process_complete', 'wp_version_check' );
409 remove_action( 'upgrader_process_complete', 'wp_update_themes' );
410
411 // Early return if unable to obtain auto_updater lock.
412 // @see https://github.com/WordPress/wordpress-develop/blob/66469efa99e7978c8824e287834135aa9842e84f/src/wp-admin/includes/class-wp-automatic-updater.php#L453.
413 if ( Constants::get_constant( 'JETPACK_PLUGIN_AUTOUPDATE' ) && ! WP_Upgrader::create_lock( 'auto_updater' ) ) {
414 return new WP_Error( 'update_fail', __( 'Updates are already in progress.', 'jetpack' ), 400 );
415 }
416
417 $result = false;
418
419 foreach ( $this->plugins as $plugin ) {
420
421 if ( ! in_array( $plugin, $plugin_updates_needed, true ) ) {
422 $this->log[ $plugin ][] = __( 'No update needed', 'jetpack' );
423 continue;
424 }
425
426 // Rely on WP_Automatic_Updater class to check if a plugin item should be updated if it is a Jetpack autoupdate request.
427 if ( Constants::get_constant( 'JETPACK_PLUGIN_AUTOUPDATE' ) && ! ( new WP_Automatic_Updater() )->should_update( 'plugin', $update_plugins->response[ $plugin ], WP_PLUGIN_DIR ) ) {
428 continue;
429 }
430
431 // Establish per plugin lock.
432 $plugin_slug = Jetpack_Autoupdate::get_plugin_slug( $plugin );
433 if ( ! WP_Upgrader::create_lock( 'jetpack_' . $plugin_slug ) ) {
434 continue;
435 }
436
437 /**
438 * Pre-upgrade action
439 *
440 * @since 3.9.3
441 *
442 * @param array $plugin Plugin data
443 * @param array $plugin Array of plugin objects
444 * @param bool $updated_attempted false for the first update, true subsequently
445 */
446 do_action( 'jetpack_pre_plugin_upgrade', $plugin, $this->plugins, $update_attempted );
447
448 $update_attempted = true;
449
450 // Object created inside the for loop to clean the messages for each plugin
451 $skin = new WP_Ajax_Upgrader_Skin();
452 // The Automatic_Upgrader_Skin skin shouldn't output anything.
453 $upgrader = new Plugin_Upgrader( $skin );
454 $upgrader->init();
455 // This avoids the plugin to be deactivated.
456 // Using bulk upgrade puts the site into maintenance mode during the upgrades
457 $result = $upgrader->bulk_upgrade( array( $plugin ) );
458 $errors = $upgrader->skin->get_errors();
459 $this->log[ $plugin ] = $upgrader->skin->get_upgrade_messages();
460
461 // release individual plugin lock.
462 WP_Upgrader::release_lock( 'jetpack_' . $plugin_slug );
463
464 if ( is_wp_error( $errors ) && $errors->get_error_code() ) {
465 return $errors;
466 }
467 }
468
469 // release auto_udpate lock.
470 if ( Constants::get_constant( 'JETPACK_PLUGIN_AUTOUPDATE' ) ) {
471 WP_Upgrader::release_lock( 'auto_updater' );
472 }
473
474 if ( ! $this->bulk && ! $result && $update_attempted ) {
475 return new WP_Error( 'update_fail', __( 'There was an error updating your plugin', 'jetpack' ), 400 );
476 }
477
478 return $this->default_action();
479 }
480
481 /**
482 * Update translations.
483 *
484 * @return bool|WP_Error
485 */
486 public function update_translations() {
487 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
488
489 // Clear the cache.
490 wp_clean_plugins_cache( false );
491 ob_start();
492 wp_update_plugins(); // Check for Plugin updates
493 ob_end_clean();
494
495 $available_updates = get_site_transient( 'update_plugins' );
496 if ( ! isset( $available_updates->translations ) || empty( $available_updates->translations ) ) {
497 return new WP_Error( 'nothing_to_translate' );
498 }
499
500 $update_attempted = false;
501 $result = false;
502 foreach ( $this->plugins as $plugin ) {
503 $this->slug = Jetpack_Autoupdate::get_plugin_slug( $plugin );
504 $translation = array_filter( $available_updates->translations, array( $this, 'get_translation' ) );
505
506 if ( empty( $translation ) ) {
507 $this->log[ $plugin ][] = __( 'No update needed', 'jetpack' );
508 continue;
509 }
510
511 /**
512 * Pre-upgrade action
513 *
514 * @since 4.4.0
515 *
516 * @param array $plugin Plugin data
517 * @param array $plugin Array of plugin objects
518 * @param bool $update_attempted false for the first update, true subsequently
519 */
520 do_action( 'jetpack_pre_plugin_upgrade_translations', $plugin, $this->plugins, $update_attempted );
521
522 $update_attempted = true;
523
524 $skin = new Automatic_Upgrader_Skin();
525 $upgrader = new Language_Pack_Upgrader( $skin );
526 $upgrader->init();
527
528 $result = $upgrader->upgrade( (object) $translation[0] );
529
530 $this->log[ $plugin ] = $upgrader->skin->get_upgrade_messages();
531 }
532
533 if ( ! $this->bulk && ! $result ) {
534 return new WP_Error( 'update_fail', __( 'There was an error updating your plugin', 'jetpack' ), 400 );
535 }
536
537 return true;
538 }
539
540 /**
541 * Test whether the translation matches `$this->slug`.
542 *
543 * @param array $translation - the translation.
544 *
545 * @return bool
546 */
547 protected function get_translation( $translation ) {
548 return ( $translation['slug'] === $this->slug );
549 }
550 }
551