Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve messaging when shuffling salts #177

Merged
merged 4 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading