Skip to content

Commit

Permalink
Improve messaging when shuffling salts (#177)
Browse files Browse the repository at this point in the history
* Fix message in shuffle salts

* Update feature test for shuffle salts

* Fix output conditionals

* Fix feature test in PHP less than 7.0
  • Loading branch information
ernilambar committed Mar 1, 2024
1 parent fdbd2d7 commit 76ffc92
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
12 changes: 6 additions & 6 deletions features/config-shuffle-salts.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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:
"""
Expand All @@ -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:
"""
Expand All @@ -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:
"""
Expand Down Expand Up @@ -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:
"""
Expand All @@ -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:
"""
Expand Down
22 changes: 19 additions & 3 deletions src/Config_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand All @@ -903,6 +909,7 @@ public function shuffle_salts( $args, $assoc_args ) {
} else {
WP_CLI::warning( "Could not shuffle the unknown key '{$key}'." );
}
++$skipped;
}
}

Expand All @@ -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.' );
}
}

/**
Expand Down

0 comments on commit 76ffc92

Please sign in to comment.