mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * WordPress User API
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 */
mp-wp_genesis 7
mp-wp_genesis 8 /**
mp-wp_genesis 9 * Authenticate user with remember capability.
mp-wp_genesis 10 *
mp-wp_genesis 11 * The credentials is an array that has 'user_login', 'user_password', and
mp-wp_genesis 12 * 'remember' indices. If the credentials is not given, then the log in form
mp-wp_genesis 13 * will be assumed and used if set.
mp-wp_genesis 14 *
mp-wp_genesis 15 * The various authentication cookies will be set by this function and will be
mp-wp_genesis 16 * set for a longer period depending on if the 'remember' credential is set to
mp-wp_genesis 17 * true.
mp-wp_genesis 18 *
mp-wp_genesis 19 * @since 2.5.0
mp-wp_genesis 20 *
mp-wp_genesis 21 * @param array $credentials Optional. User info in order to sign on.
mp-wp_genesis 22 * @param bool $secure_cookie Optional. Whether to use secure cookie.
mp-wp_genesis 23 * @return object Either WP_Error on failure, or WP_User on success.
mp-wp_genesis 24 */
mp-wp_genesis 25 function wp_signon( $credentials = '', $secure_cookie = '' ) {
mp-wp_genesis 26 if ( empty($credentials) ) {
mp-wp_genesis 27 if ( ! empty($_POST['log']) )
mp-wp_genesis 28 $credentials['user_login'] = $_POST['log'];
mp-wp_genesis 29 if ( ! empty($_POST['pwd']) )
mp-wp_genesis 30 $credentials['user_password'] = $_POST['pwd'];
mp-wp_genesis 31 if ( ! empty($_POST['rememberme']) )
mp-wp_genesis 32 $credentials['remember'] = $_POST['rememberme'];
mp-wp_genesis 33 }
mp-wp_genesis 34
mp-wp_genesis 35 if ( !empty($credentials['user_login']) )
mp-wp_genesis 36 $credentials['user_login'] = sanitize_user($credentials['user_login']);
mp-wp_genesis 37 if ( !empty($credentials['user_password']) )
mp-wp_genesis 38 $credentials['user_password'] = trim($credentials['user_password']);
mp-wp_genesis 39 if ( !empty($credentials['remember']) )
mp-wp_genesis 40 $credentials['remember'] = true;
mp-wp_genesis 41 else
mp-wp_genesis 42 $credentials['remember'] = false;
mp-wp_genesis 43
mp-wp_genesis 44 do_action_ref_array('wp_authenticate', array(&$credentials['user_login'], &$credentials['user_password']));
mp-wp_genesis 45
mp-wp_genesis 46 if ( '' === $secure_cookie )
mp-wp_genesis 47 $secure_cookie = is_ssl() ? true : false;
mp-wp_genesis 48
mp-wp_genesis 49 // If no credential info provided, check cookie.
mp-wp_genesis 50 if ( empty($credentials['user_login']) && empty($credentials['user_password']) ) {
mp-wp_genesis 51 $user = wp_validate_auth_cookie();
mp-wp_genesis 52 if ( $user )
mp-wp_genesis 53 return new WP_User($user);
mp-wp_genesis 54
mp-wp_genesis 55 if ( $secure_cookie )
mp-wp_genesis 56 $auth_cookie = SECURE_AUTH_COOKIE;
mp-wp_genesis 57 else
mp-wp_genesis 58 $auth_cookie = AUTH_COOKIE;
mp-wp_genesis 59
mp-wp_genesis 60 if ( !empty($_COOKIE[$auth_cookie]) )
mp-wp_genesis 61 return new WP_Error('expired_session', __('Please log in again.'));
mp-wp_genesis 62
mp-wp_genesis 63 // If the cookie is not set, be silent.
mp-wp_genesis 64 return new WP_Error();
mp-wp_genesis 65 }
mp-wp_genesis 66
mp-wp_genesis 67 if ( empty($credentials['user_login']) || empty($credentials['user_password']) ) {
mp-wp_genesis 68 $error = new WP_Error();
mp-wp_genesis 69
mp-wp_genesis 70 if ( empty($credentials['user_login']) )
mp-wp_genesis 71 $error->add('empty_username', __('<strong>ERROR</strong>: The username field is empty.'));
mp-wp_genesis 72 if ( empty($credentials['user_password']) )
mp-wp_genesis 73 $error->add('empty_password', __('<strong>ERROR</strong>: The password field is empty.'));
mp-wp_genesis 74 return $error;
mp-wp_genesis 75 }
mp-wp_genesis 76
mp-wp_genesis 77 $user = wp_authenticate($credentials['user_login'], $credentials['user_password']);
mp-wp_genesis 78 if ( is_wp_error($user) )
mp-wp_genesis 79 return $user;
mp-wp_genesis 80
mp-wp_genesis 81 wp_set_auth_cookie($user->ID, $credentials['remember'], $secure_cookie);
mp-wp_genesis 82 do_action('wp_login', $credentials['user_login']);
mp-wp_genesis 83 return $user;
mp-wp_genesis 84 }
mp-wp_genesis 85
mp-wp_genesis 86 /**
mp-wp_genesis 87 * Retrieve user data based on field.
mp-wp_genesis 88 *
mp-wp_genesis 89 * Use get_profile() will make a database query to get the value of the table
mp-wp_genesis 90 * column. The value might be cached using the query cache, but care should be
mp-wp_genesis 91 * taken when using the function to not make a lot of queries for retrieving
mp-wp_genesis 92 * user profile information.
mp-wp_genesis 93 *
mp-wp_genesis 94 * If the $user parameter is not used, then the user will be retrieved from a
mp-wp_genesis 95 * cookie of the user. Therefore, if the cookie does not exist, then no value
mp-wp_genesis 96 * might be returned. Sanity checking must be done to ensure that when using
mp-wp_genesis 97 * get_profile() that empty/null/false values are handled and that something is
mp-wp_genesis 98 * at least displayed.
mp-wp_genesis 99 *
mp-wp_genesis 100 * @since 1.5.0
mp-wp_genesis 101 * @uses $wpdb WordPress database object to create queries.
mp-wp_genesis 102 *
mp-wp_genesis 103 * @param string $field User field to retrieve.
mp-wp_genesis 104 * @param string $user Optional. User username.
mp-wp_genesis 105 * @return string The value in the field.
mp-wp_genesis 106 */
mp-wp_genesis 107 function get_profile($field, $user = false) {
mp-wp_genesis 108 global $wpdb;
mp-wp_genesis 109 if ( !$user )
mp-wp_genesis 110 $user = $wpdb->escape($_COOKIE[USER_COOKIE]);
mp-wp_genesis 111 return $wpdb->get_var( $wpdb->prepare("SELECT $field FROM $wpdb->users WHERE user_login = %s", $user) );
mp-wp_genesis 112 }
mp-wp_genesis 113
mp-wp_genesis 114 /**
mp-wp_genesis 115 * Number of posts user has written.
mp-wp_genesis 116 *
mp-wp_genesis 117 * @since 0.71
mp-wp_genesis 118 * @uses $wpdb WordPress database object for queries.
mp-wp_genesis 119 *
mp-wp_genesis 120 * @param int $userid User ID.
mp-wp_genesis 121 * @return int Amount of posts user has written.
mp-wp_genesis 122 */
mp-wp_genesis 123 function get_usernumposts($userid) {
mp-wp_genesis 124 global $wpdb;
mp-wp_genesis 125 $userid = (int) $userid;
mp-wp_genesis 126 $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = %d AND post_type = 'post' AND ", $userid) . get_private_posts_cap_sql('post'));
mp-wp_genesis 127 return apply_filters('get_usernumposts', $count, $userid);
mp-wp_genesis 128 }
mp-wp_genesis 129
mp-wp_genesis 130 /**
mp-wp_genesis 131 * Check that the user login name and password is correct.
mp-wp_genesis 132 *
mp-wp_genesis 133 * @since 0.71
mp-wp_genesis 134 * @todo xmlrpc only. Maybe move to xmlrpc.php.
mp-wp_genesis 135 *
mp-wp_genesis 136 * @param string $user_login User name.
mp-wp_genesis 137 * @param string $user_pass User password.
mp-wp_genesis 138 * @return bool False if does not authenticate, true if username and password authenticates.
mp-wp_genesis 139 */
mp-wp_genesis 140 function user_pass_ok($user_login, $user_pass) {
mp-wp_genesis 141 $user = wp_authenticate($user_login, $user_pass);
mp-wp_genesis 142 if ( is_wp_error($user) )
mp-wp_genesis 143 return false;
mp-wp_genesis 144
mp-wp_genesis 145 return true;
mp-wp_genesis 146 }
mp-wp_genesis 147
mp-wp_genesis 148 //
mp-wp_genesis 149 // User option functions
mp-wp_genesis 150 //
mp-wp_genesis 151
mp-wp_genesis 152 /**
mp-wp_genesis 153 * Retrieve user option that can be either global, user, or blog.
mp-wp_genesis 154 *
mp-wp_genesis 155 * If the user ID is not given, then the current user will be used instead. If
mp-wp_genesis 156 * the user ID is given, then the user data will be retrieved. The filter for
mp-wp_genesis 157 * the result, will also pass the original option name and finally the user data
mp-wp_genesis 158 * object as the third parameter.
mp-wp_genesis 159 *
mp-wp_genesis 160 * The option will first check for the non-global name, then the global name,
mp-wp_genesis 161 * and if it still doesn't find it, it will try the blog option. The option can
mp-wp_genesis 162 * either be modified or set by a plugin.
mp-wp_genesis 163 *
mp-wp_genesis 164 * @since 2.0.0
mp-wp_genesis 165 * @uses $wpdb WordPress database object for queries.
mp-wp_genesis 166 * @uses apply_filters() Calls 'get_user_option_$option' hook with result,
mp-wp_genesis 167 * option parameter, and user data object.
mp-wp_genesis 168 *
mp-wp_genesis 169 * @param string $option User option name.
mp-wp_genesis 170 * @param int $user Optional. User ID.
mp-wp_genesis 171 * @param bool $check_blog_options Whether to check for an option in the options table if a per-user option does not exist. Default is true.
mp-wp_genesis 172 * @return mixed
mp-wp_genesis 173 */
mp-wp_genesis 174 function get_user_option( $option, $user = 0, $check_blog_options = true ) {
mp-wp_genesis 175 global $wpdb;
mp-wp_genesis 176
mp-wp_genesis 177 $option = preg_replace('|[^a-z0-9_]|i', '', $option);
mp-wp_genesis 178 if ( empty($user) )
mp-wp_genesis 179 $user = wp_get_current_user();
mp-wp_genesis 180 else
mp-wp_genesis 181 $user = get_userdata($user);
mp-wp_genesis 182
mp-wp_genesis 183 if ( isset( $user->{$wpdb->prefix . $option} ) ) // Blog specific
mp-wp_genesis 184 $result = $user->{$wpdb->prefix . $option};
mp-wp_genesis 185 elseif ( isset( $user->{$option} ) ) // User specific and cross-blog
mp-wp_genesis 186 $result = $user->{$option};
mp-wp_genesis 187 elseif ( $check_blog_options ) // Blog global
mp-wp_genesis 188 $result = get_option( $option );
mp-wp_genesis 189 else
mp-wp_genesis 190 $result = false;
mp-wp_genesis 191
mp-wp_genesis 192 return apply_filters("get_user_option_{$option}", $result, $option, $user);
mp-wp_genesis 193 }
mp-wp_genesis 194
mp-wp_genesis 195 /**
mp-wp_genesis 196 * Update user option with global blog capability.
mp-wp_genesis 197 *
mp-wp_genesis 198 * User options are just like user metadata except that they have support for
mp-wp_genesis 199 * global blog options. If the 'global' parameter is false, which it is by false
mp-wp_genesis 200 * it will prepend the WordPress table prefix to the option name.
mp-wp_genesis 201 *
mp-wp_genesis 202 * @since 2.0.0
mp-wp_genesis 203 * @uses $wpdb WordPress database object for queries
mp-wp_genesis 204 *
mp-wp_genesis 205 * @param int $user_id User ID
mp-wp_genesis 206 * @param string $option_name User option name.
mp-wp_genesis 207 * @param mixed $newvalue User option value.
mp-wp_genesis 208 * @param bool $global Optional. Whether option name is blog specific or not.
mp-wp_genesis 209 * @return unknown
mp-wp_genesis 210 */
mp-wp_genesis 211 function update_user_option( $user_id, $option_name, $newvalue, $global = false ) {
mp-wp_genesis 212 global $wpdb;
mp-wp_genesis 213 if ( !$global )
mp-wp_genesis 214 $option_name = $wpdb->prefix . $option_name;
mp-wp_genesis 215 return update_usermeta( $user_id, $option_name, $newvalue );
mp-wp_genesis 216 }
mp-wp_genesis 217
mp-wp_genesis 218 /**
mp-wp_genesis 219 * Get users for the blog.
mp-wp_genesis 220 *
mp-wp_genesis 221 * For setups that use the multi-blog feature. Can be used outside of the
mp-wp_genesis 222 * multi-blog feature.
mp-wp_genesis 223 *
mp-wp_genesis 224 * @since 2.2.0
mp-wp_genesis 225 * @uses $wpdb WordPress database object for queries
mp-wp_genesis 226 * @uses $blog_id The Blog id of the blog for those that use more than one blog
mp-wp_genesis 227 *
mp-wp_genesis 228 * @param int $id Blog ID.
mp-wp_genesis 229 * @return array List of users that are part of that Blog ID
mp-wp_genesis 230 */
mp-wp_genesis 231 function get_users_of_blog( $id = '' ) {
mp-wp_genesis 232 global $wpdb, $blog_id;
mp-wp_genesis 233 if ( empty($id) )
mp-wp_genesis 234 $id = (int) $blog_id;
mp-wp_genesis 235 $users = $wpdb->get_results( "SELECT user_id, user_login, display_name, user_email, meta_value FROM $wpdb->users, $wpdb->usermeta WHERE " . $wpdb->users . ".ID = " . $wpdb->usermeta . ".user_id AND meta_key = '" . $wpdb->prefix . "capabilities' ORDER BY {$wpdb->usermeta}.user_id" );
mp-wp_genesis 236 return $users;
mp-wp_genesis 237 }
mp-wp_genesis 238
mp-wp_genesis 239 //
mp-wp_genesis 240 // User meta functions
mp-wp_genesis 241 //
mp-wp_genesis 242
mp-wp_genesis 243 /**
mp-wp_genesis 244 * Remove user meta data.
mp-wp_genesis 245 *
mp-wp_genesis 246 * @since 2.0.0
mp-wp_genesis 247 * @uses $wpdb WordPress database object for queries.
mp-wp_genesis 248 *
mp-wp_genesis 249 * @param int $user_id User ID.
mp-wp_genesis 250 * @param string $meta_key Metadata key.
mp-wp_genesis 251 * @param mixed $meta_value Metadata value.
mp-wp_genesis 252 * @return bool True deletion completed and false if user_id is not a number.
mp-wp_genesis 253 */
mp-wp_genesis 254 function delete_usermeta( $user_id, $meta_key, $meta_value = '' ) {
mp-wp_genesis 255 global $wpdb;
mp-wp_genesis 256 if ( !is_numeric( $user_id ) )
mp-wp_genesis 257 return false;
mp-wp_genesis 258 $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
mp-wp_genesis 259
mp-wp_genesis 260 if ( is_array($meta_value) || is_object($meta_value) )
mp-wp_genesis 261 $meta_value = serialize($meta_value);
mp-wp_genesis 262 $meta_value = trim( $meta_value );
mp-wp_genesis 263
mp-wp_genesis 264 if ( ! empty($meta_value) )
mp-wp_genesis 265 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s AND meta_value = %s", $user_id, $meta_key, $meta_value) );
mp-wp_genesis 266 else
mp-wp_genesis 267 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) );
mp-wp_genesis 268
mp-wp_genesis 269 wp_cache_delete($user_id, 'users');
mp-wp_genesis 270
mp-wp_genesis 271 return true;
mp-wp_genesis 272 }
mp-wp_genesis 273
mp-wp_genesis 274 /**
mp-wp_genesis 275 * Retrieve user metadata.
mp-wp_genesis 276 *
mp-wp_genesis 277 * If $user_id is not a number, then the function will fail over with a 'false'
mp-wp_genesis 278 * boolean return value. Other returned values depend on whether there is only
mp-wp_genesis 279 * one item to be returned, which be that single item type. If there is more
mp-wp_genesis 280 * than one metadata value, then it will be list of metadata values.
mp-wp_genesis 281 *
mp-wp_genesis 282 * @since 2.0.0
mp-wp_genesis 283 * @uses $wpdb WordPress database object for queries.
mp-wp_genesis 284 *
mp-wp_genesis 285 * @param int $user_id User ID
mp-wp_genesis 286 * @param string $meta_key Optional. Metadata key.
mp-wp_genesis 287 * @return mixed
mp-wp_genesis 288 */
mp-wp_genesis 289 function get_usermeta( $user_id, $meta_key = '') {
mp-wp_genesis 290 global $wpdb;
mp-wp_genesis 291 $user_id = (int) $user_id;
mp-wp_genesis 292
mp-wp_genesis 293 if ( !$user_id )
mp-wp_genesis 294 return false;
mp-wp_genesis 295
mp-wp_genesis 296 if ( !empty($meta_key) ) {
mp-wp_genesis 297 $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
mp-wp_genesis 298 $user = wp_cache_get($user_id, 'users');
mp-wp_genesis 299 // Check the cached user object
mp-wp_genesis 300 if ( false !== $user && isset($user->$meta_key) )
mp-wp_genesis 301 $metas = array($user->$meta_key);
mp-wp_genesis 302 else
mp-wp_genesis 303 $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) );
mp-wp_genesis 304 } else {
mp-wp_genesis 305 $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM $wpdb->usermeta WHERE user_id = %d", $user_id) );
mp-wp_genesis 306 }
mp-wp_genesis 307
mp-wp_genesis 308 if ( empty($metas) ) {
mp-wp_genesis 309 if ( empty($meta_key) )
mp-wp_genesis 310 return array();
mp-wp_genesis 311 else
mp-wp_genesis 312 return '';
mp-wp_genesis 313 }
mp-wp_genesis 314
mp-wp_genesis 315 $metas = array_map('maybe_unserialize', $metas);
mp-wp_genesis 316
mp-wp_genesis 317 if ( count($metas) == 1 )
mp-wp_genesis 318 return $metas[0];
mp-wp_genesis 319 else
mp-wp_genesis 320 return $metas;
mp-wp_genesis 321 }
mp-wp_genesis 322
mp-wp_genesis 323 /**
mp-wp_genesis 324 * Update metadata of user.
mp-wp_genesis 325 *
mp-wp_genesis 326 * There is no need to serialize values, they will be serialized if it is
mp-wp_genesis 327 * needed. The metadata key can only be a string with underscores. All else will
mp-wp_genesis 328 * be removed.
mp-wp_genesis 329 *
mp-wp_genesis 330 * Will remove the metadata, if the meta value is empty.
mp-wp_genesis 331 *
mp-wp_genesis 332 * @since 2.0.0
mp-wp_genesis 333 * @uses $wpdb WordPress database object for queries
mp-wp_genesis 334 *
mp-wp_genesis 335 * @param int $user_id User ID
mp-wp_genesis 336 * @param string $meta_key Metadata key.
mp-wp_genesis 337 * @param mixed $meta_value Metadata value.
mp-wp_genesis 338 * @return bool True on successful update, false on failure.
mp-wp_genesis 339 */
mp-wp_genesis 340 function update_usermeta( $user_id, $meta_key, $meta_value ) {
mp-wp_genesis 341 global $wpdb;
mp-wp_genesis 342 if ( !is_numeric( $user_id ) )
mp-wp_genesis 343 return false;
mp-wp_genesis 344 $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
mp-wp_genesis 345
mp-wp_genesis 346 /** @todo Might need fix because usermeta data is assumed to be already escaped */
mp-wp_genesis 347 if ( is_string($meta_value) )
mp-wp_genesis 348 $meta_value = stripslashes($meta_value);
mp-wp_genesis 349 $meta_value = maybe_serialize($meta_value);
mp-wp_genesis 350
mp-wp_genesis 351 if (empty($meta_value)) {
mp-wp_genesis 352 return delete_usermeta($user_id, $meta_key);
mp-wp_genesis 353 }
mp-wp_genesis 354
mp-wp_genesis 355 $cur = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) );
mp-wp_genesis 356 if ( !$cur ) {
mp-wp_genesis 357 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->usermeta ( user_id, meta_key, meta_value )
mp-wp_genesis 358 VALUES
mp-wp_genesis 359 ( %d, %s, %s )", $user_id, $meta_key, $meta_value) );
mp-wp_genesis 360 } else if ( $cur->meta_value != $meta_value ) {
mp-wp_genesis 361 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->usermeta SET meta_value = %s WHERE user_id = %d AND meta_key = %s", $meta_value, $user_id, $meta_key) );
mp-wp_genesis 362 } else {
mp-wp_genesis 363 return false;
mp-wp_genesis 364 }
mp-wp_genesis 365
mp-wp_genesis 366 wp_cache_delete($user_id, 'users');
mp-wp_genesis 367
mp-wp_genesis 368 return true;
mp-wp_genesis 369 }
mp-wp_genesis 370
mp-wp_genesis 371 //
mp-wp_genesis 372 // Private helper functions
mp-wp_genesis 373 //
mp-wp_genesis 374
mp-wp_genesis 375 /**
mp-wp_genesis 376 * Setup global user vars.
mp-wp_genesis 377 *
mp-wp_genesis 378 * Used by set_current_user() for back compat. Might be deprecated in the
mp-wp_genesis 379 * future.
mp-wp_genesis 380 *
mp-wp_genesis 381 * @since 2.0.4
mp-wp_genesis 382 * @global string $userdata User description.
mp-wp_genesis 383 * @global string $user_login The user username for logging in
mp-wp_genesis 384 * @global int $user_level The level of the user
mp-wp_genesis 385 * @global int $user_ID The ID of the user
mp-wp_genesis 386 * @global string $user_email The email address of the user
mp-wp_genesis 387 * @global string $user_url The url in the user's profile
mp-wp_genesis 388 * @global string $user_pass_md5 MD5 of the user's password
mp-wp_genesis 389 * @global string $user_identity The display name of the user
mp-wp_genesis 390 *
mp-wp_genesis 391 * @param int $user_id Optional. User ID to setup global data.
mp-wp_genesis 392 */
mp-wp_genesis 393 function setup_userdata($user_id = '') {
mp-wp_genesis 394 global $user_login, $userdata, $user_level, $user_ID, $user_email, $user_url, $user_pass_md5, $user_identity;
mp-wp_genesis 395
mp-wp_genesis 396 if ( '' == $user_id )
mp-wp_genesis 397 $user = wp_get_current_user();
mp-wp_genesis 398 else
mp-wp_genesis 399 $user = new WP_User($user_id);
mp-wp_genesis 400
mp-wp_genesis 401 if ( 0 == $user->ID )
mp-wp_genesis 402 return;
mp-wp_genesis 403
mp-wp_genesis 404 $userdata = $user->data;
mp-wp_genesis 405 $user_login = $user->user_login;
mp-wp_genesis 406 $user_level = (int) isset($user->user_level) ? $user->user_level : 0;
mp-wp_genesis 407 $user_ID = (int) $user->ID;
mp-wp_genesis 408 $user_email = $user->user_email;
mp-wp_genesis 409 $user_url = $user->user_url;
mp-wp_genesis 410 $user_pass_md5 = md5($user->user_pass);
mp-wp_genesis 411 $user_identity = $user->display_name;
mp-wp_genesis 412 }
mp-wp_genesis 413
mp-wp_genesis 414 /**
mp-wp_genesis 415 * Create dropdown HTML content of users.
mp-wp_genesis 416 *
mp-wp_genesis 417 * The content can either be displayed, which it is by default or retrieved by
mp-wp_genesis 418 * setting the 'echo' argument. The 'include' and 'exclude' arguments do not
mp-wp_genesis 419 * need to be used; all users will be displayed in that case. Only one can be
mp-wp_genesis 420 * used, either 'include' or 'exclude', but not both.
mp-wp_genesis 421 *
mp-wp_genesis 422 * The available arguments are as follows:
mp-wp_genesis 423 * <ol>
mp-wp_genesis 424 * <li>show_option_all - Text to show all and whether HTML option exists.</li>
mp-wp_genesis 425 * <li>show_option_none - Text for show none and whether HTML option exists.
mp-wp_genesis 426 * </li>
mp-wp_genesis 427 * <li>orderby - SQL order by clause for what order the users appear. Default is
mp-wp_genesis 428 * 'display_name'.</li>
mp-wp_genesis 429 * <li>order - Default is 'ASC'. Can also be 'DESC'.</li>
mp-wp_genesis 430 * <li>include - User IDs to include.</li>
mp-wp_genesis 431 * <li>exclude - User IDs to exclude.</li>
mp-wp_genesis 432 * <li>multi - Default is 'false'. Whether to skip the ID attribute on the 'select' element.</li>
mp-wp_genesis 433 * <li>show - Default is 'display_name'. User table column to display. If the selected item is empty then the user_login will be displayed in parentesis</li>
mp-wp_genesis 434 * <li>echo - Default is '1'. Whether to display or retrieve content.</li>
mp-wp_genesis 435 * <li>selected - Which User ID is selected.</li>
mp-wp_genesis 436 * <li>name - Default is 'user'. Name attribute of select element.</li>
mp-wp_genesis 437 * <li>class - Class attribute of select element.</li>
mp-wp_genesis 438 * </ol>
mp-wp_genesis 439 *
mp-wp_genesis 440 * @since 2.3.0
mp-wp_genesis 441 * @uses $wpdb WordPress database object for queries
mp-wp_genesis 442 *
mp-wp_genesis 443 * @param string|array $args Optional. Override defaults.
mp-wp_genesis 444 * @return string|null Null on display. String of HTML content on retrieve.
mp-wp_genesis 445 */
mp-wp_genesis 446 function wp_dropdown_users( $args = '' ) {
mp-wp_genesis 447 global $wpdb;
mp-wp_genesis 448 $defaults = array(
mp-wp_genesis 449 'show_option_all' => '', 'show_option_none' => '',
mp-wp_genesis 450 'orderby' => 'display_name', 'order' => 'ASC',
mp-wp_genesis 451 'include' => '', 'exclude' => '', 'multi' => 0,
mp-wp_genesis 452 'show' => 'display_name', 'echo' => 1,
mp-wp_genesis 453 'selected' => 0, 'name' => 'user', 'class' => ''
mp-wp_genesis 454 );
mp-wp_genesis 455
mp-wp_genesis 456 $defaults['selected'] = is_author() ? get_query_var( 'author' ) : 0;
mp-wp_genesis 457
mp-wp_genesis 458 $r = wp_parse_args( $args, $defaults );
mp-wp_genesis 459 extract( $r, EXTR_SKIP );
mp-wp_genesis 460
mp-wp_genesis 461 $query = "SELECT * FROM $wpdb->users";
mp-wp_genesis 462
mp-wp_genesis 463 $query_where = array();
mp-wp_genesis 464
mp-wp_genesis 465 if ( is_array($include) )
mp-wp_genesis 466 $include = join(',', $include);
mp-wp_genesis 467 $include = preg_replace('/[^0-9,]/', '', $include); // (int)
mp-wp_genesis 468 if ( $include )
mp-wp_genesis 469 $query_where[] = "ID IN ($include)";
mp-wp_genesis 470
mp-wp_genesis 471 if ( is_array($exclude) )
mp-wp_genesis 472 $exclude = join(',', $exclude);
mp-wp_genesis 473 $exclude = preg_replace('/[^0-9,]/', '', $exclude); // (int)
mp-wp_genesis 474 if ( $exclude )
mp-wp_genesis 475 $query_where[] = "ID NOT IN ($exclude)";
mp-wp_genesis 476
mp-wp_genesis 477 if ( $query_where )
mp-wp_genesis 478 $query .= " WHERE " . join(' AND', $query_where);
mp-wp_genesis 479
mp-wp_genesis 480 $query .= " ORDER BY $orderby $order";
mp-wp_genesis 481
mp-wp_genesis 482 $users = $wpdb->get_results( $query );
mp-wp_genesis 483
mp-wp_genesis 484 $output = '';
mp-wp_genesis 485 if ( !empty($users) ) {
mp-wp_genesis 486 $id = $multi ? "" : "id='$name'";
mp-wp_genesis 487
mp-wp_genesis 488 $output = "<select name='$name' $id class='$class'>\n";
mp-wp_genesis 489
mp-wp_genesis 490 if ( $show_option_all )
mp-wp_genesis 491 $output .= "\t<option value='0'>$show_option_all</option>\n";
mp-wp_genesis 492
mp-wp_genesis 493 if ( $show_option_none )
mp-wp_genesis 494 $output .= "\t<option value='-1'>$show_option_none</option>\n";
mp-wp_genesis 495
mp-wp_genesis 496 foreach ( (array) $users as $user ) {
mp-wp_genesis 497 $user->ID = (int) $user->ID;
mp-wp_genesis 498 $_selected = $user->ID == $selected ? " selected='selected'" : '';
mp-wp_genesis 499 $display = !empty($user->$show) ? $user->$show : '('. $user->user_login . ')';
mp-wp_genesis 500 $output .= "\t<option value='$user->ID'$_selected>" . wp_specialchars($display) . "</option>\n";
mp-wp_genesis 501 }
mp-wp_genesis 502
mp-wp_genesis 503 $output .= "</select>";
mp-wp_genesis 504 }
mp-wp_genesis 505
mp-wp_genesis 506 $output = apply_filters('wp_dropdown_users', $output);
mp-wp_genesis 507
mp-wp_genesis 508 if ( $echo )
mp-wp_genesis 509 echo $output;
mp-wp_genesis 510
mp-wp_genesis 511 return $output;
mp-wp_genesis 512 }
mp-wp_genesis 513
mp-wp_genesis 514 /**
mp-wp_genesis 515 * Add user meta data as properties to given user object.
mp-wp_genesis 516 *
mp-wp_genesis 517 * The finished user data is cached, but the cache is not used to fill in the
mp-wp_genesis 518 * user data for the given object. Once the function has been used, the cache
mp-wp_genesis 519 * should be used to retrieve user data. The purpose seems then to be to ensure
mp-wp_genesis 520 * that the data in the object is always fresh.
mp-wp_genesis 521 *
mp-wp_genesis 522 * @access private
mp-wp_genesis 523 * @since 2.5.0
mp-wp_genesis 524 * @uses $wpdb WordPress database object for queries
mp-wp_genesis 525 *
mp-wp_genesis 526 * @param object $user The user data object.
mp-wp_genesis 527 */
mp-wp_genesis 528 function _fill_user( &$user ) {
mp-wp_genesis 529 global $wpdb;
mp-wp_genesis 530
mp-wp_genesis 531 $show = $wpdb->hide_errors();
mp-wp_genesis 532 $metavalues = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = %d", $user->ID));
mp-wp_genesis 533 $wpdb->show_errors($show);
mp-wp_genesis 534
mp-wp_genesis 535 if ( $metavalues ) {
mp-wp_genesis 536 foreach ( (array) $metavalues as $meta ) {
mp-wp_genesis 537 $value = maybe_unserialize($meta->meta_value);
mp-wp_genesis 538 $user->{$meta->meta_key} = $value;
mp-wp_genesis 539 }
mp-wp_genesis 540 }
mp-wp_genesis 541
mp-wp_genesis 542 $level = $wpdb->prefix . 'user_level';
mp-wp_genesis 543 if ( isset( $user->{$level} ) )
mp-wp_genesis 544 $user->user_level = $user->{$level};
mp-wp_genesis 545
mp-wp_genesis 546 // For backwards compat.
mp-wp_genesis 547 if ( isset($user->first_name) )
mp-wp_genesis 548 $user->user_firstname = $user->first_name;
mp-wp_genesis 549 if ( isset($user->last_name) )
mp-wp_genesis 550 $user->user_lastname = $user->last_name;
mp-wp_genesis 551 if ( isset($user->description) )
mp-wp_genesis 552 $user->user_description = $user->description;
mp-wp_genesis 553
mp-wp_genesis 554 wp_cache_add($user->ID, $user, 'users');
mp-wp_genesis 555 wp_cache_add($user->user_login, $user->ID, 'userlogins');
mp-wp_genesis 556 wp_cache_add($user->user_email, $user->ID, 'useremail');
mp-wp_genesis 557 }
mp-wp_genesis 558
mp-wp_genesis 559 ?>