|
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 $current_user, $wp_roles; |
|
25 $user_id = (int) func_get_arg( 0 ); |
|
26 |
|
27 if ( isset( $_POST['role'] ) ) { |
|
28 // Don't let anyone with 'edit_users' (admins) edit their own role to something without it. |
|
29 if( $user_id != $current_user->id || $wp_roles->role_objects[$_POST['role']]->has_cap( 'edit_users' ) ) { |
|
30 // If the new role isn't editable by the logged-in user die with error |
|
31 $editable_roles = get_editable_roles(); |
|
32 if (!$editable_roles[$_POST['role']]) |
|
33 wp_die(__('You can’t give users that role.')); |
|
34 |
|
35 $user = new WP_User( $user_id ); |
|
36 $user->set_role( $_POST['role'] ); |
|
37 } |
|
38 } |
|
39 } else { |
|
40 add_action( 'user_register', 'add_user' ); // See above |
|
41 return edit_user(); |
|
42 } |
|
43 } |
|
44 |
|
45 /** |
|
46 * Edit user settings based on contents of $_POST |
|
47 * |
|
48 * Used on user-edit.php and profile.php to manage and process user options, passwords etc. |
|
49 * |
|
50 * @since 2.0 |
|
51 * |
|
52 * @param int $user_id Optional. User ID. |
|
53 * @return int user id of the updated user |
|
54 */ |
|
55 function edit_user( $user_id = 0 ) { |
|
56 global $current_user, $wp_roles, $wpdb; |
|
57 if ( $user_id != 0 ) { |
|
58 $update = true; |
|
59 $user->ID = (int) $user_id; |
|
60 $userdata = get_userdata( $user_id ); |
|
61 $user->user_login = $wpdb->escape( $userdata->user_login ); |
|
62 } else { |
|
63 $update = false; |
|
64 $user = ''; |
|
65 } |
|
66 |
|
67 if ( isset( $_POST['user_login'] )) |
|
68 $user->user_login = esc_html( trim( $_POST['user_login'] )); |
|
69 |
|
70 $pass1 = $pass2 = ''; |
|
71 if ( isset( $_POST['pass1'] )) |
|
72 $pass1 = $_POST['pass1']; |
|
73 if ( isset( $_POST['pass2'] )) |
|
74 $pass2 = $_POST['pass2']; |
|
75 |
|
76 if ( isset( $_POST['role'] ) && current_user_can( 'edit_users' ) ) { |
|
77 |
|
78 // Don't let anyone with 'edit_users' (admins) edit their own role to something without it. |
|
79 if( $user_id != $current_user->id || $wp_roles->role_objects[$_POST['role']]->has_cap( 'edit_users' )) |
|
80 $user->role = $_POST['role']; |
|
81 |
|
82 // If the new role isn't editable by the logged-in user die with error |
|
83 $editable_roles = get_editable_roles(); |
|
84 if (!$editable_roles[$_POST['role']]) |
|
85 wp_die(__('You can’t give users that role.')); |
|
86 } |
|
87 |
|
88 if ( isset( $_POST['email'] )) |
|
89 $user->user_email = esc_html( trim( $_POST['email'] )); |
|
90 if ( isset( $_POST['url'] ) ) { |
|
91 if ( empty ( $_POST['url'] ) || $_POST['url'] == 'http://' ) { |
|
92 $user->user_url = ''; |
|
93 } else { |
|
94 $user->user_url = esc_url( trim( $_POST['url'] )); |
|
95 $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; |
|
96 } |
|
97 } |
|
98 if ( isset( $_POST['first_name'] )) |
|
99 $user->first_name = esc_html( trim( $_POST['first_name'] )); |
|
100 if ( isset( $_POST['last_name'] )) |
|
101 $user->last_name = esc_html( trim( $_POST['last_name'] )); |
|
102 if ( isset( $_POST['nickname'] )) |
|
103 $user->nickname = esc_html( trim( $_POST['nickname'] )); |
|
104 if ( isset( $_POST['display_name'] )) |
|
105 $user->display_name = esc_html( trim( $_POST['display_name'] )); |
|
106 if ( isset( $_POST['description'] )) |
|
107 $user->description = trim( $_POST['description'] ); |
|
108 if ( isset( $_POST['jabber'] )) |
|
109 $user->jabber = esc_html( trim( $_POST['jabber'] )); |
|
110 if ( isset( $_POST['aim'] )) |
|
111 $user->aim = esc_html( trim( $_POST['aim'] )); |
|
112 if ( isset( $_POST['yim'] )) |
|
113 $user->yim = esc_html( trim( $_POST['yim'] )); |
|
114 if ( !$update ) |
|
115 $user->rich_editing = 'true'; // Default to true for new users. |
|
116 else if ( isset( $_POST['rich_editing'] ) ) |
|
117 $user->rich_editing = $_POST['rich_editing']; |
|
118 else |
|
119 $user->rich_editing = 'true'; |
|
120 |
|
121 $user->comment_shortcuts = isset( $_POST['comment_shortcuts'] )? $_POST['comment_shortcuts'] : ''; |
|
122 |
|
123 $user->use_ssl = 0; |
|
124 if ( !empty($_POST['use_ssl']) ) |
|
125 $user->use_ssl = 1; |
|
126 |
|
127 if ( !$update ) |
|
128 $user->admin_color = 'fresh'; // Default to fresh for new users. |
|
129 else if ( isset( $_POST['admin_color'] ) ) |
|
130 $user->admin_color = $_POST['admin_color']; |
|
131 else |
|
132 $user->admin_color = 'fresh'; |
|
133 |
|
134 $errors = new WP_Error(); |
|
135 |
|
136 /* checking that username has been typed */ |
|
137 if ( $user->user_login == '' ) |
|
138 $errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' )); |
|
139 |
|
140 /* checking the password has been typed twice */ |
|
141 do_action_ref_array( 'check_passwords', array ( $user->user_login, & $pass1, & $pass2 )); |
|
142 |
|
143 if ( $update ) { |
|
144 if ( empty($pass1) && !empty($pass2) ) |
|
145 $errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass1' ) ); |
|
146 elseif ( !empty($pass1) && empty($pass2) ) |
|
147 $errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass2' ) ); |
|
148 } else { |
|
149 if ( empty($pass1) ) |
|
150 $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password.' ), array( 'form-field' => 'pass1' ) ); |
|
151 elseif ( empty($pass2) ) |
|
152 $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password twice.' ), array( 'form-field' => 'pass2' ) ); |
|
153 } |
|
154 |
|
155 /* Check for "\" in password */ |
|
156 if ( false !== strpos( stripslashes($pass1), "\\" ) ) |
|
157 $errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) ); |
|
158 |
|
159 /* checking the password has been typed twice the same */ |
|
160 if ( $pass1 != $pass2 ) |
|
161 $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter the same password in the two password fields.' ), array( 'form-field' => 'pass1' ) ); |
|
162 |
|
163 if (!empty ( $pass1 )) |
|
164 $user->user_pass = $pass1; |
|
165 |
|
166 if ( !$update && !validate_username( $user->user_login ) ) |
|
167 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid. Please enter a valid username.' )); |
|
168 |
|
169 if (!$update && username_exists( $user->user_login )) |
|
170 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' )); |
|
171 |
|
172 /* checking e-mail address */ |
|
173 if ( empty ( $user->user_email ) ) { |
|
174 $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please enter an e-mail address.' ), array( 'form-field' => 'email' ) ); |
|
175 } elseif (!is_email( $user->user_email ) ) { |
|
176 $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The e-mail address isn’t correct.' ), array( 'form-field' => 'email' ) ); |
|
177 } elseif ( ( $owner_id = email_exists($user->user_email) ) && $owner_id != $user->ID ) { |
|
178 $errors->add( 'email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'), array( 'form-field' => 'email' ) ); |
|
179 } |
|
180 |
|
181 // Allow plugins to return there own errors. |
|
182 do_action_ref_array('user_profile_update_errors', array ( &$errors, $update, &$user ) ); |
|
183 |
|
184 if ( $errors->get_error_codes() ) |
|
185 return $errors; |
|
186 |
|
187 if ( $update ) { |
|
188 $user_id = wp_update_user( get_object_vars( $user )); |
|
189 } else { |
|
190 $user_id = wp_insert_user( get_object_vars( $user )); |
|
191 wp_new_user_notification( $user_id, isset($_POST['send_password']) ? $pass1 : '' ); |
|
192 } |
|
193 return $user_id; |
|
194 } |
|
195 |
|
196 /** |
|
197 * {@internal Missing Short Description}} |
|
198 * |
|
199 * {@internal Missing Long Description}} |
|
200 * |
|
201 * @since unknown |
|
202 * |
|
203 * @return array List of user IDs. |
|
204 */ |
|
205 function get_author_user_ids() { |
|
206 global $wpdb; |
|
207 $level_key = $wpdb->prefix . 'user_level'; |
|
208 return $wpdb->get_col( $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value != '0'", $level_key) ); |
|
209 } |
|
210 |
|
211 /** |
|
212 * {@internal Missing Short Description}} |
|
213 * |
|
214 * {@internal Missing Long Description}} |
|
215 * |
|
216 * @since unknown |
|
217 * |
|
218 * @param int $user_id User ID. |
|
219 * @return array|bool List of editable authors. False if no editable users. |
|
220 */ |
|
221 function get_editable_authors( $user_id ) { |
|
222 global $wpdb; |
|
223 |
|
224 $editable = get_editable_user_ids( $user_id ); |
|
225 |
|
226 if( !$editable ) { |
|
227 return false; |
|
228 } else { |
|
229 $editable = join(',', $editable); |
|
230 $authors = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($editable) ORDER BY display_name" ); |
|
231 } |
|
232 |
|
233 return apply_filters('get_editable_authors', $authors); |
|
234 } |
|
235 |
|
236 /** |
|
237 * {@internal Missing Short Description}} |
|
238 * |
|
239 * {@internal Missing Long Description}} |
|
240 * |
|
241 * @since unknown |
|
242 * |
|
243 * @param int $user_id User ID. |
|
244 * @param bool $exclude_zeros Optional, default is true. Whether to exclude zeros. |
|
245 * @return unknown |
|
246 */ |
|
247 function get_editable_user_ids( $user_id, $exclude_zeros = true, $post_type = 'post' ) { |
|
248 global $wpdb; |
|
249 |
|
250 $user = new WP_User( $user_id ); |
|
251 |
|
252 if ( ! $user->has_cap("edit_others_{$post_type}s") ) { |
|
253 if ( $user->has_cap("edit_{$post_type}s") || $exclude_zeros == false ) |
|
254 return array($user->id); |
|
255 else |
|
256 return array(); |
|
257 } |
|
258 |
|
259 $level_key = $wpdb->prefix . 'user_level'; |
|
260 |
|
261 $query = $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s", $level_key); |
|
262 if ( $exclude_zeros ) |
|
263 $query .= " AND meta_value != '0'"; |
|
264 |
|
265 return $wpdb->get_col( $query ); |
|
266 } |
|
267 |
|
268 /** |
|
269 * Fetch a filtered list of user roles that the current user is |
|
270 * allowed to edit. |
|
271 * |
|
272 * Simple function who's main purpose is to allow filtering of the |
|
273 * list of roles in the $wp_roles object so that plugins can remove |
|
274 * innappropriate ones depending on the situation or user making edits. |
|
275 * Specifically because without filtering anyone with the edit_users |
|
276 * capability can edit others to be administrators, even if they are |
|
277 * only editors or authors. This filter allows admins to delegate |
|
278 * user management. |
|
279 * |
|
280 * @since 2.8 |
|
281 * |
|
282 * @return unknown |
|
283 */ |
|
284 function get_editable_roles() { |
|
285 global $wp_roles; |
|
286 |
|
287 $all_roles = $wp_roles->roles; |
|
288 $editable_roles = apply_filters('editable_roles', $all_roles); |
|
289 |
|
290 return $editable_roles; |
|
291 } |
|
292 |
|
293 /** |
|
294 * {@internal Missing Short Description}} |
|
295 * |
|
296 * {@internal Missing Long Description}} |
|
297 * |
|
298 * @since unknown |
|
299 * |
|
300 * @return unknown |
|
301 */ |
|
302 function get_nonauthor_user_ids() { |
|
303 global $wpdb; |
|
304 $level_key = $wpdb->prefix . 'user_level'; |
|
305 |
|
306 return $wpdb->get_col( $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value = '0'", $level_key) ); |
|
307 } |
|
308 |
|
309 /** |
|
310 * Retrieve editable posts from other users. |
|
311 * |
|
312 * @since unknown |
|
313 * |
|
314 * @param int $user_id User ID to not retrieve posts from. |
|
315 * @param string $type Optional, defaults to 'any'. Post type to retrieve, can be 'draft' or 'pending'. |
|
316 * @return array List of posts from others. |
|
317 */ |
|
318 function get_others_unpublished_posts($user_id, $type='any') { |
|
319 global $wpdb; |
|
320 |
|
321 $editable = get_editable_user_ids( $user_id ); |
|
322 |
|
323 if ( in_array($type, array('draft', 'pending')) ) |
|
324 $type_sql = " post_status = '$type' "; |
|
325 else |
|
326 $type_sql = " ( post_status = 'draft' OR post_status = 'pending' ) "; |
|
327 |
|
328 $dir = ( 'pending' == $type ) ? 'ASC' : 'DESC'; |
|
329 |
|
330 if( !$editable ) { |
|
331 $other_unpubs = ''; |
|
332 } else { |
|
333 $editable = join(',', $editable); |
|
334 $other_unpubs = $wpdb->get_results( $wpdb->prepare("SELECT ID, post_title, post_author FROM $wpdb->posts WHERE post_type = 'post' AND $type_sql AND post_author IN ($editable) AND post_author != %d ORDER BY post_modified $dir", $user_id) ); |
|
335 } |
|
336 |
|
337 return apply_filters('get_others_drafts', $other_unpubs); |
|
338 } |
|
339 |
|
340 /** |
|
341 * Retrieve drafts from other users. |
|
342 * |
|
343 * @since unknown |
|
344 * |
|
345 * @param int $user_id User ID. |
|
346 * @return array List of drafts from other users. |
|
347 */ |
|
348 function get_others_drafts($user_id) { |
|
349 return get_others_unpublished_posts($user_id, 'draft'); |
|
350 } |
|
351 |
|
352 /** |
|
353 * Retrieve pending review posts from other users. |
|
354 * |
|
355 * @since unknown |
|
356 * |
|
357 * @param int $user_id User ID. |
|
358 * @return array List of posts with pending review post type from other users. |
|
359 */ |
|
360 function get_others_pending($user_id) { |
|
361 return get_others_unpublished_posts($user_id, 'pending'); |
|
362 } |
|
363 |
|
364 /** |
|
365 * Retrieve user data and filter it. |
|
366 * |
|
367 * @since unknown |
|
368 * |
|
369 * @param int $user_id User ID. |
|
370 * @return object WP_User object with user data. |
|
371 */ |
|
372 function get_user_to_edit( $user_id ) { |
|
373 $user = new WP_User( $user_id ); |
|
374 $user->user_login = esc_attr($user->user_login); |
|
375 $user->user_email = esc_attr($user->user_email); |
|
376 $user->user_url = esc_url($user->user_url); |
|
377 $user->first_name = esc_attr($user->first_name); |
|
378 $user->last_name = esc_attr($user->last_name); |
|
379 $user->display_name = esc_attr($user->display_name); |
|
380 $user->nickname = esc_attr($user->nickname); |
|
381 $user->aim = isset( $user->aim ) && !empty( $user->aim ) ? esc_attr($user->aim) : ''; |
|
382 $user->yim = isset( $user->yim ) && !empty( $user->yim ) ? esc_attr($user->yim) : ''; |
|
383 $user->jabber = isset( $user->jabber ) && !empty( $user->jabber ) ? esc_attr($user->jabber) : ''; |
|
384 $user->description = isset( $user->description ) && !empty( $user->description ) ? esc_html($user->description) : ''; |
|
385 |
|
386 return $user; |
|
387 } |
|
388 |
|
389 /** |
|
390 * Retrieve the user's drafts. |
|
391 * |
|
392 * @since unknown |
|
393 * |
|
394 * @param int $user_id User ID. |
|
395 * @return array |
|
396 */ |
|
397 function get_users_drafts( $user_id ) { |
|
398 global $wpdb; |
|
399 $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); |
|
400 $query = apply_filters('get_users_drafts', $query); |
|
401 return $wpdb->get_results( $query ); |
|
402 } |
|
403 |
|
404 /** |
|
405 * Remove user and optionally reassign posts and links to another user. |
|
406 * |
|
407 * If the $reassign parameter is not assigned to an User ID, then all posts will |
|
408 * be deleted of that user. The action 'delete_user' that is passed the User ID |
|
409 * being deleted will be run after the posts are either reassigned or deleted. |
|
410 * The user meta will also be deleted that are for that User ID. |
|
411 * |
|
412 * @since unknown |
|
413 * |
|
414 * @param int $id User ID. |
|
415 * @param int $reassign Optional. Reassign posts and links to new User ID. |
|
416 * @return bool True when finished. |
|
417 */ |
|
418 function wp_delete_user($id, $reassign = 'novalue') { |
|
419 global $wpdb; |
|
420 |
|
421 $id = (int) $id; |
|
422 $user = new WP_User($id); |
|
423 |
|
424 // allow for transaction statement |
|
425 do_action('delete_user', $id); |
|
426 |
|
427 if ($reassign == 'novalue') { |
|
428 $post_ids = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id) ); |
|
429 |
|
430 if ($post_ids) { |
|
431 foreach ($post_ids as $post_id) |
|
432 wp_delete_post($post_id); |
|
433 } |
|
434 |
|
435 // Clean links |
|
436 $link_ids = $wpdb->get_col( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id) ); |
|
437 |
|
438 if ( $link_ids ) { |
|
439 foreach ( $link_ids as $link_id ) |
|
440 wp_delete_link($link_id); |
|
441 } |
|
442 |
|
443 } else { |
|
444 $reassign = (int) $reassign; |
|
445 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $id) ); |
|
446 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $id) ); |
|
447 } |
|
448 |
|
449 // FINALLY, delete user |
|
450 |
|
451 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d", $id) ); |
|
452 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->users WHERE ID = %d", $id) ); |
|
453 |
|
454 wp_cache_delete($id, 'users'); |
|
455 wp_cache_delete($user->user_login, 'userlogins'); |
|
456 wp_cache_delete($user->user_email, 'useremail'); |
|
457 wp_cache_delete($user->user_nicename, 'userslugs'); |
|
458 |
|
459 // allow for commit transaction |
|
460 do_action('deleted_user', $id); |
|
461 |
|
462 return true; |
|
463 } |
|
464 |
|
465 /** |
|
466 * Remove all capabilities from user. |
|
467 * |
|
468 * @since unknown |
|
469 * |
|
470 * @param int $id User ID. |
|
471 */ |
|
472 function wp_revoke_user($id) { |
|
473 $id = (int) $id; |
|
474 |
|
475 $user = new WP_User($id); |
|
476 $user->remove_all_caps(); |
|
477 } |
|
478 |
|
479 if ( !class_exists('WP_User_Search') ) : |
|
480 /** |
|
481 * WordPress User Search class. |
|
482 * |
|
483 * @since unknown |
|
484 * @author Mark Jaquith |
|
485 */ |
|
486 class WP_User_Search { |
|
487 |
|
488 /** |
|
489 * {@internal Missing Description}} |
|
490 * |
|
491 * @since unknown |
|
492 * @access private |
|
493 * @var unknown_type |
|
494 */ |
|
495 var $results; |
|
496 |
|
497 /** |
|
498 * {@internal Missing Description}} |
|
499 * |
|
500 * @since unknown |
|
501 * @access private |
|
502 * @var unknown_type |
|
503 */ |
|
504 var $search_term; |
|
505 |
|
506 /** |
|
507 * Page number. |
|
508 * |
|
509 * @since unknown |
|
510 * @access private |
|
511 * @var int |
|
512 */ |
|
513 var $page; |
|
514 |
|
515 /** |
|
516 * Role name that users have. |
|
517 * |
|
518 * @since unknown |
|
519 * @access private |
|
520 * @var string |
|
521 */ |
|
522 var $role; |
|
523 |
|
524 /** |
|
525 * Raw page number. |
|
526 * |
|
527 * @since unknown |
|
528 * @access private |
|
529 * @var int|bool |
|
530 */ |
|
531 var $raw_page; |
|
532 |
|
533 /** |
|
534 * Amount of users to display per page. |
|
535 * |
|
536 * @since unknown |
|
537 * @access public |
|
538 * @var int |
|
539 */ |
|
540 var $users_per_page = 50; |
|
541 |
|
542 /** |
|
543 * {@internal Missing Description}} |
|
544 * |
|
545 * @since unknown |
|
546 * @access private |
|
547 * @var unknown_type |
|
548 */ |
|
549 var $first_user; |
|
550 |
|
551 /** |
|
552 * {@internal Missing Description}} |
|
553 * |
|
554 * @since unknown |
|
555 * @access private |
|
556 * @var int |
|
557 */ |
|
558 var $last_user; |
|
559 |
|
560 /** |
|
561 * {@internal Missing Description}} |
|
562 * |
|
563 * @since unknown |
|
564 * @access private |
|
565 * @var unknown_type |
|
566 */ |
|
567 var $query_limit; |
|
568 |
|
569 /** |
|
570 * {@internal Missing Description}} |
|
571 * |
|
572 * @since unknown |
|
573 * @access private |
|
574 * @var unknown_type |
|
575 */ |
|
576 var $query_sort; |
|
577 |
|
578 /** |
|
579 * {@internal Missing Description}} |
|
580 * |
|
581 * @since unknown |
|
582 * @access private |
|
583 * @var unknown_type |
|
584 */ |
|
585 var $query_from_where; |
|
586 |
|
587 /** |
|
588 * {@internal Missing Description}} |
|
589 * |
|
590 * @since unknown |
|
591 * @access private |
|
592 * @var int |
|
593 */ |
|
594 var $total_users_for_query = 0; |
|
595 |
|
596 /** |
|
597 * {@internal Missing Description}} |
|
598 * |
|
599 * @since unknown |
|
600 * @access private |
|
601 * @var bool |
|
602 */ |
|
603 var $too_many_total_users = false; |
|
604 |
|
605 /** |
|
606 * {@internal Missing Description}} |
|
607 * |
|
608 * @since unknown |
|
609 * @access private |
|
610 * @var unknown_type |
|
611 */ |
|
612 var $search_errors; |
|
613 |
|
614 /** |
|
615 * {@internal Missing Description}} |
|
616 * |
|
617 * @since unknown |
|
618 * @access private |
|
619 * @var unknown_type |
|
620 */ |
|
621 var $paging_text; |
|
622 |
|
623 /** |
|
624 * PHP4 Constructor - Sets up the object properties. |
|
625 * |
|
626 * @since unknown |
|
627 * |
|
628 * @param string $search_term Search terms string. |
|
629 * @param int $page Optional. Page ID. |
|
630 * @param string $role Role name. |
|
631 * @return WP_User_Search |
|
632 */ |
|
633 function WP_User_Search ($search_term = '', $page = '', $role = '') { |
|
634 $this->search_term = $search_term; |
|
635 $this->raw_page = ( '' == $page ) ? false : (int) $page; |
|
636 $this->page = (int) ( '' == $page ) ? 1 : $page; |
|
637 $this->role = $role; |
|
638 |
|
639 $this->prepare_query(); |
|
640 $this->query(); |
|
641 $this->prepare_vars_for_template_usage(); |
|
642 $this->do_paging(); |
|
643 } |
|
644 |
|
645 /** |
|
646 * {@internal Missing Short Description}} |
|
647 * |
|
648 * {@internal Missing Long Description}} |
|
649 * |
|
650 * @since unknown |
|
651 * @access public |
|
652 */ |
|
653 function prepare_query() { |
|
654 global $wpdb; |
|
655 $this->first_user = ($this->page - 1) * $this->users_per_page; |
|
656 $this->query_limit = $wpdb->prepare(" LIMIT %d, %d", $this->first_user, $this->users_per_page); |
|
657 $this->query_sort = ' ORDER BY user_login'; |
|
658 $search_sql = ''; |
|
659 if ( $this->search_term ) { |
|
660 $searches = array(); |
|
661 $search_sql = 'AND ('; |
|
662 foreach ( array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') as $col ) |
|
663 $searches[] = $col . " LIKE '%$this->search_term%'"; |
|
664 $search_sql .= implode(' OR ', $searches); |
|
665 $search_sql .= ')'; |
|
666 } |
|
667 |
|
668 $this->query_from_where = "FROM $wpdb->users"; |
|
669 if ( $this->role ) |
|
670 $this->query_from_where .= $wpdb->prepare(" INNER JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id WHERE $wpdb->usermeta.meta_key = '{$wpdb->prefix}capabilities' AND $wpdb->usermeta.meta_value LIKE %s", '%' . $this->role . '%'); |
|
671 else |
|
672 $this->query_from_where .= " WHERE 1=1"; |
|
673 $this->query_from_where .= " $search_sql"; |
|
674 |
|
675 } |
|
676 |
|
677 /** |
|
678 * {@internal Missing Short Description}} |
|
679 * |
|
680 * {@internal Missing Long Description}} |
|
681 * |
|
682 * @since unknown |
|
683 * @access public |
|
684 */ |
|
685 function query() { |
|
686 global $wpdb; |
|
687 $this->results = $wpdb->get_col('SELECT ID ' . $this->query_from_where . $this->query_sort . $this->query_limit); |
|
688 |
|
689 if ( $this->results ) |
|
690 $this->total_users_for_query = $wpdb->get_var('SELECT COUNT(ID) ' . $this->query_from_where); // no limit |
|
691 else |
|
692 $this->search_errors = new WP_Error('no_matching_users_found', __('No matching users were found!')); |
|
693 } |
|
694 |
|
695 /** |
|
696 * {@internal Missing Short Description}} |
|
697 * |
|
698 * {@internal Missing Long Description}} |
|
699 * |
|
700 * @since unknown |
|
701 * @access public |
|
702 */ |
|
703 function prepare_vars_for_template_usage() { |
|
704 $this->search_term = stripslashes($this->search_term); // done with DB, from now on we want slashes gone |
|
705 } |
|
706 |
|
707 /** |
|
708 * {@internal Missing Short Description}} |
|
709 * |
|
710 * {@internal Missing Long Description}} |
|
711 * |
|
712 * @since unknown |
|
713 * @access public |
|
714 */ |
|
715 function do_paging() { |
|
716 if ( $this->total_users_for_query > $this->users_per_page ) { // have to page the results |
|
717 $args = array(); |
|
718 if( ! empty($this->search_term) ) |
|
719 $args['usersearch'] = urlencode($this->search_term); |
|
720 if( ! empty($this->role) ) |
|
721 $args['role'] = urlencode($this->role); |
|
722 |
|
723 $this->paging_text = paginate_links( array( |
|
724 'total' => ceil($this->total_users_for_query / $this->users_per_page), |
|
725 'current' => $this->page, |
|
726 'base' => 'users.php?%_%', |
|
727 'format' => 'userspage=%#%', |
|
728 'add_args' => $args |
|
729 ) ); |
|
730 if ( $this->paging_text ) { |
|
731 $this->paging_text = sprintf( '<span class="displaying-num">' . __( 'Displaying %s–%s of %s' ) . '</span>%s', |
|
732 number_format_i18n( ( $this->page - 1 ) * $this->users_per_page + 1 ), |
|
733 number_format_i18n( min( $this->page * $this->users_per_page, $this->total_users_for_query ) ), |
|
734 number_format_i18n( $this->total_users_for_query ), |
|
735 $this->paging_text |
|
736 ); |
|
737 } |
|
738 } |
|
739 } |
|
740 |
|
741 /** |
|
742 * {@internal Missing Short Description}} |
|
743 * |
|
744 * {@internal Missing Long Description}} |
|
745 * |
|
746 * @since unknown |
|
747 * @access public |
|
748 * |
|
749 * @return unknown |
|
750 */ |
|
751 function get_results() { |
|
752 return (array) $this->results; |
|
753 } |
|
754 |
|
755 /** |
|
756 * Displaying paging text. |
|
757 * |
|
758 * @see do_paging() Builds paging text. |
|
759 * |
|
760 * @since unknown |
|
761 * @access public |
|
762 */ |
|
763 function page_links() { |
|
764 echo $this->paging_text; |
|
765 } |
|
766 |
|
767 /** |
|
768 * Whether paging is enabled. |
|
769 * |
|
770 * @see do_paging() Builds paging text. |
|
771 * |
|
772 * @since unknown |
|
773 * @access public |
|
774 * |
|
775 * @return bool |
|
776 */ |
|
777 function results_are_paged() { |
|
778 if ( $this->paging_text ) |
|
779 return true; |
|
780 return false; |
|
781 } |
|
782 |
|
783 /** |
|
784 * Whether there are search terms. |
|
785 * |
|
786 * @since unknown |
|
787 * @access public |
|
788 * |
|
789 * @return bool |
|
790 */ |
|
791 function is_search() { |
|
792 if ( $this->search_term ) |
|
793 return true; |
|
794 return false; |
|
795 } |
|
796 } |
|
797 endif; |
|
798 |
|
799 add_action('admin_init', 'default_password_nag_handler'); |
|
800 function default_password_nag_handler($errors = false) { |
|
801 global $user_ID; |
|
802 if ( ! get_usermeta($user_ID, 'default_password_nag') ) //Short circuit it. |
|
803 return; |
|
804 |
|
805 //get_user_setting = JS saved UI setting. else no-js-falback code. |
|
806 if ( 'hide' == get_user_setting('default_password_nag') || isset($_GET['default_password_nag']) && '0' == $_GET['default_password_nag'] ) { |
|
807 delete_user_setting('default_password_nag'); |
|
808 update_usermeta($user_ID, 'default_password_nag', false); |
|
809 } |
|
810 } |
|
811 |
|
812 add_action('profile_update', 'default_password_nag_edit_user', 10, 2); |
|
813 function default_password_nag_edit_user($user_ID, $old_data) { |
|
814 global $user_ID; |
|
815 if ( ! get_usermeta($user_ID, 'default_password_nag') ) //Short circuit it. |
|
816 return; |
|
817 |
|
818 $new_data = get_userdata($user_ID); |
|
819 |
|
820 if ( $new_data->user_pass != $old_data->user_pass ) { //Remove the nag if the password has been changed. |
|
821 delete_user_setting('default_password_nag'); |
|
822 update_usermeta($user_ID, 'default_password_nag', false); |
|
823 } |
|
824 } |
|
825 |
|
826 add_action('admin_notices', 'default_password_nag'); |
|
827 function default_password_nag() { |
|
828 global $user_ID; |
|
829 if ( ! get_usermeta($user_ID, 'default_password_nag') ) |
|
830 return; |
|
831 |
|
832 echo '<div class="error default-password-nag"><p>'; |
|
833 printf(__("Notice: you're using the auto-generated password for your account. Would you like to change it to something you'll remember easier?<br /> |
|
834 <a href='%s'>Yes, Take me to my profile page</a> | <a href='%s' id='default-password-nag-no'>No Thanks, Do not remind me again.</a>"), admin_url('profile.php') . '#password', '?default_password_nag=0'); |
|
835 echo '</p></div>'; |
|
836 } |
|
837 |
|
838 ?> |