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