Users: Introduce `email_exists` filter, to complement `username_exists`.

Props garrett-eclipse, apermo, mukesh27, Mista-Flo, sebastian.pisula, mikelopez.
Fixes #51379. See #35509.
Built from https://develop.svn.wordpress.org/trunk@49148


git-svn-id: http://core.svn.wordpress.org/trunk@48910 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2020-10-14 19:04:08 +00:00
parent bb67dde689
commit 118b9aea4d
2 changed files with 25 additions and 12 deletions

View File

@ -1586,8 +1586,8 @@ function clean_user_cache( $user ) {
* *
* @since 2.0.0 * @since 2.0.0
* *
* @param string $username Username. * @param string $username The username to check for existence.
* @return int|false The user's ID on success, and false on failure. * @return int|false The user ID on success, false on failure.
*/ */
function username_exists( $username ) { function username_exists( $username ) {
$user = get_user_by( 'login', $username ); $user = get_user_by( 'login', $username );
@ -1598,11 +1598,12 @@ function username_exists( $username ) {
} }
/** /**
* Filters whether the given username exists or not. * Filters whether the given username exists.
* *
* @since 4.9.0 * @since 4.9.0
* *
* @param int|false $user_id The user's ID on success, and false on failure. * @param int|false $user_id The user ID associated with the username,
* or false if the username does not exist.
* @param string $username Username to check. * @param string $username Username to check.
*/ */
return apply_filters( 'username_exists', $user_id, $username ); return apply_filters( 'username_exists', $user_id, $username );
@ -1618,31 +1619,43 @@ function username_exists( $username ) {
* @since 2.1.0 * @since 2.1.0
* *
* @param string $email Email. * @param string $email Email.
* @return int|false The user's ID on success, and false on failure. * @return int|false The user ID on success, false on failure.
*/ */
function email_exists( $email ) { function email_exists( $email ) {
$user = get_user_by( 'email', $email ); $user = get_user_by( 'email', $email );
if ( $user ) { if ( $user ) {
return $user->ID; $user_id = $user->ID;
} else {
$user_id = false;
} }
return false;
/**
* Filters whether the given email exists.
*
* @since 5.6.0
*
* @param int|false $user_id The user ID associated with the email,
* or false if the email does not exist.
* @param string $email Email.
*/
return apply_filters( 'email_exists', $user_id, $email );
} }
/** /**
* Checks whether a username is valid. * Checks whether a username is valid.
* *
* @since 2.0.1 * @since 2.0.1
* @since 4.4.0 Empty sanitized usernames are now considered invalid * @since 4.4.0 Empty sanitized usernames are now considered invalid.
* *
* @param string $username Username. * @param string $username Username.
* @return bool Whether username given is valid * @return bool Whether username given is valid.
*/ */
function validate_username( $username ) { function validate_username( $username ) {
$sanitized = sanitize_user( $username, true ); $sanitized = sanitize_user( $username, true );
$valid = ( $sanitized == $username && ! empty( $sanitized ) ); $valid = ( $sanitized == $username && ! empty( $sanitized ) );
/** /**
* Filters whether the provided username is valid or not. * Filters whether the provided username is valid.
* *
* @since 2.0.1 * @since 2.0.1
* *
@ -1833,7 +1846,7 @@ function wp_insert_user( $userdata ) {
/* /*
* If there is no update, just check for `email_exists`. If there is an update, * If there is no update, just check for `email_exists`. If there is an update,
* check if current email and new email are the same, or not, and check `email_exists` * check if current email and new email are the same, and check `email_exists`
* accordingly. * accordingly.
*/ */
if ( ( ! $update || ( ! empty( $old_user_data ) && 0 !== strcasecmp( $user_email, $old_user_data->user_email ) ) ) if ( ( ! $update || ( ! empty( $old_user_data ) && 0 !== strcasecmp( $user_email, $old_user_data->user_email ) ) )

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.6-alpha-49147'; $wp_version = '5.6-alpha-49148';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.