wp/wp-includes/user.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
--- a/wp/wp-includes/user.php	Mon Oct 14 18:06:33 2019 +0200
+++ b/wp/wp-includes/user.php	Mon Oct 14 18:28:13 2019 +0200
@@ -31,21 +31,25 @@
  * @return WP_User|WP_Error WP_User on success, WP_Error on failure.
  */
 function wp_signon( $credentials = array(), $secure_cookie = '' ) {
-	if ( empty($credentials) ) {
+	if ( empty( $credentials ) ) {
 		$credentials = array(); // Back-compat for plugins passing an empty string.
 
-		if ( ! empty($_POST['log']) )
+		if ( ! empty( $_POST['log'] ) ) {
 			$credentials['user_login'] = $_POST['log'];
-		if ( ! empty($_POST['pwd']) )
+		}
+		if ( ! empty( $_POST['pwd'] ) ) {
 			$credentials['user_password'] = $_POST['pwd'];
-		if ( ! empty($_POST['rememberme']) )
+		}
+		if ( ! empty( $_POST['rememberme'] ) ) {
 			$credentials['remember'] = $_POST['rememberme'];
+		}
 	}
 
-	if ( !empty($credentials['remember']) )
+	if ( ! empty( $credentials['remember'] ) ) {
 		$credentials['remember'] = true;
-	else
+	} else {
 		$credentials['remember'] = false;
+	}
 
 	/**
 	 * Fires before the user is authenticated.
@@ -62,8 +66,9 @@
 	 */
 	do_action_ref_array( 'wp_authenticate', array( &$credentials['user_login'], &$credentials['user_password'] ) );
 
-	if ( '' === $secure_cookie )
+	if ( '' === $secure_cookie ) {
 		$secure_cookie = is_ssl();
+	}
 
 	/**
 	 * Filters whether to use a secure sign-on cookie.
@@ -72,32 +77,28 @@
 	 *
 	 * @param bool  $secure_cookie Whether to use a secure sign-on cookie.
 	 * @param array $credentials {
- 	 *     Array of entered sign-on data.
- 	 *
- 	 *     @type string $user_login    Username.
- 	 *     @type string $user_password Password entered.
+	 *     Array of entered sign-on data.
+	 *
+	 *     @type string $user_login    Username.
+	 *     @type string $user_password Password entered.
 	 *     @type bool   $remember      Whether to 'remember' the user. Increases the time
 	 *                                 that the cookie will be kept. Default false.
- 	 * }
+	 * }
 	 */
 	$secure_cookie = apply_filters( 'secure_signon_cookie', $secure_cookie, $credentials );
 
 	global $auth_secure_cookie; // XXX ugly hack to pass this to wp_authenticate_cookie
 	$auth_secure_cookie = $secure_cookie;
 
-	add_filter('authenticate', 'wp_authenticate_cookie', 30, 3);
-
-	$user = wp_authenticate($credentials['user_login'], $credentials['user_password']);
-
-	if ( is_wp_error($user) ) {
-		if ( $user->get_error_codes() == array('empty_username', 'empty_password') ) {
-			$user = new WP_Error('', '');
-		}
-
+	add_filter( 'authenticate', 'wp_authenticate_cookie', 30, 3 );
+
+	$user = wp_authenticate( $credentials['user_login'], $credentials['user_password'] );
+
+	if ( is_wp_error( $user ) ) {
 		return $user;
 	}
 
-	wp_set_auth_cookie($user->ID, $credentials['remember'], $secure_cookie);
+	wp_set_auth_cookie( $user->ID, $credentials['remember'], $secure_cookie );
 	/**
 	 * Fires after the user has successfully logged in.
 	 *
@@ -120,30 +121,34 @@
  * @param string                $password Password for authentication.
  * @return WP_User|WP_Error WP_User on success, WP_Error on failure.
  */
-function wp_authenticate_username_password($user, $username, $password) {
+function wp_authenticate_username_password( $user, $username, $password ) {
 	if ( $user instanceof WP_User ) {
 		return $user;
 	}
 
-	if ( empty($username) || empty($password) ) {
-		if ( is_wp_error( $user ) )
+	if ( empty( $username ) || empty( $password ) ) {
+		if ( is_wp_error( $user ) ) {
 			return $user;
+		}
 
 		$error = new WP_Error();
 
-		if ( empty($username) )
-			$error->add('empty_username', __('<strong>ERROR</strong>: The username field is empty.'));
-
-		if ( empty($password) )
-			$error->add('empty_password', __('<strong>ERROR</strong>: The password field is empty.'));
+		if ( empty( $username ) ) {
+			$error->add( 'empty_username', __( '<strong>ERROR</strong>: The username field is empty.' ) );
+		}
+
+		if ( empty( $password ) ) {
+			$error->add( 'empty_password', __( '<strong>ERROR</strong>: The password field is empty.' ) );
+		}
 
 		return $error;
 	}
 
-	$user = get_user_by('login', $username);
-
-	if ( !$user ) {
-		return new WP_Error( 'invalid_username',
+	$user = get_user_by( 'login', $username );
+
+	if ( ! $user ) {
+		return new WP_Error(
+			'invalid_username',
 			__( '<strong>ERROR</strong>: Invalid username.' ) .
 			' <a href="' . wp_lostpassword_url() . '">' .
 			__( 'Lost your password?' ) .
@@ -161,11 +166,13 @@
 	 * @param string           $password Password to check against the user.
 	 */
 	$user = apply_filters( 'wp_authenticate_user', $user, $password );
-	if ( is_wp_error($user) )
+	if ( is_wp_error( $user ) ) {
 		return $user;
+	}
 
 	if ( ! wp_check_password( $password, $user->user_pass, $user->ID ) ) {
-		return new WP_Error( 'incorrect_password',
+		return new WP_Error(
+			'incorrect_password',
 			sprintf(
 				/* translators: %s: user name */
 				__( '<strong>ERROR</strong>: The password you entered for the username %s is incorrect.' ),
@@ -221,7 +228,8 @@
 	$user = get_user_by( 'email', $email );
 
 	if ( ! $user ) {
-		return new WP_Error( 'invalid_email',
+		return new WP_Error(
+			'invalid_email',
 			__( '<strong>ERROR</strong>: Invalid email address.' ) .
 			' <a href="' . wp_lostpassword_url() . '">' .
 			__( 'Lost your password?' ) .
@@ -237,7 +245,8 @@
 	}
 
 	if ( ! wp_check_password( $password, $user->user_pass, $user->ID ) ) {
-		return new WP_Error( 'incorrect_password',
+		return new WP_Error(
+			'incorrect_password',
 			sprintf(
 				/* translators: %s: email address */
 				__( '<strong>ERROR</strong>: The password you entered for the email address %s is incorrect.' ),
@@ -264,25 +273,28 @@
  * @param string                $password Password. If not empty, cancels the cookie authentication.
  * @return WP_User|WP_Error WP_User on success, WP_Error on failure.
  */
-function wp_authenticate_cookie($user, $username, $password) {
+function wp_authenticate_cookie( $user, $username, $password ) {
 	if ( $user instanceof WP_User ) {
 		return $user;
 	}
 
-	if ( empty($username) && empty($password) ) {
+	if ( empty( $username ) && empty( $password ) ) {
 		$user_id = wp_validate_auth_cookie();
-		if ( $user_id )
-			return new WP_User($user_id);
+		if ( $user_id ) {
+			return new WP_User( $user_id );
+		}
 
 		global $auth_secure_cookie;
 
-		if ( $auth_secure_cookie )
+		if ( $auth_secure_cookie ) {
 			$auth_cookie = SECURE_AUTH_COOKIE;
-		else
+		} else {
 			$auth_cookie = AUTH_COOKIE;
-
-		if ( !empty($_COOKIE[$auth_cookie]) )
-			return new WP_Error('expired_session', __('Please log in again.'));
+		}
+
+		if ( ! empty( $_COOKIE[ $auth_cookie ] ) ) {
+			return new WP_Error( 'expired_session', __( 'Please log in again.' ) );
+		}
 
 		// If the cookie is not set, be silent.
 	}
@@ -311,8 +323,9 @@
 		 */
 		$spammed = apply_filters( 'check_is_user_spammed', is_user_spammy( $user ), $user );
 
-		if ( $spammed )
+		if ( $spammed ) {
 			return new WP_Error( 'spammer_account', __( '<strong>ERROR</strong>: Your account has been marked as a spammer.' ) );
+		}
 	}
 	return $user;
 }
@@ -337,11 +350,11 @@
 		return $user_id;
 	}
 
-	if ( is_blog_admin() || is_network_admin() || empty( $_COOKIE[LOGGED_IN_COOKIE] ) ) {
+	if ( is_blog_admin() || is_network_admin() || empty( $_COOKIE[ LOGGED_IN_COOKIE ] ) ) {
 		return false;
 	}
 
-	return wp_validate_auth_cookie( $_COOKIE[LOGGED_IN_COOKIE], 'logged_in' );
+	return wp_validate_auth_cookie( $_COOKIE[ LOGGED_IN_COOKIE ], 'logged_in' );
 }
 
 /**
@@ -397,11 +410,12 @@
 	global $wpdb;
 
 	$count = array();
-	if ( empty( $users ) || ! is_array( $users ) )
+	if ( empty( $users ) || ! is_array( $users ) ) {
 		return $count;
+	}
 
 	$userlist = implode( ',', array_map( 'absint', $users ) );
-	$where = get_posts_by_author_sql( $post_type, true, null, $public_only );
+	$where    = get_posts_by_author_sql( $post_type, true, null, $public_only );
 
 	$result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N );
 	foreach ( $result as $row ) {
@@ -409,8 +423,9 @@
 	}
 
 	foreach ( $users as $id ) {
-		if ( ! isset( $count[ $id ] ) )
+		if ( ! isset( $count[ $id ] ) ) {
 			$count[ $id ] = 0;
+		}
 	}
 
 	return $count;
@@ -428,8 +443,9 @@
  * @return int The current user's ID, or 0 if no user is logged in.
  */
 function get_current_user_id() {
-	if ( ! function_exists( 'wp_get_current_user' ) )
+	if ( ! function_exists( 'wp_get_current_user' ) ) {
 		return 0;
+	}
 	$user = wp_get_current_user();
 	return ( isset( $user->ID ) ? (int) $user->ID : 0 );
 }
@@ -456,22 +472,26 @@
 function get_user_option( $option, $user = 0, $deprecated = '' ) {
 	global $wpdb;
 
-	if ( !empty( $deprecated ) )
+	if ( ! empty( $deprecated ) ) {
 		_deprecated_argument( __FUNCTION__, '3.0.0' );
-
-	if ( empty( $user ) )
+	}
+
+	if ( empty( $user ) ) {
 		$user = get_current_user_id();
-
-	if ( ! $user = get_userdata( $user ) )
+	}
+
+	if ( ! $user = get_userdata( $user ) ) {
 		return false;
+	}
 
 	$prefix = $wpdb->get_blog_prefix();
-	if ( $user->has_prop( $prefix . $option ) ) // Blog specific
+	if ( $user->has_prop( $prefix . $option ) ) { // Blog specific
 		$result = $user->get( $prefix . $option );
-	elseif ( $user->has_prop( $option ) ) // User specific and cross-blog
+	} elseif ( $user->has_prop( $option ) ) { // User specific and cross-blog
 		$result = $user->get( $option );
-	else
+	} else {
 		$result = false;
+	}
 
 	/**
 	 * Filters a specific user option value.
@@ -511,8 +531,9 @@
 function update_user_option( $user_id, $option_name, $newvalue, $global = false ) {
 	global $wpdb;
 
-	if ( !$global )
+	if ( ! $global ) {
 		$option_name = $wpdb->get_blog_prefix() . $option_name;
+	}
 
 	return update_user_meta( $user_id, $option_name, $newvalue );
 }
@@ -537,8 +558,9 @@
 function delete_user_option( $user_id, $option_name, $global = false ) {
 	global $wpdb;
 
-	if ( !$global )
+	if ( ! $global ) {
 		$option_name = $wpdb->get_blog_prefix() . $option_name;
+	}
 	return delete_user_meta( $user_id, $option_name );
 }
 
@@ -555,10 +577,10 @@
  */
 function get_users( $args = array() ) {
 
-	$args = wp_parse_args( $args );
+	$args                = wp_parse_args( $args );
 	$args['count_total'] = false;
 
-	$user_search = new WP_User_Query($args);
+	$user_search = new WP_User_Query( $args );
 
 	return (array) $user_search->get_results();
 }
@@ -567,7 +589,7 @@
  * Get the sites a user belongs to.
  *
  * @since 3.0.0
- * @since 4.7.0 Converted to use get_sites().
+ * @since 4.7.0 Converted to use `get_sites()`.
  *
  * @global wpdb $wpdb WordPress database abstraction object.
  *
@@ -583,8 +605,9 @@
 	$user_id = (int) $user_id;
 
 	// Logged out users can't have sites
-	if ( empty( $user_id ) )
+	if ( empty( $user_id ) ) {
 		return array();
+	}
 
 	/**
 	 * Filters the list of a user's sites before it is populated.
@@ -606,21 +629,22 @@
 	}
 
 	$keys = get_user_meta( $user_id );
-	if ( empty( $keys ) )
+	if ( empty( $keys ) ) {
 		return array();
+	}
 
 	if ( ! is_multisite() ) {
-		$site_id = get_current_blog_id();
-		$sites = array( $site_id => new stdClass );
+		$site_id                        = get_current_blog_id();
+		$sites                          = array( $site_id => new stdClass );
 		$sites[ $site_id ]->userblog_id = $site_id;
-		$sites[ $site_id ]->blogname = get_option('blogname');
-		$sites[ $site_id ]->domain = '';
-		$sites[ $site_id ]->path = '';
-		$sites[ $site_id ]->site_id = 1;
-		$sites[ $site_id ]->siteurl = get_option('siteurl');
-		$sites[ $site_id ]->archived = 0;
-		$sites[ $site_id ]->spam = 0;
-		$sites[ $site_id ]->deleted = 0;
+		$sites[ $site_id ]->blogname    = get_option( 'blogname' );
+		$sites[ $site_id ]->domain      = '';
+		$sites[ $site_id ]->path        = '';
+		$sites[ $site_id ]->site_id     = 1;
+		$sites[ $site_id ]->siteurl     = get_option( 'siteurl' );
+		$sites[ $site_id ]->archived    = 0;
+		$sites[ $site_id ]->spam        = 0;
+		$sites[ $site_id ]->deleted     = 0;
 		return $sites;
 	}
 
@@ -634,13 +658,16 @@
 	$keys = array_keys( $keys );
 
 	foreach ( $keys as $key ) {
-		if ( 'capabilities' !== substr( $key, -12 ) )
+		if ( 'capabilities' !== substr( $key, -12 ) ) {
 			continue;
-		if ( $wpdb->base_prefix && 0 !== strpos( $key, $wpdb->base_prefix ) )
+		}
+		if ( $wpdb->base_prefix && 0 !== strpos( $key, $wpdb->base_prefix ) ) {
 			continue;
+		}
 		$site_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key );
-		if ( ! is_numeric( $site_id ) )
+		if ( ! is_numeric( $site_id ) ) {
 			continue;
+		}
 
 		$site_ids[] = (int) $site_id;
 	}
@@ -649,8 +676,9 @@
 
 	if ( ! empty( $site_ids ) ) {
 		$args = array(
-			'number'   => '',
-			'site__in' => $site_ids,
+			'number'                 => '',
+			'site__in'               => $site_ids,
+			'update_site_meta_cache' => false,
 		);
 		if ( ! $all ) {
 			$args['archived'] = 0;
@@ -766,8 +794,8 @@
  * @param bool   $unique     Optional. Whether the same key should not be added. Default false.
  * @return int|false Meta ID on success, false on failure.
  */
-function add_user_meta($user_id, $meta_key, $meta_value, $unique = false) {
-	return add_metadata('user', $user_id, $meta_key, $meta_value, $unique);
+function add_user_meta( $user_id, $meta_key, $meta_value, $unique = false ) {
+	return add_metadata( 'user', $user_id, $meta_key, $meta_value, $unique );
 }
 
 /**
@@ -785,8 +813,8 @@
  * @param mixed  $meta_value Optional. Metadata value.
  * @return bool True on success, false on failure.
  */
-function delete_user_meta($user_id, $meta_key, $meta_value = '') {
-	return delete_metadata('user', $user_id, $meta_key, $meta_value);
+function delete_user_meta( $user_id, $meta_key, $meta_value = '' ) {
+	return delete_metadata( 'user', $user_id, $meta_key, $meta_value );
 }
 
 /**
@@ -800,8 +828,8 @@
  * @param bool   $single  Whether to return a single value.
  * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true.
  */
-function get_user_meta($user_id, $key = '', $single = false) {
-	return get_metadata('user', $user_id, $key, $single);
+function get_user_meta( $user_id, $key = '', $single = false ) {
+	return get_metadata( 'user', $user_id, $key, $single );
 }
 
 /**
@@ -821,8 +849,8 @@
  * @param mixed  $prev_value Optional. Previous value to check before removing.
  * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
  */
-function update_user_meta($user_id, $meta_key, $meta_value, $prev_value = '') {
-	return update_metadata('user', $user_id, $meta_key, $meta_value, $prev_value);
+function update_user_meta( $user_id, $meta_key, $meta_value, $prev_value = '' ) {
+	return update_metadata( 'user', $user_id, $meta_key, $meta_value, $prev_value );
 }
 
 /**
@@ -851,8 +879,26 @@
 	if ( ! $site_id ) {
 		$site_id = get_current_blog_id();
 	}
+
+	/**
+	 * Filter the user count before queries are run. Return a non-null value to cause count_users()
+	 * to return early.
+	 *
+	 * @since 5.1.0
+	 *
+	 * @param null|string $result   Default null.
+	 * @param string      $strategy Optional. The computational strategy to use when counting the users.
+	 *                              Accepts either 'time' or 'memory'. Default 'time'.
+	 * @param int|null    $site_id  Optional. The site ID to count users for. Defaults to the current site.
+	 */
+	$pre = apply_filters( 'pre_count_users', null, $strategy, $site_id );
+
+	if ( null !== $pre ) {
+		return $pre;
+	}
+
 	$blog_prefix = $wpdb->get_blog_prefix( $site_id );
-	$result = array();
+	$result      = array();
 
 	if ( 'time' == $strategy ) {
 		if ( is_multisite() && $site_id != get_current_blog_id() ) {
@@ -866,33 +912,36 @@
 		// Build a CPU-intensive query that will return concise information.
 		$select_count = array();
 		foreach ( $avail_roles as $this_role => $name ) {
-			$select_count[] = $wpdb->prepare( "COUNT(NULLIF(`meta_value` LIKE %s, false))", '%' . $wpdb->esc_like( '"' . $this_role . '"' ) . '%');
+			$select_count[] = $wpdb->prepare( 'COUNT(NULLIF(`meta_value` LIKE %s, false))', '%' . $wpdb->esc_like( '"' . $this_role . '"' ) . '%' );
 		}
 		$select_count[] = "COUNT(NULLIF(`meta_value` = 'a:0:{}', false))";
-		$select_count = implode(', ', $select_count);
+		$select_count   = implode( ', ', $select_count );
 
 		// Add the meta_value index to the selection list, then run the query.
-		$row = $wpdb->get_row( "
+		$row = $wpdb->get_row(
+			"
 			SELECT {$select_count}, COUNT(*)
 			FROM {$wpdb->usermeta}
 			INNER JOIN {$wpdb->users} ON user_id = ID
 			WHERE meta_key = '{$blog_prefix}capabilities'
-		", ARRAY_N );
+		",
+			ARRAY_N
+		);
 
 		// Run the previous loop again to associate results with role names.
-		$col = 0;
+		$col         = 0;
 		$role_counts = array();
 		foreach ( $avail_roles as $this_role => $name ) {
-			$count = (int) $row[$col++];
-			if ($count > 0) {
-				$role_counts[$this_role] = $count;
+			$count = (int) $row[ $col++ ];
+			if ( $count > 0 ) {
+				$role_counts[ $this_role ] = $count;
 			}
 		}
 
-		$role_counts['none'] = (int) $row[$col++];
+		$role_counts['none'] = (int) $row[ $col++ ];
 
 		// Get the meta_value index from the end of the result set.
-		$total_users = (int) $row[$col];
+		$total_users = (int) $row[ $col ];
 
 		$result['total_users'] = $total_users;
 		$result['avail_roles'] =& $role_counts;
@@ -901,25 +950,28 @@
 			'none' => 0,
 		);
 
-		$users_of_blog = $wpdb->get_col( "
+		$users_of_blog = $wpdb->get_col(
+			"
 			SELECT meta_value
 			FROM {$wpdb->usermeta}
 			INNER JOIN {$wpdb->users} ON user_id = ID
 			WHERE meta_key = '{$blog_prefix}capabilities'
-		" );
+		"
+		);
 
 		foreach ( $users_of_blog as $caps_meta ) {
-			$b_roles = maybe_unserialize($caps_meta);
-			if ( ! is_array( $b_roles ) )
+			$b_roles = maybe_unserialize( $caps_meta );
+			if ( ! is_array( $b_roles ) ) {
 				continue;
+			}
 			if ( empty( $b_roles ) ) {
 				$avail_roles['none']++;
 			}
 			foreach ( $b_roles as $b_role => $val ) {
-				if ( isset($avail_roles[$b_role]) ) {
-					$avail_roles[$b_role]++;
+				if ( isset( $avail_roles[ $b_role ] ) ) {
+					$avail_roles[ $b_role ]++;
 				} else {
-					$avail_roles[$b_role] = 1;
+					$avail_roles[ $b_role ] = 1;
 				}
 			}
 		}
@@ -950,29 +1002,30 @@
  * @global string  $user_url      The url in the user's profile
  * @global string  $user_identity The display name of the user
  *
- * @param int $for_user_id Optional. User ID to set up global data.
+ * @param int $for_user_id Optional. User ID to set up global data. Default 0.
  */
-function setup_userdata($for_user_id = '') {
+function setup_userdata( $for_user_id = 0 ) {
 	global $user_login, $userdata, $user_level, $user_ID, $user_email, $user_url, $user_identity;
 
-	if ( '' == $for_user_id )
+	if ( ! $for_user_id ) {
 		$for_user_id = get_current_user_id();
+	}
 	$user = get_userdata( $for_user_id );
 
 	if ( ! $user ) {
-		$user_ID = 0;
+		$user_ID    = 0;
 		$user_level = 0;
-		$userdata = null;
+		$userdata   = null;
 		$user_login = $user_email = $user_url = $user_identity = '';
 		return;
 	}
 
-	$user_ID    = (int) $user->ID;
-	$user_level = (int) $user->user_level;
-	$userdata   = $user;
-	$user_login = $user->user_login;
-	$user_email = $user->user_email;
-	$user_url   = $user->user_url;
+	$user_ID       = (int) $user->ID;
+	$user_level    = (int) $user->user_level;
+	$userdata      = $user;
+	$user_login    = $user->user_login;
+	$user_email    = $user->user_email;
+	$user_url      = $user->user_url;
 	$user_identity = $user->display_name;
 }
 
@@ -1041,16 +1094,27 @@
  */
 function wp_dropdown_users( $args = '' ) {
 	$defaults = array(
-		'show_option_all' => '', 'show_option_none' => '', 'hide_if_only_one_author' => '',
-		'orderby' => 'display_name', 'order' => 'ASC',
-		'include' => '', 'exclude' => '', 'multi' => 0,
-		'show' => 'display_name', 'echo' => 1,
-		'selected' => 0, 'name' => 'user', 'class' => '', 'id' => '',
-		'blog_id' => get_current_blog_id(), 'who' => '', 'include_selected' => false,
-		'option_none_value' => -1,
-		'role' => '',
-		'role__in' => array(),
-		'role__not_in' => array(),
+		'show_option_all'         => '',
+		'show_option_none'        => '',
+		'hide_if_only_one_author' => '',
+		'orderby'                 => 'display_name',
+		'order'                   => 'ASC',
+		'include'                 => '',
+		'exclude'                 => '',
+		'multi'                   => 0,
+		'show'                    => 'display_name',
+		'echo'                    => 1,
+		'selected'                => 0,
+		'name'                    => 'user',
+		'class'                   => '',
+		'id'                      => '',
+		'blog_id'                 => get_current_blog_id(),
+		'who'                     => '',
+		'include_selected'        => false,
+		'option_none_value'       => -1,
+		'role'                    => '',
+		'role__in'                => array(),
+		'role__not_in'            => array(),
 	);
 
 	$defaults['selected'] = is_author() ? get_query_var( 'author' ) : 0;
@@ -1070,8 +1134,8 @@
 
 	$query_args['fields'] = $fields;
 
-	$show_option_all = $r['show_option_all'];
-	$show_option_none = $r['show_option_none'];
+	$show_option_all   = $r['show_option_all'];
+	$show_option_none  = $r['show_option_none'];
 	$option_none_value = $r['option_none_value'];
 
 	/**
@@ -1102,12 +1166,12 @@
 
 		if ( $show_option_none ) {
 			$_selected = selected( $option_none_value, $r['selected'], false );
-			$output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$_selected>$show_option_none</option>\n";
+			$output   .= "\t<option value='" . esc_attr( $option_none_value ) . "'$_selected>$show_option_none</option>\n";
 		}
 
 		if ( $r['include_selected'] && ( $r['selected'] > 0 ) ) {
 			$found_selected = false;
-			$r['selected'] = (int) $r['selected'];
+			$r['selected']  = (int) $r['selected'];
 			foreach ( (array) $users as $user ) {
 				$user->ID = (int) $user->ID;
 				if ( $user->ID === $r['selected'] ) {
@@ -1131,10 +1195,10 @@
 			}
 
 			$_selected = selected( $user->ID, $r['selected'], false );
-			$output .= "\t<option value='$user->ID'$_selected>" . esc_html( $display ) . "</option>\n";
+			$output   .= "\t<option value='$user->ID'$_selected>" . esc_html( $display ) . "</option>\n";
 		}
 
-		$output .= "</select>";
+		$output .= '</select>';
 	}
 
 	/**
@@ -1168,16 +1232,19 @@
  *                        'attribute' and 'js'.
  * @return mixed Sanitized value.
  */
-function sanitize_user_field($field, $value, $user_id, $context) {
-	$int_fields = array('ID');
-	if ( in_array($field, $int_fields) )
+function sanitize_user_field( $field, $value, $user_id, $context ) {
+	$int_fields = array( 'ID' );
+	if ( in_array( $field, $int_fields ) ) {
 		$value = (int) $value;
-
-	if ( 'raw' == $context )
+	}
+
+	if ( 'raw' == $context ) {
 		return $value;
-
-	if ( !is_string($value) && !is_numeric($value) )
+	}
+
+	if ( ! is_string( $value ) && ! is_numeric( $value ) ) {
 		return $value;
+	}
 
 	$prefixed = false !== strpos( $field, 'user_' );
 
@@ -1202,10 +1269,11 @@
 			$value = apply_filters( "edit_user_{$field}", $value, $user_id );
 		}
 
-		if ( 'description' == $field )
+		if ( 'description' == $field ) {
 			$value = esc_html( $value ); // textarea_escaped?
-		else
-			$value = esc_attr($value);
+		} else {
+			$value = esc_attr( $value );
+		}
 	} elseif ( 'db' == $context ) {
 		if ( $prefixed ) {
 			/** This filter is documented in wp-includes/post.php */
@@ -1217,7 +1285,7 @@
 			 *
 			 * The dynamic portion of the hook name, `$field`, refers to the prefixed user
 			 * field being filtered, such as 'user_login', 'user_email', 'first_name', etc.
- 			 *
+			 *
 			 * @since 2.9.0
 			 *
 			 * @param mixed $value Value of the prefixed user field.
@@ -1248,8 +1316,9 @@
 		}
 	}
 
-	if ( 'user_url' == $field )
-		$value = esc_url($value);
+	if ( 'user_url' == $field ) {
+		$value = esc_url( $value );
+	}
 
 	if ( 'attribute' == $context ) {
 		$value = esc_attr( $value );
@@ -1276,10 +1345,10 @@
 		$user = $user->data;
 	}
 
-	wp_cache_add($user->ID, $user, 'users');
-	wp_cache_add($user->user_login, $user->ID, 'userlogins');
-	wp_cache_add($user->user_email, $user->ID, 'useremail');
-	wp_cache_add($user->user_nicename, $user->ID, 'userslugs');
+	wp_cache_add( $user->ID, $user, 'users' );
+	wp_cache_add( $user->user_login, $user->ID, 'userlogins' );
+	wp_cache_add( $user->user_email, $user->ID, 'useremail' );
+	wp_cache_add( $user->user_nicename, $user->ID, 'userslugs' );
 }
 
 /**
@@ -1291,11 +1360,13 @@
  * @param WP_User|int $user User object or ID to be cleaned from the cache
  */
 function clean_user_cache( $user ) {
-	if ( is_numeric( $user ) )
+	if ( is_numeric( $user ) ) {
 		$user = new WP_User( $user );
-
-	if ( ! $user->exists() )
+	}
+
+	if ( ! $user->exists() ) {
 		return;
+	}
 
 	wp_cache_delete( $user->ID, 'users' );
 	wp_cache_delete( $user->user_login, 'userlogins' );
@@ -1314,7 +1385,11 @@
 }
 
 /**
- * Checks whether the given username exists.
+ * Determines whether the given username exists.
+ *
+ * For more information on this and similar theme functions, check out
+ * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
+ * Conditional Tags} article in the Theme Developer Handbook.
  *
  * @since 2.0.0
  *
@@ -1322,7 +1397,8 @@
  * @return int|false The user's ID on success, and false on failure.
  */
 function username_exists( $username ) {
-	if ( $user = get_user_by( 'login', $username ) ) {
+	$user = get_user_by( 'login', $username );
+	if ( $user ) {
 		$user_id = $user->ID;
 	} else {
 		$user_id = false;
@@ -1340,7 +1416,11 @@
 }
 
 /**
- * Checks whether the given email exists.
+ * Determines whether the given email exists.
+ *
+ * For more information on this and similar theme functions, check out
+ * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
+ * Conditional Tags} article in the Theme Developer Handbook.
  *
  * @since 2.1.0
  *
@@ -1348,7 +1428,8 @@
  * @return int|false The user's ID on success, and false on failure.
  */
 function email_exists( $email ) {
-	if ( $user = get_user_by( 'email', $email) ) {
+	$user = get_user_by( 'email', $email );
+	if ( $user ) {
 		return $user->ID;
 	}
 	return false;
@@ -1365,7 +1446,7 @@
  */
 function validate_username( $username ) {
 	$sanitized = sanitize_user( $username, true );
-	$valid = ( $sanitized == $username && ! empty( $sanitized ) );
+	$valid     = ( $sanitized == $username && ! empty( $sanitized ) );
 
 	/**
 	 * Filters whether the provided username is valid or not.
@@ -1443,8 +1524,8 @@
 
 	// Are we updating or creating?
 	if ( ! empty( $userdata['ID'] ) ) {
-		$ID = (int) $userdata['ID'];
-		$update = true;
+		$ID            = (int) $userdata['ID'];
+		$update        = true;
 		$old_user_data = get_userdata( $ID );
 
 		if ( ! $old_user_data ) {
@@ -1477,7 +1558,7 @@
 
 	// user_login must be between 0 and 60 characters.
 	if ( empty( $user_login ) ) {
-		return new WP_Error('empty_user_login', __('Cannot create a user with an empty login name.') );
+		return new WP_Error( 'empty_user_login', __( 'Cannot create a user with an empty login name.' ) );
 	} elseif ( mb_strlen( $user_login ) > 60 ) {
 		return new WP_Error( 'user_login_too_long', __( 'Username may not be longer than 60 characters.' ) );
 	}
@@ -1635,7 +1716,7 @@
 
 	$meta['comment_shortcuts'] = empty( $userdata['comment_shortcuts'] ) || 'false' === $userdata['comment_shortcuts'] ? 'false' : 'true';
 
-	$admin_color = empty( $userdata['admin_color'] ) ? 'fresh' : $userdata['admin_color'];
+	$admin_color         = empty( $userdata['admin_color'] ) ? 'fresh' : $userdata['admin_color'];
 	$meta['admin_color'] = preg_replace( '|[^a-z0-9 _.\-@]|i', '', $admin_color );
 
 	$meta['use_ssl'] = empty( $userdata['use_ssl'] ) ? 0 : $userdata['use_ssl'];
@@ -1646,22 +1727,22 @@
 
 	$meta['locale'] = isset( $userdata['locale'] ) ? $userdata['locale'] : '';
 
-	$user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $user_nicename, $user_login));
+	$user_nicename_check = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1", $user_nicename, $user_login ) );
 
 	if ( $user_nicename_check ) {
 		$suffix = 2;
-		while ($user_nicename_check) {
+		while ( $user_nicename_check ) {
 			// user_nicename allows 50 chars. Subtract one for a hyphen, plus the length of the suffix.
-			$base_length = 49 - mb_strlen( $suffix );
-			$alt_user_nicename = mb_substr( $user_nicename, 0, $base_length ) . "-$suffix";
-			$user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $alt_user_nicename, $user_login));
+			$base_length         = 49 - mb_strlen( $suffix );
+			$alt_user_nicename   = mb_substr( $user_nicename, 0, $base_length ) . "-$suffix";
+			$user_nicename_check = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1", $alt_user_nicename, $user_login ) );
 			$suffix++;
 		}
 		$user_nicename = $alt_user_nicename;
 	}
 
 	$compacted = compact( 'user_pass', 'user_email', 'user_url', 'user_nicename', 'display_name', 'user_registered' );
-	$data = wp_unslash( $compacted );
+	$data      = wp_unslash( $compacted );
 
 	if ( ! $update ) {
 		$data = $data + compact( 'user_login' );
@@ -1705,17 +1786,17 @@
 	$user = new WP_User( $user_id );
 
 	/**
- 	 * Filters a user's meta values and keys immediately after the user is created or updated
- 	 * and before any user meta is inserted or updated.
- 	 *
- 	 * Does not include contact methods. These are added using `wp_get_user_contact_methods( $user )`.
- 	 *
- 	 * @since 4.4.0
- 	 *
- 	 * @param array $meta {
- 	 *     Default meta values and keys for the user.
- 	 *
- 	 *     @type string   $nickname             The user's nickname. Default is the user's username.
+	 * Filters a user's meta values and keys immediately after the user is created or updated
+	 * and before any user meta is inserted or updated.
+	 *
+	 * Does not include contact methods. These are added using `wp_get_user_contact_methods( $user )`.
+	 *
+	 * @since 4.4.0
+	 *
+	 * @param array $meta {
+	 *     Default meta values and keys for the user.
+	 *
+	 *     @type string   $nickname             The user's nickname. Default is the user's username.
 	 *     @type string   $first_name           The user's first name.
 	 *     @type string   $last_name            The user's last name.
 	 *     @type string   $description          The user's description.
@@ -1727,10 +1808,10 @@
 	 *                                          not forced.
 	 *     @type bool     $show_admin_bar_front Whether to show the admin bar on the front end for the user.
 	 *                                          Default true.
- 	 * }
+	 * }
 	 * @param WP_User $user   User object.
 	 * @param bool    $update Whether the user is being updated rather than created.
- 	 */
+	 */
 	$meta = apply_filters( 'insert_user_meta', $meta, $user, $update );
 
 	// Update user meta.
@@ -1747,7 +1828,7 @@
 	if ( isset( $userdata['role'] ) ) {
 		$user->set_role( $userdata['role'] );
 	} elseif ( ! $update ) {
-		$user->set_role(get_option('default_role'));
+		$user->set_role( get_option( 'default_role' ) );
 	}
 	wp_cache_delete( $user_id, 'users' );
 	wp_cache_delete( $user_login, 'userlogins' );
@@ -1789,10 +1870,10 @@
  *
  * @see wp_insert_user() For what fields can be set in $userdata.
  *
- * @param object|WP_User $userdata An array of user data or a user object of type stdClass or WP_User.
+ * @param array|object|WP_User $userdata An array of user data or a user object of type stdClass or WP_User.
  * @return int|WP_Error The updated user's ID or a WP_Error object if the user could not be updated.
  */
-function wp_update_user($userdata) {
+function wp_update_user( $userdata ) {
 	if ( $userdata instanceof stdClass ) {
 		$userdata = get_object_vars( $userdata );
 	} elseif ( $userdata instanceof WP_User ) {
@@ -1822,7 +1903,7 @@
 
 	if ( ! empty( $userdata['user_pass'] ) && $userdata['user_pass'] !== $user_obj->user_pass ) {
 		// If password is changing, hash it now
-		$plaintext_pass = $userdata['user_pass'];
+		$plaintext_pass        = $userdata['user_pass'];
 		$userdata['user_pass'] = wp_hash_password( $userdata['user_pass'] );
 
 		/**
@@ -1835,7 +1916,6 @@
 		 * @param bool  $send     Whether to send the email.
 		 * @param array $user     The original user array.
 		 * @param array $userdata The updated user array.
-		 *
 		 */
 		$send_password_change_email = apply_filters( 'send_password_change_email', true, $user, $userdata );
 	}
@@ -1851,7 +1931,6 @@
 		 * @param bool  $send     Whether to send the email.
 		 * @param array $user     The original user array.
 		 * @param array $userdata The updated user array.
-		 *
 		 */
 		$send_email_change_email = apply_filters( 'send_email_change_email', true, $user, $userdata );
 	}
@@ -1861,7 +1940,7 @@
 
 	// Merge old and new fields with new fields overwriting old ones.
 	$userdata = array_merge( $user, $userdata );
-	$user_id = wp_insert_user( $userdata );
+	$user_id  = wp_insert_user( $userdata );
 
 	if ( ! is_wp_error( $user_id ) ) {
 
@@ -1874,7 +1953,8 @@
 
 		if ( ! empty( $send_password_change_email ) ) {
 			/* translators: Do not translate USERNAME, ADMIN_EMAIL, EMAIL, SITENAME, SITEURL: those are placeholders. */
-			$pass_change_text = __( 'Hi ###USERNAME###,
+			$pass_change_text = __(
+				'Hi ###USERNAME###,
 
 This notice confirms that your password was changed on ###SITENAME###.
 
@@ -1885,12 +1965,13 @@
 
 Regards,
 All at ###SITENAME###
-###SITEURL###' );
+###SITEURL###'
+			);
 
 			$pass_change_email = array(
 				'to'      => $user['user_email'],
-				/* translators: User password change notification email subject. 1: Site name */
-				'subject' => __( '[%s] Notice of Password Change' ),
+				/* translators: Password change notification email subject. %s: Site name */
+				'subject' => __( '[%s] Password Changed' ),
 				'message' => $pass_change_text,
 				'headers' => '',
 			);
@@ -1915,7 +1996,6 @@
 			 *        }
 			 * @param array $user     The original user array.
 			 * @param array $userdata The updated user array.
-			 *
 			 */
 			$pass_change_email = apply_filters( 'password_change_email', $pass_change_email, $user, $userdata );
 
@@ -1930,7 +2010,8 @@
 
 		if ( ! empty( $send_email_change_email ) ) {
 			/* translators: Do not translate USERNAME, ADMIN_EMAIL, NEW_EMAIL, EMAIL, SITENAME, SITEURL: those are placeholders. */
-			$email_change_text = __( 'Hi ###USERNAME###,
+			$email_change_text = __(
+				'Hi ###USERNAME###,
 
 This notice confirms that your email address on ###SITENAME### was changed to ###NEW_EMAIL###.
 
@@ -1941,12 +2022,13 @@
 
 Regards,
 All at ###SITENAME###
-###SITEURL###' );
+###SITEURL###'
+			);
 
 			$email_change_email = array(
 				'to'      => $user['user_email'],
-				/* translators: User email change notification email subject. 1: Site name */
-				'subject' => __( '[%s] Notice of Email Change' ),
+				/* translators: Email change notification email subject. %s: Site name */
+				'subject' => __( '[%s] Email Changed' ),
 				'message' => $email_change_text,
 				'headers' => '',
 			);
@@ -1993,12 +2075,12 @@
 	// Update the cookies if the password changed.
 	$current_user = wp_get_current_user();
 	if ( $current_user->ID == $ID ) {
-		if ( isset($plaintext_pass) ) {
+		if ( isset( $plaintext_pass ) ) {
 			wp_clear_auth_cookie();
 
 			// Here we calculate the expiration length of the current auth cookie and compare it to the default expiration.
 			// If it's greater than this, then we know the user checked 'Remember Me' when they logged in.
-			$logged_in_cookie    = wp_parse_auth_cookie( '', 'logged_in' );
+			$logged_in_cookie = wp_parse_auth_cookie( '', 'logged_in' );
 			/** This filter is documented in wp-includes/pluggable.php */
 			$default_cookie_life = apply_filters( 'auth_cookie_expiration', ( 2 * DAY_IN_SECONDS ), $ID, false );
 			$remember            = ( ( $logged_in_cookie['expiration'] - time() ) > $default_cookie_life );
@@ -2025,13 +2107,13 @@
  * @return int|WP_Error The newly created user's ID or a WP_Error object if the user could not
  *                      be created.
  */
-function wp_create_user($username, $password, $email = '') {
+function wp_create_user( $username, $password, $email = '' ) {
 	$user_login = wp_slash( $username );
-	$user_email = wp_slash( $email    );
-	$user_pass = $password;
-
-	$userdata = compact('user_login', 'user_email', 'user_pass');
-	return wp_insert_user($userdata);
+	$user_email = wp_slash( $email );
+	$user_pass  = $password;
+
+	$userdata = compact( 'user_login', 'user_email', 'user_pass' );
+	return wp_insert_user( $userdata );
 }
 
 /**
@@ -2067,7 +2149,7 @@
 		$methods = array(
 			'aim'    => __( 'AIM' ),
 			'yim'    => __( 'Yahoo IM' ),
-			'jabber' => __( 'Jabber / Google Talk' )
+			'jabber' => __( 'Jabber / Google Talk' ),
 		);
 	}
 
@@ -2077,7 +2159,7 @@
 	 * @since 2.9.0
 	 *
 	 * @param array   $methods Array of contact methods and their labels.
- 	 * @param WP_User $user    WP_User object.
+	 * @param WP_User $user    WP_User object.
 	 */
 	return apply_filters( 'user_contactmethods', $methods, $user );
 }
@@ -2132,6 +2214,10 @@
 function get_password_reset_key( $user ) {
 	global $wpdb, $wp_hasher;
 
+	if ( ! ( $user instanceof WP_User ) ) {
+		return new WP_Error( 'invalidcombo', __( '<strong>ERROR</strong>: There is no account with that username or email address.' ) );
+	}
+
 	/**
 	 * Fires before a new password is retrieved.
 	 *
@@ -2192,7 +2278,7 @@
 		require_once ABSPATH . WPINC . '/class-phpass.php';
 		$wp_hasher = new PasswordHash( 8, true );
 	}
-	$hashed = time() . ':' . $wp_hasher->HashPassword( $key );
+	$hashed    = time() . ':' . $wp_hasher->HashPassword( $key );
 	$key_saved = $wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user->user_login ) );
 	if ( false === $key_saved ) {
 		return new WP_Error( 'no_password_key_update', __( 'Could not save password reset key to database.' ) );
@@ -2218,20 +2304,23 @@
  * @param string $login     The user login.
  * @return WP_User|WP_Error WP_User object on success, WP_Error object for invalid or expired keys.
  */
-function check_password_reset_key($key, $login) {
+function check_password_reset_key( $key, $login ) {
 	global $wpdb, $wp_hasher;
 
-	$key = preg_replace('/[^a-z0-9]/i', '', $key);
-
-	if ( empty( $key ) || !is_string( $key ) )
-		return new WP_Error('invalid_key', __('Invalid key'));
-
-	if ( empty($login) || !is_string($login) )
-		return new WP_Error('invalid_key', __('Invalid key'));
+	$key = preg_replace( '/[^a-z0-9]/i', '', $key );
+
+	if ( empty( $key ) || ! is_string( $key ) ) {
+		return new WP_Error( 'invalid_key', __( 'Invalid key.' ) );
+	}
+
+	if ( empty( $login ) || ! is_string( $login ) ) {
+		return new WP_Error( 'invalid_key', __( 'Invalid key.' ) );
+	}
 
 	$row = $wpdb->get_row( $wpdb->prepare( "SELECT ID, user_activation_key FROM $wpdb->users WHERE user_login = %s", $login ) );
-	if ( ! $row )
-		return new WP_Error('invalid_key', __('Invalid key'));
+	if ( ! $row ) {
+		return new WP_Error( 'invalid_key', __( 'Invalid key.' ) );
+	}
 
 	if ( empty( $wp_hasher ) ) {
 		require_once ABSPATH . WPINC . '/class-phpass.php';
@@ -2249,14 +2338,14 @@
 
 	if ( false !== strpos( $row->user_activation_key, ':' ) ) {
 		list( $pass_request_time, $pass_key ) = explode( ':', $row->user_activation_key, 2 );
-		$expiration_time = $pass_request_time + $expiration_duration;
+		$expiration_time                      = $pass_request_time + $expiration_duration;
 	} else {
-		$pass_key = $row->user_activation_key;
+		$pass_key        = $row->user_activation_key;
 		$expiration_time = false;
 	}
 
 	if ( ! $pass_key ) {
-		return new WP_Error( 'invalid_key', __( 'Invalid key' ) );
+		return new WP_Error( 'invalid_key', __( 'Invalid key.' ) );
 	}
 
 	$hash_is_correct = $wp_hasher->CheckPassword( $key, $pass_key );
@@ -2265,11 +2354,11 @@
 		return get_userdata( $row->ID );
 	} elseif ( $hash_is_correct && $expiration_time ) {
 		// Key has an expiration time that's passed
-		return new WP_Error( 'expired_key', __( 'Invalid key' ) );
+		return new WP_Error( 'expired_key', __( 'Invalid key.' ) );
 	}
 
 	if ( hash_equals( $row->user_activation_key, $key ) || ( $hash_is_correct && ! $expiration_time ) ) {
-		$return = new WP_Error( 'expired_key', __( 'Invalid key' ) );
+		$return  = new WP_Error( 'expired_key', __( 'Invalid key.' ) );
 		$user_id = $row->ID;
 
 		/**
@@ -2286,7 +2375,7 @@
 		return apply_filters( 'password_reset_key_expired', $return, $user_id );
 	}
 
-	return new WP_Error( 'invalid_key', __( 'Invalid key' ) );
+	return new WP_Error( 'invalid_key', __( 'Invalid key.' ) );
 }
 
 /**
@@ -2402,11 +2491,12 @@
 	 */
 	$errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
 
-	if ( $errors->get_error_code() )
+	if ( $errors->has_errors() ) {
 		return $errors;
+	}
 
 	$user_pass = wp_generate_password( 12, false );
-	$user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
+	$user_id   = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
 	if ( ! $user_id || is_wp_error( $user_id ) ) {
 		$errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn&#8217;t register you&hellip; please contact the <a href="mailto:%s">webmaster</a> !' ), get_option( 'admin_email' ) ) );
 		return $errors;
@@ -2529,14 +2619,19 @@
 		$role_names = wp_roles()->get_names();
 	}
 
-	$regex  = implode( '|', array_keys( $role_names ) );
-	$regex  = preg_replace( '/[^a-zA-Z_\|-]/', '', $regex );
-	$users  = $wpdb->get_col( $wpdb->prepare( "
+	$regex = implode( '|', array_keys( $role_names ) );
+	$regex = preg_replace( '/[^a-zA-Z_\|-]/', '', $regex );
+	$users = $wpdb->get_col(
+		$wpdb->prepare(
+			"
 		SELECT user_id
 		FROM $wpdb->usermeta
 		WHERE meta_key = '{$prefix}capabilities'
 		AND meta_value NOT REGEXP %s
-	", $regex ) );
+	",
+			$regex
+		)
+	);
 
 	return $users;
 }
@@ -2570,7 +2665,7 @@
 
 		// Upgrade stdClass to WP_User
 		if ( is_object( $current_user ) && isset( $current_user->ID ) ) {
-			$cur_id = $current_user->ID;
+			$cur_id       = $current_user->ID;
 			$current_user = null;
 			wp_set_current_user( $cur_id );
 			return $current_user;
@@ -2582,7 +2677,7 @@
 		return $current_user;
 	}
 
-	if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) {
+	if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
 		wp_set_current_user( 0 );
 		return $current_user;
 	}
@@ -2618,10 +2713,9 @@
  * @since 4.9.0 This function was moved from wp-admin/includes/ms.php so it's no longer Multisite specific.
  *
  * @global WP_Error $errors WP_Error object.
- * @global wpdb     $wpdb   WordPress database object.
  */
 function send_confirmation_on_profile_email() {
-	global $errors, $wpdb;
+	global $errors;
 
 	$current_user = wp_get_current_user();
 	if ( ! is_object( $errors ) ) {
@@ -2634,17 +2728,25 @@
 
 	if ( $current_user->user_email != $_POST['email'] ) {
 		if ( ! is_email( $_POST['email'] ) ) {
-			$errors->add( 'user_email', __( "<strong>ERROR</strong>: The email address isn&#8217;t correct." ), array(
-				'form-field' => 'email',
-			) );
+			$errors->add(
+				'user_email',
+				__( '<strong>ERROR</strong>: The email address isn&#8217;t correct.' ),
+				array(
+					'form-field' => 'email',
+				)
+			);
 
 			return;
 		}
 
-		if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_email FROM {$wpdb->users} WHERE user_email=%s", $_POST['email'] ) ) ) {
-			$errors->add( 'user_email', __( "<strong>ERROR</strong>: The email address is already used." ), array(
-				'form-field' => 'email',
-			) );
+		if ( email_exists( $_POST['email'] ) ) {
+			$errors->add(
+				'user_email',
+				__( '<strong>ERROR</strong>: The email address is already used.' ),
+				array(
+					'form-field' => 'email',
+				)
+			);
 			delete_user_meta( $current_user->ID, '_new_email' );
 
 			return;
@@ -2660,7 +2762,8 @@
 		$sitename = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
 
 		/* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
-		$email_text = __( 'Howdy ###USERNAME###,
+		$email_text = __(
+			'Howdy ###USERNAME###,
 
 You recently requested to have the email address on your account changed.
 
@@ -2674,7 +2777,8 @@
 
 Regards,
 All at ###SITENAME###
-###SITEURL###' );
+###SITEURL###'
+		);
 
 		/**
 		 * Filters the text of the email sent when a change of user email address is attempted.
@@ -2705,7 +2809,8 @@
 		$content = str_replace( '###SITENAME###', $sitename, $content );
 		$content = str_replace( '###SITEURL###', home_url(), $content );
 
-		wp_mail( $_POST['email'], sprintf( __( '[%s] New Email Address' ), $sitename ), $content );
+		/* translators: New email address notification email subject. %s: Site name */
+		wp_mail( $_POST['email'], sprintf( __( '[%s] Email Change Request' ), $sitename ), $content );
 
 		$_POST['email'] = $current_user->user_email;
 	}
@@ -2722,9 +2827,13 @@
  */
 function new_user_email_admin_notice() {
 	global $pagenow;
-	if ( 'profile.php' === $pagenow && isset( $_GET['updated'] ) && $email = get_user_meta( get_current_user_id(), '_new_email', true ) ) {
-		/* translators: %s: New email address */
-		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>';
+
+	if ( 'profile.php' === $pagenow && isset( $_GET['updated'] ) ) {
+		$email = get_user_meta( get_current_user_id(), '_new_email', true );
+		if ( $email ) {
+			/* translators: %s: New email address */
+			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>';
+		}
 	}
 }
 
@@ -2851,21 +2960,23 @@
  * @param int $request_id ID of the request.
  */
 function _wp_privacy_account_request_confirmed( $request_id ) {
-	$request_data = wp_get_user_request_data( $request_id );
-
-	if ( ! $request_data ) {
+	$request = wp_get_user_request_data( $request_id );
+
+	if ( ! $request ) {
 		return;
 	}
 
-	if ( ! in_array( $request_data->status, array( 'request-pending', 'request-failed' ), true ) ) {
+	if ( ! in_array( $request->status, array( 'request-pending', 'request-failed' ), true ) ) {
 		return;
 	}
 
 	update_post_meta( $request_id, '_wp_user_request_confirmed_timestamp', time() );
-	wp_update_post( array(
-		'ID'          => $request_id,
-		'post_status' => 'request-confirmed',
-	) );
+	wp_update_post(
+		array(
+			'ID'          => $request_id,
+			'post_status' => 'request-confirmed',
+		)
+	);
 }
 
 /**
@@ -2879,9 +2990,9 @@
  * @param int $request_id The ID of the request.
  */
 function _wp_privacy_send_request_confirmation_notification( $request_id ) {
-	$request_data = wp_get_user_request_data( $request_id );
-
-	if ( ! is_a( $request_data, 'WP_User_Request' ) || 'request-confirmed' !== $request_data->status ) {
+	$request = wp_get_user_request_data( $request_id );
+
+	if ( ! is_a( $request, 'WP_User_Request' ) || 'request-confirmed' !== $request->status ) {
 		return;
 	}
 
@@ -2891,8 +3002,8 @@
 		return;
 	}
 
-	$manage_url         = add_query_arg( 'page', $request_data->action_name, admin_url( 'tools.php' ) );
-	$action_description = wp_user_request_action_description( $request_data->action_name );
+	$manage_url         = add_query_arg( 'page', $request->action_name, admin_url( 'tools.php' ) );
+	$action_description = wp_user_request_action_description( $request->action_name );
 
 	/**
 	 * Filters the recipient of the data request confirmation notification.
@@ -2905,14 +3016,14 @@
 	 *
 	 * @since 4.9.6
 	 *
-	 * @param string          $admin_email  The email address of the notification recipient.
-	 * @param WP_User_Request $request_data The request that is initiating the notification.
+	 * @param string          $admin_email The email address of the notification recipient.
+	 * @param WP_User_Request $request     The request that is initiating the notification.
 	 */
-	$admin_email = apply_filters( 'user_request_confirmed_email_to', get_site_option( 'admin_email' ), $request_data );
+	$admin_email = apply_filters( 'user_request_confirmed_email_to', get_site_option( 'admin_email' ), $request );
 
 	$email_data = array(
-		'request'     => $request_data,
-		'user_email'  => $request_data->email,
+		'request'     => $request,
+		'user_email'  => $request->email,
 		'description' => $action_description,
 		'manage_url'  => $manage_url,
 		'sitename'    => wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ),
@@ -2974,7 +3085,7 @@
 	$content = str_replace( '###SITEURL###', esc_url_raw( $email_data['siteurl'] ), $content );
 
 	$subject = sprintf(
-		/* translators: 1: Site name. 2: Name of the confirmed action. */
+		/* translators: Privacy data request confirmed notification email subject. 1: Site title, 2: Name of the confirmed action. */
 		__( '[%1$s] Action Confirmed: %2$s' ),
 		$email_data['sitename'],
 		$action_description
@@ -3018,9 +3129,9 @@
  * @param int $request_id The privacy request post ID associated with this request.
  */
 function _wp_privacy_send_erasure_fulfillment_notification( $request_id ) {
-	$request_data = wp_get_user_request_data( $request_id );
-
-	if ( ! is_a( $request_data, 'WP_User_Request' ) || 'request-completed' !== $request_data->status ) {
+	$request = wp_get_user_request_data( $request_id );
+
+	if ( ! is_a( $request, 'WP_User_Request' ) || 'request-completed' !== $request->status ) {
 		return;
 	}
 
@@ -3030,26 +3141,35 @@
 		return;
 	}
 
+	// Localize message content for user; fallback to site default for visitors.
+	if ( ! empty( $request->user_id ) ) {
+		$locale = get_user_locale( $request->user_id );
+	} else {
+		$locale = get_locale();
+	}
+
+	$switched_locale = switch_to_locale( $locale );
+
 	/**
 	 * Filters the recipient of the data erasure fulfillment notification.
 	 *
 	 * @since 4.9.6
 	 *
-	 * @param string          $user_email   The email address of the notification recipient.
-	 * @param WP_User_Request $request_data The request that is initiating the notification.
+	 * @param string          $user_email The email address of the notification recipient.
+	 * @param WP_User_Request $request    The request that is initiating the notification.
 	 */
-	$user_email = apply_filters( 'user_erasure_fulfillment_email_to', $request_data->email, $request_data );
+	$user_email = apply_filters( 'user_erasure_fulfillment_email_to', $request->email, $request );
 
 	$email_data = array(
-		'request'            => $request_data,
+		'request'            => $request,
 		'message_recipient'  => $user_email,
 		'privacy_policy_url' => get_privacy_policy_url(),
 		'sitename'           => wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ),
 		'siteurl'            => home_url(),
 	);
 
-	$subject  = sprintf(
-		/* translators: %s: Site name. */
+	$subject = sprintf(
+		/* translators: Erasure request fulfilled notification email subject. %s: Site name. */
 		__( '[%s] Erasure Request Fulfilled' ),
 		$email_data['sitename']
 	);
@@ -3140,6 +3260,10 @@
 
 	$email_sent = wp_mail( $user_email, $subject, $content );
 
+	if ( $switched_locale ) {
+		restore_previous_locale();
+	}
+
 	if ( $email_sent ) {
 		update_post_meta( $request_id, '_wp_user_notified', true );
 	}
@@ -3157,15 +3281,15 @@
 function _wp_privacy_account_request_confirmed_message( $request_id ) {
 	$request = wp_get_user_request_data( $request_id );
 
-	$message = '<p class="success">' . __( 'Action has been confirmed.' ) . '</p>';
+	$message  = '<p class="success">' . __( 'Action has been confirmed.' ) . '</p>';
 	$message .= '<p>' . __( 'The site administrator has been notified and will fulfill your request as soon as possible.' ) . '</p>';
 
 	if ( $request && in_array( $request->action_name, _wp_privacy_action_request_types(), true ) ) {
 		if ( 'export_personal_data' === $request->action_name ) {
-			$message = '<p class="success">' . __( 'Thanks for confirming your export request.' ) . '</p>';
+			$message  = '<p class="success">' . __( 'Thanks for confirming your export request.' ) . '</p>';
 			$message .= '<p>' . __( 'The site administrator has been notified. You will receive a link to download your export via email when they fulfill your request.' ) . '</p>';
 		} elseif ( 'remove_personal_data' === $request->action_name ) {
-			$message = '<p class="success">' . __( 'Thanks for confirming your erasure request.' ) . '</p>';
+			$message  = '<p class="success">' . __( 'Thanks for confirming your erasure request.' ) . '</p>';
 			$message .= '<p>' . __( 'The site administrator has been notified. You will receive an email confirmation when they erase your data.' ) . '</p>';
 		}
 	}
@@ -3212,28 +3336,36 @@
 	$user_id = $user && ! is_wp_error( $user ) ? $user->ID : 0;
 
 	// Check for duplicates.
-	$requests_query = new WP_Query( array(
-		'post_type'     => 'user_request',
-		'post_name__in' => array( $action_name ),  // Action name stored in post_name column.
-		'title'         => $email_address, // Email address stored in post_title column.
-		'post_status'   => 'any',
-		'fields'        => 'ids',
-	) );
+	$requests_query = new WP_Query(
+		array(
+			'post_type'     => 'user_request',
+			'post_name__in' => array( $action_name ),  // Action name stored in post_name column.
+			'title'         => $email_address, // Email address stored in post_title column.
+			'post_status'   => array(
+				'request-pending',
+				'request-confirmed',
+			),
+			'fields'        => 'ids',
+		)
+	);
 
 	if ( $requests_query->found_posts ) {
-		return new WP_Error( 'duplicate_request', __( 'A request for this email address already exists.' ) );
+		return new WP_Error( 'duplicate_request', __( 'An incomplete request for this email address already exists.' ) );
 	}
 
-	$request_id = wp_insert_post( array(
-		'post_author'   => $user_id,
-		'post_name'     => $action_name,
-		'post_title'    => $email_address,
-		'post_content'  => wp_json_encode( $request_data ),
-		'post_status'   => 'request-pending',
-		'post_type'     => 'user_request',
-		'post_date'     => current_time( 'mysql', false ),
-		'post_date_gmt' => current_time( 'mysql', true ),
-	), true );
+	$request_id = wp_insert_post(
+		array(
+			'post_author'   => $user_id,
+			'post_name'     => $action_name,
+			'post_title'    => $email_address,
+			'post_content'  => wp_json_encode( $request_data ),
+			'post_status'   => 'request-pending',
+			'post_type'     => 'user_request',
+			'post_date'     => current_time( 'mysql', false ),
+			'post_date_gmt' => current_time( 'mysql', true ),
+		),
+		true
+	);
 
 	return $request_id;
 }
@@ -3279,25 +3411,37 @@
  * @since 4.9.6
  *
  * @param string $request_id ID of the request created via wp_create_user_request().
- * @return WP_Error|bool Will return true/false based on the success of sending the email, or a WP_Error object.
+ * @return bool|WP_Error True on success, `WP_Error` on failure.
  */
 function wp_send_user_request( $request_id ) {
 	$request_id = absint( $request_id );
 	$request    = wp_get_user_request_data( $request_id );
 
 	if ( ! $request ) {
-		return new WP_Error( 'user_request_error', __( 'Invalid request.' ) );
+		return new WP_Error( 'invalid_request', __( 'Invalid user request.' ) );
 	}
 
+	// Localize message content for user; fallback to site default for visitors.
+	if ( ! empty( $request->user_id ) ) {
+		$locale = get_user_locale( $request->user_id );
+	} else {
+		$locale = get_locale();
+	}
+
+	$switched_locale = switch_to_locale( $locale );
+
 	$email_data = array(
 		'request'     => $request,
 		'email'       => $request->email,
 		'description' => wp_user_request_action_description( $request->action_name ),
-		'confirm_url' => add_query_arg( array(
-			'action'      => 'confirmaction',
-			'request_id'  => $request_id,
-			'confirm_key' => wp_generate_user_request_key( $request_id ),
-		), wp_login_url() ),
+		'confirm_url' => add_query_arg(
+			array(
+				'action'      => 'confirmaction',
+				'request_id'  => $request_id,
+				'confirm_key' => wp_generate_user_request_key( $request_id ),
+			),
+			wp_login_url()
+		),
 		'sitename'    => wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ),
 		'siteurl'     => home_url(),
 	);
@@ -3353,7 +3497,7 @@
 	$content = str_replace( '###SITENAME###', $email_data['sitename'], $content );
 	$content = str_replace( '###SITEURL###', esc_url_raw( $email_data['siteurl'] ), $content );
 
-	/* translators: Privacy data request subject. 1: Site name, 2: Name of the action */
+	/* translators: Confirm privacy data request notification email subject. 1: Site title, 2: Name of the action */
 	$subject = sprintf( __( '[%1$s] Confirm Action: %2$s' ), $email_data['sitename'], $email_data['description'] );
 
 	/**
@@ -3376,7 +3520,17 @@
 	 */
 	$subject = apply_filters( 'user_request_action_email_subject', $subject, $email_data['sitename'], $email_data );
 
-	return wp_mail( $email_data['email'], $subject, $content );
+	$email_sent = wp_mail( $email_data['email'], $subject, $content );
+
+	if ( $switched_locale ) {
+		restore_previous_locale();
+	}
+
+	if ( ! $email_sent ) {
+		return new WP_Error( 'privacy_email_error', __( 'Unable to send personal data export confirmation email.' ) );
+	}
+
+	return true;
 }
 
 /**
@@ -3399,13 +3553,13 @@
 		$wp_hasher = new PasswordHash( 8, true );
 	}
 
-	wp_update_post( array(
-		'ID'                => $request_id,
-		'post_status'       => 'request-pending',
-		'post_password'     => $wp_hasher->HashPassword( $key ),
-		'post_modified'     => current_time( 'mysql', false ),
-		'post_modified_gmt' => current_time( 'mysql', true ),
-	) );
+	wp_update_post(
+		array(
+			'ID'            => $request_id,
+			'post_status'   => 'request-pending',
+			'post_password' => $wp_hasher->HashPassword( $key ),
+		)
+	);
 
 	return $key;
 }
@@ -3426,15 +3580,15 @@
 	$request    = wp_get_user_request_data( $request_id );
 
 	if ( ! $request ) {
-		return new WP_Error( 'user_request_error', __( 'Invalid request.' ) );
+		return new WP_Error( 'invalid_request', __( 'Invalid request.' ) );
 	}
 
 	if ( ! in_array( $request->status, array( 'request-pending', 'request-failed' ), true ) ) {
-		return __( 'This link has expired.' );
+		return new WP_Error( 'expired_link', __( 'This link has expired.' ) );
 	}
 
 	if ( empty( $key ) ) {
-		return new WP_Error( 'invalid_key', __( 'Invalid key' ) );
+		return new WP_Error( 'missing_key', __( 'Missing confirm key.' ) );
 	}
 
 	if ( empty( $wp_hasher ) ) {
@@ -3446,11 +3600,11 @@
 	$saved_key        = $request->confirm_key;
 
 	if ( ! $saved_key ) {
-		return new WP_Error( 'invalid_key', __( 'Invalid key' ) );
+		return new WP_Error( 'invalid_key', __( 'Invalid key.' ) );
 	}
 
 	if ( ! $key_request_time ) {
-		return new WP_Error( 'invalid_key', __( 'Invalid action' ) );
+		return new WP_Error( 'invalid_key', __( 'Invalid action.' ) );
 	}
 
 	/**
@@ -3464,7 +3618,7 @@
 	$expiration_time     = $key_request_time + $expiration_duration;
 
 	if ( ! $wp_hasher->CheckPassword( $key, $saved_key ) ) {
-		return new WP_Error( 'invalid_key', __( 'Invalid key' ) );
+		return new WP_Error( 'invalid_key', __( 'Invalid key.' ) );
 	}
 
 	if ( ! $expiration_time || time() > $expiration_time ) {
@@ -3513,7 +3667,6 @@
 	 *
 	 * @var int
 	 */
-
 	public $user_id = 0;
 
 	/**