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';
145 $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value != '0'";
147 return $wpdb->get_col( $query );
150 function get_editable_authors( $user_id ) {
153 $editable = get_editable_user_ids( $user_id );
158 $editable = join(',', $editable);
159 $authors = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($editable) ORDER BY display_name" );
162 return apply_filters('get_editable_authors', $authors);
165 function get_editable_user_ids( $user_id, $exclude_zeros = true ) {
168 $user = new WP_User( $user_id );
170 if ( ! $user->has_cap('edit_others_posts') ) {
171 if ( $user->has_cap('edit_posts') || $exclude_zeros == false )
172 return array($user->id);
177 $level_key = $wpdb->prefix . 'user_level';
179 $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key'";
180 if ( $exclude_zeros )
181 $query .= " AND meta_value != '0'";
183 return $wpdb->get_col( $query );
186 function get_nonauthor_user_ids() {
188 $level_key = $wpdb->prefix . 'user_level';
190 $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value = '0'";
192 return $wpdb->get_col( $query );
195 function get_others_unpublished_posts($user_id, $type='any') {
198 $editable = get_editable_user_ids( $user_id );
200 if ( in_array($type, array('draft', 'pending')) )
201 $type_sql = " post_status = '$type' ";
203 $type_sql = " ( post_status = 'draft' OR post_status = 'pending' ) ";
205 $dir = ( 'pending' == $type ) ? 'ASC' : 'DESC';
210 $editable = join(',', $editable);
211 $other_unpubs = $wpdb->get_results("SELECT ID, post_title, post_author FROM $wpdb->posts WHERE post_type = 'post' AND $type_sql AND post_author IN ($editable) AND post_author != '$user_id' ORDER BY post_modified $dir");
214 return apply_filters('get_others_drafts', $other_unpubs);
217 function get_others_drafts($user_id) {
218 return get_others_unpublished_posts($user_id, 'draft');
221 function get_others_pending($user_id) {
222 return get_others_unpublished_posts($user_id, 'pending');
225 function get_user_to_edit( $user_id ) {
226 $user = new WP_User( $user_id );
227 $user->user_login = attribute_escape($user->user_login);
228 $user->user_email = attribute_escape($user->user_email);
229 $user->user_url = clean_url($user->user_url);
230 $user->first_name = attribute_escape($user->first_name);
231 $user->last_name = attribute_escape($user->last_name);
232 $user->display_name = attribute_escape($user->display_name);
233 $user->nickname = attribute_escape($user->nickname);
234 $user->aim = attribute_escape($user->aim);
235 $user->yim = attribute_escape($user->yim);
236 $user->jabber = attribute_escape($user->jabber);
237 $user->description = wp_specialchars($user->description);
242 function get_users_drafts( $user_id ) {
244 $user_id = (int) $user_id;
245 $query = "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft' AND post_author = $user_id ORDER BY post_modified DESC";
246 $query = apply_filters('get_users_drafts', $query);
247 return $wpdb->get_results( $query );
250 function wp_delete_user($id, $reassign = 'novalue') {
255 if ($reassign == 'novalue') {
256 $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
259 foreach ($post_ids as $post_id)
260 wp_delete_post($post_id);
264 $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
266 $reassign = (int) $reassign;
267 $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}");
268 $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}");
271 // FINALLY, delete user
272 do_action('delete_user', $id);
274 $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
275 $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$id'");
277 wp_cache_delete($id, 'users');
278 wp_cache_delete($user->user_login, 'userlogins');
279 wp_cache_delete($user->user_email, 'useremail');
284 function wp_revoke_user($id) {
287 $user = new WP_User($id);
288 $user->remove_all_caps();
291 // WP_User_Search class
294 if ( !class_exists('WP_User_Search') ) :
295 class WP_User_Search {
301 var $users_per_page = 50;
306 var $query_from_where;
307 var $total_users_for_query = 0;
308 var $too_many_total_users = false;
311 function WP_User_Search ($search_term = '', $page = '', $role = '') { // constructor
312 $this->search_term = $search_term;
313 $this->raw_page = ( '' == $page ) ? false : (int) $page;
314 $this->page = (int) ( '' == $page ) ? 1 : $page;
317 $this->prepare_query();
319 $this->prepare_vars_for_template_usage();
323 function prepare_query() {
325 $this->first_user = ($this->page - 1) * $this->users_per_page;
326 $this->query_limit = ' LIMIT ' . $this->first_user . ',' . $this->users_per_page;
327 $this->query_sort = ' ORDER BY user_login';
329 if ( $this->search_term ) {
331 $search_sql = 'AND (';
332 foreach ( array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') as $col )
333 $searches[] = $col . " LIKE '%$this->search_term%'";
334 $search_sql .= implode(' OR ', $searches);
338 $this->query_from_where = "FROM $wpdb->users";
340 $this->query_from_where .= " 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 '%$this->role%'";
342 $this->query_from_where .= " WHERE 1=1";
343 $this->query_from_where .= " $search_sql";
349 $this->results = $wpdb->get_col('SELECT ID ' . $this->query_from_where . $this->query_sort . $this->query_limit);
351 if ( $this->results )
352 $this->total_users_for_query = $wpdb->get_var('SELECT COUNT(ID) ' . $this->query_from_where); // no limit
354 $this->search_errors = new WP_Error('no_matching_users_found', __('No matching users were found!'));
357 function prepare_vars_for_template_usage() {
358 $this->search_term = stripslashes($this->search_term); // done with DB, from now on we want slashes gone
361 function do_paging() {
362 if ( $this->total_users_for_query > $this->users_per_page ) { // have to page the results
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=%#%',
368 'add_args' => array( 'usersearch' => urlencode($this->search_term) )
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 )