author | ymh <ymh.work@gmail.com> |
Tue, 27 Sep 2022 16:37:53 +0200 | |
changeset 19 | 3d72ae0968f4 |
parent 18 | be944660c56a |
child 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
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 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* @return int|WP_Error WP_Error or User ID. |
0 | 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. |
|
19 | 28 |
* @return int|WP_Error User ID of the updated user or WP_Error on failure. |
0 | 29 |
*/ |
30 |
function edit_user( $user_id = 0 ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
31 |
$wp_roles = wp_roles(); |
9 | 32 |
$user = new stdClass; |
33 |
$user_id = (int) $user_id; |
|
0 | 34 |
if ( $user_id ) { |
9 | 35 |
$update = true; |
36 |
$user->ID = $user_id; |
|
37 |
$userdata = get_userdata( $user_id ); |
|
0 | 38 |
$user->user_login = wp_slash( $userdata->user_login ); |
39 |
} else { |
|
40 |
$update = false; |
|
41 |
} |
|
42 |
||
9 | 43 |
if ( ! $update && isset( $_POST['user_login'] ) ) { |
16 | 44 |
$user->user_login = sanitize_user( wp_unslash( $_POST['user_login'] ), true ); |
9 | 45 |
} |
0 | 46 |
|
16 | 47 |
$pass1 = ''; |
48 |
$pass2 = ''; |
|
9 | 49 |
if ( isset( $_POST['pass1'] ) ) { |
18 | 50 |
$pass1 = trim( $_POST['pass1'] ); |
9 | 51 |
} |
52 |
if ( isset( $_POST['pass2'] ) ) { |
|
18 | 53 |
$pass2 = trim( $_POST['pass2'] ); |
0 | 54 |
} |
55 |
||
9 | 56 |
if ( isset( $_POST['role'] ) && current_user_can( 'promote_users' ) && ( ! $user_id || current_user_can( 'promote_user', $user_id ) ) ) { |
57 |
$new_role = sanitize_text_field( $_POST['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( __( 'Sorry, you are not allowed to give users that role.' ), 403 ); |
|
63 |
} |
|
64 |
||
65 |
$potential_role = isset( $wp_roles->role_objects[ $new_role ] ) ? $wp_roles->role_objects[ $new_role ] : false; |
|
66 |
||
67 |
/* |
|
68 |
* Don't let anyone with 'promote_users' edit their own role to something without it. |
|
69 |
* Multisite super admins can freely edit their roles, they possess all caps. |
|
70 |
*/ |
|
71 |
if ( |
|
72 |
( is_multisite() && current_user_can( 'manage_network_users' ) ) || |
|
16 | 73 |
get_current_user_id() !== $user_id || |
9 | 74 |
( $potential_role && $potential_role->has_cap( 'promote_users' ) ) |
75 |
) { |
|
76 |
$user->role = $new_role; |
|
77 |
} |
|
78 |
} |
|
79 |
||
80 |
if ( isset( $_POST['email'] ) ) { |
|
5 | 81 |
$user->user_email = sanitize_text_field( wp_unslash( $_POST['email'] ) ); |
9 | 82 |
} |
0 | 83 |
if ( isset( $_POST['url'] ) ) { |
16 | 84 |
if ( empty( $_POST['url'] ) || 'http://' === $_POST['url'] ) { |
0 | 85 |
$user->user_url = ''; |
86 |
} else { |
|
87 |
$user->user_url = esc_url_raw( $_POST['url'] ); |
|
9 | 88 |
$protocols = implode( '|', array_map( 'preg_quote', wp_allowed_protocols() ) ); |
89 |
$user->user_url = preg_match( '/^(' . $protocols . '):/is', $user->user_url ) ? $user->user_url : 'http://' . $user->user_url; |
|
0 | 90 |
} |
91 |
} |
|
9 | 92 |
if ( isset( $_POST['first_name'] ) ) { |
0 | 93 |
$user->first_name = sanitize_text_field( $_POST['first_name'] ); |
9 | 94 |
} |
95 |
if ( isset( $_POST['last_name'] ) ) { |
|
0 | 96 |
$user->last_name = sanitize_text_field( $_POST['last_name'] ); |
9 | 97 |
} |
98 |
if ( isset( $_POST['nickname'] ) ) { |
|
0 | 99 |
$user->nickname = sanitize_text_field( $_POST['nickname'] ); |
9 | 100 |
} |
101 |
if ( isset( $_POST['display_name'] ) ) { |
|
0 | 102 |
$user->display_name = sanitize_text_field( $_POST['display_name'] ); |
9 | 103 |
} |
0 | 104 |
|
9 | 105 |
if ( isset( $_POST['description'] ) ) { |
0 | 106 |
$user->description = trim( $_POST['description'] ); |
9 | 107 |
} |
0 | 108 |
|
109 |
foreach ( wp_get_user_contact_methods( $user ) as $method => $name ) { |
|
9 | 110 |
if ( isset( $_POST[ $method ] ) ) { |
111 |
$user->$method = sanitize_text_field( $_POST[ $method ] ); |
|
112 |
} |
|
0 | 113 |
} |
114 |
||
16 | 115 |
if ( isset( $_POST['locale'] ) ) { |
116 |
$locale = sanitize_text_field( $_POST['locale'] ); |
|
117 |
if ( 'site-default' === $locale ) { |
|
118 |
$locale = ''; |
|
119 |
} elseif ( '' === $locale ) { |
|
120 |
$locale = 'en_US'; |
|
121 |
} elseif ( ! in_array( $locale, get_available_languages(), true ) ) { |
|
122 |
$locale = ''; |
|
123 |
} |
|
124 |
||
125 |
$user->locale = $locale; |
|
126 |
} |
|
127 |
||
0 | 128 |
if ( $update ) { |
9 | 129 |
$user->rich_editing = isset( $_POST['rich_editing'] ) && 'false' === $_POST['rich_editing'] ? 'false' : 'true'; |
130 |
$user->syntax_highlighting = isset( $_POST['syntax_highlighting'] ) && 'false' === $_POST['syntax_highlighting'] ? 'false' : 'true'; |
|
131 |
$user->admin_color = isset( $_POST['admin_color'] ) ? sanitize_text_field( $_POST['admin_color'] ) : 'fresh'; |
|
0 | 132 |
$user->show_admin_bar_front = isset( $_POST['admin_bar_front'] ) ? 'true' : 'false'; |
133 |
} |
|
134 |
||
16 | 135 |
$user->comment_shortcuts = isset( $_POST['comment_shortcuts'] ) && 'true' === $_POST['comment_shortcuts'] ? 'true' : ''; |
0 | 136 |
|
137 |
$user->use_ssl = 0; |
|
9 | 138 |
if ( ! empty( $_POST['use_ssl'] ) ) { |
0 | 139 |
$user->use_ssl = 1; |
9 | 140 |
} |
0 | 141 |
|
142 |
$errors = new WP_Error(); |
|
143 |
||
144 |
/* checking that username has been typed */ |
|
16 | 145 |
if ( '' === $user->user_login ) { |
146 |
$errors->add( 'user_login', __( '<strong>Error</strong>: Please enter a username.' ) ); |
|
9 | 147 |
} |
0 | 148 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
149 |
/* checking that nickname has been typed */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
150 |
if ( $update && empty( $user->nickname ) ) { |
16 | 151 |
$errors->add( 'nickname', __( '<strong>Error</strong>: Please enter a nickname.' ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
|
5 | 154 |
/** |
155 |
* Fires before the password and confirm password fields are checked for congruity. |
|
156 |
* |
|
157 |
* @since 1.5.1 |
|
158 |
* |
|
159 |
* @param string $user_login The username. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
160 |
* @param string $pass1 The password (passed by reference). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
161 |
* @param string $pass2 The confirmed password (passed by reference). |
5 | 162 |
*/ |
0 | 163 |
do_action_ref_array( 'check_passwords', array( $user->user_login, &$pass1, &$pass2 ) ); |
164 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
165 |
// Check for blank password when adding a user. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
166 |
if ( ! $update && empty( $pass1 ) ) { |
16 | 167 |
$errors->add( 'pass', __( '<strong>Error</strong>: Please enter a password.' ), array( 'form-field' => 'pass1' ) ); |
0 | 168 |
} |
169 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
170 |
// Check for "\" in password. |
9 | 171 |
if ( false !== strpos( wp_unslash( $pass1 ), '\\' ) ) { |
16 | 172 |
$errors->add( 'pass', __( '<strong>Error</strong>: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
173 |
} |
0 | 174 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
175 |
// Checking the password has been typed twice the same. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
176 |
if ( ( $update || ! empty( $pass1 ) ) && $pass1 != $pass2 ) { |
19 | 177 |
$errors->add( 'pass', __( '<strong>Error</strong>: Passwords do not match. Please enter the same password in both password fields.' ), array( 'form-field' => 'pass1' ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
178 |
} |
0 | 179 |
|
9 | 180 |
if ( ! empty( $pass1 ) ) { |
0 | 181 |
$user->user_pass = $pass1; |
9 | 182 |
} |
0 | 183 |
|
9 | 184 |
if ( ! $update && isset( $_POST['user_login'] ) && ! validate_username( $_POST['user_login'] ) ) { |
16 | 185 |
$errors->add( 'user_login', __( '<strong>Error</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) ); |
9 | 186 |
} |
0 | 187 |
|
9 | 188 |
if ( ! $update && username_exists( $user->user_login ) ) { |
16 | 189 |
$errors->add( 'user_login', __( '<strong>Error</strong>: This username is already registered. Please choose another one.' ) ); |
9 | 190 |
} |
0 | 191 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
192 |
/** This filter is documented in wp-includes/user.php */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
193 |
$illegal_logins = (array) apply_filters( 'illegal_user_logins', array() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
194 |
|
16 | 195 |
if ( in_array( strtolower( $user->user_login ), array_map( 'strtolower', $illegal_logins ), true ) ) { |
196 |
$errors->add( 'invalid_username', __( '<strong>Error</strong>: Sorry, that username is not allowed.' ) ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
197 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
198 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
199 |
/* checking email address */ |
0 | 200 |
if ( empty( $user->user_email ) ) { |
16 | 201 |
$errors->add( 'empty_email', __( '<strong>Error</strong>: Please enter an email address.' ), array( 'form-field' => 'email' ) ); |
9 | 202 |
} elseif ( ! is_email( $user->user_email ) ) { |
19 | 203 |
$errors->add( 'invalid_email', __( '<strong>Error</strong>: The email address is not correct.' ), array( 'form-field' => 'email' ) ); |
16 | 204 |
} else { |
205 |
$owner_id = email_exists( $user->user_email ); |
|
206 |
if ( $owner_id && ( ! $update || ( $owner_id != $user->ID ) ) ) { |
|
207 |
$errors->add( 'email_exists', __( '<strong>Error</strong>: This email is already registered. Please choose another one.' ), array( 'form-field' => 'email' ) ); |
|
208 |
} |
|
0 | 209 |
} |
210 |
||
5 | 211 |
/** |
212 |
* Fires before user profile update errors are returned. |
|
213 |
* |
|
214 |
* @since 2.8.0 |
|
215 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
216 |
* @param WP_Error $errors WP_Error object (passed by reference). |
16 | 217 |
* @param bool $update Whether this is a user update. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
218 |
* @param stdClass $user User object (passed by reference). |
5 | 219 |
*/ |
0 | 220 |
do_action_ref_array( 'user_profile_update_errors', array( &$errors, $update, &$user ) ); |
221 |
||
9 | 222 |
if ( $errors->has_errors() ) { |
0 | 223 |
return $errors; |
9 | 224 |
} |
0 | 225 |
|
226 |
if ( $update ) { |
|
227 |
$user_id = wp_update_user( $user ); |
|
228 |
} else { |
|
229 |
$user_id = wp_insert_user( $user ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
230 |
$notify = isset( $_POST['send_user_notification'] ) ? 'both' : 'admin'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
231 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
232 |
/** |
9 | 233 |
* Fires after a new user has been created. |
234 |
* |
|
235 |
* @since 4.4.0 |
|
236 |
* |
|
19 | 237 |
* @param int|WP_Error $user_id ID of the newly created user or WP_Error on failure. |
238 |
* @param string $notify Type of notification that should happen. See |
|
239 |
* wp_send_new_user_notifications() for more information. |
|
9 | 240 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
241 |
do_action( 'edit_user_created_user', $user_id, $notify ); |
0 | 242 |
} |
243 |
return $user_id; |
|
244 |
} |
|
245 |
||
246 |
/** |
|
247 |
* Fetch a filtered list of user roles that the current user is |
|
248 |
* allowed to edit. |
|
249 |
* |
|
9 | 250 |
* Simple function whose main purpose is to allow filtering of the |
0 | 251 |
* list of roles in the $wp_roles object so that plugins can remove |
252 |
* inappropriate ones depending on the situation or user making edits. |
|
253 |
* Specifically because without filtering anyone with the edit_users |
|
254 |
* capability can edit others to be administrators, even if they are |
|
255 |
* only editors or authors. This filter allows admins to delegate |
|
256 |
* user management. |
|
257 |
* |
|
5 | 258 |
* @since 2.8.0 |
0 | 259 |
* |
9 | 260 |
* @return array[] Array of arrays containing role information. |
0 | 261 |
*/ |
262 |
function get_editable_roles() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
263 |
$all_roles = wp_roles()->roles; |
5 | 264 |
|
265 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
266 |
* Filters the list of editable roles. |
5 | 267 |
* |
268 |
* @since 2.8.0 |
|
269 |
* |
|
9 | 270 |
* @param array[] $all_roles Array of arrays containing role information. |
5 | 271 |
*/ |
272 |
$editable_roles = apply_filters( 'editable_roles', $all_roles ); |
|
0 | 273 |
|
274 |
return $editable_roles; |
|
275 |
} |
|
276 |
||
277 |
/** |
|
278 |
* Retrieve user data and filter it. |
|
279 |
* |
|
280 |
* @since 2.0.5 |
|
281 |
* |
|
282 |
* @param int $user_id User ID. |
|
18 | 283 |
* @return WP_User|false WP_User object on success, false on failure. |
0 | 284 |
*/ |
285 |
function get_user_to_edit( $user_id ) { |
|
286 |
$user = get_userdata( $user_id ); |
|
287 |
||
9 | 288 |
if ( $user ) { |
0 | 289 |
$user->filter = 'edit'; |
9 | 290 |
} |
0 | 291 |
|
292 |
return $user; |
|
293 |
} |
|
294 |
||
295 |
/** |
|
296 |
* Retrieve the user's drafts. |
|
297 |
* |
|
298 |
* @since 2.0.0 |
|
299 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
300 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
301 |
* |
0 | 302 |
* @param int $user_id User ID. |
303 |
* @return array |
|
304 |
*/ |
|
305 |
function get_users_drafts( $user_id ) { |
|
306 |
global $wpdb; |
|
9 | 307 |
$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 | 308 |
|
309 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
310 |
* Filters the user's drafts query string. |
5 | 311 |
* |
312 |
* @since 2.0.0 |
|
313 |
* |
|
314 |
* @param string $query The user's drafts query string. |
|
315 |
*/ |
|
316 |
$query = apply_filters( 'get_users_drafts', $query ); |
|
0 | 317 |
return $wpdb->get_results( $query ); |
318 |
} |
|
319 |
||
320 |
/** |
|
321 |
* Remove user and optionally reassign posts and links to another user. |
|
322 |
* |
|
5 | 323 |
* If the $reassign parameter is not assigned to a User ID, then all posts will |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
324 |
* be deleted of that user. The action {@see 'delete_user'} that is passed the User ID |
0 | 325 |
* being deleted will be run after the posts are either reassigned or deleted. |
326 |
* The user meta will also be deleted that are for that User ID. |
|
327 |
* |
|
328 |
* @since 2.0.0 |
|
329 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
330 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
331 |
* |
0 | 332 |
* @param int $id User ID. |
333 |
* @param int $reassign Optional. Reassign posts and links to new User ID. |
|
334 |
* @return bool True when finished. |
|
335 |
*/ |
|
5 | 336 |
function wp_delete_user( $id, $reassign = null ) { |
0 | 337 |
global $wpdb; |
338 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
339 |
if ( ! is_numeric( $id ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
340 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
341 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
342 |
|
9 | 343 |
$id = (int) $id; |
0 | 344 |
$user = new WP_User( $id ); |
345 |
||
9 | 346 |
if ( ! $user->exists() ) { |
0 | 347 |
return false; |
9 | 348 |
} |
0 | 349 |
|
5 | 350 |
// Normalize $reassign to null or a user ID. 'novalue' was an older default. |
351 |
if ( 'novalue' === $reassign ) { |
|
352 |
$reassign = null; |
|
353 |
} elseif ( null !== $reassign ) { |
|
354 |
$reassign = (int) $reassign; |
|
355 |
} |
|
0 | 356 |
|
5 | 357 |
/** |
358 |
* Fires immediately before a user is deleted from the database. |
|
359 |
* |
|
360 |
* @since 2.0.0 |
|
16 | 361 |
* @since 5.5.0 Added the `$user` parameter. |
5 | 362 |
* |
363 |
* @param int $id ID of the user to delete. |
|
364 |
* @param int|null $reassign ID of the user to reassign posts and links to. |
|
365 |
* Default null, for no reassignment. |
|
16 | 366 |
* @param WP_User $user WP_User object of the user to delete. |
5 | 367 |
*/ |
16 | 368 |
do_action( 'delete_user', $id, $reassign, $user ); |
5 | 369 |
|
370 |
if ( null === $reassign ) { |
|
0 | 371 |
$post_types_to_delete = array(); |
372 |
foreach ( get_post_types( array(), 'objects' ) as $post_type ) { |
|
373 |
if ( $post_type->delete_with_user ) { |
|
374 |
$post_types_to_delete[] = $post_type->name; |
|
375 |
} elseif ( null === $post_type->delete_with_user && post_type_supports( $post_type->name, 'author' ) ) { |
|
376 |
$post_types_to_delete[] = $post_type->name; |
|
377 |
} |
|
378 |
} |
|
379 |
||
5 | 380 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
381 |
* Filters the list of post types to delete with a user. |
5 | 382 |
* |
383 |
* @since 3.4.0 |
|
384 |
* |
|
9 | 385 |
* @param string[] $post_types_to_delete Array of post types to delete. |
386 |
* @param int $id User ID. |
|
5 | 387 |
*/ |
0 | 388 |
$post_types_to_delete = apply_filters( 'post_types_to_delete_with_user', $post_types_to_delete, $id ); |
389 |
$post_types_to_delete = implode( "', '", $post_types_to_delete ); |
|
9 | 390 |
$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 ) ); |
0 | 391 |
if ( $post_ids ) { |
9 | 392 |
foreach ( $post_ids as $post_id ) { |
0 | 393 |
wp_delete_post( $post_id ); |
9 | 394 |
} |
0 | 395 |
} |
396 |
||
16 | 397 |
// Clean links. |
9 | 398 |
$link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) ); |
0 | 399 |
|
400 |
if ( $link_ids ) { |
|
9 | 401 |
foreach ( $link_ids as $link_id ) { |
402 |
wp_delete_link( $link_id ); |
|
403 |
} |
|
0 | 404 |
} |
405 |
} else { |
|
406 |
$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) ); |
|
9 | 407 |
$wpdb->update( $wpdb->posts, array( 'post_author' => $reassign ), array( 'post_author' => $id ) ); |
0 | 408 |
if ( ! empty( $post_ids ) ) { |
9 | 409 |
foreach ( $post_ids as $post_id ) { |
0 | 410 |
clean_post_cache( $post_id ); |
9 | 411 |
} |
0 | 412 |
} |
9 | 413 |
$link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) ); |
414 |
$wpdb->update( $wpdb->links, array( 'link_owner' => $reassign ), array( 'link_owner' => $id ) ); |
|
0 | 415 |
if ( ! empty( $link_ids ) ) { |
9 | 416 |
foreach ( $link_ids as $link_id ) { |
0 | 417 |
clean_bookmark_cache( $link_id ); |
9 | 418 |
} |
0 | 419 |
} |
420 |
} |
|
421 |
||
16 | 422 |
// FINALLY, delete user. |
0 | 423 |
if ( is_multisite() ) { |
424 |
remove_user_from_blog( $id, get_current_blog_id() ); |
|
425 |
} else { |
|
426 |
$meta = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d", $id ) ); |
|
9 | 427 |
foreach ( $meta as $mid ) { |
0 | 428 |
delete_metadata_by_mid( 'user', $mid ); |
9 | 429 |
} |
0 | 430 |
|
431 |
$wpdb->delete( $wpdb->users, array( 'ID' => $id ) ); |
|
432 |
} |
|
433 |
||
434 |
clean_user_cache( $user ); |
|
435 |
||
5 | 436 |
/** |
437 |
* Fires immediately after a user is deleted from the database. |
|
438 |
* |
|
439 |
* @since 2.9.0 |
|
16 | 440 |
* @since 5.5.0 Added the `$user` parameter. |
5 | 441 |
* |
442 |
* @param int $id ID of the deleted user. |
|
443 |
* @param int|null $reassign ID of the user to reassign posts and links to. |
|
444 |
* Default null, for no reassignment. |
|
16 | 445 |
* @param WP_User $user WP_User object of the deleted user. |
5 | 446 |
*/ |
16 | 447 |
do_action( 'deleted_user', $id, $reassign, $user ); |
0 | 448 |
|
449 |
return true; |
|
450 |
} |
|
451 |
||
452 |
/** |
|
453 |
* Remove all capabilities from user. |
|
454 |
* |
|
455 |
* @since 2.1.0 |
|
456 |
* |
|
457 |
* @param int $id User ID. |
|
458 |
*/ |
|
9 | 459 |
function wp_revoke_user( $id ) { |
0 | 460 |
$id = (int) $id; |
461 |
||
9 | 462 |
$user = new WP_User( $id ); |
0 | 463 |
$user->remove_all_caps(); |
464 |
} |
|
465 |
||
466 |
/** |
|
467 |
* @since 2.8.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
468 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
469 |
* @global int $user_ID |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
470 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
471 |
* @param false $errors Deprecated. |
0 | 472 |
*/ |
9 | 473 |
function default_password_nag_handler( $errors = false ) { |
0 | 474 |
global $user_ID; |
5 | 475 |
// Short-circuit it. |
9 | 476 |
if ( ! get_user_option( 'default_password_nag' ) ) { |
0 | 477 |
return; |
9 | 478 |
} |
0 | 479 |
|
16 | 480 |
// get_user_setting() = JS-saved UI setting. Else no-js-fallback code. |
481 |
if ( 'hide' === get_user_setting( 'default_password_nag' ) |
|
482 |
|| isset( $_GET['default_password_nag'] ) && '0' == $_GET['default_password_nag'] |
|
483 |
) { |
|
9 | 484 |
delete_user_setting( 'default_password_nag' ); |
18 | 485 |
update_user_meta( $user_ID, 'default_password_nag', false ); |
0 | 486 |
} |
487 |
} |
|
488 |
||
489 |
/** |
|
490 |
* @since 2.8.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
491 |
* |
18 | 492 |
* @param int $user_ID |
493 |
* @param WP_User $old_data |
|
0 | 494 |
*/ |
9 | 495 |
function default_password_nag_edit_user( $user_ID, $old_data ) { |
5 | 496 |
// Short-circuit it. |
9 | 497 |
if ( ! get_user_option( 'default_password_nag', $user_ID ) ) { |
0 | 498 |
return; |
9 | 499 |
} |
0 | 500 |
|
9 | 501 |
$new_data = get_userdata( $user_ID ); |
0 | 502 |
|
5 | 503 |
// Remove the nag if the password has been changed. |
504 |
if ( $new_data->user_pass != $old_data->user_pass ) { |
|
9 | 505 |
delete_user_setting( 'default_password_nag' ); |
18 | 506 |
update_user_meta( $user_ID, 'default_password_nag', false ); |
0 | 507 |
} |
508 |
} |
|
509 |
||
510 |
/** |
|
511 |
* @since 2.8.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
512 |
* |
19 | 513 |
* @global string $pagenow The filename of the current screen. |
0 | 514 |
*/ |
515 |
function default_password_nag() { |
|
516 |
global $pagenow; |
|
19 | 517 |
|
5 | 518 |
// Short-circuit it. |
16 | 519 |
if ( 'profile.php' === $pagenow || ! get_user_option( 'default_password_nag' ) ) { |
0 | 520 |
return; |
9 | 521 |
} |
0 | 522 |
|
523 |
echo '<div class="error default-password-nag">'; |
|
524 |
echo '<p>'; |
|
9 | 525 |
echo '<strong>' . __( 'Notice:' ) . '</strong> '; |
526 |
_e( 'You’re using the auto-generated password for your account. Would you like to change it?' ); |
|
0 | 527 |
echo '</p><p>'; |
9 | 528 |
printf( '<a href="%s">' . __( 'Yes, take me to my profile page' ) . '</a> | ', get_edit_profile_url() . '#password' ); |
529 |
printf( '<a href="%s" id="default-password-nag-no">' . __( 'No thanks, do not remind me again' ) . '</a>', '?default_password_nag=0' ); |
|
0 | 530 |
echo '</p></div>'; |
531 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
532 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
533 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
534 |
* @since 3.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
535 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
536 |
*/ |
9 | 537 |
function delete_users_add_js() { |
538 |
?> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
539 |
<script> |
19 | 540 |
jQuery( function($) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
541 |
var submit = $('#submit').prop('disabled', true); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
542 |
$('input[name="delete_option"]').one('change', function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
543 |
submit.prop('disabled', false); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
544 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
545 |
$('#reassign_user').focus( function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
546 |
$('#delete_option1').prop('checked', true).trigger('change'); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
547 |
}); |
19 | 548 |
} ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
549 |
</script> |
9 | 550 |
<?php |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
551 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
552 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
553 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
554 |
* Optional SSL preference that can be turned on by hooking to the 'personal_options' action. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
555 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
556 |
* See the {@see 'personal_options'} action. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
557 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
558 |
* @since 2.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
559 |
* |
16 | 560 |
* @param WP_User $user User data object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
561 |
*/ |
9 | 562 |
function use_ssl_preference( $user ) { |
563 |
?> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
564 |
<tr class="user-use-ssl-wrap"> |
9 | 565 |
<th scope="row"><?php _e( 'Use https' ); ?></th> |
566 |
<td><label for="use_ssl"><input name="use_ssl" type="checkbox" id="use_ssl" value="1" <?php checked( '1', $user->use_ssl ); ?> /> <?php _e( 'Always use https when visiting the admin' ); ?></label></td> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
567 |
</tr> |
9 | 568 |
<?php |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
569 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
570 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
571 |
/** |
16 | 572 |
* @since MU (3.0.0) |
573 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
574 |
* @param string $text |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
575 |
* @return string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
576 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
577 |
function admin_created_user_email( $text ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
578 |
$roles = get_editable_roles(); |
9 | 579 |
$role = $roles[ $_REQUEST['role'] ]; |
16 | 580 |
|
19 | 581 |
if ( '' !== get_bloginfo( 'name' ) ) { |
582 |
$site_title = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ); |
|
583 |
} else { |
|
584 |
$site_title = parse_url( home_url(), PHP_URL_HOST ); |
|
585 |
} |
|
586 |
||
9 | 587 |
return sprintf( |
16 | 588 |
/* translators: 1: Site title, 2: Site URL, 3: User role. */ |
9 | 589 |
__( |
590 |
'Hi, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
591 |
You\'ve been invited to join \'%1$s\' at |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
592 |
%2$s with the role of %3$s. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
593 |
If you do not want to join this site please ignore |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
594 |
this email. This invitation will expire in a few days. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
595 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
596 |
Please click the following link to activate your user account: |
9 | 597 |
%%s' |
598 |
), |
|
19 | 599 |
$site_title, |
9 | 600 |
home_url(), |
601 |
wp_specialchars_decode( translate_user_role( $role['name'] ) ) |
|
602 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
603 |
} |
18 | 604 |
|
605 |
/** |
|
606 |
* Checks if the Authorize Application Password request is valid. |
|
607 |
* |
|
608 |
* @since 5.6.0 |
|
609 |
* |
|
610 |
* @param array $request { |
|
611 |
* The array of request data. All arguments are optional and may be empty. |
|
612 |
* |
|
613 |
* @type string $app_name The suggested name of the application. |
|
19 | 614 |
* @type string $app_id A UUID provided by the application to uniquely identify it. |
615 |
* @type string $success_url The URL the user will be redirected to after approving the application. |
|
616 |
* @type string $reject_url The URL the user will be redirected to after rejecting the application. |
|
18 | 617 |
* } |
618 |
* @param WP_User $user The user authorizing the application. |
|
619 |
* @return true|WP_Error True if the request is valid, a WP_Error object contains errors if not. |
|
620 |
*/ |
|
621 |
function wp_is_authorize_application_password_request_valid( $request, $user ) { |
|
622 |
$error = new WP_Error(); |
|
623 |
||
624 |
if ( ! empty( $request['success_url'] ) ) { |
|
625 |
$scheme = wp_parse_url( $request['success_url'], PHP_URL_SCHEME ); |
|
626 |
||
627 |
if ( 'http' === $scheme ) { |
|
628 |
$error->add( |
|
629 |
'invalid_redirect_scheme', |
|
19 | 630 |
__( 'The success URL must be served over a secure connection.' ) |
18 | 631 |
); |
632 |
} |
|
633 |
} |
|
634 |
||
635 |
if ( ! empty( $request['reject_url'] ) ) { |
|
636 |
$scheme = wp_parse_url( $request['reject_url'], PHP_URL_SCHEME ); |
|
637 |
||
638 |
if ( 'http' === $scheme ) { |
|
639 |
$error->add( |
|
640 |
'invalid_redirect_scheme', |
|
19 | 641 |
__( 'The rejection URL must be served over a secure connection.' ) |
18 | 642 |
); |
643 |
} |
|
644 |
} |
|
645 |
||
646 |
if ( ! empty( $request['app_id'] ) && ! wp_is_uuid( $request['app_id'] ) ) { |
|
647 |
$error->add( |
|
648 |
'invalid_app_id', |
|
19 | 649 |
__( 'The application ID must be a UUID.' ) |
18 | 650 |
); |
651 |
} |
|
652 |
||
653 |
/** |
|
654 |
* Fires before application password errors are returned. |
|
655 |
* |
|
656 |
* @since 5.6.0 |
|
657 |
* |
|
658 |
* @param WP_Error $error The error object. |
|
659 |
* @param array $request The array of request data. |
|
660 |
* @param WP_User $user The user authorizing the application. |
|
661 |
*/ |
|
662 |
do_action( 'wp_authorize_application_password_request_errors', $error, $request, $user ); |
|
663 |
||
664 |
if ( $error->has_errors() ) { |
|
665 |
return $error; |
|
666 |
} |
|
667 |
||
668 |
return true; |
|
669 |
} |