← All changes
|
jetpack_vendor/automattic/jetpack-sync/src/modules/class-plugins.php
+135
-44
13.8.3
→
16.3-a.7
View file →
| @@ -9,8 +9,12 @@ | ||
| 9 | 9 | |
| 10 | 10 | use Automattic\Jetpack\Constants as Jetpack_Constants; |
| 11 | 11 | use WP_Error; |
| 12 | 12 | |
| 13 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 14 | + exit( 0 ); | |
| 15 | +} | |
| 16 | + | |
| 13 | 17 | /** |
| 14 | 18 | * Class to handle sync for plugins. |
| 15 | 19 | */ |
| 16 | 20 | class Plugins extends Module { |
| @@ -41,8 +45,44 @@ | ||
| 41 | 45 | */ |
| 42 | 46 | private $plugins = array(); |
| 43 | 47 | |
| 44 | 48 | /** |
| 49 | + * List of all updated plugins. | |
| 50 | + * | |
| 51 | + * @access private | |
| 52 | + * | |
| 53 | + * @var array | |
| 54 | + */ | |
| 55 | + private $plugins_updated = array(); | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * List of plugins installed during this request. | |
| 59 | + * | |
| 60 | + * @access private | |
| 61 | + * | |
| 62 | + * @var array | |
| 63 | + */ | |
| 64 | + private $plugins_installed = array(); | |
| 65 | + | |
| 66 | + /** | |
| 67 | + * List of all plugin update failures during this request. | |
| 68 | + * | |
| 69 | + * @access private | |
| 70 | + * | |
| 71 | + * @var array | |
| 72 | + */ | |
| 73 | + private $plugins_update_failures = array(); | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * State | |
| 77 | + * | |
| 78 | + * @access private | |
| 79 | + * | |
| 80 | + * @var array | |
| 81 | + */ | |
| 82 | + private $state = array(); | |
| 83 | + | |
| 84 | + /** | |
| 45 | 85 | * Sync module name. |
| 46 | 86 | * |
| 47 | 87 | * @access public |
| 48 | 88 | * |
| @@ -113,9 +153,9 @@ | ||
| 113 | 153 | if ( ! isset( $details['action'] ) ) { |
| 114 | 154 | return; |
| 115 | 155 | } |
| 116 | 156 | |
| 117 | - $plugins = ( isset( $details['plugins'] ) ? $details['plugins'] : null ); | |
| 157 | + $plugins = ( $details['plugins'] ?? null ); | |
| 118 | 158 | if ( empty( $plugins ) ) { |
| 119 | 159 | $plugins = ( isset( $details['plugin'] ) ? array( $details['plugin'] ) : null ); |
| 120 | 160 | } |
| 121 | 161 | |
| @@ -130,61 +170,43 @@ | ||
| 130 | 170 | } |
| 131 | 171 | |
| 132 | 172 | switch ( $details['action'] ) { |
| 133 | 173 | case 'update': |
| 134 | - $state = array( | |
| 174 | + $this->state = array( | |
| 135 | 175 | 'is_autoupdate' => Jetpack_Constants::is_true( 'JETPACK_PLUGIN_AUTOUPDATE' ), |
| 136 | 176 | ); |
| 137 | - $errors = $this->get_errors( $upgrader->skin ); | |
| 177 | + $errors = $this->get_errors( $upgrader->skin ); | |
| 138 | 178 | if ( $errors ) { |
| 139 | - foreach ( $plugins as $slug ) { | |
| 140 | - /** | |
| 141 | - * Sync that a plugin update failed | |
| 142 | - * | |
| 143 | - * @since 1.6.3 | |
| 144 | - * @since-jetpack 5.8.0 | |
| 145 | - * | |
| 146 | - * @module sync | |
| 147 | - * | |
| 148 | - * @param string $plugin , Plugin slug | |
| 149 | - * @param string Error code | |
| 150 | - * @param string Error message | |
| 151 | - */ | |
| 152 | - do_action( 'jetpack_plugin_update_failed', $this->get_plugin_info( $slug ), $errors['code'], $errors['message'], $state ); | |
| 179 | + foreach ( $plugins as $slug ) { // Accumulate failures and defer to shutdown, to reduce request-time lag. | |
| 180 | + $this->plugins_update_failures[] = array( | |
| 181 | + 'plugin' => $this->get_plugin_info( $slug ), | |
| 182 | + 'code' => $errors['code'], | |
| 183 | + 'message' => $errors['message'], | |
| 184 | + 'state' => $this->state, | |
| 185 | + ); | |
| 153 | 186 | } |
| 187 | + if ( ! has_action( 'shutdown', array( $this, 'sync_plugins_update_failed' ) ) ) { | |
| 188 | + add_action( 'shutdown', array( $this, 'sync_plugins_update_failed' ), 9 ); | |
| 189 | + } | |
| 154 | 190 | |
| 155 | 191 | return; |
| 156 | 192 | } |
| 157 | - /** | |
| 158 | - * Sync that a plugin update | |
| 159 | - * | |
| 160 | - * @since 1.6.3 | |
| 161 | - * @since-jetpack 5.8.0 | |
| 162 | - * | |
| 163 | - * @module sync | |
| 164 | - * | |
| 165 | - * @param array () $plugin, Plugin Data | |
| 166 | - */ | |
| 167 | - do_action( 'jetpack_plugins_updated', array_map( array( $this, 'get_plugin_info' ), $plugins ), $state ); | |
| 193 | + | |
| 194 | + $this->plugins_updated = array_map( array( $this, 'get_plugin_info' ), $plugins ); | |
| 195 | + add_action( 'shutdown', array( $this, 'sync_plugins_updated' ), 9 ); | |
| 196 | + | |
| 168 | 197 | break; |
| 169 | 198 | case 'install': |
| 170 | - } | |
| 199 | + // Accumulate installs and defer to shutdown. | |
| 200 | + $this->plugins_installed = array_merge( | |
| 201 | + $this->plugins_installed, | |
| 202 | + array_map( array( $this, 'get_plugin_info' ), $plugins ) | |
| 203 | + ); | |
| 204 | + if ( ! has_action( 'shutdown', array( $this, 'sync_plugins_installed' ) ) ) { | |
| 205 | + add_action( 'shutdown', array( $this, 'sync_plugins_installed' ), 9 ); | |
| 206 | + } | |
| 171 | 207 | |
| 172 | - if ( 'install' === $details['action'] ) { | |
| 173 | - /** | |
| 174 | - * Signals to the sync listener that a plugin was installed and a sync action | |
| 175 | - * reflecting the installation and the plugin info should be sent | |
| 176 | - * | |
| 177 | - * @since 1.6.3 | |
| 178 | - * @since-jetpack 5.8.0 | |
| 179 | - * | |
| 180 | - * @module sync | |
| 181 | - * | |
| 182 | - * @param array () $plugin, Plugin Data | |
| 183 | - */ | |
| 184 | - do_action( 'jetpack_plugin_installed', array_map( array( $this, 'get_plugin_info' ), $plugins ) ); | |
| 185 | - | |
| 186 | - return; | |
| 208 | + break; | |
| 187 | 209 | } |
| 188 | 210 | } |
| 189 | 211 | |
| 190 | 212 | /** |
| @@ -377,6 +399,75 @@ | ||
| 377 | 399 | $args[0], |
| 378 | 400 | $args[1], |
| 379 | 401 | $plugin_data, |
| 380 | 402 | ); |
| 403 | + } | |
| 404 | + | |
| 405 | + /** | |
| 406 | + * Helper method for firing the 'jetpack_plugins_updated' action on shutdown. | |
| 407 | + * | |
| 408 | + * @access public | |
| 409 | + */ | |
| 410 | + public function sync_plugins_updated() { | |
| 411 | + /** | |
| 412 | + * Sync that a plugin update | |
| 413 | + * | |
| 414 | + * @since 1.6.3 | |
| 415 | + * @since-jetpack 5.8.0 | |
| 416 | + * | |
| 417 | + * @module sync | |
| 418 | + * | |
| 419 | + * @param array () $plugin, Plugin Data | |
| 420 | + */ | |
| 421 | + do_action( 'jetpack_plugins_updated', $this->plugins_updated, $this->state ); | |
| 422 | + } | |
| 423 | + | |
| 424 | + /** | |
| 425 | + * Helper method for firing the 'jetpack_plugin_installed' action on shutdown. | |
| 426 | + * | |
| 427 | + * @access public | |
| 428 | + */ | |
| 429 | + public function sync_plugins_installed() { | |
| 430 | + if ( empty( $this->plugins_installed ) ) { | |
| 431 | + return; | |
| 432 | + } | |
| 433 | + /** | |
| 434 | + * Signals to the sync listener that a plugin was installed and a sync action | |
| 435 | + * reflecting the installation and the plugin info should be sent. | |
| 436 | + * | |
| 437 | + * @since 1.6.3 | |
| 438 | + * @since-jetpack 5.8.0 | |
| 439 | + * | |
| 440 | + * @module sync | |
| 441 | + * | |
| 442 | + * @param array () $plugin, Plugin Data | |
| 443 | + */ | |
| 444 | + do_action( 'jetpack_plugin_installed', $this->plugins_installed ); | |
| 445 | + } | |
| 446 | + | |
| 447 | + /** | |
| 448 | + * Helper method for firing the 'jetpack_plugin_update_failed' actions on shutdown. | |
| 449 | + * | |
| 450 | + * @access public | |
| 451 | + */ | |
| 452 | + public function sync_plugins_update_failed() { | |
| 453 | + if ( empty( $this->plugins_update_failures ) ) { | |
| 454 | + return; | |
| 455 | + } | |
| 456 | + foreach ( $this->plugins_update_failures as $failure ) { | |
| 457 | + /** | |
| 458 | + * Sync that a plugin update failed | |
| 459 | + * | |
| 460 | + * @since 1.6.3 | |
| 461 | + * @since-jetpack 5.8.0 | |
| 462 | + * | |
| 463 | + * @module sync | |
| 464 | + * | |
| 465 | + * @param array $plugin Plugin Data | |
| 466 | + * @param string $code Error code | |
| 467 | + * @param string $message Error message | |
| 468 | + * @param array $state State data | |
| 469 | + */ | |
| 470 | + do_action( 'jetpack_plugin_update_failed', $failure['plugin'], $failure['code'], $failure['message'], $failure['state'] ); | |
| 471 | + } | |
| 381 | 472 | } |
| 382 | 473 | } |