3 * WordPress user administration API.
6 * @subpackage Administration
10 * Creates a new user from the "Users" form using $_POST information.
12 * {@internal Missing Long Description}}
16 * @param int $user_id Optional. User ID.
17 * @return null|WP_Error|int Null when adding user, WP_Error or User ID integer when no parameters.
20 if ( func_num_args() ) { // The hackiest hack that ever did hack
21 global $current_user, $wp_roles;
22 $user_id = (int) func_get_arg( 0 );
24 if ( isset( $_POST['role'] ) ) {
25 if( $user_id != $current_user->id || $wp_roles->role_objects[$_POST['role']]->has_cap( 'edit_users' ) ) {
26 $user = new WP_User( $user_id );
27 $user->set_role( $_POST['role'] );
31 add_action( 'user_register', 'add_user' ); // See above
37 * {@internal Missing Short Description}}
39 * {@internal Missing Long Description}}
43 * @param int $user_id Optional. User ID.
46 function edit_user( $user_id = 0 ) {
47 global $current_user, $wp_roles, $wpdb;
48 if ( $user_id != 0 ) {
50 $user->ID = (int) $user_id;
51 $userdata = get_userdata( $user_id );
52 $user->user_login = $wpdb->escape( $userdata->user_login );
58 if ( isset( $_POST['user_login'] ))
59 $user->user_login = wp_specialchars( trim( $_POST['user_login'] ));
62 if ( isset( $_POST['pass1'] ))
63 $pass1 = $_POST['pass1'];
64 if ( isset( $_POST['pass2'] ))
65 $pass2 = $_POST['pass2'];
67 if ( isset( $_POST['role'] ) && current_user_can( 'edit_users' ) ) {
68 if( $user_id != $current_user->id || $wp_roles->role_objects[$_POST['role']]->has_cap( 'edit_users' ))
69 $user->role = $_POST['role'];
72 if ( isset( $_POST['email'] ))
73 $user->user_email = wp_specialchars( trim( $_POST['email'] ));
74 if ( isset( $_POST['url'] ) ) {
75 $user->user_url = clean_url( trim( $_POST['url'] ));
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;
78 if ( isset( $_POST['first_name'] ))
79 $user->first_name = wp_specialchars( trim( $_POST['first_name'] ));
80 if ( isset( $_POST['last_name'] ))
81 $user->last_name = wp_specialchars( trim( $_POST['last_name'] ));
82 if ( isset( $_POST['nickname'] ))
83 $user->nickname = wp_specialchars( trim( $_POST['nickname'] ));
84 if ( isset( $_POST['display_name'] ))
85 $user->display_name = wp_specialchars( trim( $_POST['display_name'] ));
86 if ( isset( $_POST['description'] ))
87 $user->description = trim( $_POST['description'] );
88 if ( isset( $_POST['jabber'] ))
89 $user->jabber = wp_specialchars( trim( $_POST['jabber'] ));
90 if ( isset( $_POST['aim'] ))
91 $user->aim = wp_specialchars( trim( $_POST['aim'] ));
92 if ( isset( $_POST['yim'] ))
93 $user->yim = wp_specialchars( trim( $_POST['yim'] ));
95 $user->rich_editing = 'true'; // Default to true for new users.
96 else if ( isset( $_POST['rich_editing'] ) )
97 $user->rich_editing = $_POST['rich_editing'];
99 $user->rich_editing = 'true';
101 $user->comment_shortcuts = isset( $_POST['comment_shortcuts'] )? $_POST['comment_shortcuts'] : '';
104 if ( !empty($_POST['use_ssl']) )
108 $user->admin_color = 'fresh'; // Default to fresh for new users.
109 else if ( isset( $_POST['admin_color'] ) )
110 $user->admin_color = $_POST['admin_color'];
112 $user->admin_color = 'fresh';
114 $errors = new WP_Error();
116 /* checking that username has been typed */
117 if ( $user->user_login == '' )
118 $errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ));
120 /* checking the password has been typed twice */
121 do_action_ref_array( 'check_passwords', array ( $user->user_login, & $pass1, & $pass2 ));
124 if ( empty($pass1) && !empty($pass2) )
125 $errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass1' ) );
126 elseif ( !empty($pass1) && empty($pass2) )
127 $errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass2' ) );
130 $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password.' ), array( 'form-field' => 'pass1' ) );
131 elseif ( empty($pass2) )
132 $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password twice.' ), array( 'form-field' => 'pass2' ) );
135 /* Check for "\" in password */
136 if( strpos( " ".$pass1, "\\" ) )
137 $errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) );
139 /* checking the password has been typed twice the same */
140 if ( $pass1 != $pass2 )
141 $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter the same password in the two password fields.' ), array( 'form-field' => 'pass1' ) );
143 if (!empty ( $pass1 ))
144 $user->user_pass = $pass1;
146 if ( !$update && !validate_username( $user->user_login ) )
147 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid. Please enter a valid username.' ));
149 if (!$update && username_exists( $user->user_login ))
150 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ));
152 /* checking e-mail address */
153 if ( empty ( $user->user_email ) ) {
154 $errors->add( 'user_email', __( '<strong>ERROR</strong>: Please enter an e-mail address.' ), array( 'form-field' => 'email' ) );
156 if (!is_email( $user->user_email ) ) {
157 $errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address isn't correct." ), array( 'form-field' => 'email' ) );
160 if ( $errors->get_error_codes() )
164 $user_id = wp_update_user( get_object_vars( $user ));
166 $user_id = wp_insert_user( get_object_vars( $user ));
167 wp_new_user_notification( $user_id );
173 * {@internal Missing Short Description}}
175 * {@internal Missing Long Description}}
179 * @return array List of user IDs.
181 function get_author_user_ids() {
183 $level_key = $wpdb->prefix . 'user_level';
184 return $wpdb->get_col( $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value != '0'", $level_key) );
188 * {@internal Missing Short Description}}
190 * {@internal Missing Long Description}}
194 * @param int $user_id User ID.
195 * @return array|bool List of editable authors. False if no editable users.
197 function get_editable_authors( $user_id ) {
200 $editable = get_editable_user_ids( $user_id );
205 $editable = join(',', $editable);
206 $authors = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($editable) ORDER BY display_name" );
209 return apply_filters('get_editable_authors', $authors);
213 * {@internal Missing Short Description}}
215 * {@internal Missing Long Description}}
219 * @param int $user_id User ID.
220 * @param bool $exclude_zeros Optional, default is true. Whether to exclude zeros.
223 function get_editable_user_ids( $user_id, $exclude_zeros = true, $post_type = 'post' ) {
226 $user = new WP_User( $user_id );
228 if ( ! $user->has_cap("edit_others_{$post_type}s") ) {
229 if ( $user->has_cap("edit_{$post_type}s") || $exclude_zeros == false )
230 return array($user->id);
235 $level_key = $wpdb->prefix . 'user_level';
237 $query = $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s", $level_key);
238 if ( $exclude_zeros )
239 $query .= " AND meta_value != '0'";
241 return $wpdb->get_col( $query );
245 * {@internal Missing Short Description}}
247 * {@internal Missing Long Description}}
253 function get_nonauthor_user_ids() {
255 $level_key = $wpdb->prefix . 'user_level';
257 return $wpdb->get_col( $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value = '0'", $level_key) );
261 * Retrieve editable posts from other users.
265 * @param int $user_id User ID to not retrieve posts from.
266 * @param string $type Optional, defaults to 'any'. Post type to retrieve, can be 'draft' or 'pending'.
267 * @return array List of posts from others.
269 function get_others_unpublished_posts($user_id, $type='any') {
272 $editable = get_editable_user_ids( $user_id );
274 if ( in_array($type, array('draft', 'pending')) )
275 $type_sql = " post_status = '$type' ";
277 $type_sql = " ( post_status = 'draft' OR post_status = 'pending' ) ";
279 $dir = ( 'pending' == $type ) ? 'ASC' : 'DESC';
284 $editable = join(',', $editable);
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) );
288 return apply_filters('get_others_drafts', $other_unpubs);
292 * Retrieve drafts from other users.
296 * @param int $user_id User ID.
297 * @return array List of drafts from other users.
299 function get_others_drafts($user_id) {
300 return get_others_unpublished_posts($user_id, 'draft');
304 * Retrieve pending review posts from other users.
308 * @param int $user_id User ID.
309 * @return array List of posts with pending review post type from other users.
311 function get_others_pending($user_id) {
312 return get_others_unpublished_posts($user_id, 'pending');
316 * Retrieve user data and filter it.
320 * @param int $user_id User ID.
321 * @return object WP_User object with user data.
323 function get_user_to_edit( $user_id ) {
324 $user = new WP_User( $user_id );
325 $user->user_login = attribute_escape($user->user_login);
326 $user->user_email = attribute_escape($user->user_email);
327 $user->user_url = clean_url($user->user_url);
328 $user->first_name = attribute_escape($user->first_name);
329 $user->last_name = attribute_escape($user->last_name);
330 $user->display_name = attribute_escape($user->display_name);
331 $user->nickname = attribute_escape($user->nickname);
332 $user->aim = isset( $user->aim ) && !empty( $user->aim ) ? attribute_escape($user->aim) : '';
333 $user->yim = isset( $user->yim ) && !empty( $user->yim ) ? attribute_escape($user->yim) : '';
334 $user->jabber = isset( $user->jabber ) && !empty( $user->jabber ) ? attribute_escape($user->jabber) : '';
335 $user->description = isset( $user->description ) && !empty( $user->description ) ? wp_specialchars($user->description) : '';
341 * Retrieve the user's drafts.
345 * @param int $user_id User ID.
348 function get_users_drafts( $user_id ) {
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);
351 $query = apply_filters('get_users_drafts', $query);
352 return $wpdb->get_results( $query );
356 * Remove user and optionally reassign posts and links to another user.
358 * If the $reassign parameter is not assigned to an User ID, then all posts will
359 * be deleted of that user. The action 'delete_user' that is passed the User ID
360 * being deleted will be run after the posts are either reassigned or deleted.
361 * The user meta will also be deleted that are for that User ID.
365 * @param int $id User ID.
366 * @param int $reassign Optional. Reassign posts and links to new User ID.
367 * @return bool True when finished.
369 function wp_delete_user($id, $reassign = 'novalue') {
374 if ($reassign == 'novalue') {
375 $post_ids = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id) );
378 foreach ($post_ids as $post_id)
379 wp_delete_post($post_id);
383 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->links WHERE link_owner = %d", $id) );
385 $reassign = (int) $reassign;
386 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $id) );
387 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $id) );
390 // FINALLY, delete user
391 do_action('delete_user', $id);
393 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->users WHERE ID = %d", $id) );
394 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d", $id) );
396 wp_cache_delete($id, 'users');
397 wp_cache_delete($user->user_login, 'userlogins');
398 wp_cache_delete($user->user_email, 'useremail');
404 * Remove all capabilities from user.
408 * @param int $id User ID.
410 function wp_revoke_user($id) {
413 $user = new WP_User($id);
414 $user->remove_all_caps();
417 if ( !class_exists('WP_User_Search') ) :
419 * WordPress User Search class.
422 * @author Mark Jaquith
424 class WP_User_Search {
427 * {@internal Missing Description}}
436 * {@internal Missing Description}}
454 * Role name that users have.
472 * Amount of users to display per page.
478 var $users_per_page = 50;
481 * {@internal Missing Description}}
490 * {@internal Missing Description}}
499 * {@internal Missing Description}}
508 * {@internal Missing Description}}
517 * {@internal Missing Description}}
523 var $query_from_where;
526 * {@internal Missing Description}}
532 var $total_users_for_query = 0;
535 * {@internal Missing Description}}
541 var $too_many_total_users = false;
544 * {@internal Missing Description}}
553 * {@internal Missing Description}}
562 * PHP4 Constructor - Sets up the object properties.
566 * @param string $search_term Search terms string.
567 * @param int $page Optional. Page ID.
568 * @param string $role Role name.
569 * @return WP_User_Search
571 function WP_User_Search ($search_term = '', $page = '', $role = '') {
572 $this->search_term = $search_term;
573 $this->raw_page = ( '' == $page ) ? false : (int) $page;
574 $this->page = (int) ( '' == $page ) ? 1 : $page;
577 $this->prepare_query();
579 $this->prepare_vars_for_template_usage();
584 * {@internal Missing Short Description}}
586 * {@internal Missing Long Description}}
591 function prepare_query() {
593 $this->first_user = ($this->page - 1) * $this->users_per_page;
594 $this->query_limit = $wpdb->prepare(" LIMIT %d, %d", $this->first_user, $this->users_per_page);
595 $this->query_sort = ' ORDER BY user_login';
597 if ( $this->search_term ) {
599 $search_sql = 'AND (';
600 foreach ( array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') as $col )
601 $searches[] = $col . " LIKE '%$this->search_term%'";
602 $search_sql .= implode(' OR ', $searches);
606 $this->query_from_where = "FROM $wpdb->users";
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 . '%');
610 $this->query_from_where .= " WHERE 1=1";
611 $this->query_from_where .= " $search_sql";
616 * {@internal Missing Short Description}}
618 * {@internal Missing Long Description}}
625 $this->results = $wpdb->get_col('SELECT ID ' . $this->query_from_where . $this->query_sort . $this->query_limit);
627 if ( $this->results )
628 $this->total_users_for_query = $wpdb->get_var('SELECT COUNT(ID) ' . $this->query_from_where); // no limit
630 $this->search_errors = new WP_Error('no_matching_users_found', __('No matching users were found!'));
634 * {@internal Missing Short Description}}
636 * {@internal Missing Long Description}}
641 function prepare_vars_for_template_usage() {
642 $this->search_term = stripslashes($this->search_term); // done with DB, from now on we want slashes gone
646 * {@internal Missing Short Description}}
648 * {@internal Missing Long Description}}
653 function do_paging() {
654 if ( $this->total_users_for_query > $this->users_per_page ) { // have to page the results
656 if( ! empty($this->search_term) )
657 $args['usersearch'] = urlencode($this->search_term);
658 if( ! empty($this->role) )
659 $args['role'] = urlencode($this->role);
661 $this->paging_text = paginate_links( array(
662 'total' => ceil($this->total_users_for_query / $this->users_per_page),
663 'current' => $this->page,
664 'base' => 'users.php?%_%',
665 'format' => 'userspage=%#%',
668 if ( $this->paging_text ) {
669 $this->paging_text = sprintf( '<span class="displaying-num">' . __( 'Displaying %s–%s of %s' ) . '</span>%s',
670 number_format_i18n( ( $this->page - 1 ) * $this->users_per_page + 1 ),
671 number_format_i18n( min( $this->page * $this->users_per_page, $this->total_users_for_query ) ),
672 number_format_i18n( $this->total_users_for_query ),
680 * {@internal Missing Short Description}}
682 * {@internal Missing Long Description}}
689 function get_results() {
690 return (array) $this->results;
694 * Displaying paging text.
696 * @see do_paging() Builds paging text.
701 function page_links() {
702 echo $this->paging_text;
706 * Whether paging is enabled.
708 * @see do_paging() Builds paging text.
715 function results_are_paged() {
716 if ( $this->paging_text )
722 * Whether there are search terms.
729 function is_search() {
730 if ( $this->search_term )