root/tags/1.3/wp-admin/includes/user.php

Revision 1139, 9.1 kB (checked in by donncha, 10 months ago)

Merge with WordPress? 2.3.1

Line 
1 <?php
2
3 // Creates a new user from the "Users" form using $_POST information.
4 function add_user() {
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 );
8
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'] );
13             }
14         }
15     } else {
16         add_action( 'user_register', 'add_user' ); // See above
17         return edit_user();
18     }
19 }
20
21 function edit_user( $user_id = 0 ) {
22     global $current_user, $wp_roles, $wpdb;
23     if ( $user_id != 0 ) {
24         $update = true;
25         $user->ID = (int) $user_id;
26         $userdata = get_userdata( $user_id );
27         $user->user_login = $wpdb->escape( $userdata->user_login );
28     } else {
29         $update = false;
30         $user = '';
31     }
32
33     if ( isset( $_POST['user_login'] ))
34         $user->user_login = wp_specialchars( trim( $_POST['user_login'] ));
35
36     $pass1 = $pass2 = '';
37     if ( isset( $_POST['pass1'] ))
38         $pass1 = $_POST['pass1'];
39     if ( isset( $_POST['pass2'] ))
40         $pass2 = $_POST['pass2'];
41
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'];
45     }
46
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;
52     }
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'] ));
69     if ( !$update )
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'];
73     else
74         $user->rich_editing = 'false';
75
76     $errors = new WP_Error();
77
78     /* checking that username has been typed */
79     if ( $user->user_login == '' )
80         $errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ));
81
82     /* checking the password has been typed twice */
83     do_action_ref_array( 'check_passwords', array ( $user->user_login, & $pass1, & $pass2 ));
84
85     if (!$update ) {
86         if ( $pass1 == '' || $pass2 == '' )
87             $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password twice.' ));
88     } else {
89         if ((empty ( $pass1 ) && !empty ( $pass2 ) ) || (empty ( $pass2 ) && !empty ( $pass1 ) ) )
90             $errors->add( 'pass', __( "<strong>ERROR</strong>: you typed your new password only once." ));
91     }
92
93     /* Check for "\" in password */
94     if( strpos( " ".$pass1, "\\" ) )
95         $errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ));
96
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.' ));
100
101     if (!empty ( $pass1 ))
102         $user->user_pass = $pass1;
103
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.' ));
106
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.' ));
109
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" ));
113     } else
114         if (!is_email( $user->user_email ) ) {
115             $errors->add( 'user_email', __( "<strong>ERROR</strong>: the email address isn't correct" ));
116         }
117
118     if ( $errors->get_error_codes() )
119         return $errors;
120
121     if ( $update ) {
122         $user_id = wp_update_user( get_object_vars( $user ));
123     } else {
124         $user_id = wp_insert_user( get_object_vars( $user ));
125         wp_new_user_notification( $user_id );
126     }
127     return $user_id;
128 }
129
130 function get_author_user_ids() {
131     global $wpdb;
132     // wpmu site admins don't have user_levels
133     $level_key = $wpdb->prefix . 'capabilities';
134
135     $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value != '0'";
136
137     return $wpdb->get_col( $query );
138 }
139
140 function get_editable_authors( $user_id ) {
141     global $wpdb;
142
143     $editable = get_editable_user_ids( $user_id );
144
145     if( !$editable ) {
146         return false;
147     } else {
148         $editable = join(',', $editable);
149         $authors = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($editable) ORDER BY display_name" );
150     }
151
152     return apply_filters('get_editable_authors', $authors);
153 }
154
155 function get_editable_user_ids( $user_id, $exclude_zeros = true ) {
156     global $wpdb;
157
158     $user = new WP_User( $user_id );
159
160     if ( ! $user->has_cap('edit_others_posts') ) {
161         if ( $user->has_cap('edit_posts') || $exclude_zeros == false )
162             return array($user->id);
163         else
164             return false;
165     }
166
167     // wpmu site admins don't have user_levels
168     $level_key = $wpdb->prefix . 'capabilities';
169
170     $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key'";
171     if ( $exclude_zeros )
172         $query .= " AND meta_value != 'a:1:{s:10:\"subscriber\";b:1;}'";
173
174     return $wpdb->get_col( $query );
175 }
176
177 function get_nonauthor_user_ids() {
178     global $wpdb;
179     // wpmu site admins don't have user_levels
180     $level_key = $wpdb->prefix . 'capabilities';
181
182     $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value = '0'";
183
184     return $wpdb->get_col( $query );
185 }
186
187 function get_others_unpublished_posts($user_id, $type='any') {
188     global $wpdb;
189     $user = get_userdata( $user_id );
190     $level_key = $wpdb->prefix . 'user_level';
191
192     $editable = get_editable_user_ids( $user_id );
193
194     if ( in_array($type, array('draft', 'pending')) )
195         $type_sql = " post_status = '$type' ";
196     else
197         $type_sql = " ( post_status = 'draft' OR post_status = 'pending' ) ";
198
199     $dir = ( 'pending' == $type ) ? 'ASC' : 'DESC';
200
201     if( !$editable ) {
202         $other_unpubs = '';
203     } else {
204         $editable = join(',', $editable);
205         $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");
206     }
207
208     return apply_filters('get_others_drafts', $other_unpubs);
209 }
210
211 function get_others_drafts($user_id) {
212     return get_others_unpublished_posts($user_id, 'draft');
213 }
214
215 function get_others_pending($user_id) {
216     return get_others_unpublished_posts($user_id, 'pending');
217 }
218
219 function get_user_to_edit( $user_id ) {
220     $user = new WP_User( $user_id );
221     $user->user_login   = attribute_escape($user->user_login);
222     $user->user_email   = attribute_escape($user->user_email);
223     $user->user_url     = clean_url($user->user_url);
224     $user->first_name   = attribute_escape($user->first_name);
225     $user->last_name    = attribute_escape($user->last_name);
226     $user->display_name = attribute_escape($user->display_name);
227     $user->nickname     = attribute_escape($user->nickname);
228     $user->aim          = attribute_escape($user->aim);
229     $user->yim          = attribute_escape($user->yim);
230     $user->jabber       = attribute_escape($user->jabber);
231     $user->description  wp_specialchars($user->description);
232
233     return $user;
234 }
235
236 function get_users_drafts( $user_id ) {
237     global $wpdb;
238     $user_id = (int) $user_id;
239     $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";
240     $query = apply_filters('get_users_drafts', $query);
241     return $wpdb->get_results( $query );
242 }
243
244 function wp_delete_user($id, $reassign = 'novalue') {
245     global $wpdb;
246
247     $id = (int) $id;
248     $user = get_userdata($id);
249
250     if ($reassign == 'novalue') {
251         $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
252
253         if ($post_ids) {
254             foreach ($post_ids as $post_id)
255                 wp_delete_post($post_id);
256         }
257
258         // Clean links
259         $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
260     } else {
261         $reassign = (int) $reassign;
262         $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}");
263         $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}");
264     }
265
266     // FINALLY, delete user
267     do_action('delete_user', $id);
268
269     $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = $id AND meta_key = '{$wpdb->prefix}capabilities'");
270
271     wp_cache_delete($id, 'users');
272     wp_cache_delete($user->user_login, 'userlogins');
273
274     return true;
275 }
276
277 function wp_revoke_user($id) {
278     $id = (int) $id;
279
280     $user = new WP_User($id);
281     $user->remove_all_caps();
282 }
283
284 ?>
285
Note: See TracBrowser for help on using the browser.