Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions features/plugin-update.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
<?php
WP_CLI::add_hook(
'plugin_update_finished',
static function ( $files ) {
WP_CLI::log( 'Hook fired with ' . ( count( $files ) > 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

Expand Down
26 changes: 26 additions & 0 deletions features/theme-update.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
<?php
WP_CLI::add_hook(
'theme_update_finished',
static function ( $files ) {
foreach ( $files as $file ) {
WP_CLI::log( 'Changed file: ' . $file );
}
}
);
"""

When I run `wp theme install twentytwelve --version=3.0`
Then STDOUT should not be empty

When I run `wp theme update twentytwelve --require=hook.php`
Then STDOUT should contain:
"""
Changed file: {THEME_DIR}/twentytwelve/functions.php
"""

@require-wp-4.5
Scenario: Not giving a slug on update should throw an error unless --all given
Given a WP install
Expand Down
2 changes: 1 addition & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound">
<exclude-pattern>*/src/WP_CLI/Fetchers/(Plugin|Theme)\.php$</exclude-pattern>
<exclude-pattern>*/src/WP_CLI/CommandWithUpgrade\.php$</exclude-pattern>
<exclude-pattern>*/src/WP_CLI/(CommandWith|DestructivePlugin|DestructiveTheme)Upgrader\.php$</exclude-pattern>
<exclude-pattern>*/src/WP_CLI/(CommandWith|DestructivePlugin|DestructiveTheme|Plugin|Theme)Upgrader\.php$</exclude-pattern>
<exclude-pattern>*/src/WP_CLI/Parse(Plugin|Theme)NameInput\.php$</exclude-pattern>
<exclude-pattern>*/src/WP_CLI/ExtensionUpgraderSkin\.php$</exclude-pattern>
</rule>
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Comment thread
swissspidy marked this conversation as resolved.
Resolved
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Theme_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Comment thread
swissspidy marked this conversation as resolved.
Resolved
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/WP_CLI/CommandWithUpgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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.' );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/WP_CLI/DestructivePluginUpgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/WP_CLI/DestructiveThemeUpgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
44 changes: 44 additions & 0 deletions src/WP_CLI/PluginUpgrader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace WP_CLI;

/**
* A plugin upgrader class that tracks changed files.
*/
class PluginUpgrader extends \Plugin_Upgrader {
/**
* List of files that were changed during the update process.
*
* @var array<string>
*/
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;
}
Comment thread
swissspidy marked this conversation as resolved.
Resolved

/**
* Returns a list of files that were changed during the update process.
*
* @return array<string> Changed files.
*/
public function get_changed_files() {
return $this->changed_files;
}
}
44 changes: 44 additions & 0 deletions src/WP_CLI/ThemeUpgrader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace WP_CLI;

/**
* A theme upgrader class that tracks changed files.
*/
class ThemeUpgrader extends \Theme_Upgrader {
/**
* List of files that were changed during the update process.
*
* @var array<string>
*/
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;
}
Comment thread
swissspidy marked this conversation as resolved.
Resolved

/**
* Returns a list of files that were changed during the update process.
*
* @return array<string> Changed files.
*/
public function get_changed_files() {
return $this->changed_files;
}
}
Loading