← All changes
|
json-endpoints/jetpack/class.jetpack-json-api-plugins-modify-endpoint.php
+34
-16
12.0.3
→
16.3
View file →
| @@ -1,8 +1,12 @@ | ||
| 1 | 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | 2 | |
| 3 | 3 | use Automattic\Jetpack\Constants; |
| 4 | 4 | |
| 5 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 6 | + exit( 0 ); | |
| 7 | +} | |
| 8 | + | |
| 5 | 9 | new Jetpack_JSON_API_Plugins_Modify_Endpoint( |
| 6 | 10 | array( |
| 7 | 11 | 'description' => 'Activate/Deactivate a Plugin on your Jetpack Site, or set automatic updates', |
| 8 | 12 | 'min_version' => '1', |
| @@ -15,12 +19,13 @@ | ||
| 15 | 19 | '$plugin' => '(string) The plugin ID', |
| 16 | 20 | ), |
| 17 | 21 | 'allow_jetpack_site_auth' => true, |
| 18 | 22 | '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 | + 'action' => '(string) Possible values are \'update\'', | |
| 24 | + 'autoupdate' => '(bool) Whether or not to automatically update the plugin', | |
| 25 | + 'active' => '(bool) Activate or deactivate the plugin', | |
| 26 | + 'network_wide' => '(bool) Do action network wide (default value: false)', | |
| 27 | + 'scheduled_update' => '(bool) If the update is happening as a result of a scheduled update event', | |
| 23 | 28 | ), |
| 24 | 29 | 'query_parameters' => array( |
| 25 | 30 | 'autoupdate' => '(bool=false) If the update is happening as a result of autoupdate event', |
| 26 | 31 | ), |
| @@ -111,8 +116,10 @@ | ||
| 111 | 116 | * Plugins modify endpoint class. |
| 112 | 117 | * |
| 113 | 118 | * POST /sites/%s/plugins/%s |
| 114 | 119 | * POST /sites/%s/plugins |
| 120 | + * | |
| 121 | + * @phan-constructor-used-for-side-effects | |
| 115 | 122 | */ |
| 116 | 123 | class Jetpack_JSON_API_Plugins_Modify_Endpoint extends Jetpack_JSON_API_Plugins_Endpoint { |
| 117 | 124 | |
| 118 | 125 | /** |
| @@ -152,8 +159,9 @@ | ||
| 152 | 159 | * |
| 153 | 160 | * @return bool|WP_Error |
| 154 | 161 | */ |
| 155 | 162 | public function callback( $path = '', $blog_id = 0, $object = null ) { |
| 163 | + | |
| 156 | 164 | Jetpack_JSON_API_Endpoint::validate_input( $object ); |
| 157 | 165 | switch ( $this->action ) { |
| 158 | 166 | case 'delete': |
| 159 | 167 | $this->needed_capabilities = 'delete_plugins'; |
| @@ -166,9 +174,11 @@ | ||
| 166 | 174 | $this->needed_capabilities = 'install_plugins'; |
| 167 | 175 | break; |
| 168 | 176 | } |
| 169 | 177 | |
| 170 | - if ( isset( $args['autoupdate'] ) || isset( $args['autoupdate_translations'] ) ) { | |
| 178 | + $args = $this->input(); | |
| 179 | + | |
| 180 | + if ( is_array( $args ) && ( isset( $args['autoupdate'] ) || isset( $args['autoupdate_translations'] ) ) ) { | |
| 171 | 181 | $this->needed_capabilities = 'update_plugins'; |
| 172 | 182 | } |
| 173 | 183 | |
| 174 | 184 | return parent::callback( $path, $blog_id, $object ); |
| @@ -377,14 +387,19 @@ | ||
| 377 | 387 | * |
| 378 | 388 | * @return bool|WP_Error |
| 379 | 389 | */ |
| 380 | 390 | protected function update() { |
| 391 | + $query_args = $this->query_args(); | |
| 381 | 392 | |
| 382 | - $query_args = $this->query_args(); | |
| 383 | - if ( isset( $query_args['autoupdate'] ) && $query_args['autoupdate'] ) { | |
| 384 | - Constants::set_constant( 'JETPACK_PLUGIN_AUTOUPDATE', true ); | |
| 393 | + $is_automatic_update = false; | |
| 394 | + if ( isset( $query_args['autoupdate'] ) && $query_args['autoupdate'] || $this->scheduled_update ) { | |
| 395 | + $is_automatic_update = true; | |
| 385 | 396 | } |
| 386 | - wp_clean_plugins_cache(); | |
| 397 | + | |
| 398 | + if ( $this->scheduled_update ) { | |
| 399 | + Constants::set_constant( 'SCHEDULED_AUTOUPDATE', true ); | |
| 400 | + } | |
| 401 | + wp_clean_plugins_cache( false ); | |
| 387 | 402 | ob_start(); |
| 388 | 403 | wp_update_plugins(); // Check for Plugin updates |
| 389 | 404 | ob_end_clean(); |
| 390 | 405 | |
| @@ -404,11 +419,14 @@ | ||
| 404 | 419 | remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 ); |
| 405 | 420 | remove_action( 'upgrader_process_complete', 'wp_version_check' ); |
| 406 | 421 | remove_action( 'upgrader_process_complete', 'wp_update_themes' ); |
| 407 | 422 | |
| 423 | + // Set the lock timeout to 15 minutes if it's scheduled update, otherwise default to one hour. | |
| 424 | + $lock_release_timeout = $this->scheduled_update ? 15 * MINUTE_IN_SECONDS : null; | |
| 425 | + | |
| 408 | 426 | // Early return if unable to obtain auto_updater lock. |
| 409 | 427 | // @see https://github.com/WordPress/wordpress-develop/blob/66469efa99e7978c8824e287834135aa9842e84f/src/wp-admin/includes/class-wp-automatic-updater.php#L453. |
| 410 | - if ( Constants::get_constant( 'JETPACK_PLUGIN_AUTOUPDATE' ) && ! WP_Upgrader::create_lock( 'auto_updater' ) ) { | |
| 428 | + if ( $is_automatic_update && ! WP_Upgrader::create_lock( 'auto_updater', $lock_release_timeout ) ) { | |
| 411 | 429 | return new WP_Error( 'update_fail', __( 'Updates are already in progress.', 'jetpack' ), 400 ); |
| 412 | 430 | } |
| 413 | 431 | |
| 414 | 432 | $result = false; |
| @@ -420,15 +438,15 @@ | ||
| 420 | 438 | continue; |
| 421 | 439 | } |
| 422 | 440 | |
| 423 | 441 | // Rely on WP_Automatic_Updater class to check if a plugin item should be updated if it is a Jetpack autoupdate request. |
| 424 | - if ( Constants::get_constant( 'JETPACK_PLUGIN_AUTOUPDATE' ) && ! ( new WP_Automatic_Updater() )->should_update( 'plugin', $update_plugins->response[ $plugin ], WP_PLUGIN_DIR ) ) { | |
| 442 | + if ( $is_automatic_update && ! ( new WP_Automatic_Updater() )->should_update( 'plugin', $update_plugins->response[ $plugin ], WP_PLUGIN_DIR ) ) { | |
| 425 | 443 | continue; |
| 426 | 444 | } |
| 427 | 445 | |
| 428 | 446 | // Establish per plugin lock. |
| 429 | 447 | $plugin_slug = Jetpack_Autoupdate::get_plugin_slug( $plugin ); |
| 430 | - if ( ! WP_Upgrader::create_lock( 'jetpack_' . $plugin_slug ) ) { | |
| 448 | + if ( ! WP_Upgrader::create_lock( 'jetpack_' . $plugin_slug, $lock_release_timeout ) ) { | |
| 431 | 449 | continue; |
| 432 | 450 | } |
| 433 | 451 | |
| 434 | 452 | /** |
| @@ -451,10 +469,10 @@ | ||
| 451 | 469 | $upgrader->init(); |
| 452 | 470 | // This avoids the plugin to be deactivated. |
| 453 | 471 | // Using bulk upgrade puts the site into maintenance mode during the upgrades |
| 454 | 472 | $result = $upgrader->bulk_upgrade( array( $plugin ) ); |
| 455 | - $errors = $upgrader->skin->get_errors(); | |
| 456 | - $this->log[ $plugin ] = $upgrader->skin->get_upgrade_messages(); | |
| 473 | + $errors = $skin->get_errors(); | |
| 474 | + $this->log[ $plugin ] = $skin->get_upgrade_messages(); | |
| 457 | 475 | |
| 458 | 476 | // release individual plugin lock. |
| 459 | 477 | WP_Upgrader::release_lock( 'jetpack_' . $plugin_slug ); |
| 460 | 478 | |
| @@ -463,9 +481,9 @@ | ||
| 463 | 481 | } |
| 464 | 482 | } |
| 465 | 483 | |
| 466 | 484 | // release auto_udpate lock. |
| 467 | - if ( Constants::get_constant( 'JETPACK_PLUGIN_AUTOUPDATE' ) ) { | |
| 485 | + if ( $is_automatic_update ) { | |
| 468 | 486 | WP_Upgrader::release_lock( 'auto_updater' ); |
| 469 | 487 | } |
| 470 | 488 | |
| 471 | 489 | if ( ! $this->bulk && ! $result && $update_attempted ) { |
| @@ -483,9 +501,9 @@ | ||
| 483 | 501 | public function update_translations() { |
| 484 | 502 | include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 485 | 503 | |
| 486 | 504 | // Clear the cache. |
| 487 | - wp_clean_plugins_cache(); | |
| 505 | + wp_clean_plugins_cache( false ); | |
| 488 | 506 | ob_start(); |
| 489 | 507 | wp_update_plugins(); // Check for Plugin updates |
| 490 | 508 | ob_end_clean(); |
| 491 | 509 | |