WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/27/2017 02:09:51 AM (12 months ago)
Author:
johnbillion
Message:

Users: Require a confirmation link in an email to be clicked when a user attempts to change their email address.

This adds this previously Multisite-only functionality to single site installations too. This change prevents accidental or erroneous email address changes from potentially locking users out of their account.

Props rodrigosprimo, tharsheblows, johnbillion

Fixes #16470

File:
1 edited

Legend:

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

    r41158 r41163  
    25912591    return $current_user;
    25922592}
     2593
     2594/**
     2595 * Sends an email when an email address change is requested.
     2596 *
     2597 * @since 3.0.0
     2598 * @since 4.9.0 This function was moved from wp-admin/includes/ms.php so it's no longer Multisite specific.
     2599 *
     2600 * @global WP_Error $errors WP_Error object.
     2601 * @global wpdb     $wpdb   WordPress database object.
     2602 */
     2603function send_confirmation_on_profile_email() {
     2604    global $errors, $wpdb;
     2605
     2606    $current_user = wp_get_current_user();
     2607    if ( ! is_object( $errors ) ) {
     2608        $errors = new WP_Error();
     2609    }
     2610
     2611    if ( $current_user->ID != $_POST['user_id'] ) {
     2612        return false;
     2613    }
     2614
     2615    if ( $current_user->user_email != $_POST['email'] ) {
     2616        if ( ! is_email( $_POST['email'] ) ) {
     2617            $errors->add( 'user_email', __( "<strong>ERROR</strong>: The email address isn&#8217;t correct." ), array(
     2618                'form-field' => 'email',
     2619            ) );
     2620
     2621            return;
     2622        }
     2623
     2624        if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_email FROM {$wpdb->users} WHERE user_email=%s", $_POST['email'] ) ) ) {
     2625            $errors->add( 'user_email', __( "<strong>ERROR</strong>: The email address is already used." ), array(
     2626                'form-field' => 'email',
     2627            ) );
     2628            delete_user_meta( $current_user->ID, '_new_email' );
     2629
     2630            return;
     2631        }
     2632
     2633        $hash           = md5( $_POST['email'] . time() . mt_rand() );
     2634        $new_user_email = array(
     2635            'hash'     => $hash,
     2636            'newemail' => $_POST['email'],
     2637        );
     2638        update_user_meta( $current_user->ID, '_new_email', $new_user_email );
     2639
     2640        /* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
     2641        $email_text = __( 'Howdy ###USERNAME###,
     2642
     2643You recently requested to have the email address on your account changed.
     2644
     2645If this is correct, please click on the following link to change it:
     2646###ADMIN_URL###
     2647
     2648You can safely ignore and delete this email if you do not want to
     2649take this action.
     2650
     2651This email has been sent to ###EMAIL###
     2652
     2653Regards,
     2654All at ###SITENAME###
     2655###SITEURL###' );
     2656
     2657        /**
     2658         * Filters the email text sent when a user changes emails.
     2659         *
     2660         * The following strings have a special meaning and will get replaced dynamically:
     2661         * ###USERNAME###  The current user's username.
     2662         * ###ADMIN_URL### The link to click on to confirm the email change.
     2663         * ###EMAIL###     The new email.
     2664         * ###SITENAME###  The name of the site.
     2665         * ###SITEURL###   The URL to the site.
     2666         *
     2667         * @since MU
     2668         * @since 4.9.0 This filter is no longer Multisite specific.
     2669         *
     2670         * @param string $email_text     Text in the email.
     2671         * @param string $new_user_email New user email that the current user has changed to.
     2672         */
     2673        $content = apply_filters( 'new_user_email_content', $email_text, $new_user_email );
     2674
     2675        $content = str_replace( '###USERNAME###', $current_user->user_login, $content );
     2676        $content = str_replace( '###ADMIN_URL###', esc_url( admin_url( 'profile.php?newuseremail=' . $hash ) ), $content );
     2677        $content = str_replace( '###EMAIL###', $_POST['email'], $content );
     2678        $content = str_replace( '###SITENAME###', get_site_option( 'site_name' ), $content );
     2679        $content = str_replace( '###SITEURL###', network_home_url(), $content );
     2680
     2681        wp_mail( $_POST['email'], sprintf( __( '[%s] New Email Address' ), wp_specialchars_decode( get_option( 'blogname' ) ) ), $content );
     2682
     2683        $_POST['email'] = $current_user->user_email;
     2684    }
     2685}
     2686
     2687/**
     2688 * Adds an admin notice alerting the user to check for confirmation email
     2689 * after email address change.
     2690 *
     2691 * @since 3.0.0
     2692 * @since 4.9.0 This function was moved from wp-admin/includes/ms.php so it's no longer Multisite specific.
     2693 *
     2694 * @global string $pagenow
     2695 */
     2696function new_user_email_admin_notice() {
     2697    global $pagenow;
     2698    if ( 'profile.php' === $pagenow && isset( $_GET['updated'] ) && $email = get_user_meta( get_current_user_id(), '_new_email', true ) ) {
     2699        /* translators: %s: New email address */
     2700        echo '<div class="notice notice-info"><p>' . sprintf( __( 'Your email address has not been updated yet. Please check your inbox at %s for a confirmation email.' ), '<code>' . esc_html( $email['newemail'] ) . '</code>' ) . '</p></div>';
     2701    }
     2702}
Note: See TracChangeset for help on using the changeset viewer.