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/functions.php

    r50131 r50140  
    78677867    return abs( (float) $expected - (float) $actual ) <= $precision;
    78687868}
    7869 
    7870 /**
    7871  * Handles sending a password retrieval email to a user.
    7872  *
    7873  * @since 2.5.0
    7874  * @since 5.7.0 Added `$user_login` parameter.
    7875  *
    7876  * Note: prior to 5.7.0 this function was in wp_login.php.
    7877  *
    7878  * @global wpdb         $wpdb       WordPress database abstraction object.
    7879  * @global PasswordHash $wp_hasher  Portable PHP password hashing framework.
    7880  *
    7881  * @param  string       $user_login Optional user_login, default null. Uses
    7882  *                                  `$_POST['user_login']` if `$user_login` not set.
    7883  * @return true|WP_Error True when finished, WP_Error object on error.
    7884  */
    7885 function retrieve_password( $user_login = null ) {
    7886     $errors    = new WP_Error();
    7887     $user_data = false;
    7888 
    7889     // Use the passed $user_login if available, otherwise use $_POST['user_login'].
    7890     if ( ! $user_login && ! empty( $_POST['user_login'] ) ) {
    7891         $user_login = $_POST['user_login'];
    7892     }
    7893 
    7894     if ( empty( $user_login ) ) {
    7895         $errors->add( 'empty_username', __( '<strong>Error</strong>: Please enter a username or email address.' ) );
    7896     } elseif ( strpos( $user_login, '@' ) ) {
    7897         $user_data = get_user_by( 'email', trim( wp_unslash( $user_login ) ) );
    7898         if ( empty( $user_data ) ) {
    7899             $errors->add( 'invalid_email', __( '<strong>Error</strong>: There is no account with that username or email address.' ) );
    7900         }
    7901     } else {
    7902         $user_data = get_user_by( 'login', trim( wp_unslash( $user_login ) ) );
    7903     }
    7904 
    7905     /**
    7906      * Filters the user data during a password reset request.
    7907      *
    7908      * Allows, for example, custom validation using data other than username or email address.
    7909      *
    7910      * @since 5.7.0
    7911      *
    7912      * @param WP_User|false $user_data WP_User object if found, false if the user does not exist.
    7913      * @param WP_Error      $errors    A WP_Error object containing any errors generated
    7914      *                                 by using invalid credentials.
    7915      */
    7916     $user_data = apply_filters( 'lostpassword_user_data', $user_data, $errors );
    7917 
    7918     /**
    7919      * Fires before errors are returned from a password reset request.
    7920      *
    7921      * @since 2.1.0
    7922      * @since 4.4.0 Added the `$errors` parameter.
    7923      * @since 5.4.0 Added the `$user_data` parameter.
    7924      *
    7925      * @param WP_Error      $errors    A WP_Error object containing any errors generated
    7926      *                                 by using invalid credentials.
    7927      * @param WP_User|false $user_data WP_User object if found, false if the user does not exist.
    7928      */
    7929     do_action( 'lostpassword_post', $errors, $user_data );
    7930 
    7931     /**
    7932      * Filters the errors encountered on a password reset request.
    7933      *
    7934      * The filtered WP_Error object may, for example, contain errors for an invalid
    7935      * username or email address. A WP_Error object should always be returned,
    7936      * but may or may not contain errors.
    7937      *
    7938      * If any errors are present in $errors, this will abort the password reset request.
    7939      *
    7940      * @since 5.5.0
    7941      *
    7942      * @param WP_Error      $errors    A WP_Error object containing any errors generated
    7943      *                                 by using invalid credentials.
    7944      * @param WP_User|false $user_data WP_User object if found, false if the user does not exist.
    7945      */
    7946     $errors = apply_filters( 'lostpassword_errors', $errors, $user_data );
    7947 
    7948     if ( $errors->has_errors() ) {
    7949         return $errors;
    7950     }
    7951 
    7952     if ( ! $user_data ) {
    7953         $errors->add( 'invalidcombo', __( '<strong>Error</strong>: There is no account with that username or email address.' ) );
    7954         return $errors;
    7955     }
    7956 
    7957     // Redefining user_login ensures we return the right case in the email.
    7958     $user_login = $user_data->user_login;
    7959     $user_email = $user_data->user_email;
    7960     $key        = get_password_reset_key( $user_data );
    7961 
    7962     if ( is_wp_error( $key ) ) {
    7963         return $key;
    7964     }
    7965 
    7966     if ( is_multisite() ) {
    7967         $site_name = get_network()->site_name;
    7968     } else {
    7969         /*
    7970          * The blogname option is escaped with esc_html on the way into the database
    7971          * in sanitize_option. We want to reverse this for the plain text arena of emails.
    7972          */
    7973         $site_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
    7974     }
    7975 
    7976     $message = __( 'Someone has requested a password reset for the following account:' ) . "\r\n\r\n";
    7977     /* translators: %s: Site name. */
    7978     $message .= sprintf( __( 'Site Name: %s' ), $site_name ) . "\r\n\r\n";
    7979     /* translators: %s: User login. */
    7980     $message .= sprintf( __( 'Username: %s' ), $user_login ) . "\r\n\r\n";
    7981     $message .= __( 'If this was a mistake, ignore this email and nothing will happen.' ) . "\r\n\r\n";
    7982     $message .= __( 'To reset your password, visit the following address:' ) . "\r\n\r\n";
    7983     $message .= network_site_url( "wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user_login ), 'login' ) . "\r\n\r\n";
    7984 
    7985     $requester_ip = $_SERVER['REMOTE_ADDR'];
    7986     if ( $requester_ip ) {
    7987         $message .= sprintf(
    7988             /* translators: %s: IP address of password reset requester. */
    7989             __( 'This password reset request originated from the IP address %s.' ),
    7990             $requester_ip
    7991         ) . "\r\n";
    7992     }
    7993 
    7994     /* translators: Password reset notification email subject. %s: Site title. */
    7995     $title = sprintf( __( '[%s] Password Reset' ), $site_name );
    7996 
    7997     /**
    7998      * Filters the subject of the password reset email.
    7999      *
    8000      * @since 2.8.0
    8001      * @since 4.4.0 Added the `$user_login` and `$user_data` parameters.
    8002      *
    8003      * @param string  $title      Email subject.
    8004      * @param string  $user_login The username for the user.
    8005      * @param WP_User $user_data  WP_User object.
    8006      */
    8007     $title = apply_filters( 'retrieve_password_title', $title, $user_login, $user_data );
    8008 
    8009     /**
    8010      * Filters the message body of the password reset mail.
    8011      *
    8012      * If the filtered message is empty, the password reset email will not be sent.
    8013      *
    8014      * @since 2.8.0
    8015      * @since 4.1.0 Added `$user_login` and `$user_data` parameters.
    8016      *
    8017      * @param string  $message    Email message.
    8018      * @param string  $key        The activation key.
    8019      * @param string  $user_login The username for the user.
    8020      * @param WP_User $user_data  WP_User object.
    8021      */
    8022     $message = apply_filters( 'retrieve_password_message', $message, $key, $user_login, $user_data );
    8023 
    8024     if ( $message && ! wp_mail( $user_email, wp_specialchars_decode( $title ), $message ) ) {
    8025         $errors->add(
    8026             'retrieve_password_email_failure',
    8027             sprintf(
    8028                 /* translators: %s: Documentation URL. */
    8029                 __( '<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>.' ),
    8030                 esc_url( __( 'https://wordpress.org/support/article/resetting-your-password/' ) )
    8031             )
    8032         );
    8033         return $errors;
    8034     }
    8035 
    8036     return true;
    8037 }
Note: See TracChangeset for help on using the changeset viewer.