Skip to content
Draft
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
48 changes: 48 additions & 0 deletions features/plugin-activate.feature
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,51 @@ Feature: Activate WordPress plugins
Debug (plugin): Unexpected output: Unexpected output from plugin activation
"""
And the return code should be 1

Scenario: Force activate an already active plugin to re-run activation hooks
Given a wp-content/plugins/force-test.php file:
"""
<?php
/**
* Plugin Name: Force Test Plugin
* Description: Test plugin for force activation
* Author: WP-CLI tests
*/

register_activation_hook( __FILE__, function() {
@file_put_contents( WP_CONTENT_DIR . '/activation-test.txt', 'Activation hook was run' );
});
"""

When I run `wp plugin activate force-test`
Then STDOUT should contain:
"""
Plugin 'force-test' activated.
"""
And the return code should be 0
And the wp-content/activation-test.txt file should exist

# Remove the file to test if it gets recreated with --force
When I run `rm wp-content/activation-test.txt`

# Try activating without --force (should skip)
When I try `wp plugin activate force-test`
Then STDERR should contain:
"""
Warning: Plugin 'force-test' is already active.
"""
And STDOUT should be:
"""
Success: Plugin already activated.
"""
And the return code should be 0
And the wp-content/activation-test.txt file should not exist

# Now try with --force (should re-run activation hooks)
When I run `wp plugin activate force-test --force`
Then STDOUT should contain:
"""
Plugin 'force-test' activated.
"""
And the return code should be 0
And the wp-content/activation-test.txt file should exist
59 changes: 59 additions & 0 deletions features/plugin-install.feature
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,62 @@ Feature: Install WordPress plugins
"""
active
"""

Scenario: Force reinstall and activate an already active plugin to re-run activation hooks
Given a WP install
And a wp-content/plugins/install-force-test/install-force-test.php file:
"""
<?php
/**
* Plugin Name: Install Force Test
* Description: Test plugin for force install and activate
* Version: 1.0.0
* Author: WP-CLI tests
*/

