Skip to content
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: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5280,7 +5280,7 @@ wp user remove-role <user> [<role>]
Resets the password for one or more users.

~~~
wp user reset-password <user>... [--skip-email]
wp user reset-password <user>... [--skip-email] [--show-password] [--porcelain]
~~~

**OPTIONS**
Expand All @@ -5291,6 +5291,12 @@ wp user reset-password <user>... [--skip-email]
[--skip-email]
Don't send an email notification to the affected user(s).

[--show-password]
Show the new password(s).

[--porcelain]
Output only the new password(s).

**EXAMPLES**

# Reset the password for two users and send them the change email.
Expand All @@ -5299,6 +5305,10 @@ wp user reset-password <user>... [--skip-email]
Reset password for editor.
Success: Passwords reset for 2 users.

# Reset the password for one user, displaying only the new password, and not sending the change email.
$ wp user reset-password admin --skip-email --porcelain
yV6BP*!d70wg



### wp user session
Expand Down
37 changes: 37 additions & 0 deletions features/user-reset-password.feature
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,40 @@ Feature: Reset passwords for one or more WordPress users.
"""
{ORIGINAL_PASSWORD}
"""

@require-wp-4.3
Scenario: Reset the password of a WordPress user, and show the new password
Given a WP installation

When I run `wp user get 1 --field=user_pass`
Then save STDOUT as {ORIGINAL_PASSWORD}

When I run `wp user reset-password 1 --skip-email --show-password`
Then STDOUT should contain:
"""
Password:
"""
And an email should not be sent

When I run `wp user get 1 --field=user_pass`
Then STDOUT should not contain:
"""
{ORIGINAL_PASSWORD}
"""

@require-wp-4.3
Scenario: Reset the password of a WordPress user, and show only the new password
Given a WP installation

When I run `wp user get 1 --field=user_pass`
Then save STDOUT as {ORIGINAL_PASSWORD}

When I run `wp user reset-password 1 --skip-email --porcelain`
Then STDOUT should not be empty
And an email should not be sent

When I run `wp user get 1 --field=user_pass`
Then STDOUT should not contain:
"""
{ORIGINAL_PASSWORD}
"""
45 changes: 33 additions & 12 deletions src/User_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,12 @@ public function import_csv( $args, $assoc_args ) {
* [--skip-email]
* : Don't send an email notification to the affected user(s).
*
* [--show-password]
* : Show the new password(s).
*
* [--porcelain]
* : Output only the new password(s).
*
* ## EXAMPLES
*
* # Reset the password for two users and send them the change email.
Expand All @@ -1100,35 +1106,50 @@ public function import_csv( $args, $assoc_args ) {
* Reset password for editor.
* Success: Passwords reset for 2 users.
*
* # Reset the password for one user, displaying only the new password, and not sending the change email.
* $ wp user reset-password admin --skip-email --porcelain
* yV6BP*!d70wg
*
* @subcommand reset-password
*/
public function reset_password( $args, $assoc_args ) {
$skip_email = Utils\get_flag_value( $assoc_args, 'skip-email' );
$porcelain = Utils\get_flag_value( $assoc_args, 'porcelain' );
$skip_email = Utils\get_flag_value( $assoc_args, 'skip-email' );
$show_new_pass = Utils\get_flag_value( $assoc_args, 'show-password' );

if ( $skip_email ) {
add_filter( 'send_password_change_email', '__return_false' );
}
$fetcher = new UserFetcher();
$users = $fetcher->get_many( $args );
foreach ( $users as $user ) {
$new_pass = wp_generate_password( 24 );
wp_update_user(
[
'ID' => $user->ID,
'user_pass' => wp_generate_password( 24 ),
'user_pass' => $new_pass,
]
);
WP_CLI::log( "Reset password for {$user->user_login}." );
}
if ( $skip_email ) {
remove_filter( 'send_password_change_email', '__return_false' );
if ( $porcelain ) {
WP_CLI::line( "$new_pass" );
} else {
WP_CLI::log( "Reset password for {$user->user_login}." );
if ( $show_new_pass ) {
WP_CLI::line( "Password: $new_pass" );
}
}
}

$reset_user_count = count( $users );
if ( 1 === $reset_user_count ) {
WP_CLI::success( "Password reset for {$reset_user_count} user." );
} elseif ( $reset_user_count > 1 ) {
WP_CLI::success( "Passwords reset for {$reset_user_count} users." );
} else {
WP_CLI::error( 'No user found to reset password.' );

if ( ! $porcelain ) {
if ( 1 === $reset_user_count ) {
WP_CLI::success( "Password reset for {$reset_user_count} user." );
} elseif ( $reset_user_count > 1 ) {
WP_CLI::success( "Passwords reset for {$reset_user_count} users." );
} else {
WP_CLI::error( 'No user found to reset password.' );
}
}
}

Expand Down