0
|
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 |
* |
5
|
12 |
* @since 2.0.0 |
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 |
* |
5
|
25 |
* @since 2.0.0 |
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 ) { |
5
|
31 |
global $wp_roles; |
0
|
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 = $_POST['pass1']; |
|
48 |
if ( isset( $_POST['pass2'] ) ) |
|
49 |
$pass2 = $_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'] )) |
5
|
66 |
$user->user_email = sanitize_text_field( wp_unslash( $_POST['email'] ) ); |
0
|
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_contact_methods( $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 */ |
5
|
112 |
/** |
|
113 |
* Fires before the password and confirm password fields are checked for congruity. |
|
114 |
* |
|
115 |
* @since 1.5.1 |
|
116 |
* |
|
117 |
* @param string $user_login The username. |
|
118 |
* @param string &$pass1 The password, passed by reference. |
|
119 |
* @param string &$pass2 The confirmed password, passed by reference. |
|
120 |
*/ |
0
|
121 |
do_action_ref_array( 'check_passwords', array( $user->user_login, &$pass1, &$pass2 ) ); |
|
122 |
|
|
123 |
if ( $update ) { |
|
124 |
if ( empty($pass1) && !empty($pass2) ) |
|
125 |
$errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass1' ) ); |
|
126 |
elseif ( !empty($pass1) && empty($pass2) ) |
|
127 |
$errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass2' ) ); |
|
128 |
} else { |
|
129 |
if ( empty($pass1) ) |
|
130 |
$errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password.' ), array( 'form-field' => 'pass1' ) ); |
|
131 |
elseif ( empty($pass2) ) |
|
132 |
$errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password twice.' ), array( 'form-field' => 'pass2' ) ); |
|
133 |
} |
|
134 |
|
|
135 |
/* Check for "\" in password */ |
|
136 |
if ( false !== strpos( wp_unslash( $pass1 ), "\\" ) ) |
|
137 |
$errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) ); |
|
138 |
|
|
139 |
/* checking the password has been typed twice the same */ |
|
140 |
if ( $pass1 != $pass2 ) |
|
141 |
$errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter the same password in the two password fields.' ), array( 'form-field' => 'pass1' ) ); |
|
142 |
|
|
143 |
if ( !empty( $pass1 ) ) |
|
144 |
$user->user_pass = $pass1; |
|
145 |
|
|
146 |
if ( !$update && isset( $_POST['user_login'] ) && !validate_username( $_POST['user_login'] ) ) |
|
147 |
$errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' )); |
|
148 |
|
|
149 |
if ( !$update && username_exists( $user->user_login ) ) |
|
150 |
$errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' )); |
|
151 |
|
|
152 |
/* checking e-mail address */ |
|
153 |
if ( empty( $user->user_email ) ) { |
|
154 |
$errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please enter an e-mail address.' ), array( 'form-field' => 'email' ) ); |
|
155 |
} elseif ( !is_email( $user->user_email ) ) { |
|
156 |
$errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn’t correct.' ), array( 'form-field' => 'email' ) ); |
|
157 |
} elseif ( ( $owner_id = email_exists($user->user_email) ) && ( !$update || ( $owner_id != $user->ID ) ) ) { |
|
158 |
$errors->add( 'email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'), array( 'form-field' => 'email' ) ); |
|
159 |
} |
|
160 |
|
5
|
161 |
/** |
|
162 |
* Fires before user profile update errors are returned. |
|
163 |
* |
|
164 |
* @since 2.8.0 |
|
165 |
* |
|
166 |
* @param array &$errors An array of user profile update errors, passed by reference. |
|
167 |
* @param bool $update Whether this is a user update. |
|
168 |
* @param WP_User &$user WP_User object, passed by reference. |
|
169 |
*/ |
0
|
170 |
do_action_ref_array( 'user_profile_update_errors', array( &$errors, $update, &$user ) ); |
|
171 |
|
|
172 |
if ( $errors->get_error_codes() ) |
|
173 |
return $errors; |
|
174 |
|
|
175 |
if ( $update ) { |
|
176 |
$user_id = wp_update_user( $user ); |
|
177 |
} else { |
|
178 |
$user_id = wp_insert_user( $user ); |
|
179 |
wp_new_user_notification( $user_id, isset( $_POST['send_password'] ) ? wp_unslash( $pass1 ) : '' ); |
|
180 |
} |
|
181 |
return $user_id; |
|
182 |
} |
|
183 |
|
|
184 |
/** |
|
185 |
* Fetch a filtered list of user roles that the current user is |
|
186 |
* allowed to edit. |
|
187 |
* |
|
188 |
* Simple function who's main purpose is to allow filtering of the |
|
189 |
* list of roles in the $wp_roles object so that plugins can remove |
|
190 |
* inappropriate ones depending on the situation or user making edits. |
|
191 |
* Specifically because without filtering anyone with the edit_users |
|
192 |
* capability can edit others to be administrators, even if they are |
|
193 |
* only editors or authors. This filter allows admins to delegate |
|
194 |
* user management. |
|
195 |
* |
5
|
196 |
* @since 2.8.0 |
0
|
197 |
* |
5
|
198 |
* @return array |
0
|
199 |
*/ |
|
200 |
function get_editable_roles() { |
|
201 |
global $wp_roles; |
|
202 |
|
|
203 |
$all_roles = $wp_roles->roles; |
5
|
204 |
|
|
205 |
/** |
|
206 |
* Filter the list of editable roles. |
|
207 |
* |
|
208 |
* @since 2.8.0 |
|
209 |
* |
|
210 |
* @param array $all_roles List of roles. |
|
211 |
*/ |
|
212 |
$editable_roles = apply_filters( 'editable_roles', $all_roles ); |
0
|
213 |
|
|
214 |
return $editable_roles; |
|
215 |
} |
|
216 |
|
|
217 |
/** |
|
218 |
* Retrieve user data and filter it. |
|
219 |
* |
|
220 |
* @since 2.0.5 |
|
221 |
* |
|
222 |
* @param int $user_id User ID. |
|
223 |
* @return WP_User|bool WP_User object on success, false on failure. |
|
224 |
*/ |
|
225 |
function get_user_to_edit( $user_id ) { |
|
226 |
$user = get_userdata( $user_id ); |
|
227 |
|
|
228 |
if ( $user ) |
|
229 |
$user->filter = 'edit'; |
|
230 |
|
|
231 |
return $user; |
|
232 |
} |
|
233 |
|
|
234 |
/** |
|
235 |
* Retrieve the user's drafts. |
|
236 |
* |
|
237 |
* @since 2.0.0 |
|
238 |
* |
|
239 |
* @param int $user_id User ID. |
|
240 |
* @return array |
|
241 |
*/ |
|
242 |
function get_users_drafts( $user_id ) { |
|
243 |
global $wpdb; |
|
244 |
$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); |
5
|
245 |
|
|
246 |
/** |
|
247 |
* Filter the user's drafts query string. |
|
248 |
* |
|
249 |
* @since 2.0.0 |
|
250 |
* |
|
251 |
* @param string $query The user's drafts query string. |
|
252 |
*/ |
|
253 |
$query = apply_filters( 'get_users_drafts', $query ); |
0
|
254 |
return $wpdb->get_results( $query ); |
|
255 |
} |
|
256 |
|
|
257 |
/** |
|
258 |
* Remove user and optionally reassign posts and links to another user. |
|
259 |
* |
5
|
260 |
* If the $reassign parameter is not assigned to a User ID, then all posts will |
0
|
261 |
* be deleted of that user. The action 'delete_user' that is passed the User ID |
|
262 |
* being deleted will be run after the posts are either reassigned or deleted. |
|
263 |
* The user meta will also be deleted that are for that User ID. |
|
264 |
* |
|
265 |
* @since 2.0.0 |
|
266 |
* |
|
267 |
* @param int $id User ID. |
|
268 |
* @param int $reassign Optional. Reassign posts and links to new User ID. |
|
269 |
* @return bool True when finished. |
|
270 |
*/ |
5
|
271 |
function wp_delete_user( $id, $reassign = null ) { |
0
|
272 |
global $wpdb; |
|
273 |
|
|
274 |
$id = (int) $id; |
|
275 |
$user = new WP_User( $id ); |
|
276 |
|
|
277 |
if ( !$user->exists() ) |
|
278 |
return false; |
|
279 |
|
5
|
280 |
// Normalize $reassign to null or a user ID. 'novalue' was an older default. |
|
281 |
if ( 'novalue' === $reassign ) { |
|
282 |
$reassign = null; |
|
283 |
} elseif ( null !== $reassign ) { |
|
284 |
$reassign = (int) $reassign; |
|
285 |
} |
0
|
286 |
|
5
|
287 |
/** |
|
288 |
* Fires immediately before a user is deleted from the database. |
|
289 |
* |
|
290 |
* @since 2.0.0 |
|
291 |
* |
|
292 |
* @param int $id ID of the user to delete. |
|
293 |
* @param int|null $reassign ID of the user to reassign posts and links to. |
|
294 |
* Default null, for no reassignment. |
|
295 |
*/ |
|
296 |
do_action( 'delete_user', $id, $reassign ); |
|
297 |
|
|
298 |
if ( null === $reassign ) { |
0
|
299 |
$post_types_to_delete = array(); |
|
300 |
foreach ( get_post_types( array(), 'objects' ) as $post_type ) { |
|
301 |
if ( $post_type->delete_with_user ) { |
|
302 |
$post_types_to_delete[] = $post_type->name; |
|
303 |
} elseif ( null === $post_type->delete_with_user && post_type_supports( $post_type->name, 'author' ) ) { |
|
304 |
$post_types_to_delete[] = $post_type->name; |
|
305 |
} |
|
306 |
} |
|
307 |
|
5
|
308 |
/** |
|
309 |
* Filter the list of post types to delete with a user. |
|
310 |
* |
|
311 |
* @since 3.4.0 |
|
312 |
* |
|
313 |
* @param array $post_types_to_delete Post types to delete. |
|
314 |
* @param int $id User ID. |
|
315 |
*/ |
0
|
316 |
$post_types_to_delete = apply_filters( 'post_types_to_delete_with_user', $post_types_to_delete, $id ); |
|
317 |
$post_types_to_delete = implode( "', '", $post_types_to_delete ); |
|
318 |
$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 ) ); |
|
319 |
if ( $post_ids ) { |
|
320 |
foreach ( $post_ids as $post_id ) |
|
321 |
wp_delete_post( $post_id ); |
|
322 |
} |
|
323 |
|
|
324 |
// Clean links |
|
325 |
$link_ids = $wpdb->get_col( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id) ); |
|
326 |
|
|
327 |
if ( $link_ids ) { |
|
328 |
foreach ( $link_ids as $link_id ) |
|
329 |
wp_delete_link($link_id); |
|
330 |
} |
|
331 |
} else { |
|
332 |
$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) ); |
|
333 |
$wpdb->update( $wpdb->posts, array('post_author' => $reassign), array('post_author' => $id) ); |
|
334 |
if ( ! empty( $post_ids ) ) { |
|
335 |
foreach ( $post_ids as $post_id ) |
|
336 |
clean_post_cache( $post_id ); |
|
337 |
} |
|
338 |
$link_ids = $wpdb->get_col( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id) ); |
|
339 |
$wpdb->update( $wpdb->links, array('link_owner' => $reassign), array('link_owner' => $id) ); |
|
340 |
if ( ! empty( $link_ids ) ) { |
|
341 |
foreach ( $link_ids as $link_id ) |
|
342 |
clean_bookmark_cache( $link_id ); |
|
343 |
} |
|
344 |
} |
|
345 |
|
|
346 |
// FINALLY, delete user |
|
347 |
if ( is_multisite() ) { |
|
348 |
remove_user_from_blog( $id, get_current_blog_id() ); |
|
349 |
} else { |
|
350 |
$meta = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d", $id ) ); |
|
351 |
foreach ( $meta as $mid ) |
|
352 |
delete_metadata_by_mid( 'user', $mid ); |
|
353 |
|
|
354 |
$wpdb->delete( $wpdb->users, array( 'ID' => $id ) ); |
|
355 |
} |
|
356 |
|
|
357 |
clean_user_cache( $user ); |
|
358 |
|
5
|
359 |
/** |
|
360 |
* Fires immediately after a user is deleted from the database. |
|
361 |
* |
|
362 |
* @since 2.9.0 |
|
363 |
* |
|
364 |
* @param int $id ID of the deleted user. |
|
365 |
* @param int|null $reassign ID of the user to reassign posts and links to. |
|
366 |
* Default null, for no reassignment. |
|
367 |
*/ |
|
368 |
do_action( 'deleted_user', $id, $reassign ); |
0
|
369 |
|
|
370 |
return true; |
|
371 |
} |
|
372 |
|
|
373 |
/** |
|
374 |
* Remove all capabilities from user. |
|
375 |
* |
|
376 |
* @since 2.1.0 |
|
377 |
* |
|
378 |
* @param int $id User ID. |
|
379 |
*/ |
|
380 |
function wp_revoke_user($id) { |
|
381 |
$id = (int) $id; |
|
382 |
|
|
383 |
$user = new WP_User($id); |
|
384 |
$user->remove_all_caps(); |
|
385 |
} |
|
386 |
|
|
387 |
add_action('admin_init', 'default_password_nag_handler'); |
|
388 |
/** |
|
389 |
* @since 2.8.0 |
|
390 |
*/ |
|
391 |
function default_password_nag_handler($errors = false) { |
|
392 |
global $user_ID; |
5
|
393 |
// Short-circuit it. |
|
394 |
if ( ! get_user_option('default_password_nag') ) |
0
|
395 |
return; |
|
396 |
|
5
|
397 |
// get_user_setting = JS saved UI setting. else no-js-fallback code. |
0
|
398 |
if ( 'hide' == get_user_setting('default_password_nag') || isset($_GET['default_password_nag']) && '0' == $_GET['default_password_nag'] ) { |
|
399 |
delete_user_setting('default_password_nag'); |
|
400 |
update_user_option($user_ID, 'default_password_nag', false, true); |
|
401 |
} |
|
402 |
} |
|
403 |
|
|
404 |
add_action('profile_update', 'default_password_nag_edit_user', 10, 2); |
5
|
405 |
|
0
|
406 |
/** |
|
407 |
* @since 2.8.0 |
|
408 |
*/ |
|
409 |
function default_password_nag_edit_user($user_ID, $old_data) { |
5
|
410 |
// Short-circuit it. |
|
411 |
if ( ! get_user_option('default_password_nag', $user_ID) ) |
0
|
412 |
return; |
|
413 |
|
|
414 |
$new_data = get_userdata($user_ID); |
|
415 |
|
5
|
416 |
// Remove the nag if the password has been changed. |
|
417 |
if ( $new_data->user_pass != $old_data->user_pass ) { |
0
|
418 |
delete_user_setting('default_password_nag'); |
|
419 |
update_user_option($user_ID, 'default_password_nag', false, true); |
|
420 |
} |
|
421 |
} |
|
422 |
|
|
423 |
add_action('admin_notices', 'default_password_nag'); |
5
|
424 |
|
0
|
425 |
/** |
|
426 |
* @since 2.8.0 |
|
427 |
*/ |
|
428 |
function default_password_nag() { |
|
429 |
global $pagenow; |
5
|
430 |
// Short-circuit it. |
|
431 |
if ( 'profile.php' == $pagenow || ! get_user_option('default_password_nag') ) |
0
|
432 |
return; |
|
433 |
|
|
434 |
echo '<div class="error default-password-nag">'; |
|
435 |
echo '<p>'; |
|
436 |
echo '<strong>' . __('Notice:') . '</strong> '; |
|
437 |
_e('You’re using the auto-generated password for your account. Would you like to change it to something easier to remember?'); |
|
438 |
echo '</p><p>'; |
5
|
439 |
printf( '<a href="%s">' . __('Yes, take me to my profile page') . '</a> | ', get_edit_profile_url() . '#password' ); |
0
|
440 |
printf( '<a href="%s" id="default-password-nag-no">' . __('No thanks, do not remind me again') . '</a>', '?default_password_nag=0' ); |
|
441 |
echo '</p></div>'; |
|
442 |
} |