/** * Validate username field. * * @access public * @since 1.0.0 * @return void */ public static function validate_username($passed, $fields, $values) { $nickname = $values['register']['username']; if (wpaam_get_option('exclude_usernames') && array_key_exists($nickname, wpaam_get_disabled_usernames())) { return new WP_Error('nickname-validation-error', __('This nickname cannot be used.', 'wpaam')); } // Check for nicknames if permalink structure requires unique nicknames. if (get_option('wpaam_permalink') == 'nickname') { $current_user = wp_get_current_user(); if ($username !== $current_user->user_nicename && wpaam_nickname_exists($username)) { return new WP_Error('username-validation-error', __('This nickname cannot be used.', 'wpaam')); } } return $passed; }
/** * Given $user_data checks against $method_type if the user exists. * * @since 1.0.0 * @param string $user_data Either ID/Username/Nickname * @param string $method_type Either user_id/username/nickname - usually retrieve thorugh get_option('wpaam_permalink') * @return bool */ function wpaam_user_exists($user_data, $method_type) { $exists = false; // Check if user exists by ID if (!empty($user_data) && $method_type == 'user_id' && get_user_by('id', intval($user_data))) { $exists = true; } // Check if user exists by username if (!empty($user_data) && $method_type == 'username' && get_user_by('login', esc_attr($user_data))) { $exists = true; } // Check if user exists by nickname if (!empty($user_data) && $method_type == 'nickname' && wpaam_nickname_exists($user_data)) { $exists = true; } return $exists; }