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