Skip to content

Commit 76ffc92

Browse files
authored
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
1 parent fdbd2d7 commit 76ffc92

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

features/config-shuffle-salts.feature

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Feature: Refresh the salts in the wp-config.php file
111111
When I run `wp config shuffle-salts AUTH_KEY NONCE_KEY`
112112
Then STDOUT should contain:
113113
"""
114-
Shuffled the salt keys.
114+
Shuffled 2 of 2 salts.
115115
"""
116116
And the wp-config.php file should not contain:
117117
"""
@@ -199,7 +199,7 @@ Feature: Refresh the salts in the wp-config.php file
199199
When I try `wp config shuffle-salts AUTH_KEY NEW_KEY`
200200
Then STDOUT should contain:
201201
"""
202-
Shuffled the salt keys.
202+
Shuffled 1 of 2 salts (1 skipped).
203203
"""
204204
And STDERR should contain:
205205
"""
@@ -224,7 +224,7 @@ Feature: Refresh the salts in the wp-config.php file
224224
When I run `wp config shuffle-salts AUTH_KEY NEW_KEY --force`
225225
Then STDOUT should contain:
226226
"""
227-
Shuffled the salt keys.
227+
Shuffled 2 of 2 salts.
228228
"""
229229
And the wp-config.php file should not contain:
230230
"""
@@ -247,7 +247,7 @@ Feature: Refresh the salts in the wp-config.php file
247247
When I run `wp config shuffle-salts AUTH_KEY NEW_KEY --force`
248248
Then STDOUT should contain:
249249
"""
250-
Shuffled the salt keys.
250+
Shuffled 2 of 2 salts.
251251
"""
252252
And the wp-config.php file should not contain:
253253
"""
@@ -295,7 +295,7 @@ Feature: Refresh the salts in the wp-config.php file
295295
When I try `wp config shuffle-salts AUTH_KEY NEW_KEY`
296296
Then STDOUT should contain:
297297
"""
298-
Shuffled the salt keys.
298+
Shuffled 1 of 2 salts (1 skipped).
299299
"""
300300
And STDERR should contain:
301301
"""
@@ -320,7 +320,7 @@ Feature: Refresh the salts in the wp-config.php file
320320
When I try `wp config shuffle-salts AUTH_KEY NEW_KEY --force`
321321
Then STDOUT should contain:
322322
"""
323-
Shuffled the salt keys.
323+
Shuffled 1 of 2 salts (1 skipped).
324324
"""
325325
And STDERR should contain:
326326
"""

src/Config_Command.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -877,20 +877,26 @@ public function has( $args, $assoc_args ) {
877877
* @when before_wp_load
878878
*/
879879
public function shuffle_salts( $args, $assoc_args ) {
880-
881880
$keys = $args;
882881
$force = Utils\get_flag_value( $assoc_args, 'force', false );
883882

883+
$has_keys = ( ! empty( $keys ) ) ? true : false;
884+
884885
if ( empty( $keys ) ) {
885886
$keys = self::DEFAULT_SALT_CONSTANTS;
886887
}
887888

889+
$successes = 0;
890+
$errors = 0;
891+
$skipped = 0;
892+
888893
$secret_keys = [];
889894

890895
try {
891896
foreach ( $keys as $key ) {
892897
if ( ! $force && ! in_array( $key, self::DEFAULT_SALT_CONSTANTS, true ) ) {
893898
WP_CLI::warning( "Could not shuffle the unknown key '{$key}'." );
899+
++$skipped;
894900
continue;
895901
}
896902
$secret_keys[ $key ] = trim( self::unique_key() );
@@ -903,6 +909,7 @@ public function shuffle_salts( $args, $assoc_args ) {
903909
} else {
904910
WP_CLI::warning( "Could not shuffle the unknown key '{$key}'." );
905911
}
912+
++$skipped;
906913
}
907914
}
908915

@@ -923,14 +930,23 @@ public function shuffle_salts( $args, $assoc_args ) {
923930
try {
924931
$config_transformer = new WPConfigTransformer( $path );
925932
foreach ( $secret_keys as $key => $value ) {
926-
$config_transformer->update( 'constant', $key, (string) $value );
933+
$is_updated = $config_transformer->update( 'constant', $key, (string) $value );
934+
if ( $is_updated ) {
935+
++$successes;
936+
} else {
937+
++$errors;
938+
}
927939
}
928940
} catch ( Exception $exception ) {
929941
$wp_config_file_name = basename( $path );
930942
WP_CLI::error( "Could not process the '{$wp_config_file_name}' transformation.\nReason: {$exception->getMessage()}" );
931943
}
932944

933-
WP_CLI::success( 'Shuffled the salt keys.' );
945+
if ( $has_keys ) {
946+
Utils\report_batch_operation_results( 'salt', 'shuffle', count( $keys ), $successes, $errors, $skipped );
947+
} else {
948+
WP_CLI::success( 'Shuffled the salt keys.' );
949+
}
934950
}
935951

936952
/**

0 commit comments

Comments
 (0)