From 09314e6bdccd7542db5198a6393f740f7df1ab00 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 14 Feb 2026 22:34:28 +0000 Subject: [PATCH 01/14] Initial plan From 92852e2a274e92dfa3a64c3683ddf8feb68060d3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 14 Feb 2026 22:43:07 +0000 Subject: [PATCH 02/14] Add --show-changed-files flag for opcache invalidation support Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- src/Plugin_Command.php | 4 +++ src/Theme_Command.php | 4 +++ src/WP_CLI/CommandWithUpgrade.php | 37 +++++++++++++++++++-- src/WP_CLI/ExtensionUpgraderSkin.php | 49 ++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 3 deletions(-) diff --git a/src/Plugin_Command.php b/src/Plugin_Command.php index 5ce246aca..d6e61eba8 100644 --- a/src/Plugin_Command.php +++ b/src/Plugin_Command.php @@ -707,6 +707,10 @@ protected function install_from_repo( $slug, $assoc_args ) { * [--insecure] * : Retry downloads without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack. * + * [--show-changed-files] + * : Show the list of files that were changed during the update. + * : Useful for invalidating PHP-FPM opcache using external tools. + * * ## EXAMPLES * * $ wp plugin update bbpress --version=dev diff --git a/src/Theme_Command.php b/src/Theme_Command.php index e18b17e89..73b4c9b16 100644 --- a/src/Theme_Command.php +++ b/src/Theme_Command.php @@ -691,6 +691,10 @@ public function get( $args, $assoc_args ) { * [--insecure] * : Retry downloads without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack. * + * [--show-changed-files] + * : Show the list of files that were changed during the update. + * : Useful for invalidating PHP-FPM opcache using external tools. + * * ## EXAMPLES * * # Update multiple themes diff --git a/src/WP_CLI/CommandWithUpgrade.php b/src/WP_CLI/CommandWithUpgrade.php index 8326c168d..292659d7a 100755 --- a/src/WP_CLI/CommandWithUpgrade.php +++ b/src/WP_CLI/CommandWithUpgrade.php @@ -393,7 +393,7 @@ protected static function alter_api_response( $response, $version ) { } } - protected function get_upgrader( $assoc_args ) { + protected function get_upgrader( $assoc_args, $skin = null ) { $force = Utils\get_flag_value( $assoc_args, 'force', false ); $insecure = Utils\get_flag_value( $assoc_args, 'insecure', false ); $upgrader_class = $this->get_upgrader_class( $force ); @@ -404,7 +404,11 @@ protected function get_upgrader( $assoc_args ) { } } - return Utils\get_upgrader( $upgrader_class, $insecure, new ExtensionUpgraderSkin() ); + if ( null === $skin ) { + $skin = new ExtensionUpgraderSkin(); + } + + return Utils\get_upgrader( $upgrader_class, $insecure, $skin ); } protected function update_many( $args, $assoc_args ) { @@ -521,13 +525,24 @@ function ( $item ) { $result = array(); + // Check if file tracking is requested. + $show_changed_files = Utils\get_flag_value( $assoc_args, 'show-changed-files', false ); + $skin = null; + // Only attempt to update if there is something to update. if ( ! empty( $items_to_update ) ) { $cache_manager = WP_CLI::get_http_cache_manager(); 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 ); + + // Enable file tracking if requested. + if ( $show_changed_files ) { + $skin = new ExtensionUpgraderSkin(); + $skin->enable_file_tracking(); + } + + $upgrader = $this->get_upgrader( $assoc_args, $skin ); // Ensure the upgrader uses the download offer present in each item. $transient_filter = function ( $transient ) use ( $items_to_update ) { foreach ( $items_to_update as $name => $item_data ) { @@ -604,6 +619,22 @@ static function ( $result ) { if ( null !== $exclude ) { WP_CLI::log( "Skipped updates for: $exclude" ); } + + // Output changed files if requested. + if ( $show_changed_files && null !== $skin ) { + $changed_files = $skin->get_changed_files(); + if ( ! empty( $changed_files ) ) { + // Remove duplicates and sort files. + $changed_files = array_unique( $changed_files ); + sort( $changed_files ); + + WP_CLI::log( '' ); + WP_CLI::log( 'Changed files:' ); + foreach ( $changed_files as $file ) { + WP_CLI::log( $file ); + } + } + } } // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore -- Whitelisting to provide backward compatibility to classes possibly extending this class. diff --git a/src/WP_CLI/ExtensionUpgraderSkin.php b/src/WP_CLI/ExtensionUpgraderSkin.php index cef43ad27..c750ce5f1 100644 --- a/src/WP_CLI/ExtensionUpgraderSkin.php +++ b/src/WP_CLI/ExtensionUpgraderSkin.php @@ -14,6 +14,55 @@ */ class ExtensionUpgraderSkin extends UpgraderSkin { + /** + * List of files that were changed during the update process. + * + * @var array + */ + private $changed_files = []; + + /** + * Whether to track changed files. + * + * @var bool + */ + private $track_files = false; + + /** + * Enable file tracking for opcache invalidation. + */ + public function enable_file_tracking() { + $this->track_files = true; + $this->setup_file_tracking_hooks(); + } + + /** + * Get the list of changed files. + * + * @return array List of file paths. + */ + public function get_changed_files() { + return $this->changed_files; + } + + /** + * Setup hooks to track file changes during upgrade. + */ + private function setup_file_tracking_hooks() { + // Hook into wp_opcache_invalidate_file filter to track files being invalidated + add_filter( + 'wp_opcache_invalidate_file', + function ( $file ) { + if ( $this->track_files && ! empty( $file ) ) { + $this->changed_files[] = $file; + } + return $file; + }, + 10, + 1 + ); + } + /** * Called before an update is performed. */ From b024949bf4b482d03347c0ec44754dce21d0870d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 14 Feb 2026 22:44:39 +0000 Subject: [PATCH 03/14] Use upgrader_post_install hook for reliable file tracking Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- features/plugin-update.feature | 18 +++++++++++++ features/theme-update.feature | 18 +++++++++++++ src/WP_CLI/ExtensionUpgraderSkin.php | 38 +++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/features/plugin-update.feature b/features/plugin-update.feature index 1cf5e248a..66f5b8ac1 100644 --- a/features/plugin-update.feature +++ b/features/plugin-update.feature @@ -289,3 +289,21 @@ Feature: Update WordPress plugins """ Success: Updated 2 of 2 plugins. """ + + @require-wp-5.2 + Scenario: Show changed files when updating plugins + Given a WP install + + 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 --show-changed-files` + Then STDOUT should contain: + """ + Changed files: + """ + And STDOUT should contain: + """ + wordpress-importer + """ + diff --git a/features/theme-update.feature b/features/theme-update.feature index adc546b0b..8c68a2591 100644 --- a/features/theme-update.feature +++ b/features/theme-update.feature @@ -176,3 +176,21 @@ Feature: Update WordPress themes """ Success: Updated 2 of 2 themes. """ + + Scenario: Show changed files when updating themes + Given a WP install + And I run `wp theme delete --all --force` + + When I run `wp theme install twentytwelve --version=3.0` + Then STDOUT should not be empty + + When I run `wp theme update twentytwelve --show-changed-files` + Then STDOUT should contain: + """ + Changed files: + """ + And STDOUT should contain: + """ + twentytwelve + """ + diff --git a/src/WP_CLI/ExtensionUpgraderSkin.php b/src/WP_CLI/ExtensionUpgraderSkin.php index c750ce5f1..b21bf9294 100644 --- a/src/WP_CLI/ExtensionUpgraderSkin.php +++ b/src/WP_CLI/ExtensionUpgraderSkin.php @@ -49,7 +49,20 @@ public function get_changed_files() { * Setup hooks to track file changes during upgrade. */ private function setup_file_tracking_hooks() { - // Hook into wp_opcache_invalidate_file filter to track files being invalidated + // Hook into upgrader_post_install to capture the destination directory + add_filter( + 'upgrader_post_install', + function ( $response, $hook_extra, $result ) { + if ( $this->track_files && is_array( $result ) && isset( $result['destination'] ) && is_string( $result['destination'] ) ) { + $this->scan_directory_for_files( $result['destination'] ); + } + return $response; + }, + 10, + 3 + ); + + // Also hook into wp_opcache_invalidate_file filter to track files being invalidated add_filter( 'wp_opcache_invalidate_file', function ( $file ) { @@ -63,6 +76,29 @@ function ( $file ) { ); } + /** + * Recursively scan a directory and add all PHP files to the changed files list. + * + * @param string $dir Directory to scan. + */ + private function scan_directory_for_files( $dir ) { + if ( ! is_dir( $dir ) ) { + return; + } + + $iterator = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator( $dir, \RecursiveDirectoryIterator::SKIP_DOTS ), + \RecursiveIteratorIterator::SELF_FIRST + ); + + foreach ( $iterator as $file ) { + /** @var \SplFileInfo $file */ + if ( $file->isFile() && 'php' === $file->getExtension() ) { + $this->changed_files[] = $file->getPathname(); + } + } + } + /** * Called before an update is performed. */ From f6d816f14b47668acb6d3482603c9df2c0867c65 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 14 Feb 2026 22:45:24 +0000 Subject: [PATCH 04/14] Add examples for --show-changed-files flag usage Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- src/Plugin_Command.php | 9 +++++++++ src/Theme_Command.php | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Plugin_Command.php b/src/Plugin_Command.php index d6e61eba8..ca0804279 100644 --- a/src/Plugin_Command.php +++ b/src/Plugin_Command.php @@ -757,6 +757,15 @@ protected function install_from_repo( $slug, $assoc_args ) { * | nginx-cache-controller | 3.1.1 | 3.2.0 | Updated | * +------------------------+-------------+-------------+---------+ * + * # Show changed files for opcache invalidation + * $ wp plugin update akismet --show-changed-files + * Success: Updated 1 of 1 plugins. + * + * Changed files: + * /var/www/html/wp-content/plugins/akismet/akismet.php + * /var/www/html/wp-content/plugins/akismet/class.akismet.php + * ... + * * @alias upgrade */ public function update( $args, $assoc_args ) { diff --git a/src/Theme_Command.php b/src/Theme_Command.php index 73b4c9b16..8b34853bb 100644 --- a/src/Theme_Command.php +++ b/src/Theme_Command.php @@ -740,6 +740,15 @@ public function get( $args, $assoc_args ) { * # Update all themes * $ wp theme update --all * + * # Show changed files for opcache invalidation + * $ wp theme update twentytwelve --show-changed-files + * Success: Updated 1 of 1 themes. + * + * Changed files: + * /var/www/html/wp-content/themes/twentytwelve/style.php + * /var/www/html/wp-content/themes/twentytwelve/functions.php + * ... + * * @alias upgrade */ public function update( $args, $assoc_args ) { From 94b42b8dec2b344790b338af639d3e14ea40cb2d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 14 Feb 2026 22:46:37 +0000 Subject: [PATCH 05/14] Address code review feedback: remove redundant hook and fix example Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- src/Theme_Command.php | 2 +- src/WP_CLI/ExtensionUpgraderSkin.php | 13 ------------- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/src/Theme_Command.php b/src/Theme_Command.php index 8b34853bb..fa1461a7b 100644 --- a/src/Theme_Command.php +++ b/src/Theme_Command.php @@ -745,8 +745,8 @@ public function get( $args, $assoc_args ) { * Success: Updated 1 of 1 themes. * * Changed files: - * /var/www/html/wp-content/themes/twentytwelve/style.php * /var/www/html/wp-content/themes/twentytwelve/functions.php + * /var/www/html/wp-content/themes/twentytwelve/footer.php * ... * * @alias upgrade diff --git a/src/WP_CLI/ExtensionUpgraderSkin.php b/src/WP_CLI/ExtensionUpgraderSkin.php index b21bf9294..f852bfc04 100644 --- a/src/WP_CLI/ExtensionUpgraderSkin.php +++ b/src/WP_CLI/ExtensionUpgraderSkin.php @@ -61,19 +61,6 @@ function ( $response, $hook_extra, $result ) { 10, 3 ); - - // Also hook into wp_opcache_invalidate_file filter to track files being invalidated - add_filter( - 'wp_opcache_invalidate_file', - function ( $file ) { - if ( $this->track_files && ! empty( $file ) ) { - $this->changed_files[] = $file; - } - return $file; - }, - 10, - 1 - ); } /** From bbc3db71fb23c9cfad9a3aa97c860e7d94326658 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 14 Feb 2026 22:48:41 +0000 Subject: [PATCH 06/14] Add safeguards: prevent duplicate hooks, add error handling, clarify logic Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- src/WP_CLI/CommandWithUpgrade.php | 3 ++- src/WP_CLI/ExtensionUpgraderSkin.php | 36 +++++++++++++++++++++------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/WP_CLI/CommandWithUpgrade.php b/src/WP_CLI/CommandWithUpgrade.php index 292659d7a..657ba6195 100755 --- a/src/WP_CLI/CommandWithUpgrade.php +++ b/src/WP_CLI/CommandWithUpgrade.php @@ -527,7 +527,8 @@ function ( $item ) { // Check if file tracking is requested. $show_changed_files = Utils\get_flag_value( $assoc_args, 'show-changed-files', false ); - $skin = null; + // Skin is only created if file tracking is needed; otherwise get_upgrader creates default skin. + $skin = null; // Only attempt to update if there is something to update. if ( ! empty( $items_to_update ) ) { diff --git a/src/WP_CLI/ExtensionUpgraderSkin.php b/src/WP_CLI/ExtensionUpgraderSkin.php index f852bfc04..d5873d049 100644 --- a/src/WP_CLI/ExtensionUpgraderSkin.php +++ b/src/WP_CLI/ExtensionUpgraderSkin.php @@ -28,6 +28,13 @@ class ExtensionUpgraderSkin extends UpgraderSkin { */ private $track_files = false; + /** + * Whether file tracking hooks have been set up. + * + * @var bool + */ + private $hooks_setup = false; + /** * Enable file tracking for opcache invalidation. */ @@ -49,6 +56,13 @@ public function get_changed_files() { * Setup hooks to track file changes during upgrade. */ private function setup_file_tracking_hooks() { + // Prevent duplicate hook registrations. + if ( $this->hooks_setup ) { + return; + } + + $this->hooks_setup = true; + // Hook into upgrader_post_install to capture the destination directory add_filter( 'upgrader_post_install', @@ -73,16 +87,22 @@ private function scan_directory_for_files( $dir ) { return; } - $iterator = new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator( $dir, \RecursiveDirectoryIterator::SKIP_DOTS ), - \RecursiveIteratorIterator::SELF_FIRST - ); + try { + $iterator = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator( $dir, \RecursiveDirectoryIterator::SKIP_DOTS ), + \RecursiveIteratorIterator::SELF_FIRST + ); - foreach ( $iterator as $file ) { - /** @var \SplFileInfo $file */ - if ( $file->isFile() && 'php' === $file->getExtension() ) { - $this->changed_files[] = $file->getPathname(); + foreach ( $iterator as $file ) { + /** @var \SplFileInfo $file */ + if ( $file->isFile() && 'php' === $file->getExtension() ) { + $this->changed_files[] = $file->getPathname(); + } } + // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch -- Intentionally ignoring errors to prevent update failure. + } catch ( \Exception $e ) { + // Silently handle filesystem errors to prevent update process from failing. + // The changed files list may be incomplete, but the update should still succeed. } } From b0f85facb84f92a7e2b25bfdcce055ff04542057 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Sun, 15 Feb 2026 17:15:51 +0100 Subject: [PATCH 07/14] Fix synopsis --- src/Plugin_Command.php | 2 +- src/Theme_Command.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Plugin_Command.php b/src/Plugin_Command.php index ca0804279..cc6ff865f 100644 --- a/src/Plugin_Command.php +++ b/src/Plugin_Command.php @@ -709,7 +709,7 @@ protected function install_from_repo( $slug, $assoc_args ) { * * [--show-changed-files] * : Show the list of files that were changed during the update. - * : Useful for invalidating PHP-FPM opcache using external tools. + * Useful for invalidating PHP-FPM opcache using external tools. * * ## EXAMPLES * diff --git a/src/Theme_Command.php b/src/Theme_Command.php index fa1461a7b..62960d6ab 100644 --- a/src/Theme_Command.php +++ b/src/Theme_Command.php @@ -693,7 +693,7 @@ public function get( $args, $assoc_args ) { * * [--show-changed-files] * : Show the list of files that were changed during the update. - * : Useful for invalidating PHP-FPM opcache using external tools. + * Useful for invalidating PHP-FPM opcache using external tools. * * ## EXAMPLES * From e04f2534b6e93df1ff202282b08c10d29b3f9bec Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Sun, 15 Feb 2026 17:40:08 +0100 Subject: [PATCH 08/14] Use `wp_opcache_invalidate_file` filter instead --- phpcs.xml.dist | 2 +- src/Plugin_Command.php | 2 +- src/Theme_Command.php | 2 +- src/WP_CLI/CommandWithUpgrade.php | 32 +++------ src/WP_CLI/DestructiveThemeUpgrader.php | 2 +- src/WP_CLI/ExtensionUpgraderSkin.php | 92 ------------------------- src/WP_CLI/PluginUpgrader.php | 45 ++++++++++++ src/WP_CLI/ThemeUpgrader.php | 45 ++++++++++++ 8 files changed, 102 insertions(+), 120 deletions(-) create mode 100644 src/WP_CLI/PluginUpgrader.php create mode 100644 src/WP_CLI/ThemeUpgrader.php diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 96cef65a9..c78b63b53 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 cc6ff865f..10773a310 100644 --- a/src/Plugin_Command.php +++ b/src/Plugin_Command.php @@ -79,7 +79,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 62960d6ab..9d4392d74 100644 --- a/src/Theme_Command.php +++ b/src/Theme_Command.php @@ -76,7 +76,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 657ba6195..bb48e6e86 100755 --- a/src/WP_CLI/CommandWithUpgrade.php +++ b/src/WP_CLI/CommandWithUpgrade.php @@ -393,7 +393,7 @@ protected static function alter_api_response( $response, $version ) { } } - protected function get_upgrader( $assoc_args, $skin = null ) { + protected function get_upgrader( $assoc_args ) { $force = Utils\get_flag_value( $assoc_args, 'force', false ); $insecure = Utils\get_flag_value( $assoc_args, 'insecure', false ); $upgrader_class = $this->get_upgrader_class( $force ); @@ -404,11 +404,7 @@ protected function get_upgrader( $assoc_args, $skin = null ) { } } - if ( null === $skin ) { - $skin = new ExtensionUpgraderSkin(); - } - - return Utils\get_upgrader( $upgrader_class, $insecure, $skin ); + return Utils\get_upgrader( $upgrader_class, $insecure, new ExtensionUpgraderSkin() ); } protected function update_many( $args, $assoc_args ) { @@ -525,11 +521,6 @@ function ( $item ) { $result = array(); - // Check if file tracking is requested. - $show_changed_files = Utils\get_flag_value( $assoc_args, 'show-changed-files', false ); - // Skin is only created if file tracking is needed; otherwise get_upgrader creates default skin. - $skin = null; - // Only attempt to update if there is something to update. if ( ! empty( $items_to_update ) ) { $cache_manager = WP_CLI::get_http_cache_manager(); @@ -537,13 +528,10 @@ function ( $item ) { $cache_manager->whitelist_package( $item['update_package'], $this->item_type, $item['name'], $item['update_version'] ); } - // Enable file tracking if requested. - if ( $show_changed_files ) { - $skin = new ExtensionUpgraderSkin(); - $skin->enable_file_tracking(); - } - - $upgrader = $this->get_upgrader( $assoc_args, $skin ); + /** + * @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 ) { foreach ( $items_to_update as $name => $item_data ) { @@ -622,13 +610,9 @@ static function ( $result ) { } // Output changed files if requested. - if ( $show_changed_files && null !== $skin ) { - $changed_files = $skin->get_changed_files(); + if ( Utils\get_flag_value( $assoc_args, 'show-changed-files' ) ) { + $changed_files = $upgrader->get_changed_files(); if ( ! empty( $changed_files ) ) { - // Remove duplicates and sort files. - $changed_files = array_unique( $changed_files ); - sort( $changed_files ); - WP_CLI::log( '' ); WP_CLI::log( 'Changed files:' ); foreach ( $changed_files as $file ) { diff --git a/src/WP_CLI/DestructiveThemeUpgrader.php b/src/WP_CLI/DestructiveThemeUpgrader.php index 699d6dc0e..0d89af7a1 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/ExtensionUpgraderSkin.php b/src/WP_CLI/ExtensionUpgraderSkin.php index d5873d049..cef43ad27 100644 --- a/src/WP_CLI/ExtensionUpgraderSkin.php +++ b/src/WP_CLI/ExtensionUpgraderSkin.php @@ -14,98 +14,6 @@ */ class ExtensionUpgraderSkin extends UpgraderSkin { - /** - * List of files that were changed during the update process. - * - * @var array - */ - private $changed_files = []; - - /** - * Whether to track changed files. - * - * @var bool - */ - private $track_files = false; - - /** - * Whether file tracking hooks have been set up. - * - * @var bool - */ - private $hooks_setup = false; - - /** - * Enable file tracking for opcache invalidation. - */ - public function enable_file_tracking() { - $this->track_files = true; - $this->setup_file_tracking_hooks(); - } - - /** - * Get the list of changed files. - * - * @return array List of file paths. - */ - public function get_changed_files() { - return $this->changed_files; - } - - /** - * Setup hooks to track file changes during upgrade. - */ - private function setup_file_tracking_hooks() { - // Prevent duplicate hook registrations. - if ( $this->hooks_setup ) { - return; - } - - $this->hooks_setup = true; - - // Hook into upgrader_post_install to capture the destination directory - add_filter( - 'upgrader_post_install', - function ( $response, $hook_extra, $result ) { - if ( $this->track_files && is_array( $result ) && isset( $result['destination'] ) && is_string( $result['destination'] ) ) { - $this->scan_directory_for_files( $result['destination'] ); - } - return $response; - }, - 10, - 3 - ); - } - - /** - * Recursively scan a directory and add all PHP files to the changed files list. - * - * @param string $dir Directory to scan. - */ - private function scan_directory_for_files( $dir ) { - if ( ! is_dir( $dir ) ) { - return; - } - - try { - $iterator = new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator( $dir, \RecursiveDirectoryIterator::SKIP_DOTS ), - \RecursiveIteratorIterator::SELF_FIRST - ); - - foreach ( $iterator as $file ) { - /** @var \SplFileInfo $file */ - if ( $file->isFile() && 'php' === $file->getExtension() ) { - $this->changed_files[] = $file->getPathname(); - } - } - // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch -- Intentionally ignoring errors to prevent update failure. - } catch ( \Exception $e ) { - // Silently handle filesystem errors to prevent update process from failing. - // The changed files list may be incomplete, but the update should still succeed. - } - } - /** * Called before an update is performed. */ diff --git a/src/WP_CLI/PluginUpgrader.php b/src/WP_CLI/PluginUpgrader.php new file mode 100644 index 000000000..45fa3cf44 --- /dev/null +++ b/src/WP_CLI/PluginUpgrader.php @@ -0,0 +1,45 @@ + + */ + private $changed_files = []; + + public function install_package( $args = array() ) { + parent::upgrade_strings(); // Needed for the 'remove_old' string. + + $track_files = function ( $will_invalidate, $filepath ) { + $this->changed_files[] = $filepath; + return $will_invalidate; + }; + + 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 000000000..e4a1ac4ae --- /dev/null +++ b/src/WP_CLI/ThemeUpgrader.php @@ -0,0 +1,45 @@ + + */ + private $changed_files = []; + + public function install_package( $args = array() ) { + parent::upgrade_strings(); // Needed for the 'remove_old' string. + + $track_files = function ( $will_invalidate, $filepath ) { + $this->changed_files[] = $filepath; + return $will_invalidate; + }; + + 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; + } +} From ece98b3ac7dfaa9fca406f3eb3cc211b2ae5ddc9 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 10 Mar 2026 16:39:10 +0100 Subject: [PATCH 09/14] extend PluginUpgrader --- src/WP_CLI/DestructivePluginUpgrader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WP_CLI/DestructivePluginUpgrader.php b/src/WP_CLI/DestructivePluginUpgrader.php index 5b3e17746..cfba6abba 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. From ae13d9c9564f87749e94ca36ec39ff53df9a3a35 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 10 Mar 2026 16:42:36 +0100 Subject: [PATCH 10/14] Remove flag in favor of hook --- features/plugin-update.feature | 18 ------------------ features/theme-update.feature | 18 ------------------ src/Plugin_Command.php | 13 ------------- src/Theme_Command.php | 13 ------------- src/WP_CLI/CommandWithUpgrade.php | 12 +----------- 5 files changed, 1 insertion(+), 73 deletions(-) diff --git a/features/plugin-update.feature b/features/plugin-update.feature index 66f5b8ac1..1cf5e248a 100644 --- a/features/plugin-update.feature +++ b/features/plugin-update.feature @@ -289,21 +289,3 @@ Feature: Update WordPress plugins """ Success: Updated 2 of 2 plugins. """ - - @require-wp-5.2 - Scenario: Show changed files when updating plugins - Given a WP install - - 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 --show-changed-files` - Then STDOUT should contain: - """ - Changed files: - """ - And STDOUT should contain: - """ - wordpress-importer - """ - diff --git a/features/theme-update.feature b/features/theme-update.feature index 8c68a2591..adc546b0b 100644 --- a/features/theme-update.feature +++ b/features/theme-update.feature @@ -176,21 +176,3 @@ Feature: Update WordPress themes """ Success: Updated 2 of 2 themes. """ - - Scenario: Show changed files when updating themes - Given a WP install - And I run `wp theme delete --all --force` - - When I run `wp theme install twentytwelve --version=3.0` - Then STDOUT should not be empty - - When I run `wp theme update twentytwelve --show-changed-files` - Then STDOUT should contain: - """ - Changed files: - """ - And STDOUT should contain: - """ - twentytwelve - """ - diff --git a/src/Plugin_Command.php b/src/Plugin_Command.php index 10773a310..39c0e762e 100644 --- a/src/Plugin_Command.php +++ b/src/Plugin_Command.php @@ -707,10 +707,6 @@ protected function install_from_repo( $slug, $assoc_args ) { * [--insecure] * : Retry downloads without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack. * - * [--show-changed-files] - * : Show the list of files that were changed during the update. - * Useful for invalidating PHP-FPM opcache using external tools. - * * ## EXAMPLES * * $ wp plugin update bbpress --version=dev @@ -757,15 +753,6 @@ protected function install_from_repo( $slug, $assoc_args ) { * | nginx-cache-controller | 3.1.1 | 3.2.0 | Updated | * +------------------------+-------------+-------------+---------+ * - * # Show changed files for opcache invalidation - * $ wp plugin update akismet --show-changed-files - * Success: Updated 1 of 1 plugins. - * - * Changed files: - * /var/www/html/wp-content/plugins/akismet/akismet.php - * /var/www/html/wp-content/plugins/akismet/class.akismet.php - * ... - * * @alias upgrade */ public function update( $args, $assoc_args ) { diff --git a/src/Theme_Command.php b/src/Theme_Command.php index 9d4392d74..f660581f8 100644 --- a/src/Theme_Command.php +++ b/src/Theme_Command.php @@ -691,10 +691,6 @@ public function get( $args, $assoc_args ) { * [--insecure] * : Retry downloads without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack. * - * [--show-changed-files] - * : Show the list of files that were changed during the update. - * Useful for invalidating PHP-FPM opcache using external tools. - * * ## EXAMPLES * * # Update multiple themes @@ -740,15 +736,6 @@ public function get( $args, $assoc_args ) { * # Update all themes * $ wp theme update --all * - * # Show changed files for opcache invalidation - * $ wp theme update twentytwelve --show-changed-files - * Success: Updated 1 of 1 themes. - * - * Changed files: - * /var/www/html/wp-content/themes/twentytwelve/functions.php - * /var/www/html/wp-content/themes/twentytwelve/footer.php - * ... - * * @alias upgrade */ public function update( $args, $assoc_args ) { diff --git a/src/WP_CLI/CommandWithUpgrade.php b/src/WP_CLI/CommandWithUpgrade.php index bb48e6e86..362ab2313 100755 --- a/src/WP_CLI/CommandWithUpgrade.php +++ b/src/WP_CLI/CommandWithUpgrade.php @@ -609,17 +609,7 @@ static function ( $result ) { WP_CLI::log( "Skipped updates for: $exclude" ); } - // Output changed files if requested. - if ( Utils\get_flag_value( $assoc_args, 'show-changed-files' ) ) { - $changed_files = $upgrader->get_changed_files(); - if ( ! empty( $changed_files ) ) { - WP_CLI::log( '' ); - WP_CLI::log( 'Changed files:' ); - foreach ( $changed_files as $file ) { - WP_CLI::log( $file ); - } - } - } + 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. From ee7552520087df90432cab1ac44d3c2ec7824df6 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 10 Mar 2026 17:03:40 +0100 Subject: [PATCH 11/14] PHPStan fixes --- src/WP_CLI/CommandWithUpgrade.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/WP_CLI/CommandWithUpgrade.php b/src/WP_CLI/CommandWithUpgrade.php index 362ab2313..47dcc148c 100755 --- a/src/WP_CLI/CommandWithUpgrade.php +++ b/src/WP_CLI/CommandWithUpgrade.php @@ -555,6 +555,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( 'Plugin upgrader is unable to connect to the filesystem.' ); + } + /** * @var array $items_to_update */ @@ -609,7 +614,9 @@ static function ( $result ) { WP_CLI::log( "Skipped updates for: $exclude" ); } - WP_CLI::do_hook( "{$this->item_type}_update_finished", $upgrader->get_changed_files() ); + 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. From a092d5646c0543733b312eed9bd5e8ebf8362131 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 10 Mar 2026 17:17:37 +0100 Subject: [PATCH 12/14] Tweaks --- src/WP_CLI/CommandWithUpgrade.php | 2 +- src/WP_CLI/PluginUpgrader.php | 2 -- src/WP_CLI/ThemeUpgrader.php | 2 -- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/WP_CLI/CommandWithUpgrade.php b/src/WP_CLI/CommandWithUpgrade.php index 47dcc148c..f45763e0b 100755 --- a/src/WP_CLI/CommandWithUpgrade.php +++ b/src/WP_CLI/CommandWithUpgrade.php @@ -557,7 +557,7 @@ function ( $item ) { if ( ! is_array( $result ) ) { // This should never happen, but bulk_upgrade() can return false. - WP_CLI::error( 'Plugin upgrader is unable to connect to the filesystem.' ); + WP_CLI::error( 'Unable to connect to the filesystem.' ); } /** diff --git a/src/WP_CLI/PluginUpgrader.php b/src/WP_CLI/PluginUpgrader.php index 45fa3cf44..1d8d1237d 100644 --- a/src/WP_CLI/PluginUpgrader.php +++ b/src/WP_CLI/PluginUpgrader.php @@ -14,8 +14,6 @@ class PluginUpgrader extends \Plugin_Upgrader { private $changed_files = []; public function install_package( $args = array() ) { - parent::upgrade_strings(); // Needed for the 'remove_old' string. - $track_files = function ( $will_invalidate, $filepath ) { $this->changed_files[] = $filepath; return $will_invalidate; diff --git a/src/WP_CLI/ThemeUpgrader.php b/src/WP_CLI/ThemeUpgrader.php index e4a1ac4ae..cbdf84067 100644 --- a/src/WP_CLI/ThemeUpgrader.php +++ b/src/WP_CLI/ThemeUpgrader.php @@ -14,8 +14,6 @@ class ThemeUpgrader extends \Theme_Upgrader { private $changed_files = []; public function install_package( $args = array() ) { - parent::upgrade_strings(); // Needed for the 'remove_old' string. - $track_files = function ( $will_invalidate, $filepath ) { $this->changed_files[] = $filepath; return $will_invalidate; From 2adc4ba4a4c489d54e57854855d96262bf79f719 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 1 Sep 2026 10:28:38 +0200 Subject: [PATCH 13/14] Ignore PHPStan errors for filter usage --- src/WP_CLI/PluginUpgrader.php | 1 + src/WP_CLI/ThemeUpgrader.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/WP_CLI/PluginUpgrader.php b/src/WP_CLI/PluginUpgrader.php index 1d8d1237d..945f18205 100644 --- a/src/WP_CLI/PluginUpgrader.php +++ b/src/WP_CLI/PluginUpgrader.php @@ -19,6 +19,7 @@ public function install_package( $args = array() ) { return $will_invalidate; }; + // @phpstan-ignore WPCompat.filterNotAvailable.wpopcacheinvalidatefile add_filter( 'wp_opcache_invalidate_file', $track_files, 10, 2 ); $result = parent::install_package( $args ); diff --git a/src/WP_CLI/ThemeUpgrader.php b/src/WP_CLI/ThemeUpgrader.php index cbdf84067..54f9163d5 100644 --- a/src/WP_CLI/ThemeUpgrader.php +++ b/src/WP_CLI/ThemeUpgrader.php @@ -19,6 +19,7 @@ public function install_package( $args = array() ) { return $will_invalidate; }; + // @phpstan-ignore WPCompat.filterNotAvailable.wpopcacheinvalidatefile add_filter( 'wp_opcache_invalidate_file', $track_files, 10, 2 ); $result = parent::install_package( $args ); From f84c2cd9f4d239fe936d1a44e14ecc8c2c01dc00 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 24 Nov 2026 10:33:02 +0000 Subject: [PATCH 14/14] Fire the update hook before errors are reported, and add tests Failed updates made `update_many()` exit before the hook was fired, even though files of the other items had changed. The hook now fires right after the bulk upgrade, and only when the upgrader tracks changed files, so that subclasses using another upgrader class keep working. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx --- features/plugin-update.feature | 40 +++++++++++++++++++++++++++++++ features/theme-update.feature | 26 ++++++++++++++++++++ src/WP_CLI/CommandWithUpgrade.php | 12 ++++------ 3 files changed, 71 insertions(+), 7 deletions(-) diff --git a/features/plugin-update.feature b/features/plugin-update.feature index c343a6c09..9c4e2d559 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 2d0d1f648..56bd0c599 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: + """ + 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 ) { @@ -1020,6 +1017,11 @@ 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 ) ) { @@ -1083,10 +1085,6 @@ 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.