Skip to content
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)
And 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
29 changes: 24 additions & 5 deletions src/Plugin_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ protected function get_all_items() {
* [--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 @@ protected function get_all_items() {
* 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 All @@ -374,18 +383,28 @@ public function activate( $args, $assoc_args = [] ) {
}
foreach ( $plugins as $plugin ) {
$status = $this->get_status( $plugin->file );
if ( $all && in_array( $status, [ 'active', 'active-network' ], true ) ) {
if ( $all && ! $force && in_array( $status, [ 'active', 'active-network' ], true ) ) {
continue;
}
// 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
13 changes: 11 additions & 2 deletions src/WP_CLI/CommandWithUpgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,23 @@ public function install( $args, $assoc_args ) {

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