Skip to content

Commit 1b639da

Browse files
Copilotswissspidy
andcommitted
Fix activation error handling when called from install
When activate() is called from install command and the plugin is already active, activate_plugin() returns WP_Error('plugin_already_active'). This should be treated as success, not failure, since the desired end state (active plugin) is achieved. Changes: - Restructure activation checks to always run force deactivation first - When chained_command is true and plugin is already active without force, fall through to activate_plugin() and handle 'plugin_already_active' error - Treat 'plugin_already_active' as success when called from install command Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent f119a7a commit 1b639da

1 file changed

Lines changed: 28 additions & 22 deletions

File tree

src/Plugin_Command.php

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -387,25 +387,23 @@ public function activate( $args, $assoc_args = [] ) {
387387
continue;
388388
}
389389
// Network-active is the highest level of activation status.
390-
// However, when called from install command (chained_command), always attempt activation
391-
// to handle edge cases where the plugin may have been deactivated during the install process.
392-
if ( 'active-network' === $status && ! $this->chained_command ) {
390+
if ( 'active-network' === $status ) {
393391
// If force flag is set, deactivate and reactivate to run activation hooks.
394392
if ( $force ) {
395393
deactivate_plugins( $plugin->file, false, true );
396-
} else {
394+
} elseif ( ! $this->chained_command ) {
395+
// Only skip if not called from install command.
397396
WP_CLI::warning( "Plugin '{$plugin->name}' is already network active." );
398397
continue;
399398
}
400399
}
401400
// Don't reactivate active plugins, but do let them become network-active.
402-
// However, when called from install command (chained_command), always attempt activation
403-
// to handle edge cases where the plugin may have been deactivated during the install process.
404-
if ( ! $network_wide && 'active' === $status && ! $this->chained_command ) {
401+
if ( ! $network_wide && 'active' === $status ) {
405402
// If force flag is set, deactivate and reactivate to run activation hooks.
406403
if ( $force ) {
407404
deactivate_plugins( $plugin->file, false, false );
408-
} else {
405+
} elseif ( ! $this->chained_command ) {
406+
// Only skip if not called from install command.
409407
WP_CLI::warning( "Plugin '{$plugin->name}' is already active." );
410408
continue;
411409
}
@@ -419,22 +417,30 @@ public function activate( $args, $assoc_args = [] ) {
419417
$result = activate_plugin( $plugin->file, '', $network_wide );
420418

421419
if ( is_wp_error( $result ) ) {
422-
$message = $result->get_error_message();
423-
$message = (string) preg_replace( '/<a\s[^>]+>.*<\/a>/im', '', $message );
424-
$message = wp_strip_all_tags( $message );
425-
$message = str_replace( 'Error: ', '', $message );
426-
WP_CLI::warning( "Failed to activate plugin. {$message}" );
427-
// If the error is due to unexpected output, display it for debugging
428-
if ( 'unexpected_output' === $result->get_error_code() ) {
429-
/**
430-
* @var string $output
431-
*/
432-
$output = $result->get_error_data();
433-
if ( ! empty( $output ) ) {
434-
WP_CLI::debug( "Unexpected output: {$output}", 'plugin' );
420+
// When called from install command, treat 'already_active' as success.
421+
// This handles race conditions where WordPress may have preserved activation
422+
// status during the install process.
423+
if ( $this->chained_command && 'plugin_already_active' === $result->get_error_code() ) {
424+
$this->active_output( $plugin->name, $plugin->file, $network_wide, 'activate' );
425+
++$successes;
426+
} else {
427+
$message = $result->get_error_message();
428+
$message = (string) preg_replace( '/<a\s[^>]+>.*<\/a>/im', '', $message );
429+
$message = wp_strip_all_tags( $message );
430+
$message = str_replace( 'Error: ', '', $message );
431+
WP_CLI::warning( "Failed to activate plugin. {$message}" );
432+
// If the error is due to unexpected output, display it for debugging
433+
if ( 'unexpected_output' === $result->get_error_code() ) {
434+
/**
435+
* @var string $output
436+
*/
437+
$output = $result->get_error_data();
438+
if ( ! empty( $output ) ) {
439+
WP_CLI::debug( "Unexpected output: {$output}", 'plugin' );
440+
}
435441
}
442+
++$errors;
436443
}
437-
++$errors;
438444
} else {
439445
$this->active_output( $plugin->name, $plugin->file, $network_wide, 'activate' );
440446
++$successes;

0 commit comments

Comments
 (0)