]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/user.php
Wordpress 3.1-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / user.php
1 <?php
2 /**
3  * WordPress user administration API.
4  *
5  * @package WordPress
6  * @subpackage Administration
7  */
8
9 /**
10  * Creates a new user from the "Users" form using $_POST information.
11  *
12  * It seems that the first half is for backwards compatibility, but only
13  * has the ability to alter the user's role. WordPress core seems to
14  * use this function only in the second way, running edit_user() with
15  * no id so as to create a new user.
16  *
17  * @since 2.0
18  *
19  * @param int $user_id Optional. User ID.
20  * @return null|WP_Error|int Null when adding user, WP_Error or User ID integer when no parameters.
21  */
22 function add_user() {
23         if ( func_num_args() ) { // The hackiest hack that ever did hack
24                 global $wp_roles;
25                 $user_id = (int) func_get_arg( 0 );
26
27                 if ( isset( $_POST['role'] ) ) {
28                         $new_role = sanitize_text_field( $_POST['role'] );
29                         // Don't let anyone with 'edit_users' (admins) edit their own role to something without it.
30                         if ( $user_id != get_current_user_id() || $wp_roles->role_objects[$new_role]->has_cap( 'edit_users' ) ) {
31                                 // If the new role isn't editable by the logged-in user die with error
32                                 $editable_roles = get_editable_roles();
33                                 if ( empty( $editable_roles[$new_role] ) )
34                                         wp_die(__('You can&#8217;t give users that role.'));
35
36                                 $user = new WP_User( $user_id );
37                                 $user->set_role( $new_role );
38                         }
39                 }
40         } else {
41                 add_action( 'user_register', 'add_user' ); // See above
42                 return edit_user();
43         }
44 }
45
46 /**
47  * Edit user settings based on contents of $_POST
48  *
49  * Used on user-edit.php and profile.php to manage and process user options, passwords etc.
50  *
51  * @since 2.0
52  *
53  * @param int $user_id Optional. User ID.
54  * @return int user id of the updated user
55  */
56 function edit_user( $user_id = 0 ) {
57         global $wp_roles, $wpdb;
58         $user = new stdClass;
59         if ( $user_id ) {
60                 $update = true;
61                 $user->ID = (int) $user_id;
62                 $userdata = get_userdata( $user_id );
63                 $user->user_login = $wpdb->escape( $userdata->user_login );
64         } else {
65                 $update = false;
66         }
67
68         if ( !$update && isset( $_POST['user_login'] ) )
69                 $user->user_login = sanitize_user($_POST['user_login'], true);
70
71         $pass1 = $pass2 = '';
72         if ( isset( $_POST['pass1'] ))
73                 $pass1 = $_POST['pass1'];
74         if ( isset( $_POST['pass2'] ))
75                 $pass2 = $_POST['pass2'];
76
77         if ( isset( $_POST['role'] ) && current_user_can( 'edit_users' ) ) {
78                 $new_role = sanitize_text_field( $_POST['role'] );
79                 $potential_role = isset($wp_roles->role_objects[$new_role]) ? $wp_roles->role_objects[$new_role] : false;
80                 // Don't let anyone with 'edit_users' (admins) edit their own role to something without it.
81                 // Multisite super admins can freely edit their blog roles -- they possess all caps.
82                 if ( ( is_multisite() && current_user_can( 'manage_sites' ) ) || $user_id != get_current_user_id() || ($potential_role && $potential_role->has_cap( 'edit_users' ) ) )
83                         $user->role = $new_role;
84
85                 // If the new role isn't editable by the logged-in user die with error
86                 $editable_roles = get_editable_roles();
87                 if ( ! empty( $new_role ) && empty( $editable_roles[$new_role] ) )
88                         wp_die(__('You can&#8217;t give users that role.'));
89         }
90
91         if ( isset( $_POST['email'] ))
92                 $user->user_email = sanitize_text_field( $_POST['email'] );
93         if ( isset( $_POST['url'] ) ) {
94                 if ( empty ( $_POST['url'] ) || $_POST['url'] == 'http://' ) {
95                         $user->user_url = '';
96                 } else {
97                         $user->user_url = esc_url_raw( $_POST['url'] );
98                         $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;
99                 }
100         }
101         if ( isset( $_POST['first_name'] ) )
102                 $user->first_name = sanitize_text_field( $_POST['first_name'] );
103         if ( isset( $_POST['last_name'] ) )
104                 $user->last_name = sanitize_text_field( $_POST['last_name'] );
105         if ( isset( $_POST['nickname'] ) )
106                 $user->nickname = sanitize_text_field( $_POST['nickname'] );
107         if ( isset( $_POST['display_name'] ) )
108                 $user->display_name = sanitize_text_field( $_POST['display_name'] );
109
110         if ( isset( $_POST['description'] ) )
111                 $user->description = trim( $_POST['description'] );
112
113         foreach ( _wp_get_user_contactmethods( $user ) as $method => $name ) {
114                 if ( isset( $_POST[$method] ))
115                         $user->$method = sanitize_text_field( $_POST[$method] );
116         }
117
118         if ( $update ) {
119                 $user->rich_editing = isset( $_POST['rich_editing'] ) && 'false' == $_POST['rich_editing'] ? 'false' : 'true';
120                 $user->admin_color = isset( $_POST['admin_color'] ) ? sanitize_text_field( $_POST['admin_color'] ) : 'fresh';
121                 $user->show_admin_bar_front = isset( $_POST['admin_bar_front'] ) ? 'true' : 'false';
122                 $user->show_admin_bar_admin = isset( $_POST['admin_bar_admin'] ) ? 'true' : 'false';
123         }
124
125         $user->comment_shortcuts = isset( $_POST['comment_shortcuts'] ) && 'true' == $_POST['comment_shortcuts'] ? 'true' : '';
126
127         $user->use_ssl = 0;
128         if ( !empty($_POST['use_ssl']) )
129                 $user->use_ssl = 1;
130
131         $errors = new WP_Error();
132
133         /* checking that username has been typed */
134         if ( $user->user_login == '' )
135                 $errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ));
136
137         /* checking the password has been typed twice */
138         do_action_ref_array( 'check_passwords', array ( $user->user_login, & $pass1, & $pass2 ));
139
140         if ( $update ) {
141                 if ( empty($pass1) && !empty($pass2) )
142                         $errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass1' ) );
143                 elseif ( !empty($pass1) && empty($pass2) )
144                         $errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass2' ) );
145         } else {
146                 if ( empty($pass1) )
147                         $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password.' ), array( 'form-field' => 'pass1' ) );
148                 elseif ( empty($pass2) )
149                         $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password twice.' ), array( 'form-field' => 'pass2' ) );
150         }
151
152         /* Check for "\" in password */
153         if ( false !== strpos( stripslashes($pass1), "\\" ) )
154                 $errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) );
155
156         /* checking the password has been typed twice the same */
157         if ( $pass1 != $pass2 )
158                 $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter the same password in the two password fields.' ), array( 'form-field' => 'pass1' ) );
159
160         if ( !empty( $pass1 ) )
161                 $user->user_pass = $pass1;
162
163         if ( !$update && isset( $_POST['user_login'] ) && !validate_username( $_POST['user_login'] ) )
164                 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ));
165
166         if ( !$update && username_exists( $user->user_login ) )
167                 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ));
168
169         /* checking e-mail address */
170         if ( empty( $user->user_email ) ) {
171                 $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please enter an e-mail address.' ), array( 'form-field' => 'email' ) );
172         } elseif ( !is_email( $user->user_email ) ) {
173                 $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The e-mail address isn&#8217;t correct.' ), array( 'form-field' => 'email' ) );
174         } elseif ( ( $owner_id = email_exists($user->user_email) ) && ( !$update || ( $owner_id != $user->ID ) ) ) {
175                 $errors->add( 'email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'), array( 'form-field' => 'email' ) );
176         }
177
178         // Allow plugins to return their own errors.
179         do_action_ref_array('user_profile_update_errors', array ( &$errors, $update, &$user ) );
180
181         if ( $errors->get_error_codes() )
182                 return $errors;
183
184         if ( $update ) {
185                 $user_id = wp_update_user( get_object_vars( $user ) );
186         } else {
187                 $user_id = wp_insert_user( get_object_vars( $user ) );
188                 wp_new_user_notification( $user_id, isset($_POST['send_password']) ? $pass1 : '' );
189         }
190         return $user_id;
191 }
192
193 /**
194  * Fetch a filtered list of user roles that the current user is
195  * allowed to edit.
196  *
197  * Simple function who's main purpose is to allow filtering of the
198  * list of roles in the $wp_roles object so that plugins can remove
199  * innappropriate ones depending on the situation or user making edits.
200  * Specifically because without filtering anyone with the edit_users
201  * capability can edit others to be administrators, even if they are
202  * only editors or authors. This filter allows admins to delegate
203  * user management.
204  *
205  * @since 2.8
206  *
207  * @return unknown
208  */
209 function get_editable_roles() {
210         global $wp_roles;
211
212         $all_roles = $wp_roles->roles;
213         $editable_roles = apply_filters('editable_roles', $all_roles);
214
215         return $editable_roles;
216 }
217
218 /**
219  * Retrieve user data and filter it.
220  *
221  * @since 2.0.5
222  *
223  * @param int $user_id User ID.
224  * @return object WP_User object with user data.
225  */
226 function get_user_to_edit( $user_id ) {
227         $user = new WP_User( $user_id );
228
229         $user_contactmethods = _wp_get_user_contactmethods( $user );
230         foreach ($user_contactmethods as $method => $name) {
231                 if ( empty( $user->{$method} ) )
232                         $user->{$method} = '';
233         }
234
235         if ( empty($user->description) )
236                 $user->description = '';
237
238         $user = sanitize_user_object($user, 'edit');
239
240         return $user;
241 }
242
243 /**
244  * Retrieve the user's drafts.
245  *
246  * @since 2.0.0
247  *
248  * @param int $user_id User ID.
249  * @return array
250  */
251 function get_users_drafts( $user_id ) {
252         global $wpdb;
253         $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);
254         $query = apply_filters('get_users_drafts', $query);
255         return $wpdb->get_results( $query );
256 }
257
258 /**
259  * Remove user and optionally reassign posts and links to another user.
260  *
261  * If the $reassign parameter is not assigned to an User ID, then all posts will
262  * be deleted of that user. The action 'delete_user' that is passed the User ID
263  * being deleted will be run after the posts are either reassigned or deleted.
264  * The user meta will also be deleted that are for that User ID.
265  *
266  * @since 2.0.0
267  *
268  * @param int $id User ID.
269  * @param int $reassign Optional. Reassign posts and links to new User ID.
270  * @return bool True when finished.
271  */
272 function wp_delete_user( $id, $reassign = 'novalue' ) {
273         global $wpdb;
274
275         $id = (int) $id;
276
277         // allow for transaction statement
278         do_action('delete_user', $id);
279
280         if ( 'novalue' === $reassign || null === $reassign ) {
281                 $post_ids = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id) );
282
283                 if ( $post_ids ) {
284                         foreach ( $post_ids as $post_id )
285                                 wp_delete_post($post_id);
286                 }
287
288                 // Clean links
289                 $link_ids = $wpdb->get_col( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id) );
290
291                 if ( $link_ids ) {
292                         foreach ( $link_ids as $link_id )
293                                 wp_delete_link($link_id);
294                 }
295         } else {
296                 $reassign = (int) $reassign;
297                 $wpdb->update( $wpdb->posts, array('post_author' => $reassign), array('post_author' => $id) );
298                 $wpdb->update( $wpdb->links, array('link_owner' => $reassign), array('link_owner' => $id) );
299         }
300
301         clean_user_cache($id);
302
303         // FINALLY, delete user
304         if ( !is_multisite() ) {
305                 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d", $id) );
306                 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->users WHERE ID = %d", $id) );
307         } else {
308                 $level_key = $wpdb->get_blog_prefix() . 'capabilities'; // wpmu site admins don't have user_levels
309                 $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = $id AND meta_key = '{$level_key}'");
310         }
311
312         // allow for commit transaction
313         do_action('deleted_user', $id);
314
315         return true;
316 }
317
318 /**
319  * Remove all capabilities from user.
320  *
321  * @since 2.1.0
322  *
323  * @param int $id User ID.
324  */
325 function wp_revoke_user($id) {
326         $id = (int) $id;
327
328         $user = new WP_User($id);
329         $user->remove_all_caps();
330 }
331
332 add_action('admin_init', 'default_password_nag_handler');
333 /**
334  * @since 2.8.0
335  */
336 function default_password_nag_handler($errors = false) {
337         global $user_ID;
338         if ( ! get_user_option('default_password_nag') ) //Short circuit it.
339                 return;
340
341         //get_user_setting = JS saved UI setting. else no-js-falback code.
342         if ( 'hide' == get_user_setting('default_password_nag') || isset($_GET['default_password_nag']) && '0' == $_GET['default_password_nag'] ) {
343                 delete_user_setting('default_password_nag');
344                 update_user_option($user_ID, 'default_password_nag', false, true);
345         }
346 }
347
348 add_action('profile_update', 'default_password_nag_edit_user', 10, 2);
349 /**
350  * @since 2.8.0
351  */
352 function default_password_nag_edit_user($user_ID, $old_data) {
353         if ( ! get_user_option('default_password_nag', $user_ID) ) //Short circuit it.
354                 return;
355
356         $new_data = get_userdata($user_ID);
357
358         if ( $new_data->user_pass != $old_data->user_pass ) { //Remove the nag if the password has been changed.
359                 delete_user_setting('default_password_nag', $user_ID);
360                 update_user_option($user_ID, 'default_password_nag', false, true);
361         }
362 }
363
364 add_action('admin_notices', 'default_password_nag');
365 /**
366  * @since 2.8.0
367  */
368 function default_password_nag() {
369         global $pagenow;
370         if ( 'profile.php' == $pagenow || ! get_user_option('default_password_nag') ) //Short circuit it.
371                 return;
372
373         echo '<div class="error default-password-nag">';
374         echo '<p>';
375         echo '<strong>' . __('Notice:') . '</strong> ';
376         _e('You&rsquo;re using the auto-generated password for your account. Would you like to change it to something easier to remember?');
377         echo '</p><p>';
378         printf( '<a href="%s">' . __('Yes, take me to my profile page') . '</a> | ', admin_url('profile.php') . '#password' );
379         printf( '<a href="%s" id="default-password-nag-no">' . __('No thanks, do not remind me again') . '</a>', '?default_password_nag=0' );
380         echo '</p></div>';
381 }
382
383 ?>