WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/02/2021 12:35:35 PM (8 months ago)
Author:
SergeyBiryukov
Message:

Users: Move retrieve_password() to wp-includes/user.php, for consistency with other user functions.

Follow-up to [25231], [50129].

Props jfarthing84, dimadin.
See #34281, #31039.

File:
1 edited

Legend:

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

    r49970 r50140  
    26572657
    26582658/**
     2659 * Handles sending a password retrieval email to a user.
     2660 *
     2661 * @since 2.5.0
     2662 * @since 5.7.0 Added `$user_login` parameter.
     2663 *
     2664 * @global wpdb         $wpdb       WordPress database abstraction object.
     2665 * @global PasswordHash $wp_hasher  Portable PHP password hashing framework.
     2666 *
     2667 * @param  string       $user_login Optional user_login, default null. Uses
     2668 *                                  `$_POST['user_login']` if `$user_login` not set.
     2669 * @return true|WP_Error True when finished, WP_Error object on error.
     2670 */
     2671function retrieve_password( $user_login = null ) {
     2672    $errors    = new WP_Error();
     2673    $user_data = false;
     2674
     2675    // Use the passed $user_login if available, otherwise use $_POST['user_login'].
     2676    if ( ! $user_login && ! empty( $_POST['user_login'] ) ) {
     2677        $user_login = $_POST['user_login'];
     2678    }
     2679
     2680    if ( empty( $user_login ) ) {
     2681        $errors->add( 'empty_username', __( '<strong>Error</strong>: Please enter a username or email address.' ) );
     2682    } elseif ( strpos( $user_login, '@' ) ) {
     2683        $user_data = get_user_by( 'email', trim( wp_unslash( $user_login ) ) );
     2684        if ( empty( $user_data ) ) {
     2685            $errors->add( 'invalid_email', __( '<strong>Error</strong>: There is no account with that username or email address.' ) );
     2686        }
     2687    } else {
     2688        $user_data = get_user_by( 'login', trim( wp_unslash( $user_login ) ) );
     2689    }
     2690
     2691    /**
     2692     * Filters the user data during a password reset request.
     2693     *
     2694     * Allows, for example, custom validation using data other than username or email address.
     2695     *
     2696     * @since 5.7.0
     2697     *
     2698     * @param WP_User|false $user_data WP_User object if found, false if the user does not exist.
     2699     * @param WP_Error      $errors    A WP_Error object containing any errors generated
     2700     *                                 by using invalid credentials.
     2701     */
     2702    $user_data = apply_filters( 'lostpassword_user_data', $user_data, $errors );
     2703
     2704    /**
     2705     * Fires before errors are returned from a password reset request.
     2706     *
     2707     * @since 2.1.0
     2708     * @since 4.4.0 Added the `$errors` parameter.
     2709     * @since 5.4.0 Added the `$user_data` parameter.
     2710     *
     2711     * @param WP_Error      $errors    A WP_Error object containing any errors generated
     2712     *                                 by using invalid credentials.
     2713     * @param WP_User|false $user_data WP_User object if found, false if the user does not exist.
     2714     */
     2715    do_action( 'lostpassword_post', $errors, $user_data );
     2716
     2717    /**
     2718     * Filters the errors encountered on a password reset request.
     2719     *
     2720     * The filtered WP_Error object may, for example, contain errors for an invalid
     2721     * username or email address. A WP_Error object should always be returned,
     2722     * but may or may not contain errors.
     2723     *
     2724     * If any errors are present in $errors, this will abort the password reset request.
     2725     *
     2726     * @since 5.5.0
     2727     *
     2728     * @param WP_Error      $errors    A WP_Error object containing any errors generated
     2729     *                                 by using invalid credentials.
     2730     * @param WP_User|false $user_data WP_User object if found, false if the user does not exist.
     2731     */
     2732    $errors = apply_filters( 'lostpassword_errors', $errors, $user_data );
     2733
     2734    if ( $errors->has_errors() ) {
     2735        return $errors;
     2736    }
     2737
     2738    if ( ! $user_data ) {
     2739        $errors->add( 'invalidcombo', __( '<strong>Error</strong>: There is no account with that username or email address.' ) );
     2740        return $errors;
     2741    }
     2742
     2743    // Redefining user_login ensures we return the right case in the email.
     2744    $user_login = $user_data->user_login;
     2745    $user_email = $user_data->user_email;
     2746    $key        = get_password_reset_key( $user_data );
     2747
     2748    if ( is_wp_error( $key ) ) {
     2749        return $key;
     2750    }
     2751
     2752    if ( is_multisite() ) {
     2753        $site_name = get_network()->site_name;
     2754    } else {
     2755        /*
     2756         * The blogname option is escaped with esc_html on the way into the database
     2757         * in sanitize_option. We want to reverse this for the plain text arena of emails.
     2758         */
     2759        $site_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
     2760    }
     2761
     2762    $message = __( 'Someone has requested a password reset for the following account:' ) . "\r\n\r\n";
     2763    /* translators: %s: Site name. */
     2764    $message .= sprintf( __( 'Site Name: %s' ), $site_name ) . "\r\n\r\n";
     2765    /* translators: %s: User login. */
     2766    $message .= sprintf( __( 'Username: %s' ), $user_login ) . "\r\n\r\n";
     2767    $message .= __( 'If this was a mistake, ignore this email and nothing will happen.' ) . "\r\n\r\n";
     2768    $message .= __( 'To reset your password, visit the following address:' ) . "\r\n\r\n";
     2769    $message .= network_site_url( "wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user_login ), 'login' ) . "\r\n\r\n";
     2770
     2771    $requester_ip = $_SERVER['REMOTE_ADDR'];
     2772    if ( $requester_ip ) {
     2773        $message .= sprintf(
     2774            /* translators: %s: IP address of password reset requester. */
     2775            __( 'This password reset request originated from the IP address %s.' ),
     2776            $requester_ip
     2777        ) . "\r\n";
     2778    }
     2779
     2780    /* translators: Password reset notification email subject. %s: Site title. */
     2781    $title = sprintf( __( '[%s] Password Reset' ), $site_name );
     2782
     2783    /**
     2784     * Filters the subject of the password reset email.
     2785     *
     2786     * @since 2.8.0
     2787     * @since 4.4.0 Added the `$user_login` and `$user_data` parameters.
     2788     *
     2789     * @param string  $title      Email subject.
     2790     * @param string  $user_login The username for the user.
     2791     * @param WP_User $user_data  WP_User object.
     2792     */
     2793    $title = apply_filters( 'retrieve_password_title', $title, $user_login, $user_data );
     2794
     2795    /**
     2796     * Filters the message body of the password reset mail.
     2797     *
     2798     * If the filtered message is empty, the password reset email will not be sent.
     2799     *
     2800     * @since 2.8.0
     2801     * @since 4.1.0 Added `$user_login` and `$user_data` parameters.
     2802     *
     2803     * @param string  $message    Email message.
     2804     * @param string  $key        The activation key.
     2805     * @param string  $user_login The username for the user.
     2806     * @param WP_User $user_data  WP_User object.
     2807     */
     2808    $message = apply_filters( 'retrieve_password_message', $message, $key, $user_login, $user_data );
     2809
     2810    if ( $message && ! wp_mail( $user_email, wp_specialchars_decode( $title ), $message ) ) {
     2811        $errors->add(
     2812            'retrieve_password_email_failure',
     2813            sprintf(
     2814                /* translators: %s: Documentation URL. */
     2815                __( '<strong>Error</strong>: The email could not be sent. Your site may not be correctly configured to send emails. <a href="%s">Get support for resetting your password</a>.' ),
     2816                esc_url( __( 'https://wordpress.org/support/article/resetting-your-password/' ) )
     2817            )
     2818        );
     2819        return $errors;
     2820    }
     2821
     2822    return true;
     2823}
     2824
     2825/**
    26592826 * Handles resetting the user's password.
    26602827 *
Note: See TracChangeset for help on using the changeset viewer.