From 76ffc92bcf0a6063af47ad01e938d0857c167d5a Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 1 Mar 2024 17:49:22 +0545 Subject: [PATCH] Improve messaging when shuffling salts (#177) * Fix message in shuffle salts * Update feature test for shuffle salts * Fix output conditionals * Fix feature test in PHP less than 7.0 --- features/config-shuffle-salts.feature | 12 ++++++------ src/Config_Command.php | 22 +++++++++++++++++++--- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/features/config-shuffle-salts.feature b/features/config-shuffle-salts.feature index e8c99658..50282b27 100644 --- a/features/config-shuffle-salts.feature +++ b/features/config-shuffle-salts.feature @@ -111,7 +111,7 @@ Feature: Refresh the salts in the wp-config.php file When I run `wp config shuffle-salts AUTH_KEY NONCE_KEY` Then STDOUT should contain: """ - Shuffled the salt keys. + Shuffled 2 of 2 salts. """ And the wp-config.php file should not contain: """ @@ -199,7 +199,7 @@ Feature: Refresh the salts in the wp-config.php file When I try `wp config shuffle-salts AUTH_KEY NEW_KEY` Then STDOUT should contain: """ - Shuffled the salt keys. + Shuffled 1 of 2 salts (1 skipped). """ And STDERR should contain: """ @@ -224,7 +224,7 @@ Feature: Refresh the salts in the wp-config.php file When I run `wp config shuffle-salts AUTH_KEY NEW_KEY --force` Then STDOUT should contain: """ - Shuffled the salt keys. + Shuffled 2 of 2 salts. """ And the wp-config.php file should not contain: """ @@ -247,7 +247,7 @@ Feature: Refresh the salts in the wp-config.php file When I run `wp config shuffle-salts AUTH_KEY NEW_KEY --force` Then STDOUT should contain: """ - Shuffled the salt keys. + Shuffled 2 of 2 salts. """ And the wp-config.php file should not contain: """ @@ -295,7 +295,7 @@ Feature: Refresh the salts in the wp-config.php file When I try `wp config shuffle-salts AUTH_KEY NEW_KEY` Then STDOUT should contain: """ - Shuffled the salt keys. + Shuffled 1 of 2 salts (1 skipped). """ And STDERR should contain: """ @@ -320,7 +320,7 @@ Feature: Refresh the salts in the wp-config.php file When I try `wp config shuffle-salts AUTH_KEY NEW_KEY --force` Then STDOUT should contain: """ - Shuffled the salt keys. + Shuffled 1 of 2 salts (1 skipped). """ And STDERR should contain: """ diff --git a/src/Config_Command.php b/src/Config_Command.php index 154e937a..dc55e890 100644 --- a/src/Config_Command.php +++ b/src/Config_Command.php @@ -877,20 +877,26 @@ public function has( $args, $assoc_args ) { * @when before_wp_load */ public function shuffle_salts( $args, $assoc_args ) { - $keys = $args; $force = Utils\get_flag_value( $assoc_args, 'force', false ); + $has_keys = ( ! empty( $keys ) ) ? true : false; + if ( empty( $keys ) ) { $keys = self::DEFAULT_SALT_CONSTANTS; } + $successes = 0; + $errors = 0; + $skipped = 0; + $secret_keys = []; try { foreach ( $keys as $key ) { if ( ! $force && ! in_array( $key, self::DEFAULT_SALT_CONSTANTS, true ) ) { WP_CLI::warning( "Could not shuffle the unknown key '{$key}'." ); + ++$skipped; continue; } $secret_keys[ $key ] = trim( self::unique_key() ); @@ -903,6 +909,7 @@ public function shuffle_salts( $args, $assoc_args ) { } else { WP_CLI::warning( "Could not shuffle the unknown key '{$key}'." ); } + ++$skipped; } } @@ -923,14 +930,23 @@ public function shuffle_salts( $args, $assoc_args ) { try { $config_transformer = new WPConfigTransformer( $path ); foreach ( $secret_keys as $key => $value ) { - $config_transformer->update( 'constant', $key, (string) $value ); + $is_updated = $config_transformer->update( 'constant', $key, (string) $value ); + if ( $is_updated ) { + ++$successes; + } else { + ++$errors; + } } } catch ( Exception $exception ) { $wp_config_file_name = basename( $path ); WP_CLI::error( "Could not process the '{$wp_config_file_name}' transformation.\nReason: {$exception->getMessage()}" ); } - WP_CLI::success( 'Shuffled the salt keys.' ); + if ( $has_keys ) { + Utils\report_batch_operation_results( 'salt', 'shuffle', count( $keys ), $successes, $errors, $skipped ); + } else { + WP_CLI::success( 'Shuffled the salt keys.' ); + } } /**