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';
76 $errors = new WP_Error();
78 /* checking that username has been typed */
79 if ( $user->user_login == '' )
80 $errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ));
82 /* checking the password has been typed twice */
83 do_action_ref_array( 'check_passwords', array ( $user->user_login, & $pass1, & $pass2 ));
86 if ( $pass1 == '' || $pass2 == '' )
87 $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password twice.' ));
89 if ((empty ( $pass1 ) && !empty ( $pass2 ) ) || (empty ( $pass2 ) && !empty ( $pass1 ) ) )
90 $errors->add( 'pass', __( "<strong>ERROR</strong>: you typed your new password only once." ));
93 /* Check for "\" in password */
94 if( strpos( " ".$pass1, "\\" ) )
95 $errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ));
97 /* checking the password has been typed twice the same */
98 if ( $pass1 != $pass2 )
99 $errors->add( 'pass', __( '<strong>ERROR</strong>: Please type the same password in the two password fields.' ));
101 if (!empty ( $pass1 ))
102 $user->user_pass = $pass1;
104 if ( !$update && !validate_username( $user->user_login ) )
105 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid. Please enter a valid username.' ));
107 if (!$update && username_exists( $user->user_login ))
108 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered, please choose another one.' ));
110 /* checking e-mail address */
111 if ( empty ( $user->user_email ) ) {
112 $errors->add( 'user_email', __( "<strong>ERROR</strong>: please type an e-mail address" ));
114 if (!is_email( $user->user_email ) ) {
115 $errors->add( 'user_email', __( "<strong>ERROR</strong>: the email address isn't correct" ));
118 if ( $errors->get_error_codes() )
122 $user_id = wp_update_user( get_object_vars( $user ));
124 $user_id = wp_insert_user( get_object_vars( $user ));
125 wp_new_user_notification( $user_id );
130 function get_author_user_ids() {
132 $level_key = $wpdb->prefix . 'user_level';
134 $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value != '0'";
136 return $wpdb->get_col( $query );
139 function get_editable_authors( $user_id ) {
142 $editable = get_editable_user_ids( $user_id );
147 $editable = join(',', $editable);
148 $authors = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($editable) ORDER BY display_name" );
151 return apply_filters('get_editable_authors', $authors);
154 function get_editable_user_ids( $user_id, $exclude_zeros = true ) {
157 $user = new WP_User( $user_id );
159 if ( ! $user->has_cap('edit_others_posts') ) {
160 if ( $user->has_cap('edit_posts') || $exclude_zeros == false )
161 return array($user->id);
166 $level_key = $wpdb->prefix . 'user_level';
168 $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key'";
169 if ( $exclude_zeros )
170 $query .= " AND meta_value != '0'";
172 return $wpdb->get_col( $query );
175 function get_nonauthor_user_ids() {
177 $level_key = $wpdb->prefix . 'user_level';
179 $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value = '0'";
181 return $wpdb->get_col( $query );
184 function get_others_unpublished_posts($user_id, $type='any') {
186 $user = get_userdata( $user_id );
187 $level_key = $wpdb->prefix . 'user_level';
189 $editable = get_editable_user_ids( $user_id );
191 if ( in_array($type, array('draft', 'pending')) )
192 $type_sql = " post_status = '$type' ";
194 $type_sql = " ( post_status = 'draft' OR post_status = 'pending' ) ";
196 $dir = ( 'pending' == $type ) ? 'ASC' : 'DESC';
201 $editable = join(',', $editable);
202 $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");
205 return apply_filters('get_others_drafts', $other_unpubs);
208 function get_others_drafts($user_id) {
209 return get_others_unpublished_posts($user_id, 'draft');
212 function get_others_pending($user_id) {
213 return get_others_unpublished_posts($user_id, 'pending');
216 function get_user_to_edit( $user_id ) {
217 $user = new WP_User( $user_id );
218 $user->user_login = attribute_escape($user->user_login);
219 $user->user_email = attribute_escape($user->user_email);
220 $user->user_url = clean_url($user->user_url);
221 $user->first_name = attribute_escape($user->first_name);
222 $user->last_name = attribute_escape($user->last_name);
223 $user->display_name = attribute_escape($user->display_name);
224 $user->nickname = attribute_escape($user->nickname);
225 $user->aim = attribute_escape($user->aim);
226 $user->yim = attribute_escape($user->yim);
227 $user->jabber = attribute_escape($user->jabber);
228 $user->description = wp_specialchars($user->description);
233 function get_users_drafts( $user_id ) {
235 $user_id = (int) $user_id;
236 $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";
237 $query = apply_filters('get_users_drafts', $query);
238 return $wpdb->get_results( $query );
241 function wp_delete_user($id, $reassign = 'novalue') {
245 $user = get_userdata($id);
247 if ($reassign == 'novalue') {
248 $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
251 foreach ($post_ids as $post_id)
252 wp_delete_post($post_id);
256 $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
258 $reassign = (int) $reassign;
259 $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}");
260 $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}");
263 // FINALLY, delete user
264 do_action('delete_user', $id);
266 $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
267 $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$id'");
269 wp_cache_delete($id, 'users');
270 wp_cache_delete($user->user_login, 'userlogins');
275 function wp_revoke_user($id) {
278 $user = new WP_User($id);
279 $user->remove_all_caps();