diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php
index ac6c0cc01b..65930c15e7 100644
--- a/src/wp-includes/user.php
+++ b/src/wp-includes/user.php
@@ -3038,15 +3038,22 @@ function retrieve_password( $user_login = null ) {
$user_login = $_POST['user_login'];
}
+ $user_login = trim( wp_unslash( $user_login ) );
+
if ( empty( $user_login ) ) {
$errors->add( 'empty_username', __( 'Error: Please enter a username or email address.' ) );
} elseif ( strpos( $user_login, '@' ) ) {
- $user_data = get_user_by( 'email', trim( wp_unslash( $user_login ) ) );
+ $user_data = get_user_by( 'email', $user_login );
+
+ if ( empty( $user_data ) ) {
+ $user_data = get_user_by( 'login', $user_login );
+ }
+
if ( empty( $user_data ) ) {
$errors->add( 'invalid_email', __( 'Error: There is no account with that username or email address.' ) );
}
} else {
- $user_data = get_user_by( 'login', trim( wp_unslash( $user_login ) ) );
+ $user_data = get_user_by( 'login', $user_login );
}
/**
diff --git a/tests/phpunit/tests/user/retrievePassword.php b/tests/phpunit/tests/user/retrievePassword.php
index 5a0e1d638e..96a14c408f 100644
--- a/tests/phpunit/tests/user/retrievePassword.php
+++ b/tests/phpunit/tests/user/retrievePassword.php
@@ -47,8 +47,7 @@ class Tests_User_RetrievePassword extends WP_UnitTestCase {
* @ticket 54690
*/
public function test_retrieve_password_reset_notification_email() {
- $message = 'Sending password reset notification email failed.';
- $this->assertNotWPError( retrieve_password( $this->user->user_login ), $message );
+ $this->assertNotWPError( retrieve_password( $this->user->user_login ), 'Sending password reset notification email failed.' );
}
/**
@@ -64,7 +63,21 @@ class Tests_User_RetrievePassword extends WP_UnitTestCase {
}
);
- $message = 'Sending password reset notification email succeeded.';
- $this->assertWPError( retrieve_password( $this->user->user_login ), $message );
+ $this->assertWPError( retrieve_password( $this->user->user_login ), 'Sending password reset notification email succeeded.' );
+ }
+
+ /**
+ * @ticket 53634
+ */
+ public function test_retrieve_password_should_fetch_user_by_login_if_not_found_by_email() {
+ self::factory()->user->create(
+ array(
+ 'user_login' => 'foo@example.com',
+ 'user_email' => 'bar@example.com',
+ )
+ );
+
+ $this->assertTrue( retrieve_password( 'foo@example.com' ), 'Fetching user by login failed.' );
+ $this->assertTrue( retrieve_password( 'bar@example.com' ), 'Fetching user by email failed.' );
}
}