wpmu_validate_user_signup()

wpmu_validate_user_signup( string $user_name, string $u…

wpmu_validate_user_signup( string $user_name, string $user_email )

清理和验证用户注册所需的数据。
Sanitize and validate data required for a user sign-up.

目录锚点:#说明#参数#源码


说明(Description)

验证用户名和用户电子邮件地址的有效性和唯一性,并根据管理员提供的域白名单和黑名单检查电子邮件地址。“wpmu_validate_user_signup”钩子提供了一种修改注册过程的简单方法。传递给钩子的值$result包含用户提供的信息和函数创建的错误消息。“wpmu_validate_user_signup”允许您以任何方式处理数据,并在必要时取消设置相关错误。


参数(Parameters)

参数 类型 说明
$user_name (string) 用户提供的登录名。
$user_email (string) 用户提供的电子邮件。

源码(Source)

/**
 * Sanitize and validate data required for a user sign-up.
 *
 * Verifies the validity and uniqueness of user names and user email addresses,
 * and checks email addresses against admin-provided domain whitelists and blacklists.
 *
 * The {@see '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. {@see 'wpmu_validate_user_signup'}
 * allows you to process the data in any way you'd like, and unset the relevant errors if
 * necessary.
 *
 * @since MU
 *
 * @global wpdb $wpdb
 *
 * @param string $user_name  The login name provided by the user.
 * @param string $user_email The email provided by the user.
 * @return array Contains username, email, and error messages.
 */
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', __( 'Only lowercase letters (a-z) and numbers are allowed.' ) );
		$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',  __( 'That username is not allowed.' ) );

	if ( 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.' ) );
	}

	if ( strpos( $user_name, '_' ) !== false )
		$errors->add( 'user_name', __( 'Sorry, usernames may not contain the character “_”!' ) );

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

	if ( !is_email( $user_email ) )
		$errors->add('user_email', __( 'Please enter a valid email address.' ) );

	$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);

	/**
	 * Filter 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 );
}
更新版本 源码位置 使用 被使用
MU (3.0.0) wp-includes/ms-functions.php 1 19
类别:WordPress 函数手册

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT

登录 账号发表你的看法,还没有账号?立即免费 注册