Make WordPress Core


Ignore:
Timestamp:
02/22/2016 11:14:27 PM (9 years ago)
Author:
ocean90
Message:

Authentication: Allow users to log in using their email address.

Introduces wp_authenticate_email_password() which is hooked into authenticate after wp_authenticate_username_password().

Props Denis-de-Bernardy, ericlewis, vhomenko, MikeHansenMe, swissspidy, ocean90.
Fixes #9568.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user.php

    r36501 r36617  
    164164                __( '<strong>ERROR</strong>: The password you entered for the username %s is incorrect.' ),
    165165                '<strong>' . $username . '</strong>'
     166            ) .
     167            ' <a href="' . wp_lostpassword_url() . '">' .
     168            __( 'Lost your password?' ) .
     169            '</a>'
     170        );
     171    }
     172
     173    return $user;
     174}
     175
     176/**
     177 * Authenticate the user using the email and password.
     178 *
     179 * @since 4.5.0
     180 *
     181 * @param WP_User|WP_Error|null $user     WP_User or WP_Error object if a previous
     182 *                                        callback failed authentication.
     183 * @param string                $email    Email address for authentication.
     184 * @param string                $password Password for authentication.
     185 * @return WP_User|WP_Error WP_User on success, WP_Error on failure.
     186 */
     187function wp_authenticate_email_password( $user, $email, $password ) {
     188    if ( $user instanceof WP_User ) {
     189        return $user;
     190    }
     191
     192    if ( empty( $email ) || empty( $password ) ) {
     193        if ( is_wp_error( $user ) ) {
     194            return $user;
     195        }
     196
     197        $error = new WP_Error();
     198
     199        if ( empty( $email ) ) {
     200            $error->add( 'empty_username', __( '<strong>ERROR</strong>: The email field is empty.' ) ); // Uses 'empty_username' for back-compat with wp_signon()
     201        }
     202
     203        if ( empty( $password ) ) {
     204            $error->add( 'empty_password', __( '<strong>ERROR</strong>: The password field is empty.' ) );
     205        }
     206
     207        return $error;
     208    }
     209
     210    if ( ! is_email( $email ) ) {
     211        return $user;
     212    }
     213
     214    $user = get_user_by( 'email', $email );
     215
     216    if ( ! $user ) {
     217        return new WP_Error( 'invalid_email',
     218            __( '<strong>ERROR</strong>: Invalid email address.' ) .
     219            ' <a href="' . wp_lostpassword_url() . '">' .
     220            __( 'Lost your password?' ) .
     221            '</a>'
     222        );
     223    }
     224
     225    /** This filter is documented in wp-includes/user.php */
     226    $user = apply_filters( 'wp_authenticate_user', $user, $password );
     227
     228    if ( is_wp_error( $user ) ) {
     229        return $user;
     230    }
     231
     232    if ( ! wp_check_password( $password, $user->user_pass, $user->ID ) ) {
     233        return new WP_Error( 'incorrect_password',
     234            sprintf(
     235                /* translators: %s: email address */
     236                __( '<strong>ERROR</strong>: The password you entered for the email address %s is incorrect.' ),
     237                '<strong>' . $email . '</strong>'
    166238            ) .
    167239            ' <a href="' . wp_lostpassword_url() . '">' .
Note: See TracChangeset for help on using the changeset viewer.