raw
mp-wp_genesis           1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * WordPress user administration API.
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Administration
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 /**
mp-wp_genesis 10 * Creates a new user from the "Users" form using $_POST information.
mp-wp_genesis 11 *
mp-wp_genesis 12 * {@internal Missing Long Description}}
mp-wp_genesis 13 *
mp-wp_genesis 14 * @since unknown
mp-wp_genesis 15 *
mp-wp_genesis 16 * @param int $user_id Optional. User ID.
mp-wp_genesis 17 * @return null|WP_Error|int Null when adding user, WP_Error or User ID integer when no parameters.
mp-wp_genesis 18 */
mp-wp_genesis 19 function add_user() {
mp-wp_genesis 20 if ( func_num_args() ) { // The hackiest hack that ever did hack
mp-wp_genesis 21 global $current_user, $wp_roles;
mp-wp_genesis 22 $user_id = (int) func_get_arg( 0 );
mp-wp_genesis 23
mp-wp_genesis 24 if ( isset( $_POST['role'] ) ) {
mp-wp_genesis 25 if( $user_id != $current_user->id || $wp_roles->role_objects[$_POST['role']]->has_cap( 'edit_users' ) ) {
mp-wp_genesis 26 $user = new WP_User( $user_id );
mp-wp_genesis 27 $user->set_role( $_POST['role'] );
mp-wp_genesis 28 }
mp-wp_genesis 29 }
mp-wp_genesis 30 } else {
mp-wp_genesis 31 add_action( 'user_register', 'add_user' ); // See above
mp-wp_genesis 32 return edit_user();
mp-wp_genesis 33 }
mp-wp_genesis 34 }
mp-wp_genesis 35
mp-wp_genesis 36 /**
mp-wp_genesis 37 * {@internal Missing Short Description}}
mp-wp_genesis 38 *
mp-wp_genesis 39 * {@internal Missing Long Description}}
mp-wp_genesis 40 *
mp-wp_genesis 41 * @since unknown
mp-wp_genesis 42 *
mp-wp_genesis 43 * @param int $user_id Optional. User ID.
mp-wp_genesis 44 * @return unknown
mp-wp_genesis 45 */
mp-wp_genesis 46 function edit_user( $user_id = 0 ) {
mp-wp_genesis 47 global $current_user, $wp_roles, $wpdb;
mp-wp_genesis 48 if ( $user_id != 0 ) {
mp-wp_genesis 49 $update = true;
mp-wp_genesis 50 $user->ID = (int) $user_id;
mp-wp_genesis 51 $userdata = get_userdata( $user_id );
mp-wp_genesis 52 $user->user_login = $wpdb->escape( $userdata->user_login );
mp-wp_genesis 53 } else {
mp-wp_genesis 54 $update = false;
mp-wp_genesis 55 $user = '';
mp-wp_genesis 56 }
mp-wp_genesis 57
mp-wp_genesis 58 if ( isset( $_POST['user_login'] ))
mp-wp_genesis 59 $user->user_login = wp_specialchars( trim( $_POST['user_login'] ));
mp-wp_genesis 60
mp-wp_genesis 61 $pass1 = $pass2 = '';
mp-wp_genesis 62 if ( isset( $_POST['pass1'] ))
mp-wp_genesis 63 $pass1 = $_POST['pass1'];
mp-wp_genesis 64 if ( isset( $_POST['pass2'] ))
mp-wp_genesis 65 $pass2 = $_POST['pass2'];
mp-wp_genesis 66
mp-wp_genesis 67 if ( isset( $_POST['role'] ) && current_user_can( 'edit_users' ) ) {
mp-wp_genesis 68 if( $user_id != $current_user->id || $wp_roles->role_objects[$_POST['role']]->has_cap( 'edit_users' ))
mp-wp_genesis 69 $user->role = $_POST['role'];
mp-wp_genesis 70 }
mp-wp_genesis 71
mp-wp_genesis 72 if ( isset( $_POST['email'] ))
mp-wp_genesis 73 $user->user_email = wp_specialchars( trim( $_POST['email'] ));
mp-wp_genesis 74 if ( isset( $_POST['url'] ) ) {
mp-wp_genesis 75 $user->user_url = clean_url( trim( $_POST['url'] ));
mp-wp_genesis 76 $user->user_url = preg_match('/^(https?|ftps?|mailto|news|irc|gopher|nntp|feed|telnet):/is', $user->user_url) ? $user->user_url : 'http://'.$user->user_url;
mp-wp_genesis 77 }
mp-wp_genesis 78 if ( isset( $_POST['first_name'] ))
mp-wp_genesis 79 $user->first_name = wp_specialchars( trim( $_POST['first_name'] ));
mp-wp_genesis 80 if ( isset( $_POST['last_name'] ))
mp-wp_genesis 81 $user->last_name = wp_specialchars( trim( $_POST['last_name'] ));
mp-wp_genesis 82 if ( isset( $_POST['nickname'] ))
mp-wp_genesis 83 $user->nickname = wp_specialchars( trim( $_POST['nickname'] ));
mp-wp_genesis 84 if ( isset( $_POST['display_name'] ))
mp-wp_genesis 85 $user->display_name = wp_specialchars( trim( $_POST['display_name'] ));
mp-wp_genesis 86 if ( isset( $_POST['description'] ))
mp-wp_genesis 87 $user->description = trim( $_POST['description'] );
mp-wp_genesis 88 if ( isset( $_POST['jabber'] ))
mp-wp_genesis 89 $user->jabber = wp_specialchars( trim( $_POST['jabber'] ));
mp-wp_genesis 90 if ( isset( $_POST['aim'] ))
mp-wp_genesis 91 $user->aim = wp_specialchars( trim( $_POST['aim'] ));
mp-wp_genesis 92 if ( isset( $_POST['yim'] ))
mp-wp_genesis 93 $user->yim = wp_specialchars( trim( $_POST['yim'] ));
mp-wp_genesis 94
mp-wp_genesis 95 $user->comment_shortcuts = isset( $_POST['comment_shortcuts'] )? $_POST['comment_shortcuts'] : '';
mp-wp_genesis 96
mp-wp_genesis 97 $user->use_ssl = 0;
mp-wp_genesis 98 if ( !empty($_POST['use_ssl']) )
mp-wp_genesis 99 $user->use_ssl = 1;
mp-wp_genesis 100
mp-wp_genesis 101 if ( !$update )
mp-wp_genesis 102 $user->admin_color = 'fresh'; // Default to fresh for new users.
mp-wp_genesis 103 else if ( isset( $_POST['admin_color'] ) )
mp-wp_genesis 104 $user->admin_color = $_POST['admin_color'];
mp-wp_genesis 105 else
mp-wp_genesis 106 $user->admin_color = 'fresh';
mp-wp_genesis 107
mp-wp_genesis 108 $errors = new WP_Error();
mp-wp_genesis 109
mp-wp_genesis 110 /* checking that username has been typed */
mp-wp_genesis 111 if ( $user->user_login == '' )
mp-wp_genesis 112 $errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ));
mp-wp_genesis 113
mp-wp_genesis 114 /* checking the password has been typed twice */
mp-wp_genesis 115 do_action_ref_array( 'check_passwords', array ( $user->user_login, & $pass1, & $pass2 ));
mp-wp_genesis 116
mp-wp_genesis 117 if ( $update ) {
mp-wp_genesis 118 if ( empty($pass1) && !empty($pass2) )
mp-wp_genesis 119 $errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass1' ) );
mp-wp_genesis 120 elseif ( !empty($pass1) && empty($pass2) )
mp-wp_genesis 121 $errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass2' ) );
mp-wp_genesis 122 } else {
mp-wp_genesis 123 if ( empty($pass1) )
mp-wp_genesis 124 $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password.' ), array( 'form-field' => 'pass1' ) );
mp-wp_genesis 125 elseif ( empty($pass2) )
mp-wp_genesis 126 $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password twice.' ), array( 'form-field' => 'pass2' ) );
mp-wp_genesis 127 }
mp-wp_genesis 128
mp-wp_genesis 129 /* Check for "\" in password */
mp-wp_genesis 130 if( strpos( " ".$pass1, "\\" ) )
mp-wp_genesis 131 $errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) );
mp-wp_genesis 132
mp-wp_genesis 133 /* checking the password has been typed twice the same */
mp-wp_genesis 134 if ( $pass1 != $pass2 )
mp-wp_genesis 135 $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter the same password in the two password fields.' ), array( 'form-field' => 'pass1' ) );
mp-wp_genesis 136
mp-wp_genesis 137 if (!empty ( $pass1 ))
mp-wp_genesis 138 $user->user_pass = $pass1;
mp-wp_genesis 139
mp-wp_genesis 140 if ( !$update && !validate_username( $user->user_login ) )
mp-wp_genesis 141 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid. Please enter a valid username.' ));
mp-wp_genesis 142
mp-wp_genesis 143 if (!$update && username_exists( $user->user_login ))
mp-wp_genesis 144 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ));
mp-wp_genesis 145
mp-wp_genesis 146 /* checking e-mail address */
mp-wp_genesis 147 if ( empty ( $user->user_email ) ) {
mp-wp_genesis 148 $errors->add( 'user_email', __( '<strong>ERROR</strong>: Please enter an e-mail address.' ), array( 'form-field' => 'email' ) );
mp-wp_genesis 149 } else
mp-wp_genesis 150 if (!is_email( $user->user_email ) ) {
mp-wp_genesis 151 $errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address isn't correct." ), array( 'form-field' => 'email' ) );
mp-wp_genesis 152 }
mp-wp_genesis 153
mp-wp_genesis 154 if ( $errors->get_error_codes() )
mp-wp_genesis 155 return $errors;
mp-wp_genesis 156
mp-wp_genesis 157 if ( $update ) {
mp-wp_genesis 158 $user_id = wp_update_user( get_object_vars( $user ));
mp-wp_genesis 159 } else {
mp-wp_genesis 160 $user_id = wp_insert_user( get_object_vars( $user ));
mp-wp_genesis 161 wp_new_user_notification( $user_id );
mp-wp_genesis 162 }
mp-wp_genesis 163 return $user_id;
mp-wp_genesis 164 }
mp-wp_genesis 165
mp-wp_genesis 166 /**
mp-wp_genesis 167 * {@internal Missing Short Description}}
mp-wp_genesis 168 *
mp-wp_genesis 169 * {@internal Missing Long Description}}
mp-wp_genesis 170 *
mp-wp_genesis 171 * @since unknown
mp-wp_genesis 172 *
mp-wp_genesis 173 * @return array List of user IDs.
mp-wp_genesis 174 */
mp-wp_genesis 175 function get_author_user_ids() {
mp-wp_genesis 176 global $wpdb;
mp-wp_genesis 177 $level_key = $wpdb->prefix . 'user_level';
mp-wp_genesis 178 return $wpdb->get_col( $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value != '0'", $level_key) );
mp-wp_genesis 179 }
mp-wp_genesis 180
mp-wp_genesis 181 /**
mp-wp_genesis 182 * {@internal Missing Short Description}}
mp-wp_genesis 183 *
mp-wp_genesis 184 * {@internal Missing Long Description}}
mp-wp_genesis 185 *
mp-wp_genesis 186 * @since unknown
mp-wp_genesis 187 *
mp-wp_genesis 188 * @param int $user_id User ID.
mp-wp_genesis 189 * @return array|bool List of editable authors. False if no editable users.
mp-wp_genesis 190 */
mp-wp_genesis 191 function get_editable_authors( $user_id ) {
mp-wp_genesis 192 global $wpdb;
mp-wp_genesis 193
mp-wp_genesis 194 $editable = get_editable_user_ids( $user_id );
mp-wp_genesis 195
mp-wp_genesis 196 if( !$editable ) {
mp-wp_genesis 197 return false;
mp-wp_genesis 198 } else {
mp-wp_genesis 199 $editable = join(',', $editable);
mp-wp_genesis 200 $authors = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($editable) ORDER BY display_name" );
mp-wp_genesis 201 }
mp-wp_genesis 202
mp-wp_genesis 203 return apply_filters('get_editable_authors', $authors);
mp-wp_genesis 204 }
mp-wp_genesis 205
mp-wp_genesis 206 /**
mp-wp_genesis 207 * {@internal Missing Short Description}}
mp-wp_genesis 208 *
mp-wp_genesis 209 * {@internal Missing Long Description}}
mp-wp_genesis 210 *
mp-wp_genesis 211 * @since unknown
mp-wp_genesis 212 *
mp-wp_genesis 213 * @param int $user_id User ID.
mp-wp_genesis 214 * @param bool $exclude_zeros Optional, default is true. Whether to exclude zeros.
mp-wp_genesis 215 * @return unknown
mp-wp_genesis 216 */
mp-wp_genesis 217 function get_editable_user_ids( $user_id, $exclude_zeros = true, $post_type = 'post' ) {
mp-wp_genesis 218 global $wpdb;
mp-wp_genesis 219
mp-wp_genesis 220 $user = new WP_User( $user_id );
mp-wp_genesis 221
mp-wp_genesis 222 if ( ! $user->has_cap("edit_others_{$post_type}s") ) {
mp-wp_genesis 223 if ( $user->has_cap("edit_{$post_type}s") || $exclude_zeros == false )
mp-wp_genesis 224 return array($user->id);
mp-wp_genesis 225 else
mp-wp_genesis 226 return false;
mp-wp_genesis 227 }
mp-wp_genesis 228
mp-wp_genesis 229 $level_key = $wpdb->prefix . 'user_level';
mp-wp_genesis 230
mp-wp_genesis 231 $query = $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s", $level_key);
mp-wp_genesis 232 if ( $exclude_zeros )
mp-wp_genesis 233 $query .= " AND meta_value != '0'";
mp-wp_genesis 234
mp-wp_genesis 235 return $wpdb->get_col( $query );
mp-wp_genesis 236 }
mp-wp_genesis 237
mp-wp_genesis 238 /**
mp-wp_genesis 239 * {@internal Missing Short Description}}
mp-wp_genesis 240 *
mp-wp_genesis 241 * {@internal Missing Long Description}}
mp-wp_genesis 242 *
mp-wp_genesis 243 * @since unknown
mp-wp_genesis 244 *
mp-wp_genesis 245 * @return unknown
mp-wp_genesis 246 */
mp-wp_genesis 247 function get_nonauthor_user_ids() {
mp-wp_genesis 248 global $wpdb;
mp-wp_genesis 249 $level_key = $wpdb->prefix . 'user_level';
mp-wp_genesis 250
mp-wp_genesis 251 return $wpdb->get_col( $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value = '0'", $level_key) );
mp-wp_genesis 252 }
mp-wp_genesis 253
mp-wp_genesis 254 /**
mp-wp_genesis 255 * Retrieve editable posts from other users.
mp-wp_genesis 256 *
mp-wp_genesis 257 * @since unknown
mp-wp_genesis 258 *
mp-wp_genesis 259 * @param int $user_id User ID to not retrieve posts from.
mp-wp_genesis 260 * @param string $type Optional, defaults to 'any'. Post type to retrieve, can be 'draft' or 'pending'.
mp-wp_genesis 261 * @return array List of posts from others.
mp-wp_genesis 262 */
mp-wp_genesis 263 function get_others_unpublished_posts($user_id, $type='any') {
mp-wp_genesis 264 global $wpdb;
mp-wp_genesis 265
mp-wp_genesis 266 $editable = get_editable_user_ids( $user_id );
mp-wp_genesis 267
mp-wp_genesis 268 if ( in_array($type, array('draft', 'pending')) )
mp-wp_genesis 269 $type_sql = " post_status = '$type' ";
mp-wp_genesis 270 else
mp-wp_genesis 271 $type_sql = " ( post_status = 'draft' OR post_status = 'pending' ) ";
mp-wp_genesis 272
mp-wp_genesis 273 $dir = ( 'pending' == $type ) ? 'ASC' : 'DESC';
mp-wp_genesis 274
mp-wp_genesis 275 if( !$editable ) {
mp-wp_genesis 276 $other_unpubs = '';
mp-wp_genesis 277 } else {
mp-wp_genesis 278 $editable = join(',', $editable);
mp-wp_genesis 279 $other_unpubs = $wpdb->get_results( $wpdb->prepare("SELECT ID, post_title, post_author FROM $wpdb->posts WHERE post_type = 'post' AND $type_sql AND post_author IN ($editable) AND post_author != %d ORDER BY post_modified $dir", $user_id) );
mp-wp_genesis 280 }
mp-wp_genesis 281
mp-wp_genesis 282 return apply_filters('get_others_drafts', $other_unpubs);
mp-wp_genesis 283 }
mp-wp_genesis 284
mp-wp_genesis 285 /**
mp-wp_genesis 286 * Retrieve drafts from other users.
mp-wp_genesis 287 *
mp-wp_genesis 288 * @since unknown
mp-wp_genesis 289 *
mp-wp_genesis 290 * @param int $user_id User ID.
mp-wp_genesis 291 * @return array List of drafts from other users.
mp-wp_genesis 292 */
mp-wp_genesis 293 function get_others_drafts($user_id) {
mp-wp_genesis 294 return get_others_unpublished_posts($user_id, 'draft');
mp-wp_genesis 295 }
mp-wp_genesis 296
mp-wp_genesis 297 /**
mp-wp_genesis 298 * Retrieve pending review posts from other users.
mp-wp_genesis 299 *
mp-wp_genesis 300 * @since unknown
mp-wp_genesis 301 *
mp-wp_genesis 302 * @param int $user_id User ID.
mp-wp_genesis 303 * @return array List of posts with pending review post type from other users.
mp-wp_genesis 304 */
mp-wp_genesis 305 function get_others_pending($user_id) {
mp-wp_genesis 306 return get_others_unpublished_posts($user_id, 'pending');
mp-wp_genesis 307 }
mp-wp_genesis 308
mp-wp_genesis 309 /**
mp-wp_genesis 310 * Retrieve user data and filter it.
mp-wp_genesis 311 *
mp-wp_genesis 312 * @since unknown
mp-wp_genesis 313 *
mp-wp_genesis 314 * @param int $user_id User ID.
mp-wp_genesis 315 * @return object WP_User object with user data.
mp-wp_genesis 316 */
mp-wp_genesis 317 function get_user_to_edit( $user_id ) {
mp-wp_genesis 318 $user = new WP_User( $user_id );
mp-wp_genesis 319 $user->user_login = attribute_escape($user->user_login);
mp-wp_genesis 320 $user->user_email = attribute_escape($user->user_email);
mp-wp_genesis 321 $user->user_url = clean_url($user->user_url);
mp-wp_genesis 322 $user->first_name = attribute_escape($user->first_name);
mp-wp_genesis 323 $user->last_name = attribute_escape($user->last_name);
mp-wp_genesis 324 $user->display_name = attribute_escape($user->display_name);
mp-wp_genesis 325 $user->nickname = attribute_escape($user->nickname);
mp-wp_genesis 326 $user->aim = isset( $user->aim ) && !empty( $user->aim ) ? attribute_escape($user->aim) : '';
mp-wp_genesis 327 $user->yim = isset( $user->yim ) && !empty( $user->yim ) ? attribute_escape($user->yim) : '';
mp-wp_genesis 328 $user->jabber = isset( $user->jabber ) && !empty( $user->jabber ) ? attribute_escape($user->jabber) : '';
mp-wp_genesis 329 $user->description = isset( $user->description ) && !empty( $user->description ) ? wp_specialchars($user->description) : '';
mp-wp_genesis 330
mp-wp_genesis 331 return $user;
mp-wp_genesis 332 }
mp-wp_genesis 333
mp-wp_genesis 334 /**
mp-wp_genesis 335 * Retrieve the user's drafts.
mp-wp_genesis 336 *
mp-wp_genesis 337 * @since unknown
mp-wp_genesis 338 *
mp-wp_genesis 339 * @param int $user_id User ID.
mp-wp_genesis 340 * @return array
mp-wp_genesis 341 */
mp-wp_genesis 342 function get_users_drafts( $user_id ) {
mp-wp_genesis 343 global $wpdb;
mp-wp_genesis 344 $query = $wpdb->prepare("SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft' AND post_author = %d ORDER BY post_modified DESC", $user_id);
mp-wp_genesis 345 $query = apply_filters('get_users_drafts', $query);
mp-wp_genesis 346 return $wpdb->get_results( $query );
mp-wp_genesis 347 }
mp-wp_genesis 348
mp-wp_genesis 349 /**
mp-wp_genesis 350 * Remove user and optionally reassign posts and links to another user.
mp-wp_genesis 351 *
mp-wp_genesis 352 * If the $reassign parameter is not assigned to an User ID, then all posts will
mp-wp_genesis 353 * be deleted of that user. The action 'delete_user' that is passed the User ID
mp-wp_genesis 354 * being deleted will be run after the posts are either reassigned or deleted.
mp-wp_genesis 355 * The user meta will also be deleted that are for that User ID.
mp-wp_genesis 356 *
mp-wp_genesis 357 * @since unknown
mp-wp_genesis 358 *
mp-wp_genesis 359 * @param int $id User ID.
mp-wp_genesis 360 * @param int $reassign Optional. Reassign posts and links to new User ID.
mp-wp_genesis 361 * @return bool True when finished.
mp-wp_genesis 362 */
mp-wp_genesis 363 function wp_delete_user($id, $reassign = 'novalue') {
mp-wp_genesis 364 global $wpdb;
mp-wp_genesis 365
mp-wp_genesis 366 $id = (int) $id;
mp-wp_genesis 367
mp-wp_genesis 368 if ($reassign == 'novalue') {
mp-wp_genesis 369 $post_ids = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id) );
mp-wp_genesis 370
mp-wp_genesis 371 if ($post_ids) {
mp-wp_genesis 372 foreach ($post_ids as $post_id)
mp-wp_genesis 373 wp_delete_post($post_id);
mp-wp_genesis 374 }
mp-wp_genesis 375
mp-wp_genesis 376 // Clean links
mp-wp_genesis 377 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->links WHERE link_owner = %d", $id) );
mp-wp_genesis 378 } else {
mp-wp_genesis 379 $reassign = (int) $reassign;
mp-wp_genesis 380 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $id) );
mp-wp_genesis 381 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $id) );
mp-wp_genesis 382 }
mp-wp_genesis 383
mp-wp_genesis 384 // FINALLY, delete user
mp-wp_genesis 385 do_action('delete_user', $id);
mp-wp_genesis 386
mp-wp_genesis 387 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->users WHERE ID = %d", $id) );
mp-wp_genesis 388 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d", $id) );
mp-wp_genesis 389
mp-wp_genesis 390 wp_cache_delete($id, 'users');
mp-wp_genesis 391 wp_cache_delete($user->user_login, 'userlogins');
mp-wp_genesis 392 wp_cache_delete($user->user_email, 'useremail');
mp-wp_genesis 393
mp-wp_genesis 394 return true;
mp-wp_genesis 395 }
mp-wp_genesis 396
mp-wp_genesis 397 /**
mp-wp_genesis 398 * Remove all capabilities from user.
mp-wp_genesis 399 *
mp-wp_genesis 400 * @since unknown
mp-wp_genesis 401 *
mp-wp_genesis 402 * @param int $id User ID.
mp-wp_genesis 403 */
mp-wp_genesis 404 function wp_revoke_user($id) {
mp-wp_genesis 405 $id = (int) $id;
mp-wp_genesis 406
mp-wp_genesis 407 $user = new WP_User($id);
mp-wp_genesis 408 $user->remove_all_caps();
mp-wp_genesis 409 }
mp-wp_genesis 410
mp-wp_genesis 411 if ( !class_exists('WP_User_Search') ) :
mp-wp_genesis 412 /**
mp-wp_genesis 413 * WordPress User Search class.
mp-wp_genesis 414 *
mp-wp_genesis 415 * @since unknown
mp-wp_genesis 416 * @author Mark Jaquith
mp-wp_genesis 417 */
mp-wp_genesis 418 class WP_User_Search {
mp-wp_genesis 419
mp-wp_genesis 420 /**
mp-wp_genesis 421 * {@internal Missing Description}}
mp-wp_genesis 422 *
mp-wp_genesis 423 * @since unknown
mp-wp_genesis 424 * @access private
mp-wp_genesis 425 * @var unknown_type
mp-wp_genesis 426 */
mp-wp_genesis 427 var $results;
mp-wp_genesis 428
mp-wp_genesis 429 /**
mp-wp_genesis 430 * {@internal Missing Description}}
mp-wp_genesis 431 *
mp-wp_genesis 432 * @since unknown
mp-wp_genesis 433 * @access private
mp-wp_genesis 434 * @var unknown_type
mp-wp_genesis 435 */
mp-wp_genesis 436 var $search_term;
mp-wp_genesis 437
mp-wp_genesis 438 /**
mp-wp_genesis 439 * Page number.
mp-wp_genesis 440 *
mp-wp_genesis 441 * @since unknown
mp-wp_genesis 442 * @access private
mp-wp_genesis 443 * @var int
mp-wp_genesis 444 */
mp-wp_genesis 445 var $page;
mp-wp_genesis 446
mp-wp_genesis 447 /**
mp-wp_genesis 448 * Role name that users have.
mp-wp_genesis 449 *
mp-wp_genesis 450 * @since unknown
mp-wp_genesis 451 * @access private
mp-wp_genesis 452 * @var string
mp-wp_genesis 453 */
mp-wp_genesis 454 var $role;
mp-wp_genesis 455
mp-wp_genesis 456 /**
mp-wp_genesis 457 * Raw page number.
mp-wp_genesis 458 *
mp-wp_genesis 459 * @since unknown
mp-wp_genesis 460 * @access private
mp-wp_genesis 461 * @var int|bool
mp-wp_genesis 462 */
mp-wp_genesis 463 var $raw_page;
mp-wp_genesis 464
mp-wp_genesis 465 /**
mp-wp_genesis 466 * Amount of users to display per page.
mp-wp_genesis 467 *
mp-wp_genesis 468 * @since unknown
mp-wp_genesis 469 * @access public
mp-wp_genesis 470 * @var int
mp-wp_genesis 471 */
mp-wp_genesis 472 var $users_per_page = 50;
mp-wp_genesis 473
mp-wp_genesis 474 /**
mp-wp_genesis 475 * {@internal Missing Description}}
mp-wp_genesis 476 *
mp-wp_genesis 477 * @since unknown
mp-wp_genesis 478 * @access private
mp-wp_genesis 479 * @var unknown_type
mp-wp_genesis 480 */
mp-wp_genesis 481 var $first_user;
mp-wp_genesis 482
mp-wp_genesis 483 /**
mp-wp_genesis 484 * {@internal Missing Description}}
mp-wp_genesis 485 *
mp-wp_genesis 486 * @since unknown
mp-wp_genesis 487 * @access private
mp-wp_genesis 488 * @var int
mp-wp_genesis 489 */
mp-wp_genesis 490 var $last_user;
mp-wp_genesis 491
mp-wp_genesis 492 /**
mp-wp_genesis 493 * {@internal Missing Description}}
mp-wp_genesis 494 *
mp-wp_genesis 495 * @since unknown
mp-wp_genesis 496 * @access private
mp-wp_genesis 497 * @var unknown_type
mp-wp_genesis 498 */
mp-wp_genesis 499 var $query_limit;
mp-wp_genesis 500
mp-wp_genesis 501 /**
mp-wp_genesis 502 * {@internal Missing Description}}
mp-wp_genesis 503 *
mp-wp_genesis 504 * @since unknown
mp-wp_genesis 505 * @access private
mp-wp_genesis 506 * @var unknown_type
mp-wp_genesis 507 */
mp-wp_genesis 508 var $query_sort;
mp-wp_genesis 509
mp-wp_genesis 510 /**
mp-wp_genesis 511 * {@internal Missing Description}}
mp-wp_genesis 512 *
mp-wp_genesis 513 * @since unknown
mp-wp_genesis 514 * @access private
mp-wp_genesis 515 * @var unknown_type
mp-wp_genesis 516 */
mp-wp_genesis 517 var $query_from_where;
mp-wp_genesis 518
mp-wp_genesis 519 /**
mp-wp_genesis 520 * {@internal Missing Description}}
mp-wp_genesis 521 *
mp-wp_genesis 522 * @since unknown
mp-wp_genesis 523 * @access private
mp-wp_genesis 524 * @var int
mp-wp_genesis 525 */
mp-wp_genesis 526 var $total_users_for_query = 0;
mp-wp_genesis 527
mp-wp_genesis 528 /**
mp-wp_genesis 529 * {@internal Missing Description}}
mp-wp_genesis 530 *
mp-wp_genesis 531 * @since unknown
mp-wp_genesis 532 * @access private
mp-wp_genesis 533 * @var bool
mp-wp_genesis 534 */
mp-wp_genesis 535 var $too_many_total_users = false;
mp-wp_genesis 536
mp-wp_genesis 537 /**
mp-wp_genesis 538 * {@internal Missing Description}}
mp-wp_genesis 539 *
mp-wp_genesis 540 * @since unknown
mp-wp_genesis 541 * @access private
mp-wp_genesis 542 * @var unknown_type
mp-wp_genesis 543 */
mp-wp_genesis 544 var $search_errors;
mp-wp_genesis 545
mp-wp_genesis 546 /**
mp-wp_genesis 547 * {@internal Missing Description}}
mp-wp_genesis 548 *
mp-wp_genesis 549 * @since unknown
mp-wp_genesis 550 * @access private
mp-wp_genesis 551 * @var unknown_type
mp-wp_genesis 552 */
mp-wp_genesis 553 var $paging_text;
mp-wp_genesis 554
mp-wp_genesis 555 /**
mp-wp_genesis 556 * PHP4 Constructor - Sets up the object properties.
mp-wp_genesis 557 *
mp-wp_genesis 558 * @since unknown
mp-wp_genesis 559 *
mp-wp_genesis 560 * @param string $search_term Search terms string.
mp-wp_genesis 561 * @param int $page Optional. Page ID.
mp-wp_genesis 562 * @param string $role Role name.
mp-wp_genesis 563 * @return WP_User_Search
mp-wp_genesis 564 */
mp-wp_genesis 565 function WP_User_Search ($search_term = '', $page = '', $role = '') {
mp-wp_genesis 566 $this->search_term = $search_term;
mp-wp_genesis 567 $this->raw_page = ( '' == $page ) ? false : (int) $page;
mp-wp_genesis 568 $this->page = (int) ( '' == $page ) ? 1 : $page;
mp-wp_genesis 569 $this->role = $role;
mp-wp_genesis 570
mp-wp_genesis 571 $this->prepare_query();
mp-wp_genesis 572 $this->query();
mp-wp_genesis 573 $this->prepare_vars_for_template_usage();
mp-wp_genesis 574 $this->do_paging();
mp-wp_genesis 575 }
mp-wp_genesis 576
mp-wp_genesis 577 /**
mp-wp_genesis 578 * {@internal Missing Short Description}}
mp-wp_genesis 579 *
mp-wp_genesis 580 * {@internal Missing Long Description}}
mp-wp_genesis 581 *
mp-wp_genesis 582 * @since unknown
mp-wp_genesis 583 * @access public
mp-wp_genesis 584 */
mp-wp_genesis 585 function prepare_query() {
mp-wp_genesis 586 global $wpdb;
mp-wp_genesis 587 $this->first_user = ($this->page - 1) * $this->users_per_page;
mp-wp_genesis 588 $this->query_limit = $wpdb->prepare(" LIMIT %d, %d", $this->first_user, $this->users_per_page);
mp-wp_genesis 589 $this->query_sort = ' ORDER BY user_login';
mp-wp_genesis 590 $search_sql = '';
mp-wp_genesis 591 if ( $this->search_term ) {
mp-wp_genesis 592 $searches = array();
mp-wp_genesis 593 $search_sql = 'AND (';
mp-wp_genesis 594 foreach ( array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') as $col )
mp-wp_genesis 595 $searches[] = $col . " LIKE '%$this->search_term%'";
mp-wp_genesis 596 $search_sql .= implode(' OR ', $searches);
mp-wp_genesis 597 $search_sql .= ')';
mp-wp_genesis 598 }
mp-wp_genesis 599
mp-wp_genesis 600 $this->query_from_where = "FROM $wpdb->users";
mp-wp_genesis 601 if ( $this->role )
mp-wp_genesis 602 $this->query_from_where .= $wpdb->prepare(" INNER JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id WHERE $wpdb->usermeta.meta_key = '{$wpdb->prefix}capabilities' AND $wpdb->usermeta.meta_value LIKE %s", '%' . $this->role . '%');
mp-wp_genesis 603 else
mp-wp_genesis 604 $this->query_from_where .= " WHERE 1=1";
mp-wp_genesis 605 $this->query_from_where .= " $search_sql";
mp-wp_genesis 606
mp-wp_genesis 607 }
mp-wp_genesis 608
mp-wp_genesis 609 /**
mp-wp_genesis 610 * {@internal Missing Short Description}}
mp-wp_genesis 611 *
mp-wp_genesis 612 * {@internal Missing Long Description}}
mp-wp_genesis 613 *
mp-wp_genesis 614 * @since unknown
mp-wp_genesis 615 * @access public
mp-wp_genesis 616 */
mp-wp_genesis 617 function query() {
mp-wp_genesis 618 global $wpdb;
mp-wp_genesis 619 $this->results = $wpdb->get_col('SELECT ID ' . $this->query_from_where . $this->query_sort . $this->query_limit);
mp-wp_genesis 620
mp-wp_genesis 621 if ( $this->results )
mp-wp_genesis 622 $this->total_users_for_query = $wpdb->get_var('SELECT COUNT(ID) ' . $this->query_from_where); // no limit
mp-wp_genesis 623 else
mp-wp_genesis 624 $this->search_errors = new WP_Error('no_matching_users_found', __('No matching users were found!'));
mp-wp_genesis 625 }
mp-wp_genesis 626
mp-wp_genesis 627 /**
mp-wp_genesis 628 * {@internal Missing Short Description}}
mp-wp_genesis 629 *
mp-wp_genesis 630 * {@internal Missing Long Description}}
mp-wp_genesis 631 *
mp-wp_genesis 632 * @since unknown
mp-wp_genesis 633 * @access public
mp-wp_genesis 634 */
mp-wp_genesis 635 function prepare_vars_for_template_usage() {
mp-wp_genesis 636 $this->search_term = stripslashes($this->search_term); // done with DB, from now on we want slashes gone
mp-wp_genesis 637 }
mp-wp_genesis 638
mp-wp_genesis 639 /**
mp-wp_genesis 640 * {@internal Missing Short Description}}
mp-wp_genesis 641 *
mp-wp_genesis 642 * {@internal Missing Long Description}}
mp-wp_genesis 643 *
mp-wp_genesis 644 * @since unknown
mp-wp_genesis 645 * @access public
mp-wp_genesis 646 */
mp-wp_genesis 647 function do_paging() {
mp-wp_genesis 648 if ( $this->total_users_for_query > $this->users_per_page ) { // have to page the results
mp-wp_genesis 649 $args = array();
mp-wp_genesis 650 if( ! empty($this->search_term) )
mp-wp_genesis 651 $args['usersearch'] = urlencode($this->search_term);
mp-wp_genesis 652 if( ! empty($this->role) )
mp-wp_genesis 653 $args['role'] = urlencode($this->role);
mp-wp_genesis 654
mp-wp_genesis 655 $this->paging_text = paginate_links( array(
mp-wp_genesis 656 'total' => ceil($this->total_users_for_query / $this->users_per_page),
mp-wp_genesis 657 'current' => $this->page,
mp-wp_genesis 658 'base' => 'users.php?%_%',
mp-wp_genesis 659 'format' => 'userspage=%#%',
mp-wp_genesis 660 'add_args' => $args
mp-wp_genesis 661 ) );
mp-wp_genesis 662 if ( $this->paging_text ) {
mp-wp_genesis 663 $this->paging_text = sprintf( '<span class="displaying-num">' . __( 'Displaying %s&#8211;%s of %s' ) . '</span>%s',
mp-wp_genesis 664 number_format_i18n( ( $this->page - 1 ) * $this->users_per_page + 1 ),
mp-wp_genesis 665 number_format_i18n( min( $this->page * $this->users_per_page, $this->total_users_for_query ) ),
mp-wp_genesis 666 number_format_i18n( $this->total_users_for_query ),
mp-wp_genesis 667 $this->paging_text
mp-wp_genesis 668 );
mp-wp_genesis 669 }
mp-wp_genesis 670 }
mp-wp_genesis 671 }
mp-wp_genesis 672
mp-wp_genesis 673 /**
mp-wp_genesis 674 * {@internal Missing Short Description}}
mp-wp_genesis 675 *
mp-wp_genesis 676 * {@internal Missing Long Description}}
mp-wp_genesis 677 *
mp-wp_genesis 678 * @since unknown
mp-wp_genesis 679 * @access public
mp-wp_genesis 680 *
mp-wp_genesis 681 * @return unknown
mp-wp_genesis 682 */
mp-wp_genesis 683 function get_results() {
mp-wp_genesis 684 return (array) $this->results;
mp-wp_genesis 685 }
mp-wp_genesis 686
mp-wp_genesis 687 /**
mp-wp_genesis 688 * Displaying paging text.
mp-wp_genesis 689 *
mp-wp_genesis 690 * @see do_paging() Builds paging text.
mp-wp_genesis 691 *
mp-wp_genesis 692 * @since unknown
mp-wp_genesis 693 * @access public
mp-wp_genesis 694 */
mp-wp_genesis 695 function page_links() {
mp-wp_genesis 696 echo $this->paging_text;
mp-wp_genesis 697 }
mp-wp_genesis 698
mp-wp_genesis 699 /**
mp-wp_genesis 700 * Whether paging is enabled.
mp-wp_genesis 701 *
mp-wp_genesis 702 * @see do_paging() Builds paging text.
mp-wp_genesis 703 *
mp-wp_genesis 704 * @since unknown
mp-wp_genesis 705 * @access public
mp-wp_genesis 706 *
mp-wp_genesis 707 * @return bool
mp-wp_genesis 708 */
mp-wp_genesis 709 function results_are_paged() {
mp-wp_genesis 710 if ( $this->paging_text )
mp-wp_genesis 711 return true;
mp-wp_genesis 712 return false;
mp-wp_genesis 713 }
mp-wp_genesis 714
mp-wp_genesis 715 /**
mp-wp_genesis 716 * Whether there are search terms.
mp-wp_genesis 717 *
mp-wp_genesis 718 * @since unknown
mp-wp_genesis 719 * @access public
mp-wp_genesis 720 *
mp-wp_genesis 721 * @return bool
mp-wp_genesis 722 */
mp-wp_genesis 723 function is_search() {
mp-wp_genesis 724 if ( $this->search_term )
mp-wp_genesis 725 return true;
mp-wp_genesis 726 return false;
mp-wp_genesis 727 }
mp-wp_genesis 728 }
mp-wp_genesis 729 endif;
mp-wp_genesis 730
mp-wp_genesis 731 ?>