BP_User_Query

BuddyPress User Query class.

Defined (1)

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

/bp-core/classes/class-bp-user-query.php  
  1. class BP_User_Query { 
  2.  
  3. /** Variables *************************************************************/ 
  4.  
  5. /** 
  6. * Unaltered params as passed to the constructor. 
  7. * @since 1.8.0 
  8. * @var array 
  9. */ 
  10. public $query_vars_raw = array(); 
  11.  
  12. /** 
  13. * Array of variables to query with. 
  14. * @since 1.7.0 
  15. * @var array 
  16. */ 
  17. public $query_vars = array(); 
  18.  
  19. /** 
  20. * List of found users and their respective data. 
  21. * @since 1.7.0 
  22. * @var array 
  23. */ 
  24. public $results = array(); 
  25.  
  26. /** 
  27. * Total number of found users for the current query. 
  28. * @since 1.7.0 
  29. * @var int 
  30. */ 
  31. public $total_users = 0; 
  32.  
  33. /** 
  34. * List of found user IDs. 
  35. * @since 1.7.0 
  36. * @var array 
  37. */ 
  38. public $user_ids = array(); 
  39.  
  40. /** 
  41. * SQL clauses for the user ID query. 
  42. * @since 1.7.0 
  43. * @var array 
  44. */ 
  45. public $uid_clauses = array(); 
  46.  
  47. /** 
  48. * SQL table where the user ID is being fetched from. 
  49. * @since 2.2.0 
  50. * @var string 
  51. */ 
  52. public $uid_table = ''; 
  53.  
  54. /** 
  55. * SQL database column name to order by. 
  56. * @since 1.7.0 
  57. * @var string 
  58. */ 
  59. public $uid_name = ''; 
  60.  
  61. /** 
  62. * Standard response when the query should not return any rows. 
  63. * @since 1.7.0 
  64. * @var string 
  65. */ 
  66. protected $no_results = array( 'join' => '', 'where' => '0 = 1' ); 
  67.  
  68.  
  69. /** Methods ***************************************************************/ 
  70.  
  71. /** 
  72. * Constructor. 
  73. * @since 1.7.0 
  74. * @param string|array|null $query See {@link BP_User_Query}. 
  75. */ 
  76. public function __construct( $query = null ) { 
  77.  
  78. // Store the raw query vars for later access. 
  79. $this->query_vars_raw = $query; 
  80.  
  81. // Allow extending classes to register action/filter hooks. 
  82. $this->setup_hooks(); 
  83.  
  84. if ( ! empty( $this->query_vars_raw ) ) { 
  85. $this->query_vars = wp_parse_args( $this->query_vars_raw, array( 
  86. 'type' => 'newest',  
  87. 'per_page' => 0,  
  88. 'page' => 1,  
  89. 'user_id' => 0,  
  90. 'search_terms' => false,  
  91. 'search_wildcard' => 'both',  
  92. 'include' => false,  
  93. 'exclude' => false,  
  94. 'user_ids' => false,  
  95. 'member_type' => '',  
  96. 'member_type__in' => '',  
  97. 'member_type__not_in' => '',  
  98. 'meta_key' => false,  
  99. 'meta_value' => false,  
  100. 'xprofile_query' => false,  
  101. 'populate_extras' => true,  
  102. 'count_total' => 'count_query' 
  103. ) ); 
  104.  
  105. /** 
  106. * Fires before the construction of the BP_User_Query query. 
  107. * @since 1.7.0 
  108. * @param BP_User_Query $this Current instance of the BP_User_Query. Passed by reference. 
  109. */ 
  110. do_action_ref_array( 'bp_pre_user_query_construct', array( &$this ) ); 
  111.  
  112. // Get user ids 
  113. // If the user_ids param is present, we skip the query. 
  114. if ( false !== $this->query_vars['user_ids'] ) { 
  115. $this->user_ids = wp_parse_id_list( $this->query_vars['user_ids'] ); 
  116. } else { 
  117. $this->prepare_user_ids_query(); 
  118. $this->do_user_ids_query(); 
  119.  
  120. // Bail if no user IDs were found. 
  121. if ( empty( $this->user_ids ) ) { 
  122. return; 
  123.  
  124. // Fetch additional data. First, using WP_User_Query. 
  125. $this->do_wp_user_query(); 
  126.  
  127. // Get BuddyPress specific user data. 
  128. $this->populate_extras(); 
  129.  
  130. /** 
  131. * Allow extending classes to set up action/filter hooks. 
  132. * When extending BP_User_Query, you may need to use some of its 
  133. * internal hooks to modify the output. It's not convenient to call 
  134. * add_action() or add_filter() in your class constructor, because 
  135. * BP_User_Query::__construct() contains a fair amount of logic that 
  136. * you may not want to override in your class. Define this method in 
  137. * your own class if you need a place where your extending class can 
  138. * add its hooks early in the query-building process. See 
  139. * {@link BP_Group_Member_Query::setup_hooks()} for an example. 
  140. * @since 1.8.0 
  141. */ 
  142. public function setup_hooks() {} 
  143.  
  144. /** 
  145. * Prepare the query for user_ids. 
  146. * @since 1.7.0 
  147. */ 
  148. public function prepare_user_ids_query() { 
  149. global $wpdb; 
  150.  
  151. $bp = buddypress(); 
  152.  
  153. // Default query variables used here. 
  154. $type = ''; 
  155. $per_page = 0; 
  156. $page = 1; 
  157. $user_id = 0; 
  158. $include = false; 
  159. $search_terms = false; 
  160. $exclude = false; 
  161. $meta_key = false; 
  162. $meta_value = false; 
  163.  
  164. extract( $this->query_vars ); 
  165.  
  166. // Setup the main SQL query container. 
  167. $sql = array( 
  168. 'select' => '',  
  169. 'where' => array(),  
  170. 'orderby' => '',  
  171. 'order' => '',  
  172. 'limit' => '' 
  173. ); 
  174.  
  175. /** TYPE **************************************************************/ 
  176.  
  177. // Determines the sort order, which means it also determines where the 
  178. // user IDs are drawn from (the SELECT and WHERE statements). 
  179. switch ( $type ) { 
  180.  
  181. // 'online' query happens against the last_activity usermeta key 
  182. // Filter 'bp_user_query_online_interval' to modify the 
  183. // number of minutes used as an interval. 
  184. case 'online' : 
  185. $this->uid_name = 'user_id'; 
  186. $this->uid_table = $bp->members->table_name_last_activity; 
  187. $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; 
  188. $sql['where'][] = $wpdb->prepare( "u.component = %s AND u.type = 'last_activity'", buddypress()->members->id ); 
  189.  
  190. /** 
  191. * Filters the threshold for activity timestamp minutes since to indicate online status. 
  192. * @since 1.8.0 
  193. * @param int $value Amount of minutes for threshold. Default 15. 
  194. */ 
  195. $sql['where'][] = $wpdb->prepare( "u.date_recorded >= DATE_SUB( UTC_TIMESTAMP(), INTERVAL %d MINUTE )", apply_filters( 'bp_user_query_online_interval', 15 ) ); 
  196. $sql['orderby'] = "ORDER BY u.date_recorded"; 
  197. $sql['order'] = "DESC"; 
  198.  
  199. break; 
  200.  
  201. // 'active', 'newest', and 'random' queries 
  202. // all happen against the last_activity usermeta key. 
  203. case 'active' : 
  204. case 'newest' : 
  205. case 'random' : 
  206. $this->uid_name = 'user_id'; 
  207. $this->uid_table = $bp->members->table_name_last_activity; 
  208. $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; 
  209. $sql['where'][] = $wpdb->prepare( "u.component = %s AND u.type = 'last_activity'", buddypress()->members->id ); 
  210.  
  211. if ( 'newest' == $type ) { 
  212. $sql['orderby'] = "ORDER BY u.user_id"; 
  213. $sql['order'] = "DESC"; 
  214. } elseif ( 'random' == $type ) { 
  215. $sql['orderby'] = "ORDER BY rand()"; 
  216. } else { 
  217. $sql['orderby'] = "ORDER BY u.date_recorded"; 
  218. $sql['order'] = "DESC"; 
  219.  
  220. break; 
  221.  
  222. // 'popular' sorts by the 'total_friend_count' usermeta. 
  223. case 'popular' : 
  224. $this->uid_name = 'user_id'; 
  225. $this->uid_table = $wpdb->usermeta; 
  226. $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; 
  227. $sql['where'][] = $wpdb->prepare( "u.meta_key = %s", bp_get_user_meta_key( 'total_friend_count' ) ); 
  228. $sql['orderby'] = "ORDER BY CONVERT(u.meta_value, SIGNED)"; 
  229. $sql['order'] = "DESC"; 
  230.  
  231. break; 
  232.  
  233. // 'alphabetical' sorts depend on the xprofile setup. 
  234. case 'alphabetical' : 
  235.  
  236. // We prefer to do alphabetical sorts against the display_name field 
  237. // of wp_users, because the table is smaller and better indexed. We 
  238. // can do so if xprofile sync is enabled, or if xprofile is inactive. 
  239. // 
  240. // @todo remove need for bp_is_active() check. 
  241. if ( ! bp_disable_profile_sync() || ! bp_is_active( 'xprofile' ) ) { 
  242. $this->uid_name = 'ID'; 
  243. $this->uid_table = $wpdb->users; 
  244. $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; 
  245. $sql['orderby'] = "ORDER BY u.display_name"; 
  246. $sql['order'] = "ASC"; 
  247.  
  248. // When profile sync is disabled, alphabetical sorts must happen against 
  249. // the xprofile table. 
  250. } else { 
  251. $this->uid_name = 'user_id'; 
  252. $this->uid_table = $bp->profile->table_name_data; 
  253. $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; 
  254. $sql['where'][] = $wpdb->prepare( "u.field_id = %d", bp_xprofile_fullname_field_id() ); 
  255. $sql['orderby'] = "ORDER BY u.value"; 
  256. $sql['order'] = "ASC"; 
  257.  
  258. // Alphabetical queries ignore last_activity, while BP uses last_activity 
  259. // to infer spam/deleted/non-activated users. To ensure that these users 
  260. // are filtered out, we add an appropriate sub-query. 
  261. $sql['where'][] = "u.{$this->uid_name} IN ( SELECT ID FROM {$wpdb->users} WHERE " . bp_core_get_status_sql( '' ) . " )"; 
  262.  
  263. break; 
  264.  
  265. // Any other 'type' falls through. 
  266. default : 
  267. $this->uid_name = 'ID'; 
  268. $this->uid_table = $wpdb->users; 
  269. $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; 
  270.  
  271. // In this case, we assume that a plugin is 
  272. // handling order, so we leave those clauses 
  273. // blank. 
  274. break; 
  275.  
  276. /** WHERE *************************************************************/ 
  277.  
  278. // 'include' - User ids to include in the results. 
  279. $include = false !== $include ? wp_parse_id_list( $include ) : array(); 
  280. $include_ids = $this->get_include_ids( $include ); 
  281.  
  282. // An array containing nothing but 0 should always fail. 
  283. if ( 1 === count( $include_ids ) && 0 == reset( $include_ids ) ) { 
  284. $sql['where'][] = $this->no_results['where']; 
  285. } elseif ( ! empty( $include_ids ) ) { 
  286. $include_ids = implode( ', ', wp_parse_id_list( $include_ids ) ); 
  287. $sql['where'][] = "u.{$this->uid_name} IN ({$include_ids})"; 
  288.  
  289. // 'exclude' - User ids to exclude from the results. 
  290. if ( false !== $exclude ) { 
  291. $exclude_ids = implode( ', ', wp_parse_id_list( $exclude ) ); 
  292. $sql['where'][] = "u.{$this->uid_name} NOT IN ({$exclude_ids})"; 
  293.  
  294. // 'user_id' - When a user id is passed, limit to the friends of the user 
  295. // @todo remove need for bp_is_active() check. 
  296. if ( ! empty( $user_id ) && bp_is_active( 'friends' ) ) { 
  297. $friend_ids = friends_get_friend_user_ids( $user_id ); 
  298. $friend_ids = implode( ', ', wp_parse_id_list( $friend_ids ) ); 
  299.  
  300. if ( ! empty( $friend_ids ) ) { 
  301. $sql['where'][] = "u.{$this->uid_name} IN ({$friend_ids})"; 
  302.  
  303. // If the user has no friends, the query should always 
  304. // return no users. 
  305. } else { 
  306. $sql['where'][] = $this->no_results['where']; 
  307.  
  308. /** Search Terms ******************************************************/ 
  309.  
  310. // 'search_terms' searches user_login and user_nicename 
  311. // xprofile field matches happen in bp_xprofile_bp_user_query_search(). 
  312. if ( false !== $search_terms ) { 
  313. $search_terms = bp_esc_like( wp_kses_normalize_entities( $search_terms ) ); 
  314.  
  315. if ( $search_wildcard === 'left' ) { 
  316. $search_terms_nospace = '%' . $search_terms; 
  317. $search_terms_space = '%' . $search_terms . ' %'; 
  318. } elseif ( $search_wildcard === 'right' ) { 
  319. $search_terms_nospace = $search_terms . '%'; 
  320. $search_terms_space = '% ' . $search_terms . '%'; 
  321. } else { 
  322. $search_terms_nospace = '%' . $search_terms . '%'; 
  323. $search_terms_space = '%' . $search_terms . '%'; 
  324.  
  325. $sql['where']['search'] = $wpdb->prepare( 
  326. "u.{$this->uid_name} IN ( SELECT ID FROM {$wpdb->users} WHERE ( user_login LIKE %s OR user_login LIKE %s OR user_nicename LIKE %s OR user_nicename LIKE %s ) )",  
  327. $search_terms_nospace,  
  328. $search_terms_space,  
  329. $search_terms_nospace,  
  330. $search_terms_space 
  331. ); 
  332.  
  333. // Only use $member_type__in if $member_type is not set. 
  334. if ( empty( $member_type ) && ! empty( $member_type__in ) ) { 
  335. $member_type = $member_type__in; 
  336.  
  337. // Member types to exclude. Note that this takes precedence over inclusions. 
  338. if ( ! empty( $member_type__not_in ) ) { 
  339. $member_type_clause = $this->get_sql_clause_for_member_types( $member_type__not_in, 'NOT IN' ); 
  340.  
  341. // Member types to include. 
  342. } elseif ( ! empty( $member_type ) ) { 
  343. $member_type_clause = $this->get_sql_clause_for_member_types( $member_type, 'IN' ); 
  344.  
  345. if ( ! empty( $member_type_clause ) ) { 
  346. $sql['where']['member_type'] = $member_type_clause; 
  347.  
  348. // 'meta_key', 'meta_value' allow usermeta search 
  349. // To avoid global joins, do a separate query. 
  350. if ( false !== $meta_key ) { 
  351. $meta_sql = $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s", $meta_key ); 
  352.  
  353. if ( false !== $meta_value ) { 
  354. $meta_sql .= $wpdb->prepare( " AND meta_value = %s", $meta_value ); 
  355.  
  356. $found_user_ids = $wpdb->get_col( $meta_sql ); 
  357.  
  358. if ( ! empty( $found_user_ids ) ) { 
  359. $sql['where'][] = "u.{$this->uid_name} IN (" . implode( ', ', wp_parse_id_list( $found_user_ids ) ) . ")"; 
  360. } else { 
  361. $sql['where'][] = '1 = 0'; 
  362.  
  363. // 'per_page', 'page' - handles LIMIT. 
  364. if ( !empty( $per_page ) && !empty( $page ) ) { 
  365. $sql['limit'] = $wpdb->prepare( "LIMIT %d, %d", intval( ( $page - 1 ) * $per_page ), intval( $per_page ) ); 
  366. } else { 
  367. $sql['limit'] = ''; 
  368.  
  369. /** 
  370. * Filters the clauses for the user query. 
  371. * @since 2.0.0 
  372. * @param array $sql Array of SQL clauses to be used in the query. 
  373. * @param BP_User_Query $this Current BP_User_Query instance. 
  374. */ 
  375. $sql = apply_filters_ref_array( 'bp_user_query_uid_clauses', array( $sql, &$this ) ); 
  376.  
  377. // Assemble the query chunks. 
  378. $this->uid_clauses['select'] = $sql['select']; 
  379. $this->uid_clauses['where'] = ! empty( $sql['where'] ) ? 'WHERE ' . implode( ' AND ', $sql['where'] ) : ''; 
  380. $this->uid_clauses['orderby'] = $sql['orderby']; 
  381. $this->uid_clauses['order'] = $sql['order']; 
  382. $this->uid_clauses['limit'] = $sql['limit']; 
  383.  
  384. /** 
  385. * Fires before the BP_User_Query query is made. 
  386. * @since 1.7.0 
  387. * @param BP_User_Query $this Current BP_User_Query instance. Passed by reference. 
  388. */ 
  389. do_action_ref_array( 'bp_pre_user_query', array( &$this ) ); 
  390.  
  391. /** 
  392. * Query for IDs of users that match the query parameters. 
  393. * Perform a database query to specifically get only user IDs, using 
  394. * existing query variables set previously in the constructor. 
  395. * Also used to quickly perform user total counts. 
  396. * @since 1.7.0 
  397. */ 
  398. public function do_user_ids_query() { 
  399. global $wpdb; 
  400.  
  401. // If counting using SQL_CALC_FOUND_ROWS, set it up here. 
  402. if ( 'sql_calc_found_rows' == $this->query_vars['count_total'] ) { 
  403. $this->uid_clauses['select'] = str_replace( 'SELECT', 'SELECT SQL_CALC_FOUND_ROWS', $this->uid_clauses['select'] ); 
  404.  
  405. // Get the specific user ids. 
  406. $this->user_ids = $wpdb->get_col( "{$this->uid_clauses['select']} {$this->uid_clauses['where']} {$this->uid_clauses['orderby']} {$this->uid_clauses['order']} {$this->uid_clauses['limit']}" ); 
  407.  
  408. // Get the total user count. 
  409. if ( 'sql_calc_found_rows' == $this->query_vars['count_total'] ) { 
  410.  
  411. /** 
  412. * Filters the found user SQL statements before query. 
  413. * If "sql_calc_found_rows" is the provided count_total query var 
  414. * then the value will be "SELECT FOUND_ROWS()". Otherwise it will 
  415. * use a "SELECT COUNT()" query statement. 
  416. * @since 1.7.0 
  417. * @param string $value SQL statement to select FOUND_ROWS(). 
  418. * @param BP_User_Query $this Current BP_User_Query instance. 
  419. */ 
  420. $this->total_users = $wpdb->get_var( apply_filters( 'bp_found_user_query', "SELECT FOUND_ROWS()", $this ) ); 
  421. } elseif ( 'count_query' == $this->query_vars['count_total'] ) { 
  422. $count_select = preg_replace( '/^SELECT.*?FROM (\S+) u/', "SELECT COUNT(u.{$this->uid_name}) FROM $1 u", $this->uid_clauses['select'] ); 
  423.  
  424. /** This filter is documented in bp-core/classes/class-bp-user-query.php */ 
  425. $this->total_users = $wpdb->get_var( apply_filters( 'bp_found_user_query', "{$count_select} {$this->uid_clauses['where']}", $this ) ); 
  426.  
  427. /** 
  428. * Use WP_User_Query() to pull data for the user IDs retrieved in the main query. 
  429. * @since 1.7.0 
  430. */ 
  431. public function do_wp_user_query() { 
  432. $fields = array( 'ID', 'user_login', 'user_pass', 'user_nicename', 'user_email', 'user_url', 'user_registered', 'user_activation_key', 'user_status', 'display_name' ); 
  433.  
  434. if ( is_multisite() ) { 
  435. $fields[] = 'spam'; 
  436. $fields[] = 'deleted'; 
  437.  
  438. /** 
  439. * Filters the WP User Query arguments before passing into the class. 
  440. * @since 1.7.0 
  441. * @param array $value Array of arguments for the user query. 
  442. * @param BP_User_Query $this Current BP_User_Query instance. 
  443. */ 
  444. $wp_user_query = new WP_User_Query( apply_filters( 'bp_wp_user_query_args', array( 
  445.  
  446. // Relevant. 
  447. 'fields' => $fields,  
  448. 'include' => $this->user_ids,  
  449.  
  450. // Overrides 
  451. 'blog_id' => 0, // BP does not require blog roles. 
  452. 'count_total' => false // We already have a count. 
  453.  
  454. ), $this ) ); 
  455.  
  456. // WP_User_Query doesn't cache the data it pulls from wp_users,  
  457. // and it does not give us a way to save queries by fetching 
  458. // only uncached users. However, BP does cache this data, so 
  459. // we set it here. 
  460. foreach ( $wp_user_query->results as $u ) { 
  461. wp_cache_set( 'bp_core_userdata_' . $u->ID, $u, 'bp' ); 
  462.  
  463. // We calculate total_users using a standalone query, except 
  464. // when a whitelist of user_ids is passed to the constructor. 
  465. // This clause covers the latter situation, and ensures that 
  466. // pagination works when querying by $user_ids. 
  467. if ( empty( $this->total_users ) ) { 
  468. $this->total_users = count( $wp_user_query->results ); 
  469.  
  470. // Reindex for easier matching. 
  471. $r = array(); 
  472. foreach ( $wp_user_query->results as $u ) { 
  473. $r[ $u->ID ] = $u; 
  474.  
  475. // Match up to the user ids from the main query. 
  476. foreach ( $this->user_ids as $key => $uid ) { 
  477. if ( isset( $r[ $uid ] ) ) { 
  478. $r[ $uid ]->ID = (int) $uid; 
  479. $r[ $uid ]->user_status = (int) $r[ $uid ]->user_status; 
  480.  
  481. $this->results[ $uid ] = $r[ $uid ]; 
  482.  
  483. // The BP template functions expect an 'id' 
  484. // (as opposed to 'ID') property. 
  485. $this->results[ $uid ]->id = (int) $uid; 
  486.  
  487. // Remove user ID from original user_ids property. 
  488. } else { 
  489. unset( $this->user_ids[ $key ] ); 
  490.  
  491. /** 
  492. * Fetch the IDs of users to put in the IN clause of the main query. 
  493. * By default, returns the value passed to it 
  494. * ($this->query_vars['include']). Having this abstracted into a 
  495. * standalone method means that extending classes can override the 
  496. * logic, parsing together their own user_id limits with the 'include' 
  497. * ids passed to the class constructor. See {@link BP_Group_Member_Query} 
  498. * for an example. 
  499. * @since 1.8.0 
  500. * @param array $include Sanitized array of user IDs, as passed to the 'include' 
  501. * parameter of the class constructor. 
  502. * @return array The list of users to which the main query should be 
  503. * limited. 
  504. */ 
  505. public function get_include_ids( $include = array() ) { 
  506. return $include; 
  507.  
  508. /** 
  509. * Perform a database query to populate any extra metadata we might need. 
  510. * Different components will hook into the 'bp_user_query_populate_extras' 
  511. * action to loop in the things they want. 
  512. * @since 1.7.0 
  513. * @global WPDB $wpdb Global WordPress database access object. 
  514. */ 
  515. public function populate_extras() { 
  516. global $wpdb; 
  517.  
  518. // Bail if no users. 
  519. if ( empty( $this->user_ids ) || empty( $this->results ) ) { 
  520. return; 
  521.  
  522. // Bail if the populate_extras flag is set to false 
  523. // In the case of the 'popular' sort type, we force 
  524. // populate_extras to true, because we need the friend counts. 
  525. if ( 'popular' == $this->query_vars['type'] ) { 
  526. $this->query_vars['populate_extras'] = 1; 
  527.  
  528. if ( ! (bool) $this->query_vars['populate_extras'] ) { 
  529. return; 
  530.  
  531. // Turn user ID's into a query-usable, comma separated value. 
  532. $user_ids_sql = implode( ', ', wp_parse_id_list( $this->user_ids ) ); 
  533.  
  534. /** 
  535. * Allows users to independently populate custom extras. 
  536. * Note that anything you add here should query using $user_ids_sql, to 
  537. * avoid running multiple queries per user in the loop. 
  538. * Two BuddyPress components currently do this: 
  539. * - XProfile: To override display names. 
  540. * - Friends: To set whether or not a user is the current users friend. 
  541. * @see bp_xprofile_filter_user_query_populate_extras() 
  542. * @see bp_friends_filter_user_query_populate_extras() 
  543. * @since 1.7.0 
  544. * @param BP_User_Query $this Current BP_User_Query instance. 
  545. * @param string $user_ids_sql Comma-separated string of user IDs. 
  546. */ 
  547. do_action_ref_array( 'bp_user_query_populate_extras', array( $this, $user_ids_sql ) ); 
  548.  
  549. // Fetch last_active data from the activity table. 
  550. $last_activities = BP_Core_User::get_last_activity( $this->user_ids ); 
  551.  
  552. // Set a last_activity value for each user, even if it's empty. 
  553. foreach ( $this->results as $user_id => $user ) { 
  554. $user_last_activity = isset( $last_activities[ $user_id ] ) ? $last_activities[ $user_id ]['date_recorded'] : ''; 
  555. $this->results[ $user_id ]->last_activity = $user_last_activity; 
  556.  
  557. // Fetch usermeta data 
  558. // We want the three following pieces of info from usermeta: 
  559. // - friend count 
  560. // - latest update. 
  561. $total_friend_count_key = bp_get_user_meta_key( 'total_friend_count' ); 
  562. $bp_latest_update_key = bp_get_user_meta_key( 'bp_latest_update' ); 
  563.  
  564. // Total_friend_count must be set for each user, even if its 
  565. // value is 0. 
  566. foreach ( $this->results as $uindex => $user ) { 
  567. $this->results[$uindex]->total_friend_count = 0; 
  568.  
  569. // Create, prepare, and run the separate usermeta query. 
  570. $user_metas = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, meta_key, meta_value FROM {$wpdb->usermeta} WHERE meta_key IN (%s, %s) AND user_id IN ({$user_ids_sql})", $total_friend_count_key, $bp_latest_update_key ) ); 
  571.  
  572. // The $members_template global expects the index key to be different 
  573. // from the meta_key in some cases, so we rejig things here. 
  574. foreach ( $user_metas as $user_meta ) { 
  575. switch ( $user_meta->meta_key ) { 
  576. case $total_friend_count_key : 
  577. $key = 'total_friend_count'; 
  578. break; 
  579.  
  580. case $bp_latest_update_key : 
  581. $key = 'latest_update'; 
  582. break; 
  583.  
  584. if ( isset( $this->results[ $user_meta->user_id ] ) ) { 
  585. $this->results[ $user_meta->user_id ]->{$key} = $user_meta->meta_value; 
  586.  
  587. // When meta_key or meta_value have been passed to the query,  
  588. // fetch the resulting values for use in the template functions. 
  589. if ( ! empty( $this->query_vars['meta_key'] ) ) { 
  590. $meta_sql = array( 
  591. 'select' => "SELECT user_id, meta_key, meta_value",  
  592. 'from' => "FROM $wpdb->usermeta",  
  593. 'where' => $wpdb->prepare( "WHERE meta_key = %s", $this->query_vars['meta_key'] ) 
  594. ); 
  595.  
  596. if ( false !== $this->query_vars['meta_value'] ) { 
  597. $meta_sql['where'] .= $wpdb->prepare( " AND meta_value = %s", $this->query_vars['meta_value'] ); 
  598.  
  599. $metas = $wpdb->get_results( "{$meta_sql['select']} {$meta_sql['from']} {$meta_sql['where']}" ); 
  600.  
  601. if ( ! empty( $metas ) ) { 
  602. foreach ( $metas as $meta ) { 
  603. if ( isset( $this->results[ $meta->user_id ] ) ) { 
  604. $this->results[ $meta->user_id ]->meta_key = $meta->meta_key; 
  605.  
  606. if ( ! empty( $meta->meta_value ) ) { 
  607. $this->results[ $meta->user_id ]->meta_value = $meta->meta_value; 
  608.  
  609. /** 
  610. * Get a SQL clause representing member_type include/exclusion. 
  611. * @since 2.4.0 
  612. * @param string|array $member_types Array or comma-separated list of member types. 
  613. * @param string $operator 'IN' or 'NOT IN'. 
  614. * @return string 
  615. */ 
  616. protected function get_sql_clause_for_member_types( $member_types, $operator ) { 
  617. global $wpdb; 
  618.  
  619. // Sanitize. 
  620. if ( 'NOT IN' !== $operator ) { 
  621. $operator = 'IN'; 
  622.  
  623. // Parse and sanitize types. 
  624. if ( ! is_array( $member_types ) ) { 
  625. $member_types = preg_split( '/[, \s+]/', $member_types ); 
  626.  
  627. $types = array(); 
  628. foreach ( $member_types as $mt ) { 
  629. if ( bp_get_member_type_object( $mt ) ) { 
  630. $types[] = $mt; 
  631.  
  632. $tax_query = new WP_Tax_Query( array( 
  633. array( 
  634. 'taxonomy' => bp_get_member_type_tax_name(),  
  635. 'field' => 'name',  
  636. 'operator' => $operator,  
  637. 'terms' => $types,  
  638. ),  
  639. ) ); 
  640.  
  641. // Switch to the root blog, where member type taxonomies live. 
  642. $site_id = bp_get_taxonomy_term_site_id( bp_get_member_type_tax_name() ); 
  643. $switched = false; 
  644. if ( $site_id !== get_current_blog_id() ) { 
  645. switch_to_blog( $site_id ); 
  646. $switched = true; 
  647.  
  648. $sql_clauses = $tax_query->get_sql( 'u', $this->uid_name ); 
  649.  
  650. $clause = ''; 
  651.  
  652. // The no_results clauses are the same between IN and NOT IN. 
  653. if ( false !== strpos( $sql_clauses['where'], '0 = 1' ) ) { 
  654. $clause = $this->no_results['where']; 
  655.  
  656. // The tax_query clause generated for NOT IN can be used almost as-is. We just trim the leading 'AND'. 
  657. } elseif ( 'NOT IN' === $operator ) { 
  658. $clause = preg_replace( '/^\s*AND\s*/', '', $sql_clauses['where'] ); 
  659.  
  660. // IN clauses must be converted to a subquery. 
  661. } elseif ( preg_match( '/' . $wpdb->term_relationships . '\.term_taxonomy_id IN \([0-9, ]+\)/', $sql_clauses['where'], $matches ) ) { 
  662. $clause = "u.{$this->uid_name} IN ( SELECT object_id FROM $wpdb->term_relationships WHERE {$matches[0]} )"; 
  663.  
  664. if ( $switched ) { 
  665. restore_current_blog(); 
  666.  
  667. return $clause;