diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 0a51c466..809c0516 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -59,7 +59,7 @@ */src/WP_CLI/Fetchers/(Plugin|Theme)\.php$ */src/WP_CLI/CommandWithUpgrade\.php$ - */src/WP_CLI/(CommandWith|DestructivePlugin|DestructiveTheme)Upgrader\.php$ + */src/WP_CLI/(CommandWith|DestructivePlugin|DestructiveTheme|Plugin|Theme)Upgrader\.php$ */src/WP_CLI/Parse(Plugin|Theme)NameInput\.php$ */src/WP_CLI/ExtensionUpgraderSkin\.php$ diff --git a/src/Plugin_Command.php b/src/Plugin_Command.php index 00505984..fd723deb 100644 --- a/src/Plugin_Command.php +++ b/src/Plugin_Command.php @@ -78,7 +78,7 @@ public function __construct() { } protected function get_upgrader_class( $force ) { - return $force ? '\\WP_CLI\\DestructivePluginUpgrader' : 'Plugin_Upgrader'; + return $force ? '\\WP_CLI\\DestructivePluginUpgrader' : '\\WP_CLI\\PluginUpgrader'; } /** diff --git a/src/Theme_Command.php b/src/Theme_Command.php index 642a3157..d3129742 100644 --- a/src/Theme_Command.php +++ b/src/Theme_Command.php @@ -77,7 +77,7 @@ public function __construct() { } protected function get_upgrader_class( $force ) { - return $force ? '\\WP_CLI\\DestructiveThemeUpgrader' : 'Theme_Upgrader'; + return $force ? '\\WP_CLI\\DestructiveThemeUpgrader' : '\\WP_CLI\\ThemeUpgrader'; } /** diff --git a/src/WP_CLI/CommandWithUpgrade.php b/src/WP_CLI/CommandWithUpgrade.php index 4ad5032b..4300a98d 100755 --- a/src/WP_CLI/CommandWithUpgrade.php +++ b/src/WP_CLI/CommandWithUpgrade.php @@ -807,6 +807,10 @@ function ( $item ) { foreach ( $items_to_update as $item ) { $cache_manager->whitelist_package( $item['update_package'], $this->item_type, $item['name'], $item['update_version'] ); } + + /** + * @var ThemeUpgrader|PluginUpgrader $upgrader + */ $upgrader = $this->get_upgrader( $assoc_args ); // Ensure the upgrader uses the download offer present in each item. $transient_filter = function ( $transient ) use ( $items_to_update ) { @@ -831,6 +835,11 @@ function ( $item ) { remove_filter( 'site_transient_' . $this->upgrade_transient, $transient_filter, 999 ); } + if ( ! is_array( $result ) ) { + // This should never happen, but bulk_upgrade() can return false. + WP_CLI::error( 'Unable to connect to the filesystem.' ); + } + /** * @var array $items_to_update */ @@ -887,6 +896,10 @@ static function ( $result ) { if ( null !== $exclude ) { WP_CLI::log( "Skipped updates for: $exclude" ); } + + if ( isset( $upgrader ) ) { + WP_CLI::do_hook( "{$this->item_type}_update_finished", $upgrader->get_changed_files() ); + } } // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore -- Whitelisting to provide backward compatibility to classes possibly extending this class. diff --git a/src/WP_CLI/DestructivePluginUpgrader.php b/src/WP_CLI/DestructivePluginUpgrader.php index 5b3e1774..cfba6abb 100644 --- a/src/WP_CLI/DestructivePluginUpgrader.php +++ b/src/WP_CLI/DestructivePluginUpgrader.php @@ -5,7 +5,7 @@ /** * A plugin upgrader class that clears the destination directory. */ -class DestructivePluginUpgrader extends \Plugin_Upgrader { +class DestructivePluginUpgrader extends PluginUpgrader { public function install_package( $args = array() ) { parent::upgrade_strings(); // Needed for the 'remove_old' string. diff --git a/src/WP_CLI/DestructiveThemeUpgrader.php b/src/WP_CLI/DestructiveThemeUpgrader.php index 699d6dc0..0d89af7a 100644 --- a/src/WP_CLI/DestructiveThemeUpgrader.php +++ b/src/WP_CLI/DestructiveThemeUpgrader.php @@ -5,7 +5,7 @@ /** * A theme upgrader class that clears the destination directory. */ -class DestructiveThemeUpgrader extends \Theme_Upgrader { +class DestructiveThemeUpgrader extends ThemeUpgrader { public function install_package( $args = array() ) { parent::upgrade_strings(); // Needed for the 'remove_old' string. diff --git a/src/WP_CLI/PluginUpgrader.php b/src/WP_CLI/PluginUpgrader.php new file mode 100644 index 00000000..945f1820 --- /dev/null +++ b/src/WP_CLI/PluginUpgrader.php @@ -0,0 +1,44 @@ + + */ + private $changed_files = []; + + public function install_package( $args = array() ) { + $track_files = function ( $will_invalidate, $filepath ) { + $this->changed_files[] = $filepath; + return $will_invalidate; + }; + + // @phpstan-ignore WPCompat.filterNotAvailable.wpopcacheinvalidatefile + add_filter( 'wp_opcache_invalidate_file', $track_files, 10, 2 ); + + $result = parent::install_package( $args ); + + remove_filter( 'wp_opcache_invalidate_file', $track_files ); + + // Remove duplicates and sort files. + $this->changed_files = array_unique( $this->changed_files ); + sort( $this->changed_files ); + + return $result; + } + + /** + * Returns a list of files that were changed during the update process. + * + * @return array Changed files. + */ + public function get_changed_files() { + return $this->changed_files; + } +} diff --git a/src/WP_CLI/ThemeUpgrader.php b/src/WP_CLI/ThemeUpgrader.php new file mode 100644 index 00000000..54f9163d --- /dev/null +++ b/src/WP_CLI/ThemeUpgrader.php @@ -0,0 +1,44 @@ + + */ + private $changed_files = []; + + public function install_package( $args = array() ) { + $track_files = function ( $will_invalidate, $filepath ) { + $this->changed_files[] = $filepath; + return $will_invalidate; + }; + + // @phpstan-ignore WPCompat.filterNotAvailable.wpopcacheinvalidatefile + add_filter( 'wp_opcache_invalidate_file', $track_files, 10, 2 ); + + $result = parent::install_package( $args ); + + remove_filter( 'wp_opcache_invalidate_file', $track_files ); + + // Remove duplicates and sort files. + $this->changed_files = array_unique( $this->changed_files ); + sort( $this->changed_files ); + + return $result; + } + + /** + * Returns a list of files that were changed during the update process. + * + * @return array Changed files. + */ + public function get_changed_files() { + return $this->changed_files; + } +}