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 @@ 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 Down Expand Up @@ -379,13 +388,23 @@ public function activate( $args, $assoc_args = [] ) {
}
// 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 @@ -314,14 +314,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