From 289d5426d4f4d5115043e591596b69b60f199b1b Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 1 Mar 2024 11:52:16 +0545 Subject: [PATCH 1/4] Fix message in shuffle salts --- src/Config_Command.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Config_Command.php b/src/Config_Command.php index 154e937a..988722ee 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 ); + $is_default_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; } } @@ -924,13 +931,18 @@ public function shuffle_salts( $args, $assoc_args ) { $config_transformer = new WPConfigTransformer( $path ); foreach ( $secret_keys as $key => $value ) { $config_transformer->update( 'constant', $key, (string) $value ); + ++$successes; } } 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 ( $is_default_keys ) { + WP_CLI::success( 'Shuffled the salt keys.' ); + } else { + Utils\report_batch_operation_results( 'salt', 'shuffle', count( $keys ), $successes, $errors, $skipped ); + } } /** From 05f9a02cfa790695ed54fc40f33dc237fe84d58e Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 1 Mar 2024 12:29:43 +0545 Subject: [PATCH 2/4] Update feature test for shuffle salts --- features/config-shuffle-salts.feature | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/features/config-shuffle-salts.feature b/features/config-shuffle-salts.feature index e8c99658..5ab165ba 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 2 of 2 salts. """ 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: """ From 0adf388c1496528bb1a19cda73c21327c85cfefe Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 1 Mar 2024 12:42:07 +0545 Subject: [PATCH 3/4] Fix output conditionals --- src/Config_Command.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Config_Command.php b/src/Config_Command.php index 988722ee..dc55e890 100644 --- a/src/Config_Command.php +++ b/src/Config_Command.php @@ -880,7 +880,7 @@ public function shuffle_salts( $args, $assoc_args ) { $keys = $args; $force = Utils\get_flag_value( $assoc_args, 'force', false ); - $is_default_keys = ( empty( $keys ) ) ? true : false; + $has_keys = ( ! empty( $keys ) ) ? true : false; if ( empty( $keys ) ) { $keys = self::DEFAULT_SALT_CONSTANTS; @@ -930,18 +930,22 @@ 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 ); - ++$successes; + $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()}" ); } - if ( $is_default_keys ) { - WP_CLI::success( 'Shuffled the salt keys.' ); - } else { + if ( $has_keys ) { Utils\report_batch_operation_results( 'salt', 'shuffle', count( $keys ), $successes, $errors, $skipped ); + } else { + WP_CLI::success( 'Shuffled the salt keys.' ); } } From 5f16c81eab3cfe5a528830e533905bf21109a068 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Fri, 1 Mar 2024 12:51:22 +0545 Subject: [PATCH 4/4] Fix feature test in PHP less than 7.0 --- features/config-shuffle-salts.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/config-shuffle-salts.feature b/features/config-shuffle-salts.feature index 5ab165ba..50282b27 100644 --- a/features/config-shuffle-salts.feature +++ b/features/config-shuffle-salts.feature @@ -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 2 of 2 salts. + Shuffled 1 of 2 salts (1 skipped). """ And STDERR should contain: """