diff --git a/features/plugin-update.feature b/features/plugin-update.feature index c343a6c0..9c4e2d55 100644 --- a/features/plugin-update.feature +++ b/features/plugin-update.feature @@ -36,6 +36,46 @@ Feature: Update WordPress plugins | name | status | update | version | update_version | auto_update | | wordpress-importer | inactive | none | {UPDATE_VERSION} | | off | + @require-wp-5.5 + Scenario: Expose the files changed by a plugin update via a hook + Given a WP install + And a hook.php file: + """ + 0 ? 'files' : 'no files' ) ); + foreach ( $files as $file ) { + WP_CLI::log( 'Changed file: ' . $file ); + } + } + ); + """ + + When I run `wp plugin install wordpress-importer --version=0.5 --force` + Then STDOUT should not be empty + + When I run `wp plugin update wordpress-importer --require=hook.php` + Then STDOUT should contain: + """ + Hook fired with files + """ + And STDOUT should contain: + """ + Changed file: {PLUGIN_DIR}/wordpress-importer/wordpress-importer.php + """ + And STDOUT should not contain: + """ + Changed file: {PLUGIN_DIR}/wordpress-importer/readme.txt + """ + + When I run `wp plugin update wordpress-importer --require=hook.php` + Then STDOUT should not contain: + """ + Hook fired + """ + Scenario: Error when both --minor and --patch are provided Given a WP install diff --git a/features/theme-update.feature b/features/theme-update.feature index 2d0d1f64..56bd0c59 100644 --- a/features/theme-update.feature +++ b/features/theme-update.feature @@ -32,6 +32,32 @@ Feature: Update WordPress themes | name | version | | twentytwelve | 4.0 | + @require-wp-5.5 + Scenario: Expose the files changed by a theme update via a hook + Given a WP install + And an empty wp-content/themes directory + And a hook.php file: + """ + */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 5e19380f..9f3b5a3f 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 d2e55fc2..b1b08818 100755 --- a/src/WP_CLI/CommandWithUpgrade.php +++ b/src/WP_CLI/CommandWithUpgrade.php @@ -994,6 +994,7 @@ function ( $item ) { foreach ( $items_to_update as $item ) { $cache_manager->whitelist_package( $item['update_package'], $this->item_type, $item['name'], $item['update_version'] ); } + $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 ) { @@ -1016,6 +1017,16 @@ function ( $item ) { add_filter( 'site_transient_' . $this->upgrade_transient, $transient_filter, 999 ); $result = $upgrader->bulk_upgrade( wp_list_pluck( $items_to_update, 'update_id' ) ); remove_filter( 'site_transient_' . $this->upgrade_transient, $transient_filter, 999 ); + + // Fired before any errors are reported, as files might have changed even if some updates failed. + if ( $upgrader instanceof PluginUpgrader || $upgrader instanceof ThemeUpgrader ) { + WP_CLI::do_hook( "{$this->item_type}_update_finished", $upgrader->get_changed_files() ); + } + } + + if ( ! is_array( $result ) ) { + // This should never happen, but bulk_upgrade() can return false. + WP_CLI::error( 'Unable to connect to the filesystem.' ); } /** 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; + } +}