WP_User_Search

WordPress User Search class.

Defined (1)

The class is defined in the following location(s).

/wp-admin/includes/deprecated.php  
  1. class WP_User_Search { 
  2.  
  3. /** 
  4. * {@internal Missing Description}} 
  5. * @since 2.1.0 
  6. * @access private 
  7. * @var mixed 
  8. */ 
  9. var $results; 
  10.  
  11. /** 
  12. * {@internal Missing Description}} 
  13. * @since 2.1.0 
  14. * @access private 
  15. * @var string 
  16. */ 
  17. var $search_term; 
  18.  
  19. /** 
  20. * Page number. 
  21. * @since 2.1.0 
  22. * @access private 
  23. * @var int 
  24. */ 
  25. var $page; 
  26.  
  27. /** 
  28. * Role name that users have. 
  29. * @since 2.5.0 
  30. * @access private 
  31. * @var string 
  32. */ 
  33. var $role; 
  34.  
  35. /** 
  36. * Raw page number. 
  37. * @since 2.1.0 
  38. * @access private 
  39. * @var int|bool 
  40. */ 
  41. var $raw_page; 
  42.  
  43. /** 
  44. * Amount of users to display per page. 
  45. * @since 2.1.0 
  46. * @access public 
  47. * @var int 
  48. */ 
  49. var $users_per_page = 50; 
  50.  
  51. /** 
  52. * {@internal Missing Description}} 
  53. * @since 2.1.0 
  54. * @access private 
  55. * @var int 
  56. */ 
  57. var $first_user; 
  58.  
  59. /** 
  60. * {@internal Missing Description}} 
  61. * @since 2.1.0 
  62. * @access private 
  63. * @var int 
  64. */ 
  65. var $last_user; 
  66.  
  67. /** 
  68. * {@internal Missing Description}} 
  69. * @since 2.1.0 
  70. * @access private 
  71. * @var string 
  72. */ 
  73. var $query_limit; 
  74.  
  75. /** 
  76. * {@internal Missing Description}} 
  77. * @since 3.0.0 
  78. * @access private 
  79. * @var string 
  80. */ 
  81. var $query_orderby; 
  82.  
  83. /** 
  84. * {@internal Missing Description}} 
  85. * @since 3.0.0 
  86. * @access private 
  87. * @var string 
  88. */ 
  89. var $query_from; 
  90.  
  91. /** 
  92. * {@internal Missing Description}} 
  93. * @since 3.0.0 
  94. * @access private 
  95. * @var string 
  96. */ 
  97. var $query_where; 
  98.  
  99. /** 
  100. * {@internal Missing Description}} 
  101. * @since 2.1.0 
  102. * @access private 
  103. * @var int 
  104. */ 
  105. var $total_users_for_query = 0; 
  106.  
  107. /** 
  108. * {@internal Missing Description}} 
  109. * @since 2.1.0 
  110. * @access private 
  111. * @var bool 
  112. */ 
  113. var $too_many_total_users = false; 
  114.  
  115. /** 
  116. * {@internal Missing Description}} 
  117. * @since 2.1.0 
  118. * @access private 
  119. * @var WP_Error 
  120. */ 
  121. var $search_errors; 
  122.  
  123. /** 
  124. * {@internal Missing Description}} 
  125. * @since 2.7.0 
  126. * @access private 
  127. * @var string 
  128. */ 
  129. var $paging_text; 
  130.  
  131. /** 
  132. * PHP5 Constructor - Sets up the object properties. 
  133. * @since 2.1.0 
  134. * @param string $search_term Search terms string. 
  135. * @param int $page Optional. Page ID. 
  136. * @param string $role Role name. 
  137. * @return WP_User_Search 
  138. */ 
  139. function __construct( $search_term = '', $page = '', $role = '' ) { 
  140. _deprecated_function( __FUNCTION__, '3.1.0', 'WP_User_Query' ); 
  141.  
  142. $this->search_term = wp_unslash( $search_term ); 
  143. $this->raw_page = ( '' == $page ) ? false : (int) $page; 
  144. $this->page = (int) ( '' == $page ) ? 1 : $page; 
  145. $this->role = $role; 
  146.  
  147. $this->prepare_query(); 
  148. $this->query(); 
  149. $this->do_paging(); 
  150.  
  151. /** 
  152. * PHP4 Constructor - Sets up the object properties. 
  153. * @since 2.1.0 
  154. * @param string $search_term Search terms string. 
  155. * @param int $page Optional. Page ID. 
  156. * @param string $role Role name. 
  157. * @return WP_User_Search 
  158. */ 
  159. public function WP_User_Search( $search_term = '', $page = '', $role = '' ) { 
  160. self::__construct( $search_term, $page, $role ); 
  161.  
  162. /** 
  163. * Prepares the user search query (legacy). 
  164. * @since 2.1.0 
  165. * @access public 
  166. */ 
  167. public function prepare_query() { 
  168. global $wpdb; 
  169. $this->first_user = ($this->page - 1) * $this->users_per_page; 
  170.  
  171. $this->query_limit = $wpdb->prepare(" LIMIT %d, %d", $this->first_user, $this->users_per_page); 
  172. $this->query_orderby = ' ORDER BY user_login'; 
  173.  
  174. $search_sql = ''; 
  175. if ( $this->search_term ) { 
  176. $searches = array(); 
  177. $search_sql = 'AND ('; 
  178. foreach ( array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') as $col ) 
  179. $searches[] = $wpdb->prepare( $col . ' LIKE %s', '%' . like_escape($this->search_term) . '%' ); 
  180. $search_sql .= implode(' OR ', $searches); 
  181. $search_sql .= ')'; 
  182.  
  183. $this->query_from = " FROM $wpdb->users"; 
  184. $this->query_where = " WHERE 1=1 $search_sql"; 
  185.  
  186. if ( $this->role ) { 
  187. $this->query_from .= " INNER JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id"; 
  188. $this->query_where .= $wpdb->prepare(" AND $wpdb->usermeta.meta_key = '{$wpdb->prefix}capabilities' AND $wpdb->usermeta.meta_value LIKE %s", '%' . $this->role . '%'); 
  189. } elseif ( is_multisite() ) { 
  190. $level_key = $wpdb->prefix . 'capabilities'; // wpmu site admins don't have user_levels 
  191. $this->query_from .= ", $wpdb->usermeta"; 
  192. $this->query_where .= " AND $wpdb->users.ID = $wpdb->usermeta.user_id AND meta_key = '{$level_key}'"; 
  193.  
  194. do_action_ref_array( 'pre_user_search', array( &$this ) ); 
  195.  
  196. /** 
  197. * Executes the user search query. 
  198. * @since 2.1.0 
  199. * @access public 
  200. */ 
  201. public function query() { 
  202. global $wpdb; 
  203.  
  204. $this->results = $wpdb->get_col("SELECT DISTINCT($wpdb->users.ID)" . $this->query_from . $this->query_where . $this->query_orderby . $this->query_limit); 
  205.  
  206. if ( $this->results ) 
  207. $this->total_users_for_query = $wpdb->get_var("SELECT COUNT(DISTINCT($wpdb->users.ID))" . $this->query_from . $this->query_where); // no limit 
  208. else 
  209. $this->search_errors = new WP_Error('no_matching_users_found', __('No users found.')); 
  210.  
  211. /** 
  212. * Prepares variables for use in templates. 
  213. * @since 2.1.0 
  214. * @access public 
  215. */ 
  216. function prepare_vars_for_template_usage() {} 
  217.  
  218. /** 
  219. * Handles paging for the user search query. 
  220. * @since 2.1.0 
  221. * @access public 
  222. */ 
  223. public function do_paging() { 
  224. if ( $this->total_users_for_query > $this->users_per_page ) { // have to page the results 
  225. $args = array(); 
  226. if ( ! empty($this->search_term) ) 
  227. $args['usersearch'] = urlencode($this->search_term); 
  228. if ( ! empty($this->role) ) 
  229. $args['role'] = urlencode($this->role); 
  230.  
  231. $this->paging_text = paginate_links( array( 
  232. 'total' => ceil($this->total_users_for_query / $this->users_per_page),  
  233. 'current' => $this->page,  
  234. 'base' => 'users.php?%_%',  
  235. 'format' => 'userspage=%#%',  
  236. 'add_args' => $args 
  237. ) ); 
  238. if ( $this->paging_text ) { 
  239. $this->paging_text = sprintf( '<span class="displaying-num">' . __( 'Displaying %s–%s of %s' ) . '</span>%s',  
  240. number_format_i18n( ( $this->page - 1 ) * $this->users_per_page + 1 ),  
  241. number_format_i18n( min( $this->page * $this->users_per_page, $this->total_users_for_query ) ),  
  242. number_format_i18n( $this->total_users_for_query ),  
  243. $this->paging_text 
  244. ); 
  245.  
  246. /** 
  247. * Retrieves the user search query results. 
  248. * @since 2.1.0 
  249. * @access public 
  250. * @return array 
  251. */ 
  252. public function get_results() { 
  253. return (array) $this->results; 
  254.  
  255. /** 
  256. * Displaying paging text. 
  257. * @see do_paging() Builds paging text. 
  258. * @since 2.1.0 
  259. * @access public 
  260. */ 
  261. function page_links() { 
  262. echo $this->paging_text; 
  263.  
  264. /** 
  265. * Whether paging is enabled. 
  266. * @see do_paging() Builds paging text. 
  267. * @since 2.1.0 
  268. * @access public 
  269. * @return bool 
  270. */ 
  271. function results_are_paged() { 
  272. if ( $this->paging_text ) 
  273. return true; 
  274. return false; 
  275.  
  276. /** 
  277. * Whether there are search terms. 
  278. * @since 2.1.0 
  279. * @access public 
  280. * @return bool 
  281. */ 
  282. function is_search() { 
  283. if ( $this->search_term ) 
  284. return true; 
  285. return false;