Skip to content

Commit eff3281

Browse files
Copilotswissspidy
andauthored
Add hook to expose changed files for OPCache invalidation (#501)
Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
1 parent ac1fd68 commit eff3281

10 files changed

Lines changed: 170 additions & 5 deletions

‎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

‎phpcs.xml.dist‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound">
6060
<exclude-pattern>*/src/WP_CLI/Fetchers/(Plugin|Theme)\.php$</exclude-pattern>
6161
<exclude-pattern>*/src/WP_CLI/CommandWithUpgrade\.php$</exclude-pattern>
62-
<exclude-pattern>*/src/WP_CLI/(CommandWith|DestructivePlugin|DestructiveTheme)Upgrader\.php$</exclude-pattern>
62+
<exclude-pattern>*/src/WP_CLI/(CommandWith|DestructivePlugin|DestructiveTheme|Plugin|Theme)Upgrader\.php$</exclude-pattern>
6363
<exclude-pattern>*/src/WP_CLI/Parse(Plugin|Theme)NameInput\.php$</exclude-pattern>
6464
<exclude-pattern>*/src/WP_CLI/ExtensionUpgraderSkin\.php$</exclude-pattern>
6565
</rule>

‎src/Plugin_Command.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function __construct() {
7878
}
7979

8080
protected function get_upgrader_class( $force ) {
81-
return $force ? '\\WP_CLI\\DestructivePluginUpgrader' : 'Plugin_Upgrader';
81+
return $force ? '\\WP_CLI\\DestructivePluginUpgrader' : '\\WP_CLI\\PluginUpgrader';
8282
}
8383

8484
/**

‎src/Theme_Command.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function __construct() {
7777
}
7878

7979
protected function get_upgrader_class( $force ) {
80-
return $force ? '\\WP_CLI\\DestructiveThemeUpgrader' : 'Theme_Upgrader';
80+
return $force ? '\\WP_CLI\\DestructiveThemeUpgrader' : '\\WP_CLI\\ThemeUpgrader';
8181
}
8282

8383
/**

‎src/WP_CLI/CommandWithUpgrade.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,7 @@ function ( $item ) {
994994
foreach ( $items_to_update as $item ) {
995995
$cache_manager->whitelist_package( $item['update_package'], $this->item_type, $item['name'], $item['update_version'] );
996996
}
997+
997998
$upgrader = $this->get_upgrader( $assoc_args );
998999
// Ensure the upgrader uses the download offer present in each item.
9991000
$transient_filter = function ( $transient ) use ( $items_to_update ) {
@@ -1016,6 +1017,16 @@ function ( $item ) {
10161017
add_filter( 'site_transient_' . $this->upgrade_transient, $transient_filter, 999 );
10171018
$result = $upgrader->bulk_upgrade( wp_list_pluck( $items_to_update, 'update_id' ) );
10181019
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+
}
1025+
}
1026+
1027+
if ( ! is_array( $result ) ) {
1028+
// This should never happen, but bulk_upgrade() can return false.
1029+
WP_CLI::error( 'Unable to connect to the filesystem.' );
10191030
}
10201031

10211032
/**

‎src/WP_CLI/DestructivePluginUpgrader.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* A plugin upgrader class that clears the destination directory.
77
*/
8-
class DestructivePluginUpgrader extends \Plugin_Upgrader {
8+
class DestructivePluginUpgrader extends PluginUpgrader {
99

1010
public function install_package( $args = array() ) {
1111
parent::upgrade_strings(); // Needed for the 'remove_old' string.

‎src/WP_CLI/DestructiveThemeUpgrader.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* A theme upgrader class that clears the destination directory.
77
*/
8-
class DestructiveThemeUpgrader extends \Theme_Upgrader {
8+
class DestructiveThemeUpgrader extends ThemeUpgrader {
99

1010
public function install_package( $args = array() ) {
1111
parent::upgrade_strings(); // Needed for the 'remove_old' string.

‎src/WP_CLI/PluginUpgrader.php‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace WP_CLI;
4+
5+
/**
6+
* A plugin upgrader class that tracks changed files.
7+
*/
8+
class PluginUpgrader extends \Plugin_Upgrader {
9+
/**
10+
* List of files that were changed during the update process.
11+
*
12+
* @var array<string>
13+
*/
14+
private $changed_files = [];
15+
16+
public function install_package( $args = array() ) {
17+
$track_files = function ( $will_invalidate, $filepath ) {
18+
$this->changed_files[] = $filepath;
19+
return $will_invalidate;
20+
};
21+
22+
// @phpstan-ignore WPCompat.filterNotAvailable.wpopcacheinvalidatefile
23+
add_filter( 'wp_opcache_invalidate_file', $track_files, 10, 2 );
24+
25+
$result = parent::install_package( $args );
26+
27+
remove_filter( 'wp_opcache_invalidate_file', $track_files );
28+
29+
// Remove duplicates and sort files.
30+
$this->changed_files = array_unique( $this->changed_files );
31+
sort( $this->changed_files );
32+
33+
return $result;
34+
}
35+
36+
/**
37+
* Returns a list of files that were changed during the update process.
38+
*
39+
* @return array<string> Changed files.
40+
*/
41+
public function get_changed_files() {
42+
return $this->changed_files;
43+
}
44+
}

‎src/WP_CLI/ThemeUpgrader.php‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace WP_CLI;
4+
5+
/**
6+
* A theme upgrader class that tracks changed files.
7+
*/
8+
class ThemeUpgrader extends \Theme_Upgrader {
9+
/**
10+
* List of files that were changed during the update process.
11+
*
12+
* @var array<string>
13+
*/
14+
private $changed_files = [];
15+
16+
public function install_package( $args = array() ) {
17+
$track_files = function ( $will_invalidate, $filepath ) {
18+
$this->changed_files[] = $filepath;
19+
return $will_invalidate;
20+
};
21+
22+
// @phpstan-ignore WPCompat.filterNotAvailable.wpopcacheinvalidatefile
23+
add_filter( 'wp_opcache_invalidate_file', $track_files, 10, 2 );
24+
25+
$result = parent::install_package( $args );
26+
27+
remove_filter( 'wp_opcache_invalidate_file', $track_files );
28+
29+
// Remove duplicates and sort files.
30+
$this->changed_files = array_unique( $this->changed_files );
31+
sort( $this->changed_files );
32+
33+
return $result;
34+
}
35+
36+
/**
37+
* Returns a list of files that were changed during the update process.
38+
*
39+
* @return array<string> Changed files.
40+
*/
41+
public function get_changed_files() {
42+
return $this->changed_files;
43+
}
44+
}

0 commit comments

Comments
 (0)