Skip to content

Commit

Permalink
Jetpack Sync: Move 'jetpack_plugins_updated' action to shutdown (#39717)
Browse files Browse the repository at this point in the history
* Jetpack Sync: Move 'jetpack_plugins_updated' action to shutdown
  • Loading branch information
fgiannar authored Oct 11, 2024
1 parent ec73b90 commit eabf17c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Jetpack Sync: Move 'jetpack_plugins_updated' action to shutdown to decrease its associated lag


73 changes: 49 additions & 24 deletions projects/packages/sync/src/modules/class-plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ class Plugins extends Module {
*/
private $plugins = array();

/**
* List of all updated plugins.
*
* @access private
*
* @var array
*/
private $plugins_updated = array();

/**
* State
*
* @access private
*
* @var array
*/
private $state = array();

/**
* Sync module name.
*
Expand Down Expand Up @@ -131,10 +149,10 @@ public function on_upgrader_completion( $upgrader, $details ) {

switch ( $details['action'] ) {
case 'update':
$state = array(
$this->state = array(
'is_autoupdate' => Jetpack_Constants::is_true( 'JETPACK_PLUGIN_AUTOUPDATE' ),
);
$errors = $this->get_errors( $upgrader->skin );
$errors = $this->get_errors( $upgrader->skin );
if ( $errors ) {
foreach ( $plugins as $slug ) {
/**
Expand All @@ -149,13 +167,20 @@ public function on_upgrader_completion( $upgrader, $details ) {
* @param string Error code
* @param string Error message
*/
do_action( 'jetpack_plugin_update_failed', $this->get_plugin_info( $slug ), $errors['code'], $errors['message'], $state );
do_action( 'jetpack_plugin_update_failed', $this->get_plugin_info( $slug ), $errors['code'], $errors['message'], $this->state );
}

return;
}

$this->plugins_updated = array_map( array( $this, 'get_plugin_info' ), $plugins );
add_action( 'shutdown', array( $this, 'sync_plugins_updated' ), 9 );

break;
case 'install':
/**
* Sync that a plugin update
* Signals to the sync listener that a plugin was installed and a sync action
* reflecting the installation and the plugin info should be sent
*
* @since 1.6.3
* @since-jetpack 5.8.0
Expand All @@ -164,26 +189,7 @@ public function on_upgrader_completion( $upgrader, $details ) {
*
* @param array () $plugin, Plugin Data
*/
do_action( 'jetpack_plugins_updated', array_map( array( $this, 'get_plugin_info' ), $plugins ), $state );
break;
case 'install':
}

if ( 'install' === $details['action'] ) {
/**
* Signals to the sync listener that a plugin was installed and a sync action
* reflecting the installation and the plugin info should be sent
*
* @since 1.6.3
* @since-jetpack 5.8.0
*
* @module sync
*
* @param array () $plugin, Plugin Data
*/
do_action( 'jetpack_plugin_installed', array_map( array( $this, 'get_plugin_info' ), $plugins ) );

return;
do_action( 'jetpack_plugin_installed', array_map( array( $this, 'get_plugin_info' ), $plugins ) );
}
}

Expand Down Expand Up @@ -379,4 +385,23 @@ public function expand_plugin_data( $args ) {
$plugin_data,
);
}

/**
* Helper method for firing the 'jetpack_plugins_updated' action on shutdown.
*
* @access public
*/
public function sync_plugins_updated() {
/**
* Sync that a plugin update
*
* @since 1.6.3
* @since-jetpack 5.8.0
*
* @module sync
*
* @param array () $plugin, Plugin Data
*/
do_action( 'jetpack_plugins_updated', $this->plugins_updated, $this->state );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Jetpack plugin: Update Sync related unit tests


Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Automattic\Jetpack\Constants;
use Automattic\Jetpack\Sync\Modules;

require_once __DIR__ . '/test_class.jetpack-sync-plugins.php';
require_once __DIR__ . '/class.silent-upgrader-skin.php';
Expand Down Expand Up @@ -50,18 +51,28 @@ public static function tear_down_after_class() {

public function test_updating_a_plugin_is_synced() {
$this->update_the_plugin( new Silent_Upgrader_Skin() );
$plugins_module = Modules::get_module( 'plugins' );
'@phan-var \Automattic\Jetpack\Sync\Modules\Plugins $plugins_module';
$plugins_module->sync_plugins_updated();
$has_action = has_action( 'shutdown', array( $plugins_module, 'sync_plugins_updated' ) );
$this->sender->do_sync();
$updated_plugin = $this->server_event_storage->get_most_recent_event( 'jetpack_plugins_updated' );

$this->assertEquals( 'the/the.php', $updated_plugin->args[0][0]['slug'] );
$this->assertTrue( (bool) $has_action );
$this->server_event_storage->reset();
}

public function test_updating_plugin_in_bulk_is_synced() {
$this->update_bulk_plugins( new Silent_Upgrader_Skin() );
$plugins_module = Modules::get_module( 'plugins' );
'@phan-var \Automattic\Jetpack\Sync\Modules\Plugins $plugins_module';
$plugins_module->sync_plugins_updated();
$has_action = has_action( 'shutdown', array( $plugins_module, 'sync_plugins_updated' ) );
$this->sender->do_sync();
$updated_plugin = $this->server_event_storage->get_most_recent_event( 'jetpack_plugins_updated' );
$this->assertEquals( 'the/the.php', $updated_plugin->args[0][0]['slug'] );
$this->assertTrue( (bool) $has_action );
$this->server_event_storage->reset();
}

Expand Down Expand Up @@ -121,9 +132,14 @@ public function test_updating_error_with_autoupdate_constant_results_in_proper_s
public function test_updating_with_autoupdate_constant_results_in_proper_state() {
Constants::set_constant( 'JETPACK_PLUGIN_AUTOUPDATE', true );
$this->update_bulk_plugins( new Silent_Upgrader_Skin() );
$plugins_module = Modules::get_module( 'plugins' );
'@phan-var \Automattic\Jetpack\Sync\Modules\Plugins $plugins_module';
$plugins_module->sync_plugins_updated();
$has_action = has_action( 'shutdown', array( $plugins_module, 'sync_plugins_updated' ) );
$this->sender->do_sync();
$updated_plugin = $this->server_event_storage->get_most_recent_event( 'jetpack_plugins_updated' );
$this->assertTrue( $updated_plugin->args[1]['is_autoupdate'] );
$this->assertTrue( (bool) $has_action );
$this->server_event_storage->reset();
}

Expand Down

0 comments on commit eabf17c

Please sign in to comment.