3 // Creates a new user from the "Users" form using $_POST information.
5 if ( func_num_args() ) { // The hackiest hack that ever did hack
6 global $current_user, $wp_roles;
7 $user_id = (int) func_get_arg( 0 );
9 if ( isset( $_POST['role'] ) ) {
10 if( $user_id != $current_user->id || $wp_roles->role_objects[$_POST['role']]->has_cap( 'edit_users' ) ) {
11 $user = new WP_User( $user_id );
12 $user->set_role( $_POST['role'] );
16 add_action( 'user_register', 'add_user' ); // See above
21 function edit_user( $user_id = 0 ) {
22 global $current_user, $wp_roles, $wpdb;
23 if ( $user_id != 0 ) {
25 $user->ID = (int) $user_id;
26 $userdata = get_userdata( $user_id );
27 $user->user_login = $wpdb->escape( $userdata->user_login );
33 if ( isset( $_POST['user_login'] ))
34 $user->user_login = wp_specialchars( trim( $_POST['user_login'] ));
37 if ( isset( $_POST['pass1'] ))
38 $pass1 = $_POST['pass1'];
39 if ( isset( $_POST['pass2'] ))
40 $pass2 = $_POST['pass2'];
42 if ( isset( $_POST['role'] ) && current_user_can( 'edit_users' ) ) {
43 if( $user_id != $current_user->id || $wp_roles->role_objects[$_POST['role']]->has_cap( 'edit_users' ))
44 $user->role = $_POST['role'];
47 if ( isset( $_POST['email'] ))
48 $user->user_email = wp_specialchars( trim( $_POST['email'] ));
49 if ( isset( $_POST['url'] ) ) {
50 $user->user_url = clean_url( trim( $_POST['url'] ));
51 $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;
53 if ( isset( $_POST['first_name'] ))
54 $user->first_name = wp_specialchars( trim( $_POST['first_name'] ));
55 if ( isset( $_POST['last_name'] ))
56 $user->last_name = wp_specialchars( trim( $_POST['last_name'] ));
57 if ( isset( $_POST['nickname'] ))
58 $user->nickname = wp_specialchars( trim( $_POST['nickname'] ));
59 if ( isset( $_POST['display_name'] ))
60 $user->display_name = wp_specialchars( trim( $_POST['display_name'] ));
61 if ( isset( $_POST['description'] ))
62 $user->description = trim( $_POST['description'] );
63 if ( isset( $_POST['jabber'] ))
64 $user->jabber = wp_specialchars( trim( $_POST['jabber'] ));
65 if ( isset( $_POST['aim'] ))
66 $user->aim = wp_specialchars( trim( $_POST['aim'] ));
67 if ( isset( $_POST['yim'] ))
68 $user->yim = wp_specialchars( trim( $_POST['yim'] ));
70 $user->rich_editing = 'true'; // Default to true for new users.
71 else if ( isset( $_POST['rich_editing'] ) )
72 $user->rich_editing = $_POST['rich_editing'];
74 $user->rich_editing = 'false';
77 $user->admin_color = 'fresh'; // Default to fresh for new users.
78 else if ( isset( $_POST['admin_color'] ) )
79 $user->admin_color = $_POST['admin_color'];
81 $user->admin_color = 'fresh';
83 $errors = new WP_Error();
85 /* checking that username has been typed */
86 if ( $user->user_login == '' )
87 $errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ));
89 /* checking the password has been typed twice */
90 do_action_ref_array( 'check_passwords', array ( $user->user_login, & $pass1, & $pass2 ));
93 if ( empty($pass1) && !empty($pass2) )
94 $errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass1' ) );
95 elseif ( !empty($pass1) && empty($pass2) )
96 $errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass2' ) );
99 $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password.' ), array( 'form-field' => 'pass1' ) );
100 elseif ( empty($pass2) )
101 $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password twice.' ), array( 'form-field' => 'pass2' ) );
104 /* Check for "\" in password */
105 if( strpos( " ".$pass1, "\\" ) )
106 $errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) );
108 /* checking the password has been typed twice the same */
109 if ( $pass1 != $pass2 )
110 $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter the same password in the two password fields.' ), array( 'form-field' => 'pass1' ) );
112 if (!empty ( $pass1 ))
113 $user->user_pass = $pass1;
115 if ( !$update && !validate_username( $user->user_login ) )
116 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid. Please enter a valid username.' ));
118 if (!$update && username_exists( $user->user_login ))
119 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ));
121 /* checking e-mail address */
122 if ( empty ( $user->user_email ) ) {
123 $errors->add( 'user_email', __( '<strong>ERROR</strong>: Please enter an e-mail address.' ), array( 'form-field' => 'email' ) );
125 if (!is_email( $user->user_email ) ) {
126 $errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address isn't correct." ), array( 'form-field' => 'email' ) );
129 if ( $errors->get_error_codes() )
133 $user_id = wp_update_user( get_object_vars( $user ));
135 $user_id = wp_insert_user( get_object_vars( $user ));
136 wp_new_user_notification( $user_id );
141 function get_author_user_ids() {
143 $level_key = $wpdb->prefix . 'user_level';
144 return $wpdb->get_col( $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value != '0'", $level_key) );
147 function get_editable_authors( $user_id ) {
150 $editable = get_editable_user_ids( $user_id );
155 $editable = join(',', $editable);
156 $authors = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($editable) ORDER BY display_name" );
159 return apply_filters('get_editable_authors', $authors);
162 function get_editable_user_ids( $user_id, $exclude_zeros = true ) {
165 $user = new WP_User( $user_id );
167 if ( ! $user->has_cap('edit_others_posts') ) {
168 if ( $user->has_cap('edit_posts') || $exclude_zeros == false )
169 return array($user->id);
174 $level_key = $wpdb->prefix . 'user_level';
176 $query = $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s", $level_key);
177 if ( $exclude_zeros )
178 $query .= " AND meta_value != '0'";
180 return $wpdb->get_col( $query );
183 function get_nonauthor_user_ids() {
185 $level_key = $wpdb->prefix . 'user_level';
187 return $wpdb->get_col( $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value = '0'", $level_key) );
190 function get_others_unpublished_posts($user_id, $type='any') {
193 $editable = get_editable_user_ids( $user_id );
195 if ( in_array($type, array('draft', 'pending')) )
196 $type_sql = " post_status = '$type' ";
198 $type_sql = " ( post_status = 'draft' OR post_status = 'pending' ) ";
200 $dir = ( 'pending' == $type ) ? 'ASC' : 'DESC';
205 $editable = join(',', $editable);
206 $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) );
209 return apply_filters('get_others_drafts', $other_unpubs);
212 function get_others_drafts($user_id) {
213 return get_others_unpublished_posts($user_id, 'draft');
216 function get_others_pending($user_id) {
217 return get_others_unpublished_posts($user_id, 'pending');
220 function get_user_to_edit( $user_id ) {
221 $user = new WP_User( $user_id );
222 $user->user_login = attribute_escape($user->user_login);
223 $user->user_email = attribute_escape($user->user_email);
224 $user->user_url = clean_url($user->user_url);
225 $user->first_name = attribute_escape($user->first_name);
226 $user->last_name = attribute_escape($user->last_name);
227 $user->display_name = attribute_escape($user->display_name);
228 $user->nickname = attribute_escape($user->nickname);
229 $user->aim = attribute_escape($user->aim);
230 $user->yim = attribute_escape($user->yim);
231 $user->jabber = attribute_escape($user->jabber);
232 $user->description = wp_specialchars($user->description);
237 function get_users_drafts( $user_id ) {
239 $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);
240 $query = apply_filters('get_users_drafts', $query);
241 return $wpdb->get_results( $query );
244 function wp_delete_user($id, $reassign = 'novalue') {
249 if ($reassign == 'novalue') {
250 $post_ids = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id) );
253 foreach ($post_ids as $post_id)
254 wp_delete_post($post_id);
258 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->links WHERE link_owner = %d", $id) );
260 $reassign = (int) $reassign;
261 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $id) );
262 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $id) );
265 // FINALLY, delete user
266 do_action('delete_user', $id);
268 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->users WHERE ID = %d", $id) );
269 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d", $id) );
271 wp_cache_delete($id, 'users');
272 wp_cache_delete($user->user_login, 'userlogins');
273 wp_cache_delete($user->user_email, 'useremail');
278 function wp_revoke_user($id) {
281 $user = new WP_User($id);
282 $user->remove_all_caps();
285 // WP_User_Search class
288 if ( !class_exists('WP_User_Search') ) :
289 class WP_User_Search {
295 var $users_per_page = 50;
300 var $query_from_where;
301 var $total_users_for_query = 0;
302 var $too_many_total_users = false;
305 function WP_User_Search ($search_term = '', $page = '', $role = '') { // constructor
306 $this->search_term = $search_term;
307 $this->raw_page = ( '' == $page ) ? false : (int) $page;
308 $this->page = (int) ( '' == $page ) ? 1 : $page;
311 $this->prepare_query();
313 $this->prepare_vars_for_template_usage();
317 function prepare_query() {
319 $this->first_user = ($this->page - 1) * $this->users_per_page;
320 $this->query_limit = $wpdb->prepare(" LIMIT %d, %d", $this->first_user, $this->users_per_page);
321 $this->query_sort = ' ORDER BY user_login';
323 if ( $this->search_term ) {
325 $search_sql = 'AND (';
326 foreach ( array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') as $col )
327 $searches[] = $col . " LIKE '%$this->search_term%'";
328 $search_sql .= implode(' OR ', $searches);
332 $this->query_from_where = "FROM $wpdb->users";
334 $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 . '%');
336 $this->query_from_where .= " WHERE 1=1";
337 $this->query_from_where .= " $search_sql";
343 $this->results = $wpdb->get_col('SELECT ID ' . $this->query_from_where . $this->query_sort . $this->query_limit);
345 if ( $this->results )
346 $this->total_users_for_query = $wpdb->get_var('SELECT COUNT(ID) ' . $this->query_from_where); // no limit
348 $this->search_errors = new WP_Error('no_matching_users_found', __('No matching users were found!'));
351 function prepare_vars_for_template_usage() {
352 $this->search_term = stripslashes($this->search_term); // done with DB, from now on we want slashes gone
355 function do_paging() {
356 if ( $this->total_users_for_query > $this->users_per_page ) { // have to page the results
358 if( ! empty($this->search_term) )
359 $args['usersearch'] = urlencode($this->search_term);
360 if( ! empty($this->role) )
361 $args['role'] = urlencode($this->role);
363 $this->paging_text = paginate_links( array(
364 'total' => ceil($this->total_users_for_query / $this->users_per_page),
365 'current' => $this->page,
366 'base' => 'users.php?%_%',
367 'format' => 'userspage=%#%',
373 function get_results() {
374 return (array) $this->results;
377 function page_links() {
378 echo $this->paging_text;
381 function results_are_paged() {
382 if ( $this->paging_text )
387 function is_search() {
388 if ( $this->search_term )