Make WordPress Core

Ticket #9568: 9568.12.diff

File 9568.12.diff, 6.1 KB (added by swissspidy, 9 years ago)
  • src/wp-includes/default-filters.php

    diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php
    index 098c251..76111ff 100644
    add_filter( 'heartbeat_nopriv_send', 'wp_auth_check' ); 
    342342
    343343// Default authentication filters
    344344add_filter( 'authenticate', 'wp_authenticate_username_password',  20, 3 );
     345add_filter( 'authenticate', 'wp_authenticate_email_password',     20, 3 );
    345346add_filter( 'authenticate', 'wp_authenticate_spam_check',         99    );
    346347add_filter( 'determine_current_user', 'wp_validate_auth_cookie'          );
    347348add_filter( 'determine_current_user', 'wp_validate_logged_in_cookie', 20 );
  • src/wp-includes/pluggable.php

    diff --git src/wp-includes/pluggable.php src/wp-includes/pluggable.php
    index 8e24100..c1dc3fb 100644
    if ( !function_exists('wp_authenticate') ) : 
    548548 *
    549549 * @since 2.5.0
    550550 *
    551  * @param string $username User's username.
     551 * @param string $username User's username or email address.
    552552 * @param string $password User's password.
    553553 * @return WP_User|WP_Error WP_User object if the credentials are valid,
    554554 *                          otherwise WP_Error.
    function wp_authenticate($username, $password) { 
    575575        if ( $user == null ) {
    576576                // TODO what should the error message be? (Or would these even happen?)
    577577                // Only needed if all authentication handlers fail to return anything.
    578                 $user = new WP_Error('authentication_failed', __('<strong>ERROR</strong>: Invalid username or incorrect password.'));
     578                $user = new WP_Error( 'authentication_failed', __( '<strong>ERROR</strong>: Invalid username/email address or incorrect password.' ) );
    579579        }
    580580
    581581        $ignore_codes = array('empty_username', 'empty_password');
  • src/wp-includes/user.php

    diff --git src/wp-includes/user.php src/wp-includes/user.php
    index c1c6304..125d3ec 100644
    function wp_authenticate_username_password($user, $username, $password) { 
    174174}
    175175
    176176/**
     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 ( ! is_email( $email ) || empty( $password ) ) {
     193                return $user;
     194        }
     195
     196        $user = get_user_by( 'email', $email );
     197
     198        if ( ! $user ) {
     199                return new WP_Error( 'invalid_email',
     200                        __( '<strong>ERROR</strong>: Invalid email address.' ) .
     201                        ' <a href="' . wp_lostpassword_url() . '">' .
     202                        __( 'Lost your password?' ) .
     203                        '</a>'
     204                );
     205        }
     206
     207        /** This filter is documented in wp-includes/user.php */
     208        $user = apply_filters( 'wp_authenticate_user', $user, $password );
     209
     210        if ( is_wp_error( $user ) ) {
     211                return $user;
     212        }
     213
     214        if ( ! wp_check_password( $password, $user->user_pass, $user->ID ) ) {
     215                return new WP_Error( 'incorrect_password',
     216                        sprintf(
     217                        /* translators: %s: email address */
     218                                __( '<strong>ERROR</strong>: The password you entered for the email address %s is incorrect.' ),
     219                                '<strong>' . $email . '</strong>'
     220                        ) .
     221                        ' <a href="' . wp_lostpassword_url() . '">' .
     222                        __( 'Lost your password?' ) .
     223                        '</a>'
     224                );
     225        }
     226
     227        return $user;
     228}
     229
     230
     231/**
    177232 * Authenticate the user using the WordPress auth cookie.
    178233 *
    179234 * @since 2.8.0
  • src/wp-login.php

    diff --git src/wp-login.php src/wp-login.php
    index 28dbaae..992b60a 100644
    case 'retrievepassword' : 
    529529
    530530<form name="lostpasswordform" id="lostpasswordform" action="<?php echo esc_url( network_site_url( 'wp-login.php?action=lostpassword', 'login_post' ) ); ?>" method="post">
    531531        <p>
    532                 <label for="user_login" ><?php _e('Username or Email:') ?><br />
     532                <label for="user_login" ><?php _e('Username or email') ?><br />
    533533                <input type="text" name="user_login" id="user_login" class="input" value="<?php echo esc_attr($user_login); ?>" size="20" /></label>
    534534        </p>
    535535        <?php
    default: 
    756756        // If the user wants ssl but the session is not ssl, force a secure cookie.
    757757        if ( !empty($_POST['log']) && !force_ssl_admin() ) {
    758758                $user_name = sanitize_user($_POST['log']);
    759                 if ( $user = get_user_by('login', $user_name) ) {
     759                $user = get_user_by( 'login', $user_name );
     760
     761                if ( ! $user ) {
     762                        $user = get_user_by( 'email', $user_name );
     763                }
     764
     765                if ( $user ) {
    760766                        if ( get_user_option('use_ssl', $user->ID) ) {
    761767                                $secure_cookie = true;
    762768                                force_ssl_admin(true);
    default: 
    882888
    883889<form name="loginform" id="loginform" action="<?php echo esc_url( site_url( 'wp-login.php', 'login_post' ) ); ?>" method="post">
    884890        <p>
    885                 <label for="user_login"><?php _e('Username') ?><br />
     891                <label for="user_login"><?php _e('Username or email') ?><br />
    886892                <input type="text" name="log" id="user_login"<?php echo $aria_describedby_error; ?> class="input" value="<?php echo esc_attr( $user_login ); ?>" size="20" /></label>
    887893        </p>
    888894        <p>
  • tests/phpunit/tests/auth.php

    diff --git tests/phpunit/tests/auth.php tests/phpunit/tests/auth.php
    index c8aa6c6..ea696a6 100644
    class Tests_Auth extends WP_UnitTestCase { 
    311311                $check = check_password_reset_key( '', $this->user->user_login );
    312312                $this->assertInstanceOf( 'WP_Error', $check );
    313313        }
     314
     315        /**
     316         * Ensure users can log in using both their username and their email address.
     317         *
     318         * @ticket 9568
     319         */
     320        function test_log_in_using_email() {
     321                $user_args = array(
     322                        'user_login' => 'johndoe',
     323                        'user_email' => 'mail@example.com',
     324                        'user_pass'  => 'password',
     325                );
     326                $this->factory->user->create( $user_args );
     327
     328                $this->assertInstanceOf( 'WP_User', wp_authenticate( $user_args['user_email'], $user_args['user_pass'] ) );
     329                $this->assertInstanceOf( 'WP_User', wp_authenticate( $user_args['user_login'], $user_args['user_pass'] ) );
     330        }
    314331}