Skip to content

Commit f84c2cd

Browse files
swissspidyclaude
andcommitted
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent b812100 commit f84c2cd

3 files changed

Lines changed: 71 additions & 7 deletions

File tree

‎features/plugin-update.feature‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,46 @@ Feature: Update WordPress plugins
3636
| name | status | update | version | update_version | auto_update |
3737
| wordpress-importer | inactive | none | {UPDATE_VERSION} | | off |
3838

39+
@require-wp-5.5
40+
Scenario: Expose the files changed by a plugin update via a hook
41+
Given a WP install
42+
And a hook.php file:
43+
"""
44+
<?php
45+
WP_CLI::add_hook(
46+
'plugin_update_finished',
47+
static function ( $files ) {
48+
WP_CLI::log( 'Hook fired with ' . ( count( $files ) > 0 ? 'files' : 'no files' ) );
49+
foreach ( $files as $file ) {
50+
WP_CLI::log( 'Changed file: ' . $file );
51+
}
52+
}
53+
);
54+
"""
55+
56+
When I run `wp plugin install wordpress-importer --version=0.5 --force`
57+
Then STDOUT should not be empty
58+
59+
When I run `wp plugin update wordpress-importer --require=hook.php`
60+
Then STDOUT should contain:
61+
"""
62+
Hook fired with files
63+
"""
64+
And STDOUT should contain:
65+
"""
66+
Changed file: {PLUGIN_DIR}/wordpress-importer/wordpress-importer.php
67+
"""
68+
And STDOUT should not contain:
69+
"""
70+
Changed file: {PLUGIN_DIR}/wordpress-importer/readme.txt
71+
"""
72+
73+
When I run `wp plugin update wordpress-importer --require=hook.php`
74+
Then STDOUT should not contain:
75+
"""
76+
Hook fired
77+
"""
78+
3979
Scenario: Error when both --minor and --patch are provided
4080
Given a WP install
4181

‎features/theme-update.feature‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,32 @@ Feature: Update WordPress themes
3232
| name | version |
3333
| twentytwelve | 4.0 |
3434

35+
@require-wp-5.5
36+
Scenario: Expose the files changed by a theme update via a hook
37+
Given a WP install
38+
And an empty wp-content/themes directory
39+
And a hook.php file:
40+
"""
41+
<?php
42+
WP_CLI::add_hook(
43+
'theme_update_finished',
44+
static function ( $files ) {
45+
foreach ( $files as $file ) {
46+
WP_CLI::log( 'Changed file: ' . $file );
47+
}
48+
}
49+
);
50+
"""
51+
52+
When I run `wp theme install twentytwelve --version=3.0`
53+
Then STDOUT should not be empty
54+
55+
When I run `wp theme update twentytwelve --require=hook.php`
56+
Then STDOUT should contain:
57+
"""
58+
Changed file: {THEME_DIR}/twentytwelve/functions.php
59+
"""
60+
3561
@require-wp-4.5
3662
Scenario: Not giving a slug on update should throw an error unless --all given
3763
Given a WP install

‎src/WP_CLI/CommandWithUpgrade.php‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -995,9 +995,6 @@ function ( $item ) {
995995
$cache_manager->whitelist_package( $item['update_package'], $this->item_type, $item['name'], $item['update_version'] );
996996
}
997997

998-
/**
999-
* @var ThemeUpgrader|PluginUpgrader $upgrader
1000-
*/
1001998
$upgrader = $this->get_upgrader( $assoc_args );
1002999
// Ensure the upgrader uses the download offer present in each item.
10031000
$transient_filter = function ( $transient ) use ( $items_to_update ) {
@@ -1020,6 +1017,11 @@ function ( $item ) {
10201017
add_filter( 'site_transient_' . $this->upgrade_transient, $transient_filter, 999 );
10211018
$result = $upgrader->bulk_upgrade( wp_list_pluck( $items_to_update, 'update_id' ) );
10221019
remove_filter( 'site_transient_' . $this->upgrade_transient, $transient_filter, 999 );
1020+
1021+
// Fired before any errors are reported, as files might have changed even if some updates failed.
1022+
if ( $upgrader instanceof PluginUpgrader || $upgrader instanceof ThemeUpgrader ) {
1023+
WP_CLI::do_hook( "{$this->item_type}_update_finished", $upgrader->get_changed_files() );
1024+
}
10231025
}
10241026

10251027
if ( ! is_array( $result ) ) {
@@ -1083,10 +1085,6 @@ static function ( $result ) {
10831085
if ( null !== $exclude ) {
10841086
WP_CLI::log( "Skipped updates for: $exclude" );
10851087
}
1086-
1087-
if ( isset( $upgrader ) ) {
1088-
WP_CLI::do_hook( "{$this->item_type}_update_finished", $upgrader->get_changed_files() );
1089-
}
10901088
}
10911089

10921090
// phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore -- Whitelisting to provide backward compatibility to classes possibly extending this class.

0 commit comments

Comments
 (0)