Make WordPress Core

Changeset 52604


Ignore:
Timestamp:
01/19/2022 12:15:13 PM (17 months ago)
Author:
audrasjb
Message:

Users: Add new hooks to filter retrieve password emails.

This change introduces two new hooks to help developers to filter retrieve password emails:

  • send_retrieve_password_email can be used to filter whether to send the retrieve password email;
  • retrieve_password_notification_email can be used to filter the contents of the reset password notification email sent to the user.

This changesets also adds unit tests for these new filters.

Props connapptivity, costdev, audrasjb, johnbillion.
Fixes #54690.

Location:
trunk
Files:
1 added
1 edited

Legend:

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

    r52398 r52604  
    29392939    }
    29402940
     2941    /**
     2942     * Filter whether to send the retrieve password email.
     2943     *
     2944     * @since 6.0.0
     2945     *
     2946     * @param bool $send False to prevent sending. Default: true
     2947     */
     2948    if ( ! apply_filters( 'send_retrieve_password_email', true ) ) {
     2949        return true;
     2950    }
     2951
    29412952    // Redefining user_login ensures we return the right case in the email.
    29422953    $user_login = $user_data->user_login;
     
    30133024    $message = apply_filters( 'retrieve_password_message', $message, $key, $user_login, $user_data );
    30143025
     3026    // Short-circuit on falsey $message value for backwards compatibility.
     3027    if ( ! $message ) {
     3028        return true;
     3029    }
     3030
     3031    // Wrap the single notification email arguments in an array to pass them to the retrieve_password_notification_email filter.
     3032    $defaults = array(
     3033        'to'      => $user_email,
     3034        'subject' => $title,
     3035        'message' => $message,
     3036        'headers' => '',
     3037    );
     3038
     3039    $data = compact( 'key', 'user_login', 'user_data' );
     3040
     3041    /**
     3042     * Filter the contents of the reset password notification email sent to the user.
     3043     *
     3044     * @since 6.0.0
     3045     *
     3046     * @param array $defaults {
     3047     *     The default notification email arguments. Used to build wp_mail().
     3048     *
     3049     *     @type string $to      The intended recipient - user email address.
     3050     *     @type string $subject The subject of the email.
     3051     *     @type string $message The body of the email.
     3052     *     @type string $headers The headers of the email.
     3053     * }
     3054     * @param array $data {
     3055     *     Additional information for extenders.
     3056     *
     3057     *     @type string  $key        The activation key.
     3058     *     @type string  $user_login The username for the user.
     3059     *     @type WP_User $user_data  WP_User object.
     3060     * }
     3061     */
     3062    $notification_email = apply_filters( 'retrieve_password_notification_email', $defaults, $data );
     3063
    30153064    if ( $switched_locale ) {
    30163065        restore_previous_locale();
    30173066    }
    30183067
    3019     if ( $message && ! wp_mail( $user_email, wp_specialchars_decode( $title ), $message ) ) {
     3068    if ( is_array( $notification_email ) ) {
     3069        // Force key order and merge defaults in case any value is missing in the filtered array.
     3070        $notification_email = array_merge( $defaults, $notification_email );
     3071    } else {
     3072        $notification_email = $defaults;
     3073    }
     3074
     3075    list( $to, $subject, $message, $headers ) = array_values( $notification_email );
     3076
     3077    $subject = wp_specialchars_decode( $subject );
     3078
     3079    if ( ! wp_mail( $to, $subject, $message, $headers ) ) {
    30203080        $errors->add(
    30213081            'retrieve_password_email_failure',
Note: See TracChangeset for help on using the changeset viewer.