wp/wp-includes/user.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * WordPress User API
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     6
 * @subpackage Users
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 * Authenticate user with remember capability.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 * The credentials is an array that has 'user_login', 'user_password', and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
 * 'remember' indices. If the credentials is not given, then the log in form
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
 * will be assumed and used if set.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 * The various authentication cookies will be set by this function and will be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 * set for a longer period depending on if the 'remember' credential is set to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 * true.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    22
 * @param array       $credentials   Optional. User info in order to sign on.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    23
 * @param string|bool $secure_cookie Optional. Whether to use secure cookie.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    24
 * @return WP_User|WP_Error WP_User on success, WP_Error on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    26
function wp_signon( $credentials = array(), $secure_cookie = '' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
	if ( empty($credentials) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
		if ( ! empty($_POST['log']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
			$credentials['user_login'] = $_POST['log'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
		if ( ! empty($_POST['pwd']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
			$credentials['user_password'] = $_POST['pwd'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
		if ( ! empty($_POST['rememberme']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
			$credentials['remember'] = $_POST['rememberme'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
	if ( !empty($credentials['remember']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
		$credentials['remember'] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
		$credentials['remember'] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    41
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    42
	 * Fires before the user is authenticated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    43
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    44
	 * The variables passed to the callbacks are passed by reference,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    45
	 * and can be modified by callback functions.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    46
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    47
	 * @since 1.5.1
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    48
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    49
	 * @todo Decide whether to deprecate the wp_authenticate action.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    50
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    51
	 * @param string $user_login    Username, passed by reference.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    52
	 * @param string $user_password User password, passed by reference.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    53
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    54
	do_action_ref_array( 'wp_authenticate', array( &$credentials['user_login'], &$credentials['user_password'] ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
	if ( '' === $secure_cookie )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
		$secure_cookie = is_ssl();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    59
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    60
	 * Filter whether to use a secure sign-on cookie.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    61
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    62
	 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    63
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    64
	 * @param bool  $secure_cookie Whether to use a secure sign-on cookie.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    65
	 * @param array $credentials {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    66
 	 *     Array of entered sign-on data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    67
 	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    68
 	 *     @type string $user_login    Username.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    69
 	 *     @type string $user_password Password entered.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    70
	 *     @type bool   $remember      Whether to 'remember' the user. Increases the time
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    71
	 *                                 that the cookie will be kept. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    72
 	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    73
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    74
	$secure_cookie = apply_filters( 'secure_signon_cookie', $secure_cookie, $credentials );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
	global $auth_secure_cookie; // XXX ugly hack to pass this to wp_authenticate_cookie
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
	$auth_secure_cookie = $secure_cookie;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
	add_filter('authenticate', 'wp_authenticate_cookie', 30, 3);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
	$user = wp_authenticate($credentials['user_login'], $credentials['user_password']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
	if ( is_wp_error($user) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
		if ( $user->get_error_codes() == array('empty_username', 'empty_password') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
			$user = new WP_Error('', '');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
		return $user;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
	wp_set_auth_cookie($user->ID, $credentials['remember'], $secure_cookie);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    92
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    93
	 * Fires after the user has successfully logged in.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    94
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    95
	 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    96
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    97
	 * @param string  $user_login Username.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    98
	 * @param WP_User $user       WP_User object of the logged-in user.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    99
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   100
	do_action( 'wp_login', $user->user_login, $user );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
	return $user;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
 * Authenticate the user using the username and password.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   106
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   107
 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   108
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   109
 * @param WP_User|WP_Error|null $user     WP_User or WP_Error object from a previous callback. Default null.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   110
 * @param string                $username Username for authentication.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   111
 * @param string                $password Password for authentication.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   112
 * @return WP_User|WP_Error WP_User on success, WP_Error on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
function wp_authenticate_username_password($user, $username, $password) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   115
	if ( $user instanceof WP_User ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   116
		return $user;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   117
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
	if ( empty($username) || empty($password) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
		if ( is_wp_error( $user ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
			return $user;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
		$error = new WP_Error();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
		if ( empty($username) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
			$error->add('empty_username', __('<strong>ERROR</strong>: The username field is empty.'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
		if ( empty($password) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
			$error->add('empty_password', __('<strong>ERROR</strong>: The password field is empty.'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
		return $error;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
	$user = get_user_by('login', $username);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
	if ( !$user )
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   137
		return new WP_Error( 'invalid_username', sprintf( __( '<strong>ERROR</strong>: Invalid username. <a href="%s">Lost your password?</a>' ), wp_lostpassword_url() ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   139
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   140
	 * Filter whether the given user can be authenticated with the provided $password.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   141
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   142
	 * @since 2.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   143
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   144
	 * @param WP_User|WP_Error $user     WP_User or WP_Error object if a previous
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   145
	 *                                   callback failed authentication.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   146
	 * @param string           $password Password to check against the user.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   147
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   148
	$user = apply_filters( 'wp_authenticate_user', $user, $password );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
	if ( is_wp_error($user) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
		return $user;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
	if ( !wp_check_password($password, $user->user_pass, $user->ID) )
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   153
		return new WP_Error( 'incorrect_password', sprintf( __( '<strong>ERROR</strong>: The password you entered for the username <strong>%1$s</strong> is incorrect. <a href="%2$s">Lost your password?</a>' ),
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
		$username, wp_lostpassword_url() ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
	return $user;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
 * Authenticate the user using the WordPress auth cookie.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   161
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   162
 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   163
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   164
 * @param WP_User|WP_Error|null $user     WP_User or WP_Error object from a previous callback. Default null.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   165
 * @param string                $username Username. If not empty, cancels the cookie authentication.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   166
 * @param string                $password Password. If not empty, cancels the cookie authentication.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   167
 * @return WP_User|WP_Error WP_User on success, WP_Error on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
function wp_authenticate_cookie($user, $username, $password) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   170
	if ( $user instanceof WP_User ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   171
		return $user;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   172
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
	if ( empty($username) && empty($password) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
		$user_id = wp_validate_auth_cookie();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
		if ( $user_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
			return new WP_User($user_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
		global $auth_secure_cookie;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
		if ( $auth_secure_cookie )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
			$auth_cookie = SECURE_AUTH_COOKIE;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
			$auth_cookie = AUTH_COOKIE;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
		if ( !empty($_COOKIE[$auth_cookie]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
			return new WP_Error('expired_session', __('Please log in again.'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
		// If the cookie is not set, be silent.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
	return $user;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   196
 * For Multisite blogs, check if the authenticated user has been marked as a
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
 * spammer, or if the user's primary blog has been marked as spam.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
 * @since 3.7.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   200
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   201
 * @param WP_User|WP_Error|null $user WP_User or WP_Error object from a previous callback. Default null.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   202
 * @return WP_User|WP_Error WP_User on success, WP_Error if the user is considered a spammer.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
function wp_authenticate_spam_check( $user ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   205
	if ( $user instanceof WP_User && is_multisite() ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   206
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   207
		 * Filter whether the user has been marked as a spammer.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   208
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   209
		 * @since 3.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   210
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   211
		 * @param bool    $spammed Whether the user is considered a spammer.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   212
		 * @param WP_User $user    User to check against.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   213
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
		$spammed = apply_filters( 'check_is_user_spammed', is_user_spammy(), $user );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
		if ( $spammed )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
			return new WP_Error( 'spammer_account', __( '<strong>ERROR</strong>: Your account has been marked as a spammer.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
	return $user;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   223
 * Validate the logged-in cookie.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   224
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   225
 * Checks the logged-in cookie if the previous auth cookie could not be
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   226
 * validated and parsed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   227
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   228
 * This is a callback for the determine_current_user filter, rather than API.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   229
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   230
 * @since 3.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   231
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   232
 * @param int|bool $user_id The user ID (or false) as received from the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   233
 *                       determine_current_user filter.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   234
 * @return int|bool User ID if validated, false otherwise. If a user ID from
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   235
 *                  an earlier filter callback is received, that value is returned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   236
 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   237
function wp_validate_logged_in_cookie( $user_id ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   238
	if ( $user_id ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   239
		return $user_id;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   240
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   241
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   242
	if ( is_blog_admin() || is_network_admin() || empty( $_COOKIE[LOGGED_IN_COOKIE] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   243
		return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   244
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   245
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   246
	return wp_validate_auth_cookie( $_COOKIE[LOGGED_IN_COOKIE], 'logged_in' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   247
}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   248
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   249
/**
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
 * Number of posts user has written.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
 * @since 3.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   253
 * @since 4.1.0 Added `$post_type` argument.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   254
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   255
 * @global wpdb $wpdb WordPress database object for queries.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   257
 * @param int    $userid    User ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   258
 * @param string $post_type Optional. Post type to count the number of posts for. Default 'post'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   259
 * @return int Number of posts the user has written in this post type.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   261
function count_user_posts( $userid, $post_type = 'post' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   264
	$where = get_posts_by_author_sql( $post_type, true, $userid );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
	$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   268
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   269
	 * Filter the number of posts a user has written.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   270
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   271
	 * @since 2.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   272
	 * @since 4.1.0 Added `$post_type` argument.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   273
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   274
	 * @param int    $count     The user's post count.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   275
	 * @param int    $userid    User ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   276
	 * @param string $post_type Post type to count the number of posts for.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   277
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   278
	return apply_filters( 'get_usernumposts', $count, $userid, $post_type );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
 * Number of posts written by a list of users.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
 * @param array $users Array of user IDs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
 * @param string $post_type Optional. Post type to check. Defaults to post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
 * @param bool $public_only Optional. Only return counts for public posts.  Defaults to false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
 * @return array Amount of posts each user has written.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
	$count = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
	if ( empty( $users ) || ! is_array( $users ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
		return $count;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
	$userlist = implode( ',', array_map( 'absint', $users ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
	$where = get_posts_by_author_sql( $post_type, true, null, $public_only );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
	$result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
	foreach ( $result as $row ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
		$count[ $row[0] ] = $row[1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
	foreach ( $users as $id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
		if ( ! isset( $count[ $id ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
			$count[ $id ] = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
	return $count;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
// User option functions
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
 * Get the current user's ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
 * @since MU
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
 * @return int The current user's ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
function get_current_user_id() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
	if ( ! function_exists( 'wp_get_current_user' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
		return 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
	$user = wp_get_current_user();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
	return ( isset( $user->ID ) ? (int) $user->ID : 0 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
 * Retrieve user option that can be either per Site or per Network.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
 * If the user ID is not given, then the current user will be used instead. If
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
 * the user ID is given, then the user data will be retrieved. The filter for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
 * the result, will also pass the original option name and finally the user data
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
 * object as the third parameter.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
 * The option will first check for the per site name and then the per Network name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
 * @since 2.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   343
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   344
 * @global wpdb $wpdb WordPress database object for queries.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   346
 * @param string $option     User option name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   347
 * @param int    $user       Optional. User ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   348
 * @param string $deprecated Use get_option() to check for an option in the options table.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   349
 * @return mixed User option value on success, false on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
function get_user_option( $option, $user = 0, $deprecated = '' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
	if ( !empty( $deprecated ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
		_deprecated_argument( __FUNCTION__, '3.0' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
	if ( empty( $user ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
		$user = get_current_user_id();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
	if ( ! $user = get_userdata( $user ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
	$prefix = $wpdb->get_blog_prefix();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
	if ( $user->has_prop( $prefix . $option ) ) // Blog specific
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
		$result = $user->get( $prefix . $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
	elseif ( $user->has_prop( $option ) ) // User specific and cross-blog
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
		$result = $user->get( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
		$result = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   371
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   372
	 * Filter a specific user option value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   373
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   374
	 * The dynamic portion of the hook name, `$option`, refers to the user option name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   375
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   376
	 * @since 2.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   377
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   378
	 * @param mixed   $result Value for the user's option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   379
	 * @param string  $option Name of the option being retrieved.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   380
	 * @param WP_User $user   WP_User object of the user whose option is being retrieved.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   381
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   382
	return apply_filters( "get_user_option_{$option}", $result, $option, $user );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
 * Update user option with global blog capability.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
 * User options are just like user metadata except that they have support for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
 * global blog options. If the 'global' parameter is false, which it is by default
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
 * it will prepend the WordPress table prefix to the option name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
 * Deletes the user option if $newvalue is empty.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   396
 * @global wpdb $wpdb WordPress database object for queries.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   397
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   398
 * @param int    $user_id     User ID.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
 * @param string $option_name User option name.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   400
 * @param mixed  $newvalue    User option value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   401
 * @param bool   $global      Optional. Whether option name is global or blog specific.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   402
 *                            Default false (blog specific).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   403
 * @return int|bool User meta ID if the option didn't exist, true on successful update,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   404
 *                  false on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
function update_user_option( $user_id, $option_name, $newvalue, $global = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
	if ( !$global )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
		$option_name = $wpdb->get_blog_prefix() . $option_name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
	return update_user_meta( $user_id, $option_name, $newvalue );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
 * Delete user option with global blog capability.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
 * User options are just like user metadata except that they have support for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
 * global blog options. If the 'global' parameter is false, which it is by default
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
 * it will prepend the WordPress table prefix to the option name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
 * @since 3.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   423
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   424
 * @global wpdb $wpdb WordPress database object for queries.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   426
 * @param int    $user_id     User ID
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
 * @param string $option_name User option name.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   428
 * @param bool   $global      Optional. Whether option name is global or blog specific.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   429
 *                            Default false (blog specific).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   430
 * @return bool True on success, false on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
function delete_user_option( $user_id, $option_name, $global = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
	if ( !$global )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
		$option_name = $wpdb->get_blog_prefix() . $option_name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
	return delete_user_meta( $user_id, $option_name );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
 * WordPress User Query class.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
 * @since 3.1.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   444
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   445
 * @see WP_User_Query::prepare_query() for information on accepted arguments.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
class WP_User_Query {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
	 * Query vars, after parsing
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
	 * @since 3.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
	 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   456
	public $query_vars = array();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
	 * List of found user ids
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
	 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
	 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   465
	private $results;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
	 * Total number of found users for the current query
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
	 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
	 * @var int
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   474
	private $total_users = 0;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   475
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   476
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   477
	 * Metadata query container.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   478
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   479
	 * @since 4.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   480
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   481
	 * @var object WP_Meta_Query
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   482
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   483
	public $meta_query = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   484
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   485
	private $compat_fields = array( 'results', 'total_users' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
	// SQL clauses
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   488
	public $query_fields;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   489
	public $query_from;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   490
	public $query_where;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   491
	public $query_orderby;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   492
	public $query_limit;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
	/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   495
	 * PHP5 constructor.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
	 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   499
	 * @param null|string|array $args Optional. The query variables.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   501
	public function __construct( $query = null ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   502
		if ( ! empty( $query ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   503
			$this->prepare_query( $query );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   504
			$this->query();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   505
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   506
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   507
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   508
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   509
	 * Prepare the query variables.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   510
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   511
	 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   512
	 * @since 4.2.0 Added 'meta_value_num' support for `$orderby` parameter. Added multi-dimensional array syntax
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   513
	 *              for `$orderby` parameter.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   514
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   515
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   516
	 * @param string|array $query {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   517
	 *     Optional. Array or string of Query parameters.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   518
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   519
	 *     @type int          $blog_id         The site ID. Default is the global blog id.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   520
	 *     @type string       $role            Role name. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   521
	 *     @type string       $meta_key        User meta key. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   522
	 *     @type string       $meta_value      User meta value. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   523
	 *     @type string       $meta_compare    Comparison operator to test the `$meta_value`. Accepts '=', '!=',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   524
	 *                                         '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   525
	 *                                         'NOT BETWEEN', 'EXISTS', 'NOT EXISTS', 'REGEXP', 'NOT REGEXP',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   526
	 *                                         or 'RLIKE'. Default '='.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   527
	 *     @type array        $include         An array of user IDs to include. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   528
	 *     @type array        $exclude         An array of user IDs to exclude. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   529
	 *     @type string       $search          Search keyword. Searches for possible string matches on columns.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   530
	 *                                         When `$search_columns` is left empty, it tries to determine which
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   531
	 *                                         column to search in based on search string. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   532
	 *     @type array        $search_columns  Array of column names to be searched. Accepts 'ID', 'login',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   533
	 *                                         'nicename', 'email', 'url'. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   534
	 *     @type string|array $orderby         Field(s) to sort the retrieved users by. May be a single value,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   535
	 *                                         an array of values, or a multi-dimensional array with fields as keys
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   536
	 *                                         and orders ('ASC' or 'DESC') as values. Accepted values are'ID',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   537
	 *                                         'display_name' (or 'name'), 'user_login' (or 'login'),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   538
	 *                                         'user_nicename' (or 'nicename'), 'user_email' (or 'email'),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   539
	 *                                         'user_url' (or 'url'), 'user_registered' (or 'registered'),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   540
	 *                                         'post_count', 'meta_value', 'meta_value_num', the value of
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   541
	 *                                         `$meta_key`, or an array key of `$meta_query`. To use 'meta_value'
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   542
	 *                                         or 'meta_value_num', `$meta_key` must be also be defined.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   543
	 *                                         Default 'user_login'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   544
	 *     @type string       $order           Designates ascending or descending order of users. Order values
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   545
	 *                                         passed as part of an `$orderby` array take precedence over this
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   546
	 *                                         parameter. Accepts 'ASC', 'DESC'. Default 'ASC'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   547
	 *     @type int          $offset          Number of users to offset in retrieved results. Can be used in
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   548
	 *                                         conjunction with pagination. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   549
	 *     @type int          $number          Number of users to limit the query for. Can be used in conjunction
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   550
	 *                                         with pagination. Value -1 (all) is not supported.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   551
	 *                                         Default empty (all users).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   552
	 *     @type bool         $count_total     Whether to count the total number of users found. If pagination is not
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   553
	 *                                         needed, setting this to false can improve performance. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   554
	 *     @type string|array $fields          Which fields to return. Single or all fields (string), or array
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   555
	 *                                         of fields. Accepts 'ID', 'display_name', 'login', 'nicename', 'email',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   556
	 *                                         'url', 'registered'. Use 'all' for all fields and 'all_with_meta' to
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   557
	 *                                         include meta fields. Default 'all'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   558
	 *     @type string       $who             Type of users to query. Accepts 'authors'. Default empty (all users).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   559
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   560
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   561
	public function prepare_query( $query = array() ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   562
		global $wpdb;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   563
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   564
		if ( empty( $this->query_vars ) || ! empty( $query ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   565
			$this->query_limit = null;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
			$this->query_vars = wp_parse_args( $query, array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
				'blog_id' => $GLOBALS['blog_id'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
				'role' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
				'meta_key' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
				'meta_value' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
				'meta_compare' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
				'include' => array(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
				'exclude' => array(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
				'search' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
				'search_columns' => array(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
				'orderby' => 'login',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
				'order' => 'ASC',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
				'offset' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   579
				'number' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
				'count_total' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
				'fields' => 'all',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
				'who' => ''
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
			) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   585
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   586
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   587
		 * Fires before the WP_User_Query has been parsed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   588
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   589
		 * The passed WP_User_Query object contains the query variables, not
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   590
		 * yet passed into SQL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   591
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   592
		 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   593
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   594
		 * @param WP_User_Query $this The current WP_User_Query instance,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   595
		 *                            passed by reference.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   596
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   597
		do_action( 'pre_get_users', $this );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
		$qv =& $this->query_vars;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
		if ( is_array( $qv['fields'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
			$qv['fields'] = array_unique( $qv['fields'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
			$this->query_fields = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
			foreach ( $qv['fields'] as $field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
				$field = 'ID' === $field ? 'ID' : sanitize_key( $field );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
				$this->query_fields[] = "$wpdb->users.$field";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
			$this->query_fields = implode( ',', $this->query_fields );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
		} elseif ( 'all' == $qv['fields'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
			$this->query_fields = "$wpdb->users.*";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
			$this->query_fields = "$wpdb->users.ID";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
		if ( isset( $qv['count_total'] ) && $qv['count_total'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
			$this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
		$this->query_from = "FROM $wpdb->users";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
		$this->query_where = "WHERE 1=1";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   622
		// Parse and sanitize 'include', for use by 'orderby' as well as 'include' below.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   623
		if ( ! empty( $qv['include'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   624
			$include = wp_parse_id_list( $qv['include'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   625
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   626
			$include = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   627
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   628
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   629
		$blog_id = 0;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   630
		if ( isset( $qv['blog_id'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   631
			$blog_id = absint( $qv['blog_id'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   632
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   633
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   634
		if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   635
			$qv['meta_key'] = $wpdb->get_blog_prefix( $blog_id ) . 'user_level';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   636
			$qv['meta_value'] = 0;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   637
			$qv['meta_compare'] = '!=';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   638
			$qv['blog_id'] = $blog_id = 0; // Prevent extra meta query
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   639
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   640
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   641
		// Meta query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   642
		$this->meta_query = new WP_Meta_Query();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   643
		$this->meta_query->parse_query_vars( $qv );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   644
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   645
		$role = '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   646
		if ( isset( $qv['role'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   647
			$role = trim( $qv['role'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   648
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   649
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   650
		if ( $blog_id && ( $role || is_multisite() ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   651
			$cap_meta_query = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   652
			$cap_meta_query['key'] = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   653
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   654
			if ( $role ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   655
				$cap_meta_query['value'] = '"' . $role . '"';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   656
				$cap_meta_query['compare'] = 'like';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   657
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   658
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   659
			if ( empty( $this->meta_query->queries ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   660
				$this->meta_query->queries = array( $cap_meta_query );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   661
			} elseif ( ! in_array( $cap_meta_query, $this->meta_query->queries, true ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   662
				// Append the cap query to the original queries and reparse the query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   663
				$this->meta_query->queries = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   664
					'relation' => 'AND',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   665
					array( $this->meta_query->queries, $cap_meta_query ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   666
				);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   667
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   668
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   669
			$this->meta_query->parse_query_vars( $this->meta_query->queries );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   670
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   671
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   672
		if ( ! empty( $this->meta_query->queries ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   673
			$clauses = $this->meta_query->get_sql( 'user', $wpdb->users, 'ID', $this );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   674
			$this->query_from .= $clauses['join'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   675
			$this->query_where .= $clauses['where'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   676
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   677
			if ( 'OR' == $this->meta_query->relation ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   678
				$this->query_fields = 'DISTINCT ' . $this->query_fields;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   679
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   680
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   681
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   682
		// sorting
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   683
		$qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   684
		$order = $this->parse_order( $qv['order'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   685
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   686
		if ( empty( $qv['orderby'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   687
			// Default order is by 'user_login'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   688
			$ordersby = array( 'user_login' => $order );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   689
		} else if ( is_array( $qv['orderby'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   690
			$ordersby = $qv['orderby'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   691
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   692
			// 'orderby' values may be a comma- or space-separated list.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   693
			$ordersby = preg_split( '/[,\s]+/', $qv['orderby'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   694
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   695
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   696
		$orderby_array = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   697
		foreach ( $ordersby as $_key => $_value ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   698
			if ( ! $_value ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   699
				continue;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   700
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   701
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   702
			if ( is_int( $_key ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   703
				// Integer key means this is a flat array of 'orderby' fields.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   704
				$_orderby = $_value;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   705
				$_order = $order;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   706
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   707
				// Non-integer key means this the key is the field and the value is ASC/DESC.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   708
				$_orderby = $_key;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   709
				$_order = $_value;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   710
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   711
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   712
			$parsed = $this->parse_orderby( $_orderby );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   713
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   714
			if ( ! $parsed ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   715
				continue;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   716
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   717
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   718
			$orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   719
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   720
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   721
		// If no valid clauses were found, order by user_login.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   722
		if ( empty( $orderby_array ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   723
			$orderby_array[] = "user_login $order";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   724
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   725
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   726
		$this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   727
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   728
		// limit
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   729
		if ( isset( $qv['number'] ) && $qv['number'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   730
			if ( $qv['offset'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   731
				$this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   732
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   733
				$this->query_limit = $wpdb->prepare("LIMIT %d", $qv['number']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   734
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   735
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   736
		$search = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   737
		if ( isset( $qv['search'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   738
			$search = trim( $qv['search'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   739
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   740
		if ( $search ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   741
			$leading_wild = ( ltrim($search, '*') != $search );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   742
			$trailing_wild = ( rtrim($search, '*') != $search );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   743
			if ( $leading_wild && $trailing_wild )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   744
				$wild = 'both';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   745
			elseif ( $leading_wild )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   746
				$wild = 'leading';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   747
			elseif ( $trailing_wild )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   748
				$wild = 'trailing';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   749
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   750
				$wild = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   751
			if ( $wild )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   752
				$search = trim($search, '*');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   753
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   754
			$search_columns = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   755
			if ( $qv['search_columns'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   756
				$search_columns = array_intersect( $qv['search_columns'], array( 'ID', 'user_login', 'user_email', 'user_url', 'user_nicename' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   757
			if ( ! $search_columns ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   758
				if ( false !== strpos( $search, '@') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   759
					$search_columns = array('user_email');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   760
				elseif ( is_numeric($search) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   761
					$search_columns = array('user_login', 'ID');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   762
				elseif ( preg_match('|^https?://|', $search) && ! ( is_multisite() && wp_is_large_network( 'users' ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   763
					$search_columns = array('user_url');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   764
				else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   765
					$search_columns = array('user_login', 'user_nicename');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   766
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   767
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   768
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   769
			 * Filter the columns to search in a WP_User_Query search.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   770
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   771
			 * The default columns depend on the search term, and include 'user_email',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   772
			 * 'user_login', 'ID', 'user_url', and 'user_nicename'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   773
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   774
			 * @since 3.6.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   775
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   776
			 * @param array         $search_columns Array of column names to be searched.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   777
			 * @param string        $search         Text being searched.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   778
			 * @param WP_User_Query $this           The current WP_User_Query instance.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   779
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   780
			$search_columns = apply_filters( 'user_search_columns', $search_columns, $search, $this );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   781
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   782
			$this->query_where .= $this->get_search_sql( $search, $search_columns, $wild );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   783
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   784
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   785
		if ( ! empty( $include ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   786
			// Sanitized earlier.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   787
			$ids = implode( ',', $include );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   788
			$this->query_where .= " AND $wpdb->users.ID IN ($ids)";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
		} elseif ( ! empty( $qv['exclude'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
			$ids = implode( ',', wp_parse_id_list( $qv['exclude'] ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   791
			$this->query_where .= " AND $wpdb->users.ID NOT IN ($ids)";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   792
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   793
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   794
		// Date queries are allowed for the user_registered field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   795
		if ( ! empty( $qv['date_query'] ) && is_array( $qv['date_query'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   796
			$date_query = new WP_Date_Query( $qv['date_query'], 'user_registered' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   797
			$this->query_where .= $date_query->get_sql();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   798
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   799
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   800
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   801
		 * Fires after the WP_User_Query has been parsed, and before
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   802
		 * the query is executed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   803
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   804
		 * The passed WP_User_Query object contains SQL parts formed
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   805
		 * from parsing the given query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   806
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   807
		 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   808
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   809
		 * @param WP_User_Query $this The current WP_User_Query instance,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   810
		 *                            passed by reference.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   811
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   812
		do_action_ref_array( 'pre_user_query', array( &$this ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   813
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   814
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   815
	/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   816
	 * Execute the query, with the current variables.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   817
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   818
	 * @since 3.1.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   819
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   820
	 * @global wpdb $wpdb WordPress database object for queries.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   821
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   822
	public function query() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   823
		global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   824
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   825
		$qv =& $this->query_vars;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   826
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   827
		$query = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   828
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   829
		if ( is_array( $qv['fields'] ) || 'all' == $qv['fields'] ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   830
			$this->results = $wpdb->get_results( $query );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   831
		} else {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   832
			$this->results = $wpdb->get_col( $query );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   833
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   834
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   835
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   836
		 * Filter SELECT FOUND_ROWS() query for the current WP_User_Query instance.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   837
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   838
		 * @since 3.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   839
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   840
		 * @global wpdb $wpdb WordPress database abstraction object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   841
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   842
		 * @param string $sql The SELECT FOUND_ROWS() query for the current WP_User_Query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   843
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   844
		if ( isset( $qv['count_total'] ) && $qv['count_total'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   845
			$this->total_users = $wpdb->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   846
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   847
		if ( !$this->results )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   848
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   849
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   850
		if ( 'all_with_meta' == $qv['fields'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   851
			cache_users( $this->results );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   852
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   853
			$r = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   854
			foreach ( $this->results as $userid )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   855
				$r[ $userid ] = new WP_User( $userid, '', $qv['blog_id'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   856
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   857
			$this->results = $r;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
		} elseif ( 'all' == $qv['fields'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
			foreach ( $this->results as $key => $user ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   860
				$this->results[ $key ] = new WP_User( $user, '', $qv['blog_id'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   861
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   862
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   863
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   864
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   865
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   866
	 * Retrieve query variable.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   867
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   868
	 * @since 3.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   869
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   870
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   871
	 * @param string $query_var Query variable key.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   872
	 * @return mixed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   873
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   874
	public function get( $query_var ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   875
		if ( isset( $this->query_vars[$query_var] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   876
			return $this->query_vars[$query_var];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   877
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   878
		return null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   879
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   880
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   881
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   882
	 * Set query variable.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   883
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   884
	 * @since 3.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   885
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   886
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   887
	 * @param string $query_var Query variable key.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   888
	 * @param mixed $value Query variable value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   889
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   890
	public function set( $query_var, $value ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   891
		$this->query_vars[$query_var] = $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   892
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   893
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   894
	/**
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   895
	 * Used internally to generate an SQL string for searching across multiple columns
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   896
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   897
	 * @access protected
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   898
	 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   899
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   900
	 * @param string $string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   901
	 * @param array $cols
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   902
	 * @param bool $wild Whether to allow wildcard searches. Default is false for Network Admin, true for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   903
	 *  single site. Single site allows leading and trailing wildcards, Network Admin only trailing.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   904
	 * @return string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   905
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   906
	protected function get_search_sql( $string, $cols, $wild = false ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   907
		global $wpdb;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   908
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   909
		$searches = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   910
		$leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   911
		$trailing_wild = ( 'trailing' == $wild || 'both' == $wild ) ? '%' : '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   912
		$like = $leading_wild . $wpdb->esc_like( $string ) . $trailing_wild;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   913
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   914
		foreach ( $cols as $col ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   915
			if ( 'ID' == $col ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   916
				$searches[] = $wpdb->prepare( "$col = %s", $string );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   917
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   918
				$searches[] = $wpdb->prepare( "$col LIKE %s", $like );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   919
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   920
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   921
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   922
		return ' AND (' . implode(' OR ', $searches) . ')';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   923
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   924
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   925
	/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   926
	 * Return the list of users.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   927
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   928
	 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   929
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   930
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   931
	 * @return array Array of results.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   932
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   933
	public function get_results() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   934
		return $this->results;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   935
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   936
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   937
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   938
	 * Return the total number of users for the current query.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   939
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   940
	 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   941
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   942
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   943
	 * @return int Number of total users.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   944
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   945
	public function get_total() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   946
		return $this->total_users;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   947
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   948
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   949
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   950
	 * Parse and sanitize 'orderby' keys passed to the user query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   951
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   952
	 * @since 4.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   953
	 * @access protected
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   954
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   955
	 * @global wpdb $wpdb WordPress database abstraction object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   956
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   957
	 * @param string $orderby Alias for the field to order by.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   958
	 * @return string|bool Value to used in the ORDER clause, if `$orderby` is valid. False otherwise.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   959
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   960
	protected function parse_orderby( $orderby ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   961
		global $wpdb;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   962
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   963
		$meta_query_clauses = $this->meta_query->get_clauses();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   964
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   965
		$_orderby = '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   966
		if ( in_array( $orderby, array( 'login', 'nicename', 'email', 'url', 'registered' ) ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   967
			$_orderby = 'user_' . $orderby;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   968
		} elseif ( in_array( $orderby, array( 'user_login', 'user_nicename', 'user_email', 'user_url', 'user_registered' ) ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   969
			$_orderby = $orderby;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   970
		} elseif ( 'name' == $orderby || 'display_name' == $orderby ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   971
			$_orderby = 'display_name';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   972
		} elseif ( 'post_count' == $orderby ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   973
			// todo: avoid the JOIN
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   974
			$where = get_posts_by_author_sql( 'post' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   975
			$this->query_from .= " LEFT OUTER JOIN (
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   976
				SELECT post_author, COUNT(*) as post_count
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   977
				FROM $wpdb->posts
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   978
				$where
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   979
				GROUP BY post_author
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   980
			) p ON ({$wpdb->users}.ID = p.post_author)
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   981
			";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   982
			$_orderby = 'post_count';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   983
		} elseif ( 'ID' == $orderby || 'id' == $orderby ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   984
			$_orderby = 'ID';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   985
		} elseif ( 'meta_value' == $orderby || $this->get( 'meta_key' ) == $orderby ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   986
			$_orderby = "$wpdb->usermeta.meta_value";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   987
		} elseif ( 'meta_value_num' == $orderby ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   988
			$_orderby = "$wpdb->usermeta.meta_value+0";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   989
		} elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   990
			$include = wp_parse_id_list( $this->query_vars['include'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   991
			$include_sql = implode( ',', $include );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   992
			$_orderby = "FIELD( $wpdb->users.ID, $include_sql )";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   993
		} elseif ( isset( $meta_query_clauses[ $orderby ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   994
			$meta_clause = $meta_query_clauses[ $orderby ];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   995
			$_orderby = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   996
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   997
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   998
		return $_orderby;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   999
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1000
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1001
	/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1002
	 * Parse an 'order' query variable and cast it to ASC or DESC as necessary.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1003
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1004
	 * @since 4.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1005
	 * @access protected
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1006
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1007
	 * @param string $order The 'order' query variable.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1008
	 * @return string The sanitized 'order' query variable.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1009
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1010
	protected function parse_order( $order ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1011
		if ( ! is_string( $order ) || empty( $order ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1012
			return 'DESC';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1013
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1014
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1015
		if ( 'ASC' === strtoupper( $order ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1016
			return 'ASC';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1017
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1018
			return 'DESC';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1019
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1020
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1021
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1022
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1023
	 * Make private properties readable for backwards compatibility.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1024
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1025
	 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1026
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1027
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1028
	 * @param string $name Property to get.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1029
	 * @return mixed Property.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1030
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1031
	public function __get( $name ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1032
		if ( in_array( $name, $this->compat_fields ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1033
			return $this->$name;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1034
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1035
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1036
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1037
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1038
	 * Make private properties settable for backwards compatibility.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1039
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1040
	 * @since 4.0.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1041
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1042
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1043
	 * @param string $name  Property to check if set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1044
	 * @param mixed  $value Property value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1045
	 * @return mixed Newly-set property.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1046
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1047
	public function __set( $name, $value ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1048
		if ( in_array( $name, $this->compat_fields ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1049
			return $this->$name = $value;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1050
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1051
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1052
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1053
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1054
	 * Make private properties checkable for backwards compatibility.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1055
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1056
	 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1057
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1058
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1059
	 * @param string $name Property to check if set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1060
	 * @return bool Whether the property is set.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1061
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1062
	public function __isset( $name ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1063
		if ( in_array( $name, $this->compat_fields ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1064
			return isset( $this->$name );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1065
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1066
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1067
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1068
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1069
	 * Make private properties un-settable for backwards compatibility.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1070
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1071
	 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1072
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1073
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1074
	 * @param string $name Property to unset.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1075
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1076
	public function __unset( $name ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1077
		if ( in_array( $name, $this->compat_fields ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1078
			unset( $this->$name );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1079
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1080
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1081
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1082
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1083
	 * Make private/protected methods readable for backwards compatibility.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1084
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1085
	 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1086
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1087
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1088
	 * @param callable $name      Method to call.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1089
	 * @param array    $arguments Arguments to pass when calling.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1090
	 * @return mixed|bool Return value of the callback, false otherwise.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1091
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1092
	public function __call( $name, $arguments ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1093
		if ( 'get_search_sql' === $name ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1094
			return call_user_func_array( array( $this, $name ), $arguments );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1095
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1096
		return false;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1097
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1098
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1099
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1100
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1101
 * Retrieve list of users matching criteria.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1102
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1103
 * @since 3.1.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1104
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1105
 * @see WP_User_Query
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1106
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1107
 * @param array $args Optional. Arguments to retrieve users. See {@see WP_User_Query::prepare_query()}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1108
 *                    for more information on accepted arguments.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1109
 * @return array List of users.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1110
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1111
function get_users( $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1112
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1113
	$args = wp_parse_args( $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1114
	$args['count_total'] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1115
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1116
	$user_search = new WP_User_Query($args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1117
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1118
	return (array) $user_search->get_results();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1119
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1120
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1121
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1122
 * Get the blogs a user belongs to.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1123
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1124
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1125
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1126
 * @global wpdb $wpdb WordPress database object for queries.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1127
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1128
 * @param int  $user_id User ID
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1129
 * @param bool $all     Whether to retrieve all blogs, or only blogs that are not
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1130
 *                      marked as deleted, archived, or spam.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1131
 * @return array A list of the user's blogs. An empty array if the user doesn't exist
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1132
 *               or belongs to no blogs.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1133
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1134
function get_blogs_of_user( $user_id, $all = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1135
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1136
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1137
	$user_id = (int) $user_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1138
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1139
	// Logged out users can't have blogs
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1140
	if ( empty( $user_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1141
		return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1142
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1143
	$keys = get_user_meta( $user_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1144
	if ( empty( $keys ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1145
		return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1146
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1147
	if ( ! is_multisite() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1148
		$blog_id = get_current_blog_id();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1149
		$blogs = array( $blog_id => new stdClass );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1150
		$blogs[ $blog_id ]->userblog_id = $blog_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1151
		$blogs[ $blog_id ]->blogname = get_option('blogname');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1152
		$blogs[ $blog_id ]->domain = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1153
		$blogs[ $blog_id ]->path = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1154
		$blogs[ $blog_id ]->site_id = 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1155
		$blogs[ $blog_id ]->siteurl = get_option('siteurl');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1156
		$blogs[ $blog_id ]->archived = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1157
		$blogs[ $blog_id ]->spam = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1158
		$blogs[ $blog_id ]->deleted = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1159
		return $blogs;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1160
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1161
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1162
	$blogs = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1163
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1164
	if ( isset( $keys[ $wpdb->base_prefix . 'capabilities' ] ) && defined( 'MULTISITE' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1165
		$blog = get_blog_details( 1 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1166
		if ( $blog && isset( $blog->domain ) && ( $all || ( ! $blog->archived && ! $blog->spam && ! $blog->deleted ) ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1167
			$blogs[ 1 ] = (object) array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1168
				'userblog_id' => 1,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1169
				'blogname'    => $blog->blogname,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1170
				'domain'      => $blog->domain,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1171
				'path'        => $blog->path,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1172
				'site_id'     => $blog->site_id,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1173
				'siteurl'     => $blog->siteurl,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1174
				'archived'    => 0,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1175
				'spam'        => 0,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1176
				'deleted'     => 0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1177
			);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1178
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1179
		unset( $keys[ $wpdb->base_prefix . 'capabilities' ] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1180
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1181
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1182
	$keys = array_keys( $keys );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1183
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1184
	foreach ( $keys as $key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1185
		if ( 'capabilities' !== substr( $key, -12 ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1186
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1187
		if ( $wpdb->base_prefix && 0 !== strpos( $key, $wpdb->base_prefix ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1188
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1189
		$blog_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1190
		if ( ! is_numeric( $blog_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1191
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1192
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1193
		$blog_id = (int) $blog_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1194
		$blog = get_blog_details( $blog_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1195
		if ( $blog && isset( $blog->domain ) && ( $all || ( ! $blog->archived && ! $blog->spam && ! $blog->deleted ) ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1196
			$blogs[ $blog_id ] = (object) array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1197
				'userblog_id' => $blog_id,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1198
				'blogname'    => $blog->blogname,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1199
				'domain'      => $blog->domain,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1200
				'path'        => $blog->path,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1201
				'site_id'     => $blog->site_id,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1202
				'siteurl'     => $blog->siteurl,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1203
				'archived'    => 0,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1204
				'spam'        => 0,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1205
				'deleted'     => 0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1206
			);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1207
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1208
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1209
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1210
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1211
	 * Filter the list of blogs a user belongs to.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1212
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1213
	 * @since MU
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1214
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1215
	 * @param array $blogs   An array of blog objects belonging to the user.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1216
	 * @param int   $user_id User ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1217
	 * @param bool  $all     Whether the returned blogs array should contain all blogs, including
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1218
	 *                       those marked 'deleted', 'archived', or 'spam'. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1219
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1220
	return apply_filters( 'get_blogs_of_user', $blogs, $user_id, $all );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1221
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1222
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1223
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1224
 * Find out whether a user is a member of a given blog.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1225
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1226
 * @since MU 1.1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1227
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1228
 * @param int $user_id Optional. The unique ID of the user. Defaults to the current user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1229
 * @param int $blog_id Optional. ID of the blog to check. Defaults to the current site.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1230
 * @return bool
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1231
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1232
function is_user_member_of_blog( $user_id = 0, $blog_id = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1233
	$user_id = (int) $user_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1234
	$blog_id = (int) $blog_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1235
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1236
	if ( empty( $user_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1237
		$user_id = get_current_user_id();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1238
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1239
	if ( empty( $blog_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1240
		$blog_id = get_current_blog_id();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1241
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1242
	$blogs = get_blogs_of_user( $user_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1243
	return array_key_exists( $blog_id, $blogs );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1244
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1245
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1246
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1247
 * Add meta data field to a user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1248
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1249
 * Post meta data is called "Custom Fields" on the Administration Screens.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1250
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1251
 * @since 3.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1252
 * @link https://codex.wordpress.org/Function_Reference/add_user_meta
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1253
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1254
 * @param int $user_id User ID.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1255
 * @param string $meta_key Metadata name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1256
 * @param mixed $meta_value Metadata value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1257
 * @param bool $unique Optional, default is false. Whether the same key should not be added.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1258
 * @return int|bool Meta ID on success, false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1259
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1260
function add_user_meta($user_id, $meta_key, $meta_value, $unique = false) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1261
	return add_metadata('user', $user_id, $meta_key, $meta_value, $unique);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1262
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1263
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1264
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1265
 * Remove metadata matching criteria from a user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1266
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1267
 * You can match based on the key, or key and value. Removing based on key and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1268
 * value, will keep from removing duplicate metadata with the same key. It also
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1269
 * allows removing all metadata matching key, if needed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1270
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1271
 * @since 3.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1272
 * @link https://codex.wordpress.org/Function_Reference/delete_user_meta
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1273
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1274
 * @param int $user_id user ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1275
 * @param string $meta_key Metadata name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1276
 * @param mixed $meta_value Optional. Metadata value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1277
 * @return bool True on success, false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1278
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1279
function delete_user_meta($user_id, $meta_key, $meta_value = '') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1280
	return delete_metadata('user', $user_id, $meta_key, $meta_value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1281
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1282
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1283
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1284
 * Retrieve user meta field for a user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1285
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1286
 * @since 3.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1287
 * @link https://codex.wordpress.org/Function_Reference/get_user_meta
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1288
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1289
 * @param int $user_id User ID.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1290
 * @param string $key Optional. The meta key to retrieve. By default, returns data for all keys.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1291
 * @param bool $single Whether to return a single value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1292
 * @return mixed Will be an array if $single is false. Will be value of meta data field if $single
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1293
 *  is true.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1294
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1295
function get_user_meta($user_id, $key = '', $single = false) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1296
	return get_metadata('user', $user_id, $key, $single);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1297
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1298
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1299
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1300
 * Update user meta field based on user ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1301
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1302
 * Use the $prev_value parameter to differentiate between meta fields with the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1303
 * same key and user ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1304
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1305
 * If the meta field for the user does not exist, it will be added.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1306
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1307
 * @since 3.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1308
 * @link https://codex.wordpress.org/Function_Reference/update_user_meta
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1309
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1310
 * @param int $user_id User ID.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1311
 * @param string $meta_key Metadata key.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1312
 * @param mixed $meta_value Metadata value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1313
 * @param mixed $prev_value Optional. Previous value to check before removing.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1314
 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1315
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1316
function update_user_meta($user_id, $meta_key, $meta_value, $prev_value = '') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1317
	return update_metadata('user', $user_id, $meta_key, $meta_value, $prev_value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1318
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1319
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1320
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1321
 * Count number of users who have each of the user roles.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1322
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1323
 * Assumes there are neither duplicated nor orphaned capabilities meta_values.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1324
 * Assumes role names are unique phrases. Same assumption made by WP_User_Query::prepare_query()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1325
 * Using $strategy = 'time' this is CPU-intensive and should handle around 10^7 users.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1326
 * Using $strategy = 'memory' this is memory-intensive and should handle around 10^5 users, but see WP Bug #12257.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1327
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1328
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1329
 * @param string $strategy 'time' or 'memory'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1330
 * @return array Includes a grand total and an array of counts indexed by role strings.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1331
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1332
function count_users($strategy = 'time') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1333
	global $wpdb, $wp_roles;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1334
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1335
	// Initialize
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1336
	$id = get_current_blog_id();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1337
	$blog_prefix = $wpdb->get_blog_prefix($id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1338
	$result = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1339
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1340
	if ( 'time' == $strategy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1341
		global $wp_roles;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1342
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1343
		if ( ! isset( $wp_roles ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1344
			$wp_roles = new WP_Roles();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1345
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1346
		$avail_roles = $wp_roles->get_names();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1347
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1348
		// Build a CPU-intensive query that will return concise information.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1349
		$select_count = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1350
		foreach ( $avail_roles as $this_role => $name ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1351
			$select_count[] = $wpdb->prepare( "COUNT(NULLIF(`meta_value` LIKE %s, false))", '%' . $wpdb->esc_like( '"' . $this_role . '"' ) . '%');
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1352
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1353
		$select_count = implode(', ', $select_count);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1354
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1355
		// Add the meta_value index to the selection list, then run the query.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1356
		$row = $wpdb->get_row( "SELECT $select_count, COUNT(*) FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'", ARRAY_N );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1357
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1358
		// Run the previous loop again to associate results with role names.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1359
		$col = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1360
		$role_counts = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1361
		foreach ( $avail_roles as $this_role => $name ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1362
			$count = (int) $row[$col++];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1363
			if ($count > 0) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1364
				$role_counts[$this_role] = $count;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1365
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1366
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1367
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1368
		// Get the meta_value index from the end of the result set.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1369
		$total_users = (int) $row[$col];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1370
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1371
		$result['total_users'] = $total_users;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1372
		$result['avail_roles'] =& $role_counts;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1373
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1374
		$avail_roles = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1375
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1376
		$users_of_blog = $wpdb->get_col( "SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1377
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1378
		foreach ( $users_of_blog as $caps_meta ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1379
			$b_roles = maybe_unserialize($caps_meta);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1380
			if ( ! is_array( $b_roles ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1381
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1382
			foreach ( $b_roles as $b_role => $val ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1383
				if ( isset($avail_roles[$b_role]) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1384
					$avail_roles[$b_role]++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1385
				} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1386
					$avail_roles[$b_role] = 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1387
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1388
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1389
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1390
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1391
		$result['total_users'] = count( $users_of_blog );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1392
		$result['avail_roles'] =& $avail_roles;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1393
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1394
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1395
	return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1396
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1397
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1398
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1399
// Private helper functions
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1400
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1401
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1402
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1403
 * Set up global user vars.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1404
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1405
 * Used by wp_set_current_user() for back compat. Might be deprecated in the future.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1406
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1407
 * @since 2.0.4
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1408
 * @global string $userdata User description.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1409
 * @global string $user_login The user username for logging in
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1410
 * @global int $user_level The level of the user
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1411
 * @global int $user_ID The ID of the user
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1412
 * @global string $user_email The email address of the user
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1413
 * @global string $user_url The url in the user's profile
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1414
 * @global string $user_identity The display name of the user
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1415
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1416
 * @param int $for_user_id Optional. User ID to set up global data.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1417
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1418
function setup_userdata($for_user_id = '') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1419
	global $user_login, $userdata, $user_level, $user_ID, $user_email, $user_url, $user_identity;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1420
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1421
	if ( '' == $for_user_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1422
		$for_user_id = get_current_user_id();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1423
	$user = get_userdata( $for_user_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1424
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1425
	if ( ! $user ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1426
		$user_ID = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1427
		$user_level = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1428
		$userdata = null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1429
		$user_login = $user_email = $user_url = $user_identity = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1430
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1431
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1432
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1433
	$user_ID    = (int) $user->ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1434
	$user_level = (int) $user->user_level;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1435
	$userdata   = $user;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1436
	$user_login = $user->user_login;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1437
	$user_email = $user->user_email;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1438
	$user_url   = $user->user_url;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1439
	$user_identity = $user->display_name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1440
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1441
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1442
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1443
 * Create dropdown HTML content of users.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1444
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1445
 * The content can either be displayed, which it is by default or retrieved by
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1446
 * setting the 'echo' argument. The 'include' and 'exclude' arguments do not
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1447
 * need to be used; all users will be displayed in that case. Only one can be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1448
 * used, either 'include' or 'exclude', but not both.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1449
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1450
 * The available arguments are as follows:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1451
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1452
 * @since 2.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1453
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1454
 * @global wpdb $wpdb WordPress database object for queries.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1455
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1456
 * @param array|string $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1457
 *     Optional. Array or string of arguments to generate a drop-down of users.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1458
 *     {@see WP_User_Query::prepare_query() for additional available arguments.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1459
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1460
 *     @type string       $show_option_all         Text to show as the drop-down default (all).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1461
 *                                                 Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1462
 *     @type string       $show_option_none        Text to show as the drop-down default when no
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1463
 *                                                 users were found. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1464
 *     @type int|string   $option_none_value       Value to use for $show_option_non when no users
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1465
 *                                                 were found. Default -1.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1466
 *     @type string       $hide_if_only_one_author Whether to skip generating the drop-down
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1467
 *                                                 if only one user was found. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1468
 *     @type string       $orderby                 Field to order found users by. Accepts user fields.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1469
 *                                                 Default 'display_name'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1470
 *     @type string       $order                   Whether to order users in ascending or descending
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1471
 *                                                 order. Accepts 'ASC' (ascending) or 'DESC' (descending).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1472
 *                                                 Default 'ASC'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1473
 *     @type array|string $include                 Array or comma-separated list of user IDs to include.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1474
 *                                                 Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1475
 *     @type array|string $exclude                 Array or comma-separated list of user IDs to exclude.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1476
 *                                                 Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1477
 *     @type bool|int     $multi                   Whether to skip the ID attribute on the 'select' element.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1478
 *                                                 Accepts 1|true or 0|false. Default 0|false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1479
 *     @type string       $show                    User table column to display. If the selected item is empty
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1480
 *                                                 then the 'user_login' will be displayed in parentheses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1481
 *                                                 Accepts user fields. Default 'display_name'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1482
 *     @type int|bool     $echo                    Whether to echo or return the drop-down. Accepts 1|true (echo)
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1483
 *                                                 or 0|false (return). Default 1|true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1484
 *     @type int          $selected                Which user ID should be selected. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1485
 *     @type bool         $include_selected        Whether to always include the selected user ID in the drop-
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1486
 *                                                 down. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1487
 *     @type string       $name                    Name attribute of select element. Default 'user'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1488
 *     @type string       $id                      ID attribute of the select element. Default is the value of $name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1489
 *     @type string       $class                   Class attribute of the select element. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1490
 *     @type int          $blog_id                 ID of blog (Multisite only). Default is ID of the current blog.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1491
 *     @type string       $who                     Which type of users to query. Accepts only an empty string or
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1492
 *                                                 'authors'. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1493
 * }
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1494
 * @return string|null Null on display. String of HTML content on retrieve.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1495
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1496
function wp_dropdown_users( $args = '' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1497
	$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1498
		'show_option_all' => '', 'show_option_none' => '', 'hide_if_only_one_author' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1499
		'orderby' => 'display_name', 'order' => 'ASC',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1500
		'include' => '', 'exclude' => '', 'multi' => 0,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1501
		'show' => 'display_name', 'echo' => 1,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1502
		'selected' => 0, 'name' => 'user', 'class' => '', 'id' => '',
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1503
		'blog_id' => $GLOBALS['blog_id'], 'who' => '', 'include_selected' => false,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1504
		'option_none_value' => -1
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1505
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1506
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1507
	$defaults['selected'] = is_author() ? get_query_var( 'author' ) : 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1508
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1509
	$r = wp_parse_args( $args, $defaults );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1510
	$show = $r['show'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1511
	$show_option_all = $r['show_option_all'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1512
	$show_option_none = $r['show_option_none'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1513
	$option_none_value = $r['option_none_value'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1514
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1515
	$query_args = wp_array_slice_assoc( $r, array( 'blog_id', 'include', 'exclude', 'orderby', 'order', 'who' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1516
	$query_args['fields'] = array( 'ID', 'user_login', $show );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1517
	$users = get_users( $query_args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1518
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1519
	$output = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1520
	if ( ! empty( $users ) && ( empty( $r['hide_if_only_one_author'] ) || count( $users ) > 1 ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1521
		$name = esc_attr( $r['name'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1522
		if ( $r['multi'] && ! $r['id'] ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1523
			$id = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1524
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1525
			$id = $r['id'] ? " id='" . esc_attr( $r['id'] ) . "'" : " id='$name'";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1526
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1527
		$output = "<select name='{$name}'{$id} class='" . $r['class'] . "'>\n";
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1528
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1529
		if ( $show_option_all ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1530
			$output .= "\t<option value='0'>$show_option_all</option>\n";
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1531
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1532
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1533
		if ( $show_option_none ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1534
			$_selected = selected( $option_none_value, $r['selected'], false );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1535
			$output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$_selected>$show_option_none</option>\n";
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1536
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1537
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1538
		$found_selected = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1539
		foreach ( (array) $users as $user ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1540
			$user->ID = (int) $user->ID;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1541
			$_selected = selected( $user->ID, $r['selected'], false );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1542
			if ( $_selected ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1543
				$found_selected = true;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1544
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1545
			$display = ! empty( $user->$show ) ? $user->$show : '('. $user->user_login . ')';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1546
			$output .= "\t<option value='$user->ID'$_selected>" . esc_html( $display ) . "</option>\n";
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1547
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1548
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1549
		if ( $r['include_selected'] && ! $found_selected && ( $r['selected'] > 0 ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1550
			$user = get_userdata( $r['selected'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1551
			$_selected = selected( $user->ID, $r['selected'], false );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1552
			$display = ! empty( $user->$show ) ? $user->$show : '('. $user->user_login . ')';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1553
			$output .= "\t<option value='$user->ID'$_selected>" . esc_html( $display ) . "</option>\n";
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1554
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1555
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1556
		$output .= "</select>";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1557
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1558
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1559
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1560
	 * Filter the wp_dropdown_users() HTML output.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1561
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1562
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1563
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1564
	 * @param string $output HTML output generated by wp_dropdown_users().
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1565
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1566
	$html = apply_filters( 'wp_dropdown_users', $output );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1567
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1568
	if ( $r['echo'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1569
		echo $html;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1570
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1571
	return $html;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1572
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1573
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1574
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1575
 * Sanitize user field based on context.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1576
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1577
 * Possible context values are:  'raw', 'edit', 'db', 'display', 'attribute' and 'js'. The
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1578
 * 'display' context is used by default. 'attribute' and 'js' contexts are treated like 'display'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1579
 * when calling filters.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1580
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1581
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1582
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1583
 * @param string $field The user Object field name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1584
 * @param mixed $value The user Object value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1585
 * @param int $user_id user ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1586
 * @param string $context How to sanitize user fields. Looks for 'raw', 'edit', 'db', 'display',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1587
 *               'attribute' and 'js'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1588
 * @return mixed Sanitized value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1589
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1590
function sanitize_user_field($field, $value, $user_id, $context) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1591
	$int_fields = array('ID');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1592
	if ( in_array($field, $int_fields) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1593
		$value = (int) $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1594
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1595
	if ( 'raw' == $context )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1596
		return $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1597
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1598
	if ( !is_string($value) && !is_numeric($value) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1599
		return $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1600
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1601
	$prefixed = false !== strpos( $field, 'user_' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1602
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1603
	if ( 'edit' == $context ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1604
		if ( $prefixed ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1605
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1606
			/** This filter is documented in wp-includes/post.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1607
			$value = apply_filters( "edit_{$field}", $value, $user_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1608
		} else {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1609
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1610
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1611
			 * Filter a user field value in the 'edit' context.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1612
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1613
			 * The dynamic portion of the hook name, `$field`, refers to the prefixed user
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1614
			 * field being filtered, such as 'user_login', 'user_email', 'first_name', etc.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1615
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1616
			 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1617
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1618
			 * @param mixed $value   Value of the prefixed user field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1619
			 * @param int   $user_id User ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1620
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1621
			$value = apply_filters( "edit_user_{$field}", $value, $user_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1622
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1623
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1624
		if ( 'description' == $field )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1625
			$value = esc_html( $value ); // textarea_escaped?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1626
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1627
			$value = esc_attr($value);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1628
	} elseif ( 'db' == $context ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1629
		if ( $prefixed ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1630
			/** This filter is documented in wp-includes/post.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1631
			$value = apply_filters( "pre_{$field}", $value );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1632
		} else {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1633
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1634
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1635
			 * Filter the value of a user field in the 'db' context.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1636
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1637
			 * The dynamic portion of the hook name, `$field`, refers to the prefixed user
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1638
			 * field being filtered, such as 'user_login', 'user_email', 'first_name', etc.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1639
 			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1640
			 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1641
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1642
			 * @param mixed $value Value of the prefixed user field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1643
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1644
			$value = apply_filters( "pre_user_{$field}", $value );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1645
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1646
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1647
		// Use display filters by default.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1648
		if ( $prefixed ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1649
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1650
			/** This filter is documented in wp-includes/post.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1651
			$value = apply_filters( $field, $value, $user_id, $context );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1652
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1653
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1654
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1655
			 * Filter the value of a user field in a standard context.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1656
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1657
			 * The dynamic portion of the hook name, `$field`, refers to the prefixed user
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1658
			 * field being filtered, such as 'user_login', 'user_email', 'first_name', etc.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1659
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1660
			 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1661
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1662
			 * @param mixed  $value   The user object value to sanitize.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1663
			 * @param int    $user_id User ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1664
			 * @param string $context The context to filter within.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1665
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1666
			$value = apply_filters( "user_{$field}", $value, $user_id, $context );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1667
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1668
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1669
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1670
	if ( 'user_url' == $field )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1671
		$value = esc_url($value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1672
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1673
	if ( 'attribute' == $context ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1674
		$value = esc_attr( $value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1675
	} elseif ( 'js' == $context ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1676
		$value = esc_js( $value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1677
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1678
	return $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1679
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1680
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1681
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1682
 * Update all user caches
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1683
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1684
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1685
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1686
 * @param object $user User object to be cached
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1687
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1688
function update_user_caches($user) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1689
	wp_cache_add($user->ID, $user, 'users');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1690
	wp_cache_add($user->user_login, $user->ID, 'userlogins');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1691
	wp_cache_add($user->user_email, $user->ID, 'useremail');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1692
	wp_cache_add($user->user_nicename, $user->ID, 'userslugs');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1693
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1694
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1695
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1696
 * Clean all user caches
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1697
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1698
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1699
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1700
 * @param WP_User|int $user User object or ID to be cleaned from the cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1701
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1702
function clean_user_cache( $user ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1703
	if ( is_numeric( $user ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1704
		$user = new WP_User( $user );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1705
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1706
	if ( ! $user->exists() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1707
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1708
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1709
	wp_cache_delete( $user->ID, 'users' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1710
	wp_cache_delete( $user->user_login, 'userlogins' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1711
	wp_cache_delete( $user->user_email, 'useremail' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1712
	wp_cache_delete( $user->user_nicename, 'userslugs' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1713
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1714
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1715
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1716
 * Checks whether the given username exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1717
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1718
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1719
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1720
 * @param string $username Username.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1721
 * @return null|int The user's ID on success, and null on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1722
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1723
function username_exists( $username ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1724
	if ( $user = get_user_by('login', $username ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1725
		return $user->ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1726
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1727
		return null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1728
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1729
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1730
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1731
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1732
 * Checks whether the given email exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1733
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1734
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1735
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1736
 * @param string $email Email.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1737
 * @return bool|int The user's ID on success, and false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1738
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1739
function email_exists( $email ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1740
	if ( $user = get_user_by('email', $email) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1741
		return $user->ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1742
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1743
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1744
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1745
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1746
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1747
 * Checks whether a username is valid.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1748
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1749
 * @since 2.0.1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1750
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1751
 * @param string $username Username.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1752
 * @return bool Whether username given is valid
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1753
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1754
function validate_username( $username ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1755
	$sanitized = sanitize_user( $username, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1756
	$valid = ( $sanitized == $username );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1757
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1758
	 * Filter whether the provided username is valid or not.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1759
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1760
	 * @since 2.0.1
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1761
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1762
	 * @param bool   $valid    Whether given username is valid.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1763
	 * @param string $username Username to check.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1764
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1765
	return apply_filters( 'validate_username', $valid, $username );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1766
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1767
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1768
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1769
 * Insert a user into the database.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1770
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1771
 * Most of the $userdata array fields have filters associated with the values.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1772
 * The exceptions are 'rich_editing', 'role', 'jabber', 'aim', 'yim',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1773
 * 'user_registered', and 'ID'. The filters have the prefix 'pre_user_' followed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1774
 * by the field name. An example using 'description' would have the filter
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1775
 * called, 'pre_user_description' that can be hooked into.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1776
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1777
 * @since 2.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1778
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1779
 * @global wpdb $wpdb WordPress database object for queries.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1780
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1781
 * @param array $userdata {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1782
 *     An array, object, or WP_User object of user data arguments.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1783
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1784
 *     @type int         $ID              User ID. If supplied, the user will be updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1785
 *     @type string      $user_pass       The plain-text user password.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1786
 *     @type string      $user_login      The user's login username.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1787
 *     @type string      $user_nicename   The URL-friendly user name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1788
 *     @type string      $user_url        The user URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1789
 *     @type string      $user_email      The user email address.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1790
 *     @type string      $display_name    The user's display name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1791
 *                                        Default is the the user's username.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1792
 *     @type string      $nickname        The user's nickname. Default
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1793
 *                                        Default is the the user's username.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1794
 *     @type string      $first_name      The user's first name. For new users, will be used
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1795
 *                                        to build $display_name if unspecified.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1796
 *     @type stirng      $last_name       The user's last name. For new users, will be used
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1797
 *                                        to build $display_name if unspecified.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1798
 *     @type string|bool $rich_editing    Whether to enable the rich-editor for the user. False
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1799
 *                                        if not empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1800
 *     @type string      $user_registered Date the user registered. Format is 'Y-m-d H:i:s'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1801
 *     @type string      $role            User's role.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1802
 *     @type string      $jabber          User's Jabber account username.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1803
 *     @type string      $aim             User's AIM account username.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1804
 *     @type string      $yim             User's Yahoo! messenger username.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1805
 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1806
 * @return int|WP_Error The newly created user's ID or a WP_Error object if the user could not
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1807
 *                      be created.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1808
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1809
function wp_insert_user( $userdata ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1810
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1811
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1812
	if ( $userdata instanceof stdClass ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1813
		$userdata = get_object_vars( $userdata );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1814
	} elseif ( $userdata instanceof WP_User ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1815
		$userdata = $userdata->to_array();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1816
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1817
	// Are we updating or creating?
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1818
	if ( ! empty( $userdata['ID'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1819
		$ID = (int) $userdata['ID'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1820
		$update = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1821
		$old_user_data = WP_User::get_data_by( 'id', $ID );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1822
		// hashed in wp_update_user(), plaintext if called directly
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1823
		$user_pass = $userdata['user_pass'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1824
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1825
		$update = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1826
		// Hash the password
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1827
		$user_pass = wp_hash_password( $userdata['user_pass'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1828
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1829
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1830
	$sanitized_user_login = sanitize_user( $userdata['user_login'], true );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1831
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1832
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1833
	 * Filter a username after it has been sanitized.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1834
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1835
	 * This filter is called before the user is created or updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1836
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1837
	 * @since 2.0.3
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1838
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1839
	 * @param string $sanitized_user_login Username after it has been sanitized.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1840
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1841
	$pre_user_login = apply_filters( 'pre_user_login', $sanitized_user_login );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1842
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1843
	//Remove any non-printable chars from the login string to see if we have ended up with an empty username
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1844
	$user_login = trim( $pre_user_login );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1845
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1846
	if ( empty( $user_login ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1847
		return new WP_Error('empty_user_login', __('Cannot create a user with an empty login name.') );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1848
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1849
	if ( ! $update && username_exists( $user_login ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1850
		return new WP_Error( 'existing_user_login', __( 'Sorry, that username already exists!' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1851
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1852
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1853
	// If a nicename is provided, remove unsafe user characters before
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1854
	// using it. Otherwise build a nicename from the user_login.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1855
	if ( ! empty( $userdata['user_nicename'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1856
		$user_nicename = sanitize_user( $userdata['user_nicename'], true );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1857
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1858
		$user_nicename = $user_login;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1859
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1860
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1861
	$user_nicename = sanitize_title( $user_nicename );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1862
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1863
	// Store values to save in user meta.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1864
	$meta = array();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1865
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1866
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1867
	 * Filter a user's nicename before the user is created or updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1868
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1869
	 * @since 2.0.3
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1870
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1871
	 * @param string $user_nicename The user's nicename.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1872
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1873
	$user_nicename = apply_filters( 'pre_user_nicename', $user_nicename );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1874
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1875
	$raw_user_url = empty( $userdata['user_url'] ) ? '' : $userdata['user_url'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1876
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1877
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1878
	 * Filter a user's URL before the user is created or updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1879
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1880
	 * @since 2.0.3
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1881
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1882
	 * @param string $raw_user_url The user's URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1883
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1884
	$user_url = apply_filters( 'pre_user_url', $raw_user_url );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1885
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1886
	$raw_user_email = empty( $userdata['user_email'] ) ? '' : $userdata['user_email'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1887
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1888
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1889
	 * Filter a user's email before the user is created or updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1890
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1891
	 * @since 2.0.3
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1892
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1893
	 * @param string $raw_user_email The user's email.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1894
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1895
	$user_email = apply_filters( 'pre_user_email', $raw_user_email );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1896
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1897
	/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1898
	 * If there is no update, just check for `email_exists`. If there is an update,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1899
	 * check if current email and new email are the same, or not, and check `email_exists`
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1900
	 * accordingly.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1901
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1902
	if ( ( ! $update || ( ! empty( $old_user_data ) && $user_email !== $old_user_data->user_email ) )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1903
		&& ! defined( 'WP_IMPORTING' )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1904
		&& email_exists( $user_email )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1905
	) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1906
		return new WP_Error( 'existing_user_email', __( 'Sorry, that email address is already used!' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1907
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1908
	$nickname = empty( $userdata['nickname'] ) ? $user_login : $userdata['nickname'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1909
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1910
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1911
	 * Filter a user's nickname before the user is created or updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1912
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1913
	 * @since 2.0.3
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1914
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1915
	 * @param string $nickname The user's nickname.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1916
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1917
	$meta['nickname'] = apply_filters( 'pre_user_nickname', $nickname );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1918
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1919
	$first_name = empty( $userdata['first_name'] ) ? '' : $userdata['first_name'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1920
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1921
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1922
	 * Filter a user's first name before the user is created or updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1923
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1924
	 * @since 2.0.3
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1925
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1926
	 * @param string $first_name The user's first name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1927
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1928
	$meta['first_name'] = apply_filters( 'pre_user_first_name', $first_name );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1929
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1930
	$last_name = empty( $userdata['last_name'] ) ? '' : $userdata['last_name'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1931
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1932
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1933
	 * Filter a user's last name before the user is created or updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1934
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1935
	 * @since 2.0.3
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1936
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1937
	 * @param string $last_name The user's last name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1938
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1939
	$meta['last_name'] = apply_filters( 'pre_user_last_name', $last_name );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1940
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1941
	if ( empty( $userdata['display_name'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1942
		if ( $update ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1943
			$display_name = $user_login;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1944
		} elseif ( $meta['first_name'] && $meta['last_name'] ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1945
			/* translators: 1: first name, 2: last name */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1946
			$display_name = sprintf( _x( '%1$s %2$s', 'Display name based on first name and last name' ), $meta['first_name'], $meta['last_name'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1947
		} elseif ( $meta['first_name'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1948
			$display_name = $meta['first_name'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1949
		} elseif ( $meta['last_name'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1950
			$display_name = $meta['last_name'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1951
		} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1952
			$display_name = $user_login;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1953
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1954
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1955
		$display_name = $userdata['display_name'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1956
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1957
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1958
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1959
	 * Filter a user's display name before the user is created or updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1960
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1961
	 * @since 2.0.3
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1962
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1963
	 * @param string $display_name The user's display name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1964
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1965
	$display_name = apply_filters( 'pre_user_display_name', $display_name );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1966
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1967
	$description = empty( $userdata['description'] ) ? '' : $userdata['description'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1968
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1969
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1970
	 * Filter a user's description before the user is created or updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1971
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1972
	 * @since 2.0.3
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1973
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1974
	 * @param string $description The user's description.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1975
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1976
	$meta['description'] = apply_filters( 'pre_user_description', $description );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1977
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1978
	$meta['rich_editing'] = empty( $userdata['rich_editing'] ) ? 'true' : $userdata['rich_editing'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1979
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1980
	$meta['comment_shortcuts'] = empty( $userdata['comment_shortcuts'] ) ? 'false' : $userdata['comment_shortcuts'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1981
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1982
	$admin_color = empty( $userdata['admin_color'] ) ? 'fresh' : $userdata['admin_color'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1983
	$meta['admin_color'] = preg_replace( '|[^a-z0-9 _.\-@]|i', '', $admin_color );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1984
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1985
	$meta['use_ssl'] = empty( $userdata['use_ssl'] ) ? 0 : $userdata['use_ssl'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1986
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1987
	$user_registered = empty( $userdata['user_registered'] ) ? gmdate( 'Y-m-d H:i:s' ) : $userdata['user_registered'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1988
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1989
	$meta['show_admin_bar_front'] = empty( $userdata['show_admin_bar_front'] ) ? 'true' : $userdata['show_admin_bar_front'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1990
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1991
	$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));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1992
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1993
	if ( $user_nicename_check ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1994
		$suffix = 2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1995
		while ($user_nicename_check) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1996
			$alt_user_nicename = $user_nicename . "-$suffix";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1997
			$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));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1998
			$suffix++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1999
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2000
		$user_nicename = $alt_user_nicename;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2001
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2002
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2003
	$compacted = compact( 'user_pass', 'user_email', 'user_url', 'user_nicename', 'display_name', 'user_registered' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2004
	$data = wp_unslash( $compacted );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2005
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2006
	if ( $update ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2007
		if ( $user_email !== $old_user_data->user_email ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2008
			$data['user_activation_key'] = '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2009
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2010
		$wpdb->update( $wpdb->users, $data, compact( 'ID' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2011
		$user_id = (int) $ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2012
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2013
		$wpdb->insert( $wpdb->users, $data + compact( 'user_login' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2014
		$user_id = (int) $wpdb->insert_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2015
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2016
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2017
	$user = new WP_User( $user_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2018
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2019
	// Update user meta.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2020
	foreach ( $meta as $key => $value ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2021
		update_user_meta( $user_id, $key, $value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2022
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2023
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2024
	foreach ( wp_get_user_contact_methods( $user ) as $key => $value ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2025
		if ( isset( $userdata[ $key ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2026
			update_user_meta( $user_id, $key, $userdata[ $key ] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2027
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2028
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2029
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2030
	if ( isset( $userdata['role'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2031
		$user->set_role( $userdata['role'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2032
	} elseif ( ! $update ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2033
		$user->set_role(get_option('default_role'));
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2034
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2035
	wp_cache_delete( $user_id, 'users' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2036
	wp_cache_delete( $user_login, 'userlogins' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2037
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2038
	if ( $update ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2039
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2040
		 * Fires immediately after an existing user is updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2041
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2042
		 * @since 2.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2043
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2044
		 * @param int    $user_id       User ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2045
		 * @param object $old_user_data Object containing user's data prior to update.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2046
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2047
		do_action( 'profile_update', $user_id, $old_user_data );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2048
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2049
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2050
		 * Fires immediately after a new user is registered.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2051
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2052
		 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2053
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2054
		 * @param int $user_id User ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2055
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2056
		do_action( 'user_register', $user_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2057
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2058
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2059
	return $user_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2060
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2061
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2062
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2063
 * Update a user in the database.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2064
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2065
 * It is possible to update a user's password by specifying the 'user_pass'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2066
 * value in the $userdata parameter array.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2067
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2068
 * If current user's password is being updated, then the cookies will be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2069
 * cleared.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2070
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2071
 * @since 2.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2072
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2073
 * @see wp_insert_user() For what fields can be set in $userdata.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2074
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2075
 * @param mixed $userdata An array of user data or a user object of type stdClass or WP_User.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2076
 * @return int|WP_Error The updated user's ID or a WP_Error object if the user could not be updated.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2077
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2078
function wp_update_user($userdata) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2079
	if ( $userdata instanceof stdClass ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2080
		$userdata = get_object_vars( $userdata );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2081
	} elseif ( $userdata instanceof WP_User ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2082
		$userdata = $userdata->to_array();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2083
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2084
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2085
	$ID = isset( $userdata['ID'] ) ? (int) $userdata['ID'] : 0;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2086
	if ( ! $ID ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2087
		return new WP_Error( 'invalid_user_id', __( 'Invalid user ID.' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2088
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2089
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2090
	// First, get all of the original fields
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2091
	$user_obj = get_userdata( $ID );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2092
	if ( ! $user_obj ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2093
		return new WP_Error( 'invalid_user_id', __( 'Invalid user ID.' ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2094
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2095
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2096
	$user = $user_obj->to_array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2097
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2098
	// Add additional custom fields
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2099
	foreach ( _get_additional_user_keys( $user_obj ) as $key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2100
		$user[ $key ] = get_user_meta( $ID, $key, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2101
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2102
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2103
	// Escape data pulled from DB.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2104
	$user = add_magic_quotes( $user );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2105
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2106
	// If password is changing, hash it now.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2107
	if ( ! empty($userdata['user_pass']) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2108
		$plaintext_pass = $userdata['user_pass'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2109
		$userdata['user_pass'] = wp_hash_password($userdata['user_pass']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2110
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2111
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2112
	wp_cache_delete($user[ 'user_email' ], 'useremail');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2113
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2114
	// Merge old and new fields with new fields overwriting old ones.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2115
	$userdata = array_merge($user, $userdata);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2116
	$user_id = wp_insert_user($userdata);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2117
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2118
	// Update the cookies if the password changed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2119
	$current_user = wp_get_current_user();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2120
	if ( $current_user->ID == $ID ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2121
		if ( isset($plaintext_pass) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2122
			wp_clear_auth_cookie();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2123
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2124
			// Here we calculate the expiration length of the current auth cookie and compare it to the default expiration.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2125
			// If it's greater than this, then we know the user checked 'Remember Me' when they logged in.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2126
			$logged_in_cookie    = wp_parse_auth_cookie( '', 'logged_in' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2127
			/** This filter is documented in wp-includes/pluggable.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2128
			$default_cookie_life = apply_filters( 'auth_cookie_expiration', ( 2 * DAY_IN_SECONDS ), $ID, false );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2129
			$remember            = ( ( $logged_in_cookie['expiration'] - time() ) > $default_cookie_life );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2130
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2131
			wp_set_auth_cookie( $ID, $remember );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2132
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2133
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2134
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2135
	return $user_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2136
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2137
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2138
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2139
 * A simpler way of inserting a user into the database.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2140
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2141
 * Creates a new user with just the username, password, and email. For more
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2142
 * complex user creation use {@see wp_insert_user()} to specify more information.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2143
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2144
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2145
 * @see wp_insert_user() More complete way to create a new user
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2146
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2147
 * @param string $username The user's username.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2148
 * @param string $password The user's password.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2149
 * @param string $email    Optional. The user's email. Default empty.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2150
 * @return int The new user's ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2151
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2152
function wp_create_user($username, $password, $email = '') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2153
	$user_login = wp_slash( $username );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2154
	$user_email = wp_slash( $email    );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2155
	$user_pass = $password;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2156
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2157
	$userdata = compact('user_login', 'user_email', 'user_pass');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2158
	return wp_insert_user($userdata);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2159
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2160
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2161
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2162
 * Return a list of meta keys that wp_insert_user() is supposed to set.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2163
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2164
 * @since 3.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2165
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2166
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2167
 * @param WP_User $user WP_User instance.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2168
 * @return array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2169
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2170
function _get_additional_user_keys( $user ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2171
	$keys = array( 'first_name', 'last_name', 'nickname', 'description', 'rich_editing', 'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2172
	return array_merge( $keys, array_keys( wp_get_user_contact_methods( $user ) ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2173
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2174
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2175
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2176
 * Set up the user contact methods.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2177
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2178
 * Default contact methods were removed in 3.6. A filter dictates contact methods.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2179
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2180
 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2181
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2182
 * @param WP_User $user Optional. WP_User object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2183
 * @return array Array of contact methods and their labels.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2184
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2185
function wp_get_user_contact_methods( $user = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2186
	$methods = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2187
	if ( get_site_option( 'initial_db_version' ) < 23588 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2188
		$methods = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2189
			'aim'    => __( 'AIM' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2190
			'yim'    => __( 'Yahoo IM' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2191
			'jabber' => __( 'Jabber / Google Talk' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2192
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2193
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2194
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2195
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2196
	 * Filter the user contact methods.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2197
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2198
	 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2199
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2200
	 * @param array   $methods Array of contact methods and their labels.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2201
 	 * @param WP_User $user    WP_User object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2202
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2203
	return apply_filters( 'user_contactmethods', $methods, $user );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2204
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2205
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2206
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2207
 * The old private function for setting up user contact methods.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2208
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2209
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2210
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2211
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2212
function _wp_get_user_contactmethods( $user = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2213
	return wp_get_user_contact_methods( $user );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2214
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2215
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2216
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2217
 * Gets the text suggesting how to create strong passwords.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2218
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2219
 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2220
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2221
 * @return string The password hint text.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2222
 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2223
function wp_get_password_hint() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2224
	$hint = __( 'Hint: The password should be at least seven characters long. To make it stronger, use upper and lower case letters, numbers, and symbols like ! " ? $ % ^ &amp; ).' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2225
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2226
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2227
	 * Filter the text describing the site's password complexity policy.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2228
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2229
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2230
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2231
	 * @param string $hint The password hint text.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2232
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2233
	return apply_filters( 'password_hint', $hint );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2234
}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2235
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2236
/**
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2237
 * Retrieves a user row based on password reset key and login
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2238
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2239
 * A key is considered 'expired' if it exactly matches the value of the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2240
 * user_activation_key field, rather than being matched after going through the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2241
 * hashing process. This field is now hashed; old values are no longer accepted
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2242
 * but have a different WP_Error code so good user feedback can be provided.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2243
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2244
 * @global wpdb         $wpdb      WordPress database object for queries.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2245
 * @global PasswordHash $wp_hasher Portable PHP password hashing framework instance.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2246
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2247
 * @param string $key       Hash to validate sending user's password.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2248
 * @param string $login     The user login.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2249
 * @return WP_User|WP_Error WP_User object on success, WP_Error object for invalid or expired keys.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2250
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2251
function check_password_reset_key($key, $login) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2252
	global $wpdb, $wp_hasher;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2253
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2254
	$key = preg_replace('/[^a-z0-9]/i', '', $key);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2255
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2256
	if ( empty( $key ) || !is_string( $key ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2257
		return new WP_Error('invalid_key', __('Invalid key'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2258
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2259
	if ( empty($login) || !is_string($login) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2260
		return new WP_Error('invalid_key', __('Invalid key'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2261
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2262
	$row = $wpdb->get_row( $wpdb->prepare( "SELECT ID, user_activation_key FROM $wpdb->users WHERE user_login = %s", $login ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2263
	if ( ! $row )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2264
		return new WP_Error('invalid_key', __('Invalid key'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2265
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2266
	if ( empty( $wp_hasher ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2267
		require_once ABSPATH . WPINC . '/class-phpass.php';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2268
		$wp_hasher = new PasswordHash( 8, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2269
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2270
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2271
	if ( $wp_hasher->CheckPassword( $key, $row->user_activation_key ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2272
		return get_userdata( $row->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2273
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2274
	if ( $key === $row->user_activation_key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2275
		$return = new WP_Error( 'expired_key', __( 'Invalid key' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2276
		$user_id = $row->ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2277
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2278
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2279
		 * Filter the return value of check_password_reset_key() when an
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2280
		 * old-style key is used (plain-text key was stored in the database).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2281
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2282
		 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2283
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2284
		 * @param WP_Error $return  A WP_Error object denoting an expired key.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2285
		 *                          Return a WP_User object to validate the key.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2286
		 * @param int      $user_id The matched user ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2287
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2288
		return apply_filters( 'password_reset_key_expired', $return, $user_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2289
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2290
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2291
	return new WP_Error( 'invalid_key', __( 'Invalid key' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2292
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2293
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2294
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2295
 * Handles resetting the user's password.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2296
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2297
 * @param object $user The user
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2298
 * @param string $new_pass New password for the user in plaintext
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2299
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2300
function reset_password( $user, $new_pass ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2301
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2302
	 * Fires before the user's password is reset.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2303
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2304
	 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2305
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2306
	 * @param object $user     The user.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2307
	 * @param string $new_pass New user password.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2308
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2309
	do_action( 'password_reset', $user, $new_pass );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2310
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2311
	wp_set_password( $new_pass, $user->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2312
	update_user_option( $user->ID, 'default_password_nag', false, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2313
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2314
	wp_password_change_notification( $user );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2315
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2316
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2317
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2318
 * Handles registering a new user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2319
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2320
 * @param string $user_login User's username for logging in
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2321
 * @param string $user_email User's email address to send password and add
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2322
 * @return int|WP_Error Either user's ID or error on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2323
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2324
function register_new_user( $user_login, $user_email ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2325
	$errors = new WP_Error();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2326
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2327
	$sanitized_user_login = sanitize_user( $user_login );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2328
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2329
	 * Filter the email address of a user being registered.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2330
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2331
	 * @since 2.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2332
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2333
	 * @param string $user_email The email address of the new user.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2334
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2335
	$user_email = apply_filters( 'user_registration_email', $user_email );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2336
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2337
	// Check the username
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2338
	if ( $sanitized_user_login == '' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2339
		$errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2340
	} elseif ( ! validate_username( $user_login ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2341
		$errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2342
		$sanitized_user_login = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2343
	} elseif ( username_exists( $sanitized_user_login ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2344
		$errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2345
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2346
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2347
	// Check the e-mail address
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2348
	if ( $user_email == '' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2349
		$errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your e-mail address.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2350
	} elseif ( ! is_email( $user_email ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2351
		$errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn&#8217;t correct.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2352
		$user_email = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2353
	} elseif ( email_exists( $user_email ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2354
		$errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2355
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2356
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2357
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2358
	 * Fires when submitting registration form data, before the user is created.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2359
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2360
	 * @since 2.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2361
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2362
	 * @param string   $sanitized_user_login The submitted username after being sanitized.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2363
	 * @param string   $user_email           The submitted email.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2364
	 * @param WP_Error $errors               Contains any errors with submitted username and email,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2365
	 *                                       e.g., an empty field, an invalid username or email,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2366
	 *                                       or an existing username or email.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2367
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2368
	do_action( 'register_post', $sanitized_user_login, $user_email, $errors );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2369
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2370
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2371
	 * Filter the errors encountered when a new user is being registered.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2372
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2373
	 * The filtered WP_Error object may, for example, contain errors for an invalid
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2374
	 * or existing username or email address. A WP_Error object should always returned,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2375
	 * but may or may not contain errors.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2376
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2377
	 * If any errors are present in $errors, this will abort the user's registration.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2378
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2379
	 * @since 2.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2380
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2381
	 * @param WP_Error $errors               A WP_Error object containing any errors encountered
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2382
	 *                                       during registration.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2383
	 * @param string   $sanitized_user_login User's username after it has been sanitized.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2384
	 * @param string   $user_email           User's email.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2385
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2386
	$errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2387
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2388
	if ( $errors->get_error_code() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2389
		return $errors;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2390
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2391
	$user_pass = wp_generate_password( 12, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2392
	$user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2393
	if ( ! $user_id || is_wp_error( $user_id ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2394
		$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' ) ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2395
		return $errors;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2396
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2397
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2398
	update_user_option( $user_id, 'default_password_nag', true, true ); //Set up the Password change nag.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2399
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2400
	wp_new_user_notification( $user_id, $user_pass );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2401
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2402
	return $user_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2403
}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2404
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2405
/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2406
 * Retrieve the current session token from the logged_in cookie.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2407
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2408
 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2409
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2410
 * @return string Token.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2411
 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2412
function wp_get_session_token() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2413
	$cookie = wp_parse_auth_cookie( '', 'logged_in' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2414
	return ! empty( $cookie['token'] ) ? $cookie['token'] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2415
}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2416
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2417
/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2418
 * Retrieve a list of sessions for the current user.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2419
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2420
 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2421
 * @return array Array of sessions.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2422
 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2423
function wp_get_all_sessions() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2424
	$manager = WP_Session_Tokens::get_instance( get_current_user_id() );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2425
	return $manager->get_all();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2426
}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2427
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2428
/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2429
 * Remove the current session token from the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2430
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2431
 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2432
 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2433
function wp_destroy_current_session() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2434
	$token = wp_get_session_token();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2435
	if ( $token ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2436
		$manager = WP_Session_Tokens::get_instance( get_current_user_id() );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2437
		$manager->destroy( $token );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2438
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2439
}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2440
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2441
/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2442
 * Remove all but the current session token for the current user for the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2443
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2444
 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2445
 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2446
function wp_destroy_other_sessions() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2447
	$token = wp_get_session_token();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2448
	if ( $token ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2449
		$manager = WP_Session_Tokens::get_instance( get_current_user_id() );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2450
		$manager->destroy_others( $token );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2451
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2452
}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2453
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2454
/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2455
 * Remove all session tokens for the current user from the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2456
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2457
 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2458
 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2459
function wp_destroy_all_sessions() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2460
	$manager = WP_Session_Tokens::get_instance( get_current_user_id() );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2461
	$manager->destroy_all();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2462
}