Skip to content

Commit 62abd24

Browse files
Copilotswissspidy
andcommitted
Add --force flag to plugin activate and wire it up with plugin install
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent dec35ff commit 62abd24

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/Plugin_Command.php

Lines changed: 23 additions & 4 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, the plugin activation hooks will be run even if the plugin is 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
@@ -379,13 +388,23 @@ public function activate( $args, $assoc_args = [] ) {
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
@@ -314,14 +314,23 @@ public function install( $args, $assoc_args ) {
314314

315315
if ( true === $allow_activation && count( $extension ) > 0 ) {
316316
$this->chained_command = true;
317+
$force = Utils\get_flag_value( $assoc_args, 'force', false );
317318
if ( Utils\get_flag_value( $assoc_args, 'activate-network' ) ) {
318319
WP_CLI::log( "Network-activating '$slug'..." );
319-
$this->activate( array( $slug ), array( 'network' => true ) );
320+
$activate_args = array( 'network' => true );
321+
if ( $force ) {
322+
$activate_args['force'] = true;
323+
}
324+
$this->activate( array( $slug ), $activate_args );
320325
}
321326

322327
if ( Utils\get_flag_value( $assoc_args, 'activate' ) ) {
323328
WP_CLI::log( "Activating '$slug'..." );
324-
$this->activate( array( $slug ) );
329+
$activate_args = array();
330+
if ( $force ) {
331+
$activate_args['force'] = true;
332+
}
333+
$this->activate( array( $slug ), $activate_args );
325334
}
326335
$this->chained_command = false;
327336
}

0 commit comments

Comments
 (0)