Skip to content

Commit 4e3f60c

Browse files
committed
Merge branch 'main' into copilot/add-plugin-update-identification
2 parents 63e9298 + a8dc5aa commit 4e3f60c

4 files changed

Lines changed: 92 additions & 8 deletions

File tree

‎README.md‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ See the WordPress [Plugin Handbook](https://developer.wordpress.org/plugins/) de
5757
Activates one or more plugins.
5858

5959
~~~
60-
wp plugin activate [<plugin>...] [--all] [--exclude=<name>] [--network]
60+
wp plugin activate [<plugin>...] [--all] [--exclude=<name>] [--network] [--force]
6161
~~~
6262

6363
**OPTIONS**
@@ -74,6 +74,9 @@ wp plugin activate [<plugin>...] [--all] [--exclude=<name>] [--network]
7474
[--network]
7575
If set, the plugin will be activated for the entire multisite network.
7676

77+
[--force]
78+
If set, deactivates and reactivates the plugin to re-run activation hooks, even if already active.
79+
7780
**EXAMPLES**
7881

7982
# Activate plugin
@@ -98,6 +101,11 @@ wp plugin activate [<plugin>...] [--all] [--exclude=<name>] [--network]
98101
Plugin 'buddypress' network activated.
99102
Success: Activated 2 of 2 plugins.
100103

104+
# Force re-running activation hooks for an already active plugin.
105+
$ wp plugin activate hello --force
106+
Plugin 'hello' activated.
107+
Success: Activated 1 of 1 plugins.
108+
101109

102110

103111
### wp plugin deactivate

‎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
@@ -322,6 +322,9 @@ protected function get_all_items() {
322322
* [--network]
323323
* : If set, the plugin will be activated for the entire multisite network.
324324
*
325+
* [--force]
326+
* : If set, deactivates and reactivates the plugin to re-run activation hooks, even if already active.
327+
*
325328
* ## EXAMPLES
326329
*
327330
* # Activate plugin
@@ -346,13 +349,19 @@ protected function get_all_items() {
346349
* Plugin 'buddypress' network activated.
347350
* Success: Activated 2 of 2 plugins.
348351
*
352+
* # Force re-running activation hooks for an already active plugin.
353+
* $ wp plugin activate hello --force
354+
* Plugin 'hello' activated.
355+
* Success: Activated 1 of 1 plugins.
356+
*
349357
* @param array $args
350358
* @param array $assoc_args
351359
*/
352360
public function activate( $args, $assoc_args = [] ) {
353361
$network_wide = Utils\get_flag_value( $assoc_args, 'network', false );
354362
$all = Utils\get_flag_value( $assoc_args, 'all', false );
355363
$all_exclude = Utils\get_flag_value( $assoc_args, 'exclude', '' );
364+
$force = Utils\get_flag_value( $assoc_args, 'force', false );
356365

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

392411
// 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)