PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/user.php

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