Skip to content

Commit 2d7e1f3

Browse files
authored
Merge pull request #496 from wp-cli/copilot/fix-activation-hooks-issue
Add `--force` flag to plugin activate to re-run activation hooks
2 parents 7ddbd20 + 515161c commit 2d7e1f3

File tree

3 files changed

+83
-7
lines changed

3 files changed

+83
-7
lines changed

features/plugin-activate.feature

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,51 @@ Feature: Activate WordPress plugins
177177
Debug (plugin): Unexpected output: Unexpected output from plugin activation
178178
"""
179179
And the return code should be 1
180+
181+
Scenario: Force activate an already active plugin to re-run activation hooks
182+
Given a wp-content/plugins/force-test.php file:
183+
"""
184+
<?php
185+
/**
186+
* Plugin Name: Force Test Plugin
187+
* Description: Test plugin for force activation
188+
* Author: WP-CLI tests
189+
*/
190+
191+
register_activation_hook( __FILE__, function() {
192+
@file_put_contents( WP_CONTENT_DIR . '/activation-test.txt', 'Activation hook was run' );
193+
});
194+
"""
195+
196+
When I run `wp plugin activate force-test`
197+
Then STDOUT should contain:
198+
"""
199+
Plugin 'force-test' activated.
200+
"""
201+
And the return code should be 0
202+
And the wp-content/activation-test.txt file should exist
203+
204+
# Remove the file to test if it gets recreated with --force
205+
When I run `rm wp-content/activation-test.txt`
206+
207+
# Try activating without --force (should skip)
208+
And I try `wp plugin activate force-test`
209+
Then STDERR should contain:
210+
"""
211+
Warning: Plugin 'force-test' is already active.
212+
"""
213+
And STDOUT should be:
214+
"""
215+
Success: Plugin already activated.
216+
"""
217+
And the return code should be 0
218+
And the wp-content/activation-test.txt file should not exist
219+
220+
# Now try with --force (should re-run activation hooks)
221+
When I run `wp plugin activate force-test --force`
222+
Then STDOUT should contain:
223+
"""
224+
Plugin 'force-test' activated.
225+
"""
226+
And the return code should be 0
227+
And the wp-content/activation-test.txt file should exist

src/Plugin_Command.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ protected function get_all_items() {
321321
* [--network]
322322
* : If set, the plugin will be activated for the entire multisite network.
323323
*
324+
* [--force]
325+
* : If set, deactivates and reactivates the plugin to re-run activation hooks, even if already active.
326+
*
324327
* ## EXAMPLES
325328
*
326329
* # Activate plugin
@@ -345,13 +348,19 @@ protected function get_all_items() {
345348
* Plugin 'buddypress' network activated.
346349
* Success: Activated 2 of 2 plugins.
347350
*
351+
* # Force re-running activation hooks for an already active plugin.
352+
* $ wp plugin activate hello --force
353+
* Plugin 'hello' activated.
354+
* Success: Activated 1 of 1 plugins.
355+
*
348356
* @param array $args
349357
* @param array $assoc_args
350358
*/
351359
public function activate( $args, $assoc_args = [] ) {
352360
$network_wide = Utils\get_flag_value( $assoc_args, 'network', false );
353361
$all = Utils\get_flag_value( $assoc_args, 'all', false );
354362
$all_exclude = Utils\get_flag_value( $assoc_args, 'exclude', '' );
363+
$force = Utils\get_flag_value( $assoc_args, 'force', false );
355364

356365
/**
357366
* @var string $all_exclude
@@ -374,18 +383,28 @@ public function activate( $args, $assoc_args = [] ) {
374383
}
375384
foreach ( $plugins as $plugin ) {
376385
$status = $this->get_status( $plugin->file );
377-
if ( $all && in_array( $status, [ 'active', 'active-network' ], true ) ) {
386+
if ( $all && ! $force && in_array( $status, [ 'active', 'active-network' ], true ) ) {
378387
continue;
379388
}
380389
// Network-active is the highest level of activation status.
381390
if ( 'active-network' === $status ) {
382-
WP_CLI::warning( "Plugin '{$plugin->name}' is already network active." );
383-
continue;
391+
// If force flag is set, deactivate and reactivate to run activation hooks.
392+
if ( $force ) {
393+
deactivate_plugins( $plugin->file, false, true );
394+
} else {
395+
WP_CLI::warning( "Plugin '{$plugin->name}' is already network active." );
396+
continue;
397+
}
384398
}
385399
// Don't reactivate active plugins, but do let them become network-active.
386400
if ( ! $network_wide && 'active' === $status ) {
387-
WP_CLI::warning( "Plugin '{$plugin->name}' is already active." );
388-
continue;
401+
// If force flag is set, deactivate and reactivate to run activation hooks.
402+
if ( $force ) {
403+
deactivate_plugins( $plugin->file, false, false );
404+
} else {
405+
WP_CLI::warning( "Plugin '{$plugin->name}' is already active." );
406+
continue;
407+
}
389408
}
390409

391410
// Plugins need to be deactivated before being network activated.

src/WP_CLI/CommandWithUpgrade.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,23 @@ public function install( $args, $assoc_args ) {
322322

323323
if ( true === $allow_activation && count( $extension ) > 0 ) {
324324
$this->chained_command = true;
325+
$force = Utils\get_flag_value( $assoc_args, 'force', false );
325326
if ( Utils\get_flag_value( $assoc_args, 'activate-network' ) ) {
326327
WP_CLI::log( "Network-activating '$slug'..." );
327-
$this->activate( array( $slug ), array( 'network' => true ) );
328+
$activate_args = array( 'network' => true );
329+
if ( $force ) {
330+
$activate_args['force'] = true;
331+
}
332+
$this->activate( array( $slug ), $activate_args );
328333
}
329334

330335
if ( Utils\get_flag_value( $assoc_args, 'activate' ) ) {
331336
WP_CLI::log( "Activating '$slug'..." );
332-
$this->activate( array( $slug ) );
337+
$activate_args = array();
338+
if ( $force ) {
339+
$activate_args['force'] = true;
340+
}
341+
$this->activate( array( $slug ), $activate_args );
333342
}
334343
$this->chained_command = false;
335344
}

0 commit comments

Comments
 (0)