返回介绍

wpmu_validate_user_signup()

发布于 2017-09-11 11:19:17 字数 7811 浏览 923 评论 0 收藏 0

wpmu_validate_user_signup( string $user_name,  string $user_email )

Sanitize and validate data required for a user sign-up.


description

Verifies the validity and uniqueness of user names and user email addresses, and checks email addresses against admin-provided domain whitelists and blacklists.

The ‘wpmu_validate_user_signup’ hook provides an easy way to modify the sign-up process. The value $result, which is passed to the hook, contains both the user-provided info and the error messages created by the function. ‘wpmu_validate_user_signup’ allows you to process the data in any way you’d like, and unset the relevant errors if necessary.


参数

$user_name

(string) (Required) The login name provided by the user.

$user_email

(string) (Required) The email provided by the user.


返回值

(array) Contains username, email, and error messages.


源代码

File: wp-includes/ms-functions.php

function wpmu_validate_user_signup($user_name, $user_email) {
	global $wpdb;

	$errors = new WP_Error();

	$orig_username = $user_name;
	$user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );

	if ( $user_name != $orig_username || preg_match( '/[^a-z0-9]/', $user_name ) ) {
		$errors->add( 'user_name', __( 'Usernames can only contain lowercase letters (a-z) and numbers.' ) );
		$user_name = $orig_username;
	}

	$user_email = sanitize_email( $user_email );

	if ( empty( $user_name ) )
	   	$errors->add('user_name', __( 'Please enter a username.' ) );

	$illegal_names = get_site_option( 'illegal_names' );
	if ( ! is_array( $illegal_names ) ) {
		$illegal_names = array(  'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
		add_site_option( 'illegal_names', $illegal_names );
	}
	if ( in_array( $user_name, $illegal_names ) ) {
		$errors->add( 'user_name',  __( 'Sorry, that username is not allowed.' ) );
	}

	/** This filter is documented in wp-includes/user.php */
	$illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );

	if ( in_array( strtolower( $user_name ), array_map( 'strtolower', $illegal_logins ) ) ) {
		$errors->add( 'user_name',  __( 'Sorry, that username is not allowed.' ) );
	}

	if ( ! is_email( $user_email ) ) {
		$errors->add( 'user_email', __( 'Please enter a valid email address.' ) );
	} elseif ( is_email_address_unsafe( $user_email ) ) {
		$errors->add( 'user_email', __( 'You cannot use that email address to signup. We are having problems with them blocking some of our email. Please use another email provider.' ) );
	}

	if ( strlen( $user_name ) < 4 )
		$errors->add('user_name',  __( 'Username must be at least 4 characters.' ) );

	if ( strlen( $user_name ) > 60 ) {
		$errors->add( 'user_name', __( 'Username may not be longer than 60 characters.' ) );
	}

	// all numeric?
	if ( preg_match( '/^[0-9]*$/', $user_name ) )
		$errors->add('user_name', __('Sorry, usernames must have letters too!'));

	$limited_email_domains = get_site_option( 'limited_email_domains' );
	if ( is_array( $limited_email_domains ) && ! empty( $limited_email_domains ) ) {
		$emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) );
		if ( ! in_array( $emaildomain, $limited_email_domains ) ) {
			$errors->add('user_email', __('Sorry, that email address is not allowed!'));
		}
	}

	// Check if the username has been used already.
	if ( username_exists($user_name) )
		$errors->add( 'user_name', __( 'Sorry, that username already exists!' ) );

	// Check if the email address has been used already.
	if ( email_exists($user_email) )
		$errors->add( 'user_email', __( 'Sorry, that email address is already used!' ) );

	// Has someone already signed up for this username?
	$signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name) );
	if ( $signup != null ) {
		$registered_at =  mysql2date('U', $signup->registered);
		$now = current_time( 'timestamp', true );
		$diff = $now - $registered_at;
		// If registered more than two days ago, cancel registration and let this signup go through.
		if ( $diff > 2 * DAY_IN_SECONDS )
			$wpdb->delete( $wpdb->signups, array( 'user_login' => $user_name ) );
		else
			$errors->add('user_name', __('That username is currently reserved but may be available in a couple of days.'));
	}

	$signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email) );
	if ( $signup != null ) {
		$diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
		// If registered more than two days ago, cancel registration and let this signup go through.
		if ( $diff > 2 * DAY_IN_SECONDS )
			$wpdb->delete( $wpdb->signups, array( 'user_email' => $user_email ) );
		else
			$errors->add('user_email', __('That email address has already been used. Please check your inbox for an activation email. It will become available in a couple of days if you do nothing.'));
	}

	$result = array('user_name' => $user_name, 'orig_username' => $orig_username, 'user_email' => $user_email, 'errors' => $errors);

	/**
	 * Filters the validated user registration details.
	 *
	 * This does not allow you to override the username or email of the user during
	 * registration. The values are solely used for validation and error handling.
	 *
	 * @since MU
	 *
	 * @param array $result {
	 *     The array of user name, email and the error messages.
	 *
	 *     @type string   $user_name     Sanitized and unique username.
	 *     @type string   $orig_username Original username.
	 *     @type string   $user_email    User email address.
	 *     @type WP_Error $errors        WP_Error object containing any errors found.
	 * }
	 */
	return apply_filters( 'wpmu_validate_user_signup', $result );
}

更新日志

Versiondescription
MUIntroduced.

相关函数

Uses

  • wp-includes/user.php: illegal_user_logins
  • wp-includes/l10n.php: __()
  • wp-includes/formatting.php: sanitize_email()
  • wp-includes/formatting.php: is_email()
  • wp-includes/formatting.php: sanitize_user()
  • wp-includes/functions.php: mysql2date()
  • wp-includes/functions.php: current_time()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/option.php: add_site_option()
  • wp-includes/option.php: get_site_option()
  • wp-includes/user.php: username_exists()
  • wp-includes/user.php: email_exists()
  • wp-includes/ms-functions.php: is_email_address_unsafe()
  • wp-includes/ms-functions.php: wpmu_validate_user_signup
  • wp-includes/wp-db.php: wpdb::get_row()
  • wp-includes/wp-db.php: wpdb::delete()
  • wp-includes/wp-db.php: wpdb::prepare()
  • wp-includes/class-wp-error.php: WP_Error::__construct()
  • Show 13 more uses Hide more uses

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::create_item()
  • wp-signup.php: validate_user_form()
  • wp-signup.php: validate_blog_signup()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文