BP_Group_Member_Query

Query for the members of a group.

Defined (1)

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

/bp-groups/classes/class-bp-group-member-query.php  
  1. class BP_Group_Member_Query extends BP_User_Query { 
  2.  
  3. /** 
  4. * Array of group member ids, cached to prevent redundant lookups. 
  5. * @since 1.8.1 
  6. * @var null|array Null if not yet defined, otherwise an array of ints. 
  7. */ 
  8. protected $group_member_ids; 
  9.  
  10. /** 
  11. * Set up action hooks. 
  12. * @since 1.8.0 
  13. */ 
  14. public function setup_hooks() { 
  15. // Take this early opportunity to set the default 'type' param 
  16. // to 'last_joined', which will ensure that BP_User_Query 
  17. // trusts our order and does not try to apply its own. 
  18. if ( empty( $this->query_vars_raw['type'] ) ) { 
  19. $this->query_vars_raw['type'] = 'last_joined'; 
  20.  
  21. // Set the sort order. 
  22. add_action( 'bp_pre_user_query', array( $this, 'set_orderby' ) ); 
  23.  
  24. // Set up our populate_extras method. 
  25. add_action( 'bp_user_query_populate_extras', array( $this, 'populate_group_member_extras' ), 10, 2 ); 
  26.  
  27. /** 
  28. * Get a list of user_ids to include in the IN clause of the main query. 
  29. * Overrides BP_User_Query::get_include_ids(), adding our additional 
  30. * group-member logic. 
  31. * @since 1.8.0 
  32. * @param array $include Existing group IDs in the $include parameter,  
  33. * as calculated in BP_User_Query. 
  34. * @return array 
  35. */ 
  36. public function get_include_ids( $include = array() ) { 
  37. // The following args are specific to group member queries, and 
  38. // are not present in the query_vars of a normal BP_User_Query. 
  39. // We loop through to make sure that defaults are set (though 
  40. // values passed to the constructor will, as usual, override 
  41. // these defaults). 
  42. $this->query_vars = wp_parse_args( $this->query_vars, array( 
  43. 'group_id' => 0,  
  44. 'group_role' => array( 'member' ),  
  45. 'is_confirmed' => true,  
  46. 'invite_sent' => null,  
  47. 'inviter_id' => null,  
  48. 'type' => 'last_joined',  
  49. ) ); 
  50.  
  51. $group_member_ids = $this->get_group_member_ids(); 
  52.  
  53. // If the group member query returned no users, bail with an 
  54. // array that will guarantee no matches for BP_User_Query. 
  55. if ( empty( $group_member_ids ) ) { 
  56. return array( 0 ); 
  57.  
  58. if ( ! empty( $include ) ) { 
  59. $group_member_ids = array_intersect( $include, $group_member_ids ); 
  60.  
  61. return $group_member_ids; 
  62.  
  63. /** 
  64. * Get the members of the queried group. 
  65. * @since 1.8.0 
  66. * @return array $ids User IDs of relevant group member ids. 
  67. */ 
  68. protected function get_group_member_ids() { 
  69. global $wpdb; 
  70.  
  71. if ( is_array( $this->group_member_ids ) ) { 
  72. return $this->group_member_ids; 
  73.  
  74. $bp = buddypress(); 
  75. $sql = array( 
  76. 'select' => "SELECT user_id FROM {$bp->groups->table_name_members}",  
  77. 'where' => array(),  
  78. 'orderby' => '',  
  79. 'order' => '',  
  80. ); 
  81.  
  82. /** WHERE clauses *****************************************************/ 
  83.  
  84. // Group id. 
  85. $sql['where'][] = $wpdb->prepare( "group_id = %d", $this->query_vars['group_id'] ); 
  86.  
  87. // If is_confirmed. 
  88. $is_confirmed = ! empty( $this->query_vars['is_confirmed'] ) ? 1 : 0; 
  89. $sql['where'][] = $wpdb->prepare( "is_confirmed = %d", $is_confirmed ); 
  90.  
  91. // If invite_sent. 
  92. if ( ! is_null( $this->query_vars['invite_sent'] ) ) { 
  93. $invite_sent = ! empty( $this->query_vars['invite_sent'] ) ? 1 : 0; 
  94. $sql['where'][] = $wpdb->prepare( "invite_sent = %d", $invite_sent ); 
  95.  
  96. // If inviter_id. 
  97. if ( ! is_null( $this->query_vars['inviter_id'] ) ) { 
  98. $inviter_id = $this->query_vars['inviter_id']; 
  99.  
  100. // Empty: inviter_id = 0. (pass false, 0, or empty array). 
  101. if ( empty( $inviter_id ) ) { 
  102. $sql['where'][] = "inviter_id = 0"; 
  103.  
  104. // The string 'any' matches any non-zero value (inviter_id != 0). 
  105. } elseif ( 'any' === $inviter_id ) { 
  106. $sql['where'][] = "inviter_id != 0"; 
  107.  
  108. // Assume that a list of inviter IDs has been passed. 
  109. } else { 
  110. // Parse and sanitize. 
  111. $inviter_ids = wp_parse_id_list( $inviter_id ); 
  112. if ( ! empty( $inviter_ids ) ) { 
  113. $inviter_ids_sql = implode( ', ', $inviter_ids ); 
  114. $sql['where'][] = "inviter_id IN ({$inviter_ids_sql})"; 
  115.  
  116. // Role information is stored as follows: admins have 
  117. // is_admin = 1, mods have is_mod = 1, banned have is_banned = 
  118. // 1, and members have all three set to 0. 
  119. $roles = !empty( $this->query_vars['group_role'] ) ? $this->query_vars['group_role'] : array(); 
  120. if ( is_string( $roles ) ) { 
  121. $roles = explode( ', ', $roles ); 
  122.  
  123. // Sanitize: Only 'admin', 'mod', 'member', and 'banned' are valid. 
  124. $allowed_roles = array( 'admin', 'mod', 'member', 'banned' ); 
  125. foreach ( $roles as $role_key => $role_value ) { 
  126. if ( ! in_array( $role_value, $allowed_roles ) ) { 
  127. unset( $roles[ $role_key ] ); 
  128.  
  129. $roles = array_unique( $roles ); 
  130.  
  131. // When querying for a set of roles containing 'member' (for 
  132. // which there is no dedicated is_ column), figure out a list 
  133. // of columns *not* to match. 
  134. $roles_sql = ''; 
  135. if ( in_array( 'member', $roles ) ) { 
  136. $role_columns = array(); 
  137. foreach ( array_diff( $allowed_roles, $roles ) as $excluded_role ) { 
  138. $role_columns[] = 'is_' . $excluded_role . ' = 0'; 
  139.  
  140. if ( ! empty( $role_columns ) ) { 
  141. $roles_sql = '(' . implode( ' AND ', $role_columns ) . ')'; 
  142.  
  143. // When querying for a set of roles *not* containing 'member',  
  144. // simply construct a list of is_* = 1 clauses. 
  145. } else { 
  146. $role_columns = array(); 
  147. foreach ( $roles as $role ) { 
  148. $role_columns[] = 'is_' . $role . ' = 1'; 
  149.  
  150. if ( ! empty( $role_columns ) ) { 
  151. $roles_sql = '(' . implode( ' OR ', $role_columns ) . ')'; 
  152.  
  153. if ( ! empty( $roles_sql ) ) { 
  154. $sql['where'][] = $roles_sql; 
  155.  
  156. $sql['where'] = ! empty( $sql['where'] ) ? 'WHERE ' . implode( ' AND ', $sql['where'] ) : ''; 
  157.  
  158. // We fetch group members in order of last_joined, regardless 
  159. // of 'type'. If the 'type' value is not 'last_joined' or 
  160. // 'first_joined', the order will be overridden in 
  161. // BP_Group_Member_Query::set_orderby(). 
  162. $sql['orderby'] = "ORDER BY date_modified"; 
  163. $sql['order'] = 'first_joined' === $this->query_vars['type'] ? 'ASC' : 'DESC'; 
  164.  
  165. $this->group_member_ids = $wpdb->get_col( "{$sql['select']} {$sql['where']} {$sql['orderby']} {$sql['order']}" ); 
  166.  
  167. /** 
  168. * Filters the member IDs for the current group member query. 
  169. * Use this filter to build a custom query (such as when you've 
  170. * defined a custom 'type'). 
  171. * @since 2.0.0 
  172. * @param array $group_member_ids Array of associated member IDs. 
  173. * @param BP_Group_Member_Query $this Current BP_Group_Member_Query instance. 
  174. */ 
  175. $this->group_member_ids = apply_filters( 'bp_group_member_query_group_member_ids', $this->group_member_ids, $this ); 
  176.  
  177. return $this->group_member_ids; 
  178.  
  179. /** 
  180. * Tell BP_User_Query to order by the order of our query results. 
  181. * We only override BP_User_Query's native ordering in case of the 
  182. * 'last_joined' and 'first_joined' $type parameters. 
  183. * @since 1.8.1 
  184. * @param BP_User_Query $query BP_User_Query object. 
  185. */ 
  186. public function set_orderby( $query ) { 
  187. $gm_ids = $this->get_group_member_ids(); 
  188. if ( empty( $gm_ids ) ) { 
  189. $gm_ids = array( 0 ); 
  190.  
  191. // For 'last_joined', 'first_joined', and 'group_activity' 
  192. // types, we override the default orderby clause of 
  193. // BP_User_Query. In the case of 'group_activity', we perform 
  194. // a separate query to get the necessary order. In the case of 
  195. // 'last_joined' and 'first_joined', we can trust the order of 
  196. // results from BP_Group_Member_Query::get_group_members(). 
  197. // In all other cases, we fall through and let BP_User_Query 
  198. // do its own (non-group-specific) ordering. 
  199. if ( in_array( $query->query_vars['type'], array( 'last_joined', 'first_joined', 'group_activity' ) ) ) { 
  200.  
  201. // Group Activity DESC. 
  202. if ( 'group_activity' == $query->query_vars['type'] ) { 
  203. $gm_ids = $this->get_gm_ids_ordered_by_activity( $query, $gm_ids ); 
  204.  
  205. // The first param in the FIELD() clause is the sort column id. 
  206. $gm_ids = array_merge( array( 'u.id' ), wp_parse_id_list( $gm_ids ) ); 
  207. $gm_ids_sql = implode( ', ', $gm_ids ); 
  208.  
  209. $query->uid_clauses['orderby'] = "ORDER BY FIELD(" . $gm_ids_sql . ")"; 
  210.  
  211. // Prevent this filter from running on future BP_User_Query 
  212. // instances on the same page. 
  213. remove_action( 'bp_pre_user_query', array( $this, 'set_orderby' ) ); 
  214.  
  215. /** 
  216. * Fetch additional data required in bp_group_has_members() loops. 
  217. * Additional data fetched: 
  218. * - is_banned 
  219. * - date_modified 
  220. * @since 1.8.0 
  221. * @param BP_User_Query $query BP_User_Query object. Because we're 
  222. * filtering the current object, we use 
  223. * $this inside of the method instead. 
  224. * @param string $user_ids_sql Sanitized, comma-separated string of 
  225. * the user ids returned by the main query. 
  226. */ 
  227. public function populate_group_member_extras( $query, $user_ids_sql ) { 
  228. global $wpdb; 
  229.  
  230. $bp = buddypress(); 
  231. $extras = $wpdb->get_results( $wpdb->prepare( "SELECT id, user_id, date_modified, is_admin, is_mod, comments, user_title, invite_sent, is_confirmed, inviter_id, is_banned FROM {$bp->groups->table_name_members} WHERE user_id IN ({$user_ids_sql}) AND group_id = %d", $this->query_vars['group_id'] ) ); 
  232.  
  233. foreach ( (array) $extras as $extra ) { 
  234. if ( isset( $this->results[ $extra->user_id ] ) ) { 
  235. // The user_id is provided for backward compatibility. 
  236. $this->results[ $extra->user_id ]->user_id = (int) $extra->user_id; 
  237. $this->results[ $extra->user_id ]->is_admin = (int) $extra->is_admin; 
  238. $this->results[ $extra->user_id ]->is_mod = (int) $extra->is_mod; 
  239. $this->results[ $extra->user_id ]->is_banned = (int) $extra->is_banned; 
  240. $this->results[ $extra->user_id ]->date_modified = $extra->date_modified; 
  241. $this->results[ $extra->user_id ]->user_title = $extra->user_title; 
  242. $this->results[ $extra->user_id ]->comments = $extra->comments; 
  243. $this->results[ $extra->user_id ]->invite_sent = (int) $extra->invite_sent; 
  244. $this->results[ $extra->user_id ]->inviter_id = (int) $extra->inviter_id; 
  245. $this->results[ $extra->user_id ]->is_confirmed = (int) $extra->is_confirmed; 
  246. $this->results[ $extra->user_id ]->membership_id = (int) $extra->id; 
  247.  
  248. // Don't filter other BP_User_Query objects on the same page. 
  249. remove_action( 'bp_user_query_populate_extras', array( $this, 'populate_group_member_extras' ), 10 ); 
  250.  
  251. /** 
  252. * Sort user IDs by how recently they have generated activity within a given group. 
  253. * @since 2.1.0 
  254. * @param BP_User_Query $query BP_User_Query object. 
  255. * @param array $gm_ids array of group member ids. 
  256. * @return array 
  257. */ 
  258. public function get_gm_ids_ordered_by_activity( $query, $gm_ids = array() ) { 
  259. global $wpdb; 
  260.  
  261. if ( empty( $gm_ids ) ) { 
  262. return $gm_ids; 
  263.  
  264. if ( ! bp_is_active( 'activity' ) ) { 
  265. return $gm_ids; 
  266.  
  267. $activity_table = buddypress()->activity->table_name; 
  268.  
  269. $sql = array( 
  270. 'select' => "SELECT user_id, max( date_recorded ) as date_recorded FROM {$activity_table}",  
  271. 'where' => array(),  
  272. 'groupby' => 'GROUP BY user_id',  
  273. 'orderby' => 'ORDER BY date_recorded',  
  274. 'order' => 'DESC',  
  275. ); 
  276.  
  277. $sql['where'] = array( 
  278. 'user_id IN (' . implode( ', ', wp_parse_id_list( $gm_ids ) ) . ')',  
  279. 'item_id = ' . absint( $query->query_vars['group_id'] ),  
  280. $wpdb->prepare( "component = %s", buddypress()->groups->id ),  
  281. ); 
  282.  
  283. $sql['where'] = 'WHERE ' . implode( ' AND ', $sql['where'] ); 
  284.  
  285. $group_user_ids = $wpdb->get_results( "{$sql['select']} {$sql['where']} {$sql['groupby']} {$sql['orderby']} {$sql['order']}" ); 
  286.  
  287. return wp_list_pluck( $group_user_ids, 'user_id' );