register_activation_hook( __FILE__, function() {
@file_put_contents( WP_CONTENT_DIR . '/install-activation-test.txt', 'Activation hook was run' );
});
"""

When I run `wp plugin activate install-force-test`
Then STDOUT should contain:
"""
Plugin 'install-force-test' activated.
"""
And the wp-content/install-activation-test.txt file should exist

# Remove the file to test if it gets recreated with --force
When I run `rm wp-content/install-activation-test.txt`

# Create a zip of the plugin for reinstallation
When I run `cd wp-content/plugins && zip -r /tmp/install-force-test.zip install-force-test`

# Try install --activate without --force (should skip activation hooks)
When I run `wp plugin install /tmp/install-force-test.zip --activate`
Then STDOUT should contain:
"""
Unpacking the package...
"""
And STDOUT should contain:
"""
Activating 'install-force-test'...
"""
And STDERR should contain:
"""
Warning: Plugin 'install-force-test' is already active.
"""
And the wp-content/install-activation-test.txt file should not exist

# Now try install --activate --force (should re-run activation hooks)
When I run `wp plugin install /tmp/install-force-test.zip --activate --force`
Then STDOUT should contain:
"""
Activating 'install-force-test'...
"""
And STDOUT should contain:
"""
Plugin 'install-force-test' activated.
"""
And the return code should be 0
And the wp-content/install-activation-test.txt file should exist
27 changes: 23 additions & 4 deletions src/Plugin_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@
* [--network]
* : If set, the plugin will be activated for the entire multisite network.
*
* [--force]
* : If set, deactivates and reactivates the plugin to re-run activation hooks, even if already active.
*
* ## EXAMPLES
*
* # Activate plugin
Expand All @@ -345,13 +348,19 @@
* Plugin 'buddypress' network activated.
* Success: Activated 2 of 2 plugins.
*
* # Force re-running activation hooks for an already active plugin.
* $ wp plugin activate hello --force
* Plugin 'hello' activated.
* Success: Activated 1 of 1 plugins.
*
* @param array $args
* @param array $assoc_args
*/
public function activate( $args, $assoc_args = [] ) {
$network_wide = Utils\get_flag_value( $assoc_args, 'network', false );
$all = Utils\get_flag_value( $assoc_args, 'all', false );
$all_exclude = Utils\get_flag_value( $assoc_args, 'exclude', '' );
$force = Utils\get_flag_value( $assoc_args, 'force', false );

/**
* @var string $all_exclude
Expand Down Expand Up @@ -379,13 +388,23 @@
}
// Network-active is the highest level of activation status.
if ( 'active-network' === $status ) {
WP_CLI::warning( "Plugin '{$plugin->name}' is already network active." );
continue;
// If force flag is set, deactivate and reactivate to run activation hooks.
if ( $force ) {
deactivate_plugins( $plugin->file, false, true );
} else {
WP_CLI::warning( "Plugin '{$plugin->name}' is already network active." );
continue;
}
}
// Don't reactivate active plugins, but do let them become network-active.
if ( ! $network_wide && 'active' === $status ) {
WP_CLI::warning( "Plugin '{$plugin->name}' is already active." );
continue;
// If force flag is set, deactivate and reactivate to run activation hooks.
if ( $force ) {
deactivate_plugins( $plugin->file, false, false );
} else {
WP_CLI::warning( "Plugin '{$plugin->name}' is already active." );
continue;
}
}

// Plugins need to be deactivated before being network activated.
Expand Down Expand Up @@ -1217,7 +1236,7 @@
}

// Check if requires_plugins field exists and is not empty
if ( ! empty( $api->requires_plugins ) && is_array( $api->requires_plugins ) ) {

Check failure on line 1239 in src/Plugin_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Access to an undefined property array|object::$requires_plugins.
return $api->requires_plugins;
}

Expand Down
13 changes: 11 additions & 2 deletions src/WP_CLI/CommandWithUpgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@
}
$new_path = substr_replace( $source, $slug_dir, (int) strrpos( $source, $source_dir ), strlen( $source_dir ) );

if ( $GLOBALS['wp_filesystem']->move( $source, $new_path ) ) {

Check failure on line 258 in src/WP_CLI/CommandWithUpgrade.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Cannot call method move() on mixed.
WP_CLI::log( sprintf( "Renamed Github-based project from '%s' to '%s'.", $source_dir, $slug_dir ) );
return $new_path;
}
Expand Down Expand Up @@ -314,14 +314,23 @@

if ( true === $allow_activation && count( $extension ) > 0 ) {
$this->chained_command = true;
$force = Utils\get_flag_value( $assoc_args, 'force', false );
if ( Utils\get_flag_value( $assoc_args, 'activate-network' ) ) {
WP_CLI::log( "Network-activating '$slug'..." );
$this->activate( array( $slug ), array( 'network' => true ) );
$activate_args = array( 'network' => true );
if ( $force ) {
$activate_args['force'] = true;
}
$this->activate( array( $slug ), $activate_args );
}

if ( Utils\get_flag_value( $assoc_args, 'activate' ) ) {
WP_CLI::log( "Activating '$slug'..." );
$this->activate( array( $slug ) );
$activate_args = array();
if ( $force ) {
$activate_args['force'] = true;
}
$this->activate( array( $slug ), $activate_args );
}
$this->chained_command = false;
}
Expand Down Expand Up @@ -617,15 +626,15 @@

foreach ( $all_items as $key => &$item ) {

if ( empty( $item['version'] ) ) {

Check failure on line 629 in src/WP_CLI/CommandWithUpgrade.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Cannot access offset 'version' on mixed.
$item['version'] = '';

Check failure on line 630 in src/WP_CLI/CommandWithUpgrade.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Cannot access offset 'version' on mixed.
}

if ( empty( $item['update_version'] ) ) {

Check failure on line 633 in src/WP_CLI/CommandWithUpgrade.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Cannot access offset 'update_version' on mixed.
$item['update_version'] = '';

Check failure on line 634 in src/WP_CLI/CommandWithUpgrade.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Cannot access offset 'update_version' on mixed.
}

foreach ( $item as $field => &$value ) {

Check failure on line 637 in src/WP_CLI/CommandWithUpgrade.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Argument of an invalid type mixed supplied for foreach, only iterables are supported.
if ( 'update' === $field ) {
// If an update is unavailable, make sure to also show these fields which will explain why
if ( 'unavailable' === $value ) {
Expand Down Expand Up @@ -654,8 +663,8 @@
// Also, it is not forbidden for a value to contain a comma (in which case we can filter only by one).
$field_filter = $assoc_args[ $field ];
if (
$item[ $field ] !== $field_filter

Check failure on line 666 in src/WP_CLI/CommandWithUpgrade.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Cannot access offset mixed on mixed.
&& ! in_array( $item[ $field ], array_map( 'trim', explode( ',', $field_filter ) ), true )

Check failure on line 667 in src/WP_CLI/CommandWithUpgrade.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Cannot access offset mixed on mixed.
) {
unset( $all_items[ $key ] );
}
Expand Down Expand Up @@ -867,7 +876,7 @@
// In older WP versions these used to be objects.
foreach ( $items as $index => $item_object ) {
if ( is_array( $item_object ) ) {
$items[ $index ]['url'] = "https://wordpress.org/{$plural}/{$item_object['slug']}/";

Check failure on line 879 in src/WP_CLI/CommandWithUpgrade.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Part $item_object['slug'] (mixed) of encapsed string cannot be cast to string.
} elseif ( $item_object instanceof \stdClass ) {
$item_object->url = "https://wordpress.org/{$plural}/{$item_object->slug}/";
}
Expand Down
Loading