BP_Groups_Member

BuddyPress Group Membership object.

Defined (1)

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

/bp-groups/classes/class-bp-groups-member.php  
  1. class BP_Groups_Member { 
  2.  
  3. /** 
  4. * ID of the membership. 
  5. * @since 1.6.0 
  6. * @var int 
  7. */ 
  8. var $id; 
  9.  
  10. /** 
  11. * ID of the group associated with the membership. 
  12. * @since 1.6.0 
  13. * @var int 
  14. */ 
  15. var $group_id; 
  16.  
  17. /** 
  18. * ID of the user associated with the membership. 
  19. * @since 1.6.0 
  20. * @var int 
  21. */ 
  22. var $user_id; 
  23.  
  24. /** 
  25. * ID of the user whose invitation initiated the membership. 
  26. * @since 1.6.0 
  27. * @var int 
  28. */ 
  29. var $inviter_id; 
  30.  
  31. /** 
  32. * Whether the member is an admin of the group. 
  33. * @since 1.6.0 
  34. * @var int 
  35. */ 
  36. var $is_admin; 
  37.  
  38. /** 
  39. * Whether the member is a mod of the group. 
  40. * @since 1.6.0 
  41. * @var int 
  42. */ 
  43. var $is_mod; 
  44.  
  45. /** 
  46. * Whether the member is banned from the group. 
  47. * @since 1.6.0 
  48. * @var int 
  49. */ 
  50. var $is_banned; 
  51.  
  52. /** 
  53. * Title used to describe the group member's role in the group. 
  54. * Eg, 'Group Admin'. 
  55. * @since 1.6.0 
  56. * @var int 
  57. */ 
  58. var $user_title; 
  59.  
  60. /** 
  61. * Last modified date of the membership. 
  62. * This value is updated when, eg, invitations are accepted. 
  63. * @since 1.6.0 
  64. * @var string 
  65. */ 
  66. var $date_modified; 
  67.  
  68. /** 
  69. * Whether the membership has been confirmed. 
  70. * @since 1.6.0 
  71. * @var int 
  72. */ 
  73. var $is_confirmed; 
  74.  
  75. /** 
  76. * Comments associated with the membership. 
  77. * In BP core, these are limited to the optional message users can 
  78. * include when requesting membership to a private group. 
  79. * @since 1.6.0 
  80. * @var string 
  81. */ 
  82. var $comments; 
  83.  
  84. /** 
  85. * Whether an invitation has been sent for this membership. 
  86. * The purpose of this flag is to mark when an invitation has been 
  87. * "drafted" (the user has been added via the interface at Send 
  88. * Invites), but the Send button has not been pressed, so the 
  89. * invitee has not yet been notified. 
  90. * @since 1.6.0 
  91. * @var int 
  92. */ 
  93. var $invite_sent; 
  94.  
  95. /** 
  96. * WP_User object representing the membership's user. 
  97. * @since 1.6.0 
  98. * @var WP_User 
  99. */ 
  100. protected $user; 
  101.  
  102. /** 
  103. * Constructor method. 
  104. * @since 1.6.0 
  105. * @param int $user_id Optional. Along with $group_id, can be used to 
  106. * look up a membership. 
  107. * @param int $group_id Optional. Along with $user_id, can be used to 
  108. * look up a membership. 
  109. * @param int|bool $id Optional. The unique ID of the membership object. 
  110. * @param bool $populate Whether to populate the properties of the 
  111. * located membership. Default: true. 
  112. */ 
  113. public function __construct( $user_id = 0, $group_id = 0, $id = false, $populate = true ) { 
  114.  
  115. // User and group are not empty, and ID is. 
  116. if ( !empty( $user_id ) && !empty( $group_id ) && empty( $id ) ) { 
  117. $this->user_id = $user_id; 
  118. $this->group_id = $group_id; 
  119.  
  120. if ( !empty( $populate ) ) { 
  121. $this->populate(); 
  122.  
  123. // ID is not empty. 
  124. if ( !empty( $id ) ) { 
  125. $this->id = $id; 
  126.  
  127. if ( !empty( $populate ) ) { 
  128. $this->populate(); 
  129.  
  130. /** 
  131. * Populate the object's properties. 
  132. * @since 1.6.0 
  133. */ 
  134. public function populate() { 
  135. global $wpdb; 
  136.  
  137. $bp = buddypress(); 
  138.  
  139. if ( $this->user_id && $this->group_id && !$this->id ) 
  140. $sql = $wpdb->prepare( "SELECT * FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $this->user_id, $this->group_id ); 
  141.  
  142. if ( !empty( $this->id ) ) 
  143. $sql = $wpdb->prepare( "SELECT * FROM {$bp->groups->table_name_members} WHERE id = %d", $this->id ); 
  144.  
  145. $member = $wpdb->get_row($sql); 
  146.  
  147. if ( !empty( $member ) ) { 
  148. $this->id = (int) $member->id; 
  149. $this->group_id = (int) $member->group_id; 
  150. $this->user_id = (int) $member->user_id; 
  151. $this->inviter_id = (int) $member->inviter_id; 
  152. $this->is_admin = (int) $member->is_admin; 
  153. $this->is_mod = (int) $member->is_mod; 
  154. $this->is_banned = (int) $member->is_banned; 
  155. $this->user_title = $member->user_title; 
  156. $this->date_modified = $member->date_modified; 
  157. $this->is_confirmed = (int) $member->is_confirmed; 
  158. $this->comments = $member->comments; 
  159. $this->invite_sent = (int) $member->invite_sent; 
  160.  
  161. /** 
  162. * Magic getter. 
  163. * @since 2.8.0 
  164. * @param string $key Key. 
  165. * @return mixed 
  166. */ 
  167. public function __get( $key ) { 
  168. switch ( $key ) { 
  169. case 'user' : 
  170. return $this->get_user_object( $this->user_id ); 
  171.  
  172. /** 
  173. * Magic issetter. 
  174. * @since 2.8.0 
  175. * @param string $key Key. 
  176. * @return mixed 
  177. */ 
  178. public function __isset( $key ) { 
  179. switch ( $key ) { 
  180. case 'user' : 
  181. return true; 
  182.  
  183. default : 
  184. return isset( $this->{$key} ); 
  185.  
  186. /** 
  187. * Get the user object corresponding to this membership. 
  188. * Used for lazyloading the protected `user` property. 
  189. * @since 2.8.0 
  190. * @return BP_Core_User 
  191. */ 
  192. protected function get_user_object() { 
  193. if ( empty( $this->user ) ) { 
  194. $this->user = new BP_Core_User( $this->user_id ); 
  195.  
  196. return $this->user; 
  197.  
  198. /** 
  199. * Save the membership data to the database. 
  200. * @since 1.6.0 
  201. * @return bool True on success, false on failure. 
  202. */ 
  203. public function save() { 
  204. global $wpdb; 
  205.  
  206. $bp = buddypress(); 
  207.  
  208. $this->user_id = apply_filters( 'groups_member_user_id_before_save', $this->user_id, $this->id ); 
  209. $this->group_id = apply_filters( 'groups_member_group_id_before_save', $this->group_id, $this->id ); 
  210. $this->inviter_id = apply_filters( 'groups_member_inviter_id_before_save', $this->inviter_id, $this->id ); 
  211. $this->is_admin = apply_filters( 'groups_member_is_admin_before_save', $this->is_admin, $this->id ); 
  212. $this->is_mod = apply_filters( 'groups_member_is_mod_before_save', $this->is_mod, $this->id ); 
  213. $this->is_banned = apply_filters( 'groups_member_is_banned_before_save', $this->is_banned, $this->id ); 
  214. $this->user_title = apply_filters( 'groups_member_user_title_before_save', $this->user_title, $this->id ); 
  215. $this->date_modified = apply_filters( 'groups_member_date_modified_before_save', $this->date_modified, $this->id ); 
  216. $this->is_confirmed = apply_filters( 'groups_member_is_confirmed_before_save', $this->is_confirmed, $this->id ); 
  217. $this->comments = apply_filters( 'groups_member_comments_before_save', $this->comments, $this->id ); 
  218. $this->invite_sent = apply_filters( 'groups_member_invite_sent_before_save', $this->invite_sent, $this->id ); 
  219.  
  220. /** 
  221. * Fires before the current group membership item gets saved. 
  222. * Please use this hook to filter the properties above. Each part will be passed in. 
  223. * @since 1.0.0 
  224. * @param BP_Groups_Member $this Current instance of the group membership item being saved. Passed by reference. 
  225. */ 
  226. do_action_ref_array( 'groups_member_before_save', array( &$this ) ); 
  227.  
  228. // The following properties are required; bail if not met. 
  229. if ( empty( $this->user_id ) || empty( $this->group_id ) ) { 
  230. return false; 
  231.  
  232. if ( !empty( $this->id ) ) { 
  233. $sql = $wpdb->prepare( "UPDATE {$bp->groups->table_name_members} SET inviter_id = %d, is_admin = %d, is_mod = %d, is_banned = %d, user_title = %s, date_modified = %s, is_confirmed = %d, comments = %s, invite_sent = %d WHERE id = %d", $this->inviter_id, $this->is_admin, $this->is_mod, $this->is_banned, $this->user_title, $this->date_modified, $this->is_confirmed, $this->comments, $this->invite_sent, $this->id ); 
  234. } else { 
  235. // Ensure that user is not already a member of the group before inserting. 
  236. if ( $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 1 LIMIT 1", $this->user_id, $this->group_id ) ) ) { 
  237. return false; 
  238.  
  239. $sql = $wpdb->prepare( "INSERT INTO {$bp->groups->table_name_members} ( user_id, group_id, inviter_id, is_admin, is_mod, is_banned, user_title, date_modified, is_confirmed, comments, invite_sent ) VALUES ( %d, %d, %d, %d, %d, %d, %s, %s, %d, %s, %d )", $this->user_id, $this->group_id, $this->inviter_id, $this->is_admin, $this->is_mod, $this->is_banned, $this->user_title, $this->date_modified, $this->is_confirmed, $this->comments, $this->invite_sent ); 
  240.  
  241. if ( !$wpdb->query( $sql ) ) 
  242. return false; 
  243.  
  244. $this->id = $wpdb->insert_id; 
  245.  
  246. // Update the user's group count. 
  247. self::refresh_total_group_count_for_user( $this->user_id ); 
  248.  
  249. // Update the group's member count. 
  250. self::refresh_total_member_count_for_group( $this->group_id ); 
  251.  
  252. /** 
  253. * Fires after the current group membership item has been saved. 
  254. * Please use this hook to filter the properties above. Each part will be passed in. 
  255. * @since 1.0.0 
  256. * @param BP_Groups_Member $this Current instance of the group membership item has been saved. Passed by reference. 
  257. */ 
  258. do_action_ref_array( 'groups_member_after_save', array( &$this ) ); 
  259.  
  260. return true; 
  261.  
  262. /** 
  263. * Promote a member to a new status. 
  264. * @since 1.6.0 
  265. * @param string $status The new status. 'mod' or 'admin'. 
  266. * @return bool True on success, false on failure. 
  267. */ 
  268. public function promote( $status = 'mod' ) { 
  269. if ( 'mod' == $status ) { 
  270. $this->is_admin = 0; 
  271. $this->is_mod = 1; 
  272. $this->user_title = __( 'Group Mod', 'buddypress' ); 
  273.  
  274. if ( 'admin' == $status ) { 
  275. $this->is_admin = 1; 
  276. $this->is_mod = 0; 
  277. $this->user_title = __( 'Group Admin', 'buddypress' ); 
  278.  
  279. return $this->save(); 
  280.  
  281. /** 
  282. * Demote membership to Member status (non-admin, non-mod). 
  283. * @since 1.6.0 
  284. * @return bool True on success, false on failure. 
  285. */ 
  286. public function demote() { 
  287. $this->is_mod = 0; 
  288. $this->is_admin = 0; 
  289. $this->user_title = false; 
  290.  
  291. return $this->save(); 
  292.  
  293. /** 
  294. * Ban the user from the group. 
  295. * @since 1.6.0 
  296. * @return bool True on success, false on failure. 
  297. */ 
  298. public function ban() { 
  299. if ( !empty( $this->is_admin ) ) 
  300. return false; 
  301.  
  302. $this->is_mod = 0; 
  303. $this->is_banned = 1; 
  304.  
  305. return $this->save(); 
  306.  
  307. /** 
  308. * Unban the user from the group. 
  309. * @since 1.6.0 
  310. * @return bool True on success, false on failure. 
  311. */ 
  312. public function unban() { 
  313. if ( !empty( $this->is_admin ) ) 
  314. return false; 
  315.  
  316. $this->is_banned = 0; 
  317.  
  318. return $this->save(); 
  319.  
  320. /** 
  321. * Mark a pending invitation as accepted. 
  322. * @since 1.6.0 
  323. */ 
  324. public function accept_invite() { 
  325. $this->inviter_id = 0; 
  326. $this->is_confirmed = 1; 
  327. $this->date_modified = bp_core_current_time(); 
  328.  
  329. /** 
  330. * Confirm a membership request. 
  331. * @since 1.6.0 
  332. */ 
  333. public function accept_request() { 
  334. $this->is_confirmed = 1; 
  335. $this->date_modified = bp_core_current_time(); 
  336.  
  337. /** 
  338. * Remove the current membership. 
  339. * @since 1.6.0 
  340. * @return bool True on success, false on failure. 
  341. */ 
  342. public function remove() { 
  343. global $wpdb; 
  344.  
  345. /** 
  346. * Fires before a member is removed from a group. 
  347. * @since 2.3.0 
  348. * @param BP_Groups_Member $this Current group membership object. 
  349. */ 
  350. do_action_ref_array( 'groups_member_before_remove', array( $this ) ); 
  351.  
  352. $bp = buddypress(); 
  353. $sql = $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $this->user_id, $this->group_id ); 
  354.  
  355. if ( !$result = $wpdb->query( $sql ) ) 
  356. return false; 
  357.  
  358. // Update the user's group count. 
  359. self::refresh_total_group_count_for_user( $this->user_id ); 
  360.  
  361. // Update the group's member count. 
  362. self::refresh_total_member_count_for_group( $this->group_id ); 
  363.  
  364. /** 
  365. * Fires after a member is removed from a group. 
  366. * @since 2.3.0 
  367. * @param BP_Groups_Member $this Current group membership object. 
  368. */ 
  369. do_action_ref_array( 'groups_member_after_remove', array( $this ) ); 
  370.  
  371. return $result; 
  372.  
  373. /** Static Methods ****************************************************/ 
  374.  
  375. /** 
  376. * Refresh the total_group_count for a user. 
  377. * @since 1.8.0 
  378. * @param int $user_id ID of the user. 
  379. * @return bool True on success, false on failure. 
  380. */ 
  381. public static function refresh_total_group_count_for_user( $user_id ) { 
  382. return bp_update_user_meta( $user_id, 'total_group_count', (int) self::total_group_count( $user_id ) ); 
  383.  
  384. /** 
  385. * Refresh the total_member_count for a group. 
  386. * @since 1.8.0 
  387. * @param int $group_id ID of the group. 
  388. * @return bool True on success, false on failure. 
  389. */ 
  390. public static function refresh_total_member_count_for_group( $group_id ) { 
  391. return groups_update_groupmeta( $group_id, 'total_member_count', (int) BP_Groups_Group::get_total_member_count( $group_id ) ); 
  392.  
  393. /** 
  394. * Delete a membership, based on user + group IDs. 
  395. * @since 1.6.0 
  396. * @param int $user_id ID of the user. 
  397. * @param int $group_id ID of the group. 
  398. * @return True on success, false on failure. 
  399. */ 
  400. public static function delete( $user_id, $group_id ) { 
  401. global $wpdb; 
  402.  
  403. /** 
  404. * Fires before a group membership is deleted. 
  405. * @since 2.3.0 
  406. * @param int $user_id ID of the user. 
  407. * @param int $group_id ID of the group. 
  408. */ 
  409. do_action( 'bp_groups_member_before_delete', $user_id, $group_id ); 
  410.  
  411. $bp = buddypress(); 
  412. $remove = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $user_id, $group_id ) ); 
  413.  
  414. // Update the user's group count. 
  415. self::refresh_total_group_count_for_user( $user_id ); 
  416.  
  417. // Update the group's member count. 
  418. self::refresh_total_member_count_for_group( $group_id ); 
  419.  
  420. /** 
  421. * Fires after a member is removed from a group. 
  422. * @since 2.3.0 
  423. * @param int $user_id ID of the user. 
  424. * @param int $group_id ID of the group. 
  425. */ 
  426. do_action( 'bp_groups_member_after_delete', $user_id, $group_id ); 
  427.  
  428. return $remove; 
  429.  
  430. /** 
  431. * Get the IDs of the groups of which a specified user is a member. 
  432. * @since 1.6.0 
  433. * @param int $user_id ID of the user. 
  434. * @param int|bool $limit Optional. Max number of results to return. 
  435. * Default: false (no limit). 
  436. * @param int|bool $page Optional. Page offset of results to return. 
  437. * Default: false (no limit). 
  438. * @return array { 
  439. * @type array $groups Array of groups returned by paginated query. 
  440. * @type int $total Count of groups matching query. 
  441. * } 
  442. */ 
  443. public static function get_group_ids( $user_id, $limit = false, $page = false ) { 
  444. global $wpdb; 
  445.  
  446. $pag_sql = ''; 
  447. if ( !empty( $limit ) && !empty( $page ) ) 
  448. $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 
  449.  
  450. $bp = buddypress(); 
  451.  
  452. // If the user is logged in and viewing their own groups, we can show hidden and private groups. 
  453. if ( $user_id != bp_loggedin_user_id() ) { 
  454. $group_sql = $wpdb->prepare( "SELECT DISTINCT m.group_id FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.status != 'hidden' AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0{$pag_sql}", $user_id ); 
  455. $total_groups = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.status != 'hidden' AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0", $user_id ) ); 
  456. } else { 
  457. $group_sql = $wpdb->prepare( "SELECT DISTINCT group_id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND is_confirmed = 1 AND is_banned = 0{$pag_sql}", $user_id ); 
  458. $total_groups = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT group_id) FROM {$bp->groups->table_name_members} WHERE user_id = %d AND is_confirmed = 1 AND is_banned = 0", $user_id ) ); 
  459.  
  460. $groups = $wpdb->get_col( $group_sql ); 
  461.  
  462. return array( 'groups' => $groups, 'total' => (int) $total_groups ); 
  463.  
  464. /** 
  465. * Get the IDs of the groups of which a specified user is a member, sorted by the date joined. 
  466. * @since 1.6.0 
  467. * @param int $user_id ID of the user. 
  468. * @param int|bool $limit Optional. Max number of results to return. 
  469. * Default: false (no limit). 
  470. * @param int|bool $page Optional. Page offset of results to return. 
  471. * Default: false (no limit). 
  472. * @param string|bool $filter Optional. Limit results to groups whose name or 
  473. * description field matches search terms. 
  474. * @return array { 
  475. * @type array $groups Array of groups returned by paginated query. 
  476. * @type int $total Count of groups matching query. 
  477. * } 
  478. */ 
  479. public static function get_recently_joined( $user_id, $limit = false, $page = false, $filter = false ) { 
  480. global $wpdb; 
  481.  
  482. $user_id_sql = $pag_sql = $hidden_sql = $filter_sql = ''; 
  483.  
  484. $user_id_sql = $wpdb->prepare( 'm.user_id = %d', $user_id ); 
  485.  
  486. if ( !empty( $limit ) && !empty( $page ) ) 
  487. $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 
  488.  
  489. if ( !empty( $filter ) ) { 
  490. $search_terms_like = '%' . bp_esc_like( $filter ) . '%'; 
  491. $filter_sql = $wpdb->prepare( " AND ( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like ); 
  492.  
  493. if ( $user_id != bp_loggedin_user_id() ) 
  494. $hidden_sql = " AND g.status != 'hidden'"; 
  495.  
  496. $bp = buddypress(); 
  497.  
  498. $paged_groups = $wpdb->get_results( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count'{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_confirmed = 1 AND m.is_banned = 0 ORDER BY m.date_modified DESC {$pag_sql}" ); 
  499. $total_groups = $wpdb->get_var( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_banned = 0 AND m.is_confirmed = 1 ORDER BY m.date_modified DESC" ); 
  500.  
  501. return array( 'groups' => $paged_groups, 'total' => $total_groups ); 
  502.  
  503. /** 
  504. * Get the IDs of the groups of which a specified user is an admin. 
  505. * @since 1.6.0 
  506. * @param int $user_id ID of the user. 
  507. * @param int|bool $limit Optional. Max number of results to return. 
  508. * Default: false (no limit). 
  509. * @param int|bool $page Optional. Page offset of results to return. 
  510. * Default: false (no limit). 
  511. * @param string|bool $filter Optional. Limit results to groups whose name or 
  512. * description field matches search terms. 
  513. * @return array { 
  514. * @type array $groups Array of groups returned by paginated query. 
  515. * @type int $total Count of groups matching query. 
  516. * } 
  517. */ 
  518. public static function get_is_admin_of( $user_id, $limit = false, $page = false, $filter = false ) { 
  519. global $wpdb; 
  520.  
  521. $user_id_sql = $pag_sql = $hidden_sql = $filter_sql = ''; 
  522.  
  523. $user_id_sql = $wpdb->prepare( 'm.user_id = %d', $user_id ); 
  524.  
  525. if ( !empty( $limit ) && !empty( $page ) ) 
  526. $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 
  527.  
  528. if ( !empty( $filter ) ) { 
  529. $search_terms_like = '%' . bp_esc_like( $filter ) . '%'; 
  530. $filter_sql = $wpdb->prepare( " AND ( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like ); 
  531.  
  532. if ( $user_id != bp_loggedin_user_id() ) 
  533. $hidden_sql = " AND g.status != 'hidden'"; 
  534.  
  535. $bp = buddypress(); 
  536.  
  537. $paged_groups = $wpdb->get_results( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count'{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_confirmed = 1 AND m.is_banned = 0 AND m.is_admin = 1 ORDER BY m.date_modified ASC {$pag_sql}" ); 
  538. $total_groups = $wpdb->get_var( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_confirmed = 1 AND m.is_banned = 0 AND m.is_admin = 1 ORDER BY date_modified ASC" ); 
  539.  
  540. return array( 'groups' => $paged_groups, 'total' => $total_groups ); 
  541.  
  542. /** 
  543. * Get the IDs of the groups of which a specified user is a moderator. 
  544. * @since 1.6.0 
  545. * @param int $user_id ID of the user. 
  546. * @param int|bool $limit Optional. Max number of results to return. 
  547. * Default: false (no limit). 
  548. * @param int|bool $page Optional. Page offset of results to return. 
  549. * Default: false (no limit). 
  550. * @param string|bool $filter Optional. Limit results to groups whose name or 
  551. * description field matches search terms. 
  552. * @return array { 
  553. * @type array $groups Array of groups returned by paginated query. 
  554. * @type int $total Count of groups matching query. 
  555. * } 
  556. */ 
  557. public static function get_is_mod_of( $user_id, $limit = false, $page = false, $filter = false ) { 
  558. global $wpdb; 
  559.  
  560. $user_id_sql = $pag_sql = $hidden_sql = $filter_sql = ''; 
  561.  
  562. $user_id_sql = $wpdb->prepare( 'm.user_id = %d', $user_id ); 
  563.  
  564. if ( !empty( $limit ) && !empty( $page ) ) 
  565. $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 
  566.  
  567. if ( !empty( $filter ) ) { 
  568. $search_terms_like = '%' . bp_esc_like( $filter ) . '%'; 
  569. $filter_sql = $wpdb->prepare( " AND ( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like ); 
  570.  
  571. if ( $user_id != bp_loggedin_user_id() ) 
  572. $hidden_sql = " AND g.status != 'hidden'"; 
  573.  
  574. $bp = buddypress(); 
  575.  
  576. $paged_groups = $wpdb->get_results( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count'{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_confirmed = 1 AND m.is_banned = 0 AND m.is_mod = 1 ORDER BY m.date_modified ASC {$pag_sql}" ); 
  577. $total_groups = $wpdb->get_var( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_confirmed = 1 AND m.is_banned = 0 AND m.is_mod = 1 ORDER BY date_modified ASC" ); 
  578.  
  579. return array( 'groups' => $paged_groups, 'total' => $total_groups ); 
  580.  
  581. /** 
  582. * Get the groups of which a specified user is banned from. 
  583. * @since 2.4.0 
  584. * @param int $user_id ID of the user. 
  585. * @param int|bool $limit Optional. Max number of results to return. 
  586. * Default: false (no limit). 
  587. * @param int|bool $page Optional. Page offset of results to return. 
  588. * Default: false (no limit). 
  589. * @param string|bool $filter Optional. Limit results to groups whose name or 
  590. * description field matches search terms. 
  591. * @return array { 
  592. * @type array $groups Array of groups returned by paginated query. 
  593. * @type int $total Count of groups matching query. 
  594. * } 
  595. */ 
  596. public static function get_is_banned_of( $user_id, $limit = false, $page = false, $filter = false ) { 
  597. global $wpdb; 
  598.  
  599. $bp = buddypress(); 
  600.  
  601. $user_id_sql = $pag_sql = $hidden_sql = $filter_sql = ''; 
  602. $user_id_sql = $wpdb->prepare( 'm.user_id = %d', $user_id ); 
  603.  
  604. if ( $limit && $page ) { 
  605. $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit ), intval( $limit ) ); 
  606.  
  607. if ( $filter ) { 
  608. $search_terms_like = '%' . bp_esc_like( $filter ) . '%'; 
  609. $filter_sql = $wpdb->prepare( " AND ( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like ); 
  610.  
  611. if ( $user_id !== bp_loggedin_user_id() && ! bp_current_user_can( 'bp_moderate' ) ) { 
  612. $hidden_sql = " AND g.status != 'hidden'"; 
  613.  
  614. $paged_groups = $wpdb->get_results( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count'{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_banned = 1 ORDER BY m.date_modified ASC {$pag_sql}" ); 
  615. $total_groups = $wpdb->get_var( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_banned = 1 ORDER BY date_modified ASC" ); 
  616.  
  617. return array( 'groups' => $paged_groups, 'total' => $total_groups ); 
  618.  
  619. /** 
  620. * Get the count of groups of which the specified user is a member. 
  621. * @since 1.6.0 
  622. * @param int $user_id Optional. Default: ID of the displayed user. 
  623. * @return int Group count. 
  624. */ 
  625. public static function total_group_count( $user_id = 0 ) { 
  626. global $wpdb; 
  627.  
  628. if ( empty( $user_id ) ) 
  629. $user_id = bp_displayed_user_id(); 
  630.  
  631. $bp = buddypress(); 
  632.  
  633. if ( $user_id != bp_loggedin_user_id() && !bp_current_user_can( 'bp_moderate' ) ) { 
  634. return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id AND g.status != 'hidden' AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0", $user_id ) ); 
  635. } else { 
  636. return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0", $user_id ) ); 
  637.  
  638. /** 
  639. * Get a user's outstanding group invitations. 
  640. * @since 1.6.0 
  641. * @param int $user_id ID of the invitee. 
  642. * @param int|bool $limit Optional. Max number of results to return. 
  643. * Default: false (no limit). 
  644. * @param int|bool $page Optional. Page offset of results to return. 
  645. * Default: false (no limit). 
  646. * @param string|array|bool $exclude Optional. Array or comma-separated list 
  647. * of group IDs to exclude from results. 
  648. * @return array { 
  649. * @type array $groups Array of groups returned by paginated query. 
  650. * @type int $total Count of groups matching query. 
  651. * } 
  652. */ 
  653. public static function get_invites( $user_id, $limit = false, $page = false, $exclude = false ) { 
  654. global $wpdb; 
  655.  
  656. $pag_sql = ( !empty( $limit ) && !empty( $page ) ) ? $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ) : ''; 
  657.  
  658. if ( !empty( $exclude ) ) { 
  659. $exclude = implode( ', ', wp_parse_id_list( $exclude ) ); 
  660. $exclude_sql = " AND g.id NOT IN ({$exclude})"; 
  661. } else { 
  662. $exclude_sql = ''; 
  663.  
  664. $bp = buddypress(); 
  665.  
  666. $paged_groups = $wpdb->get_results( $wpdb->prepare( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' AND m.is_confirmed = 0 AND m.inviter_id != 0 AND m.invite_sent = 1 AND m.user_id = %d {$exclude_sql} ORDER BY m.date_modified ASC {$pag_sql}", $user_id ) ); 
  667.  
  668. return array( 'groups' => $paged_groups, 'total' => self::get_invite_count_for_user( $user_id ) ); 
  669.  
  670. /** 
  671. * Gets the total group invite count for a user. 
  672. * @since 2.0.0 
  673. * @param int $user_id The user ID. 
  674. * @return int 
  675. */ 
  676. public static function get_invite_count_for_user( $user_id = 0 ) { 
  677. global $wpdb; 
  678.  
  679. $bp = buddypress(); 
  680.  
  681. $count = wp_cache_get( $user_id, 'bp_group_invite_count' ); 
  682.  
  683. if ( false === $count ) { 
  684. $count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id AND m.is_confirmed = 0 AND m.inviter_id != 0 AND m.invite_sent = 1 AND m.user_id = %d", $user_id ) ); 
  685. wp_cache_set( $user_id, $count, 'bp_group_invite_count' ); 
  686.  
  687. return $count; 
  688.  
  689. /** 
  690. * Check whether a user has an outstanding invitation to a given group. 
  691. * @since 1.6.0 
  692. * @param int $user_id ID of the potential invitee. 
  693. * @param int $group_id ID of the group. 
  694. * @param string $type If 'sent', results are limited to those invitations 
  695. * that have actually been sent (non-draft). Default: 'sent'. 
  696. * @return int|null The ID of the invitation if found; null if not found. 
  697. */ 
  698. public static function check_has_invite( $user_id, $group_id, $type = 'sent' ) { 
  699. global $wpdb; 
  700.  
  701. if ( empty( $user_id ) ) 
  702. return false; 
  703.  
  704. $bp = buddypress(); 
  705. $sql = "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 0 AND inviter_id != 0"; 
  706.  
  707. if ( 'sent' == $type ) 
  708. $sql .= " AND invite_sent = 1"; 
  709.  
  710. $query = $wpdb->get_var( $wpdb->prepare( $sql, $user_id, $group_id ) ); 
  711.  
  712. return is_numeric( $query ) ? (int) $query : $query; 
  713.  
  714. /** 
  715. * Delete an invitation, by specifying user ID and group ID. 
  716. * @since 1.6.0 
  717. * @global WPDB $wpdb 
  718. * @param int $user_id ID of the user. 
  719. * @param int $group_id ID of the group. 
  720. * @return int Number of records deleted. 
  721. */ 
  722. public static function delete_invite( $user_id, $group_id ) { 
  723. global $wpdb; 
  724.  
  725. if ( empty( $user_id ) ) { 
  726. return false; 
  727.  
  728. /** 
  729. * Fires before a group invitation is deleted. 
  730. * @since 2.6.0 
  731. * @param int $user_id ID of the user. 
  732. * @param int $group_id ID of the group. 
  733. */ 
  734. do_action( 'bp_groups_member_before_delete_invite', $user_id, $group_id ); 
  735.  
  736. $table_name = buddypress()->groups->table_name_members; 
  737.  
  738. $sql = "DELETE FROM {$table_name} 
  739. WHERE user_id = %d 
  740. AND group_id = %d 
  741. AND is_confirmed = 0 
  742. AND inviter_id != 0"; 
  743.  
  744. $prepared = $wpdb->prepare( $sql, $user_id, $group_id ); 
  745.  
  746. return $wpdb->query( $prepared ); 
  747.  
  748. /** 
  749. * Delete an unconfirmed membership request, by user ID and group ID. 
  750. * @since 1.6.0 
  751. * @param int $user_id ID of the user. 
  752. * @param int $group_id ID of the group. 
  753. * @return int Number of records deleted. 
  754. */ 
  755. public static function delete_request( $user_id, $group_id ) { 
  756. global $wpdb; 
  757.  
  758. if ( empty( $user_id ) ) 
  759. return false; 
  760.  
  761. $bp = buddypress(); 
  762.  
  763. return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 0 AND inviter_id = 0 AND invite_sent = 0", $user_id, $group_id ) ); 
  764.  
  765. /** 
  766. * Check whether a user is an admin of a given group. 
  767. * @since 1.6.0 
  768. * @param int $user_id ID of the user. 
  769. * @param int $group_id ID of the group. 
  770. * @return mixed 
  771. */ 
  772. public static function check_is_admin( $user_id, $group_id ) { 
  773. global $wpdb; 
  774.  
  775. if ( empty( $user_id ) ) 
  776. return false; 
  777.  
  778. $bp = buddypress(); 
  779.  
  780. return $wpdb->query( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_admin = 1 AND is_banned = 0", $user_id, $group_id ) ); 
  781.  
  782. /** 
  783. * Check whether a user is a mod of a given group. 
  784. * @since 1.6.0 
  785. * @param int $user_id ID of the user. 
  786. * @param int $group_id ID of the group. 
  787. * @return mixed 
  788. */ 
  789. public static function check_is_mod( $user_id, $group_id ) { 
  790. global $wpdb; 
  791.  
  792. if ( empty( $user_id ) ) 
  793. return false; 
  794.  
  795. $bp = buddypress(); 
  796.  
  797. return $wpdb->query( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_mod = 1 AND is_banned = 0", $user_id, $group_id ) ); 
  798.  
  799. /** 
  800. * Check whether a user is a member of a given group. 
  801. * @since 1.6.0 
  802. * @param int $user_id ID of the user. 
  803. * @param int $group_id ID of the group. 
  804. * @return mixed 
  805. */ 
  806. public static function check_is_member( $user_id, $group_id ) { 
  807. global $wpdb; 
  808.  
  809. if ( empty( $user_id ) ) 
  810. return false; 
  811.  
  812. $bp = buddypress(); 
  813.  
  814. return $wpdb->query( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 1 AND is_banned = 0", $user_id, $group_id ) ); 
  815.  
  816. /** 
  817. * Check whether a user is banned from a given group. 
  818. * @since 1.6.0 
  819. * @param int $user_id ID of the user. 
  820. * @param int $group_id ID of the group. 
  821. * @return int|null int 1 if user is banned; int 0 if user is not banned; 
  822. * null if user is not part of the group or if group doesn't exist. 
  823. */ 
  824. public static function check_is_banned( $user_id, $group_id ) { 
  825. global $wpdb; 
  826.  
  827. if ( empty( $user_id ) ) 
  828. return false; 
  829.  
  830. $bp = buddypress(); 
  831.  
  832. $query = $wpdb->get_var( $wpdb->prepare( "SELECT is_banned FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $user_id, $group_id ) ); 
  833.  
  834. return is_numeric( $query ) ? (int) $query : $query; 
  835.  
  836. /** 
  837. * Is the specified user the creator of the group? 
  838. * @since 1.2.6 
  839. * @param int $user_id ID of the user. 
  840. * @param int $group_id ID of the group. 
  841. * @return int|null int of group ID if user is the creator; null on failure. 
  842. */ 
  843. public static function check_is_creator( $user_id, $group_id ) { 
  844. global $wpdb; 
  845.  
  846. if ( empty( $user_id ) ) 
  847. return false; 
  848.  
  849. $bp = buddypress(); 
  850.  
  851. $query = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name} WHERE creator_id = %d AND id = %d", $user_id, $group_id ) ); 
  852.  
  853. return is_numeric( $query ) ? (int) $query : $query; 
  854.  
  855. /** 
  856. * Check whether a user has an outstanding membership request for a given group. 
  857. * @since 1.6.0 
  858. * @param int $user_id ID of the user. 
  859. * @param int $group_id ID of the group. 
  860. * @return int Database ID of the membership if found; int 0 on failure. 
  861. */ 
  862. public static function check_for_membership_request( $user_id, $group_id ) { 
  863. global $wpdb; 
  864.  
  865. if ( empty( $user_id ) ) 
  866. return false; 
  867.  
  868. $bp = buddypress(); 
  869.  
  870. return $wpdb->query( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 0 AND is_banned = 0 AND inviter_id = 0", $user_id, $group_id ) ); 
  871.  
  872. /** 
  873. * Get a list of randomly selected IDs of groups that the member belongs to. 
  874. * @since 1.6.0 
  875. * @param int $user_id ID of the user. 
  876. * @param int $total_groups Max number of group IDs to return. Default: 5. 
  877. * @return array Group IDs. 
  878. */ 
  879. public static function get_random_groups( $user_id = 0, $total_groups = 5 ) { 
  880. global $wpdb; 
  881.  
  882. $bp = buddypress(); 
  883.  
  884. // If the user is logged in and viewing their random groups, we can show hidden and private groups. 
  885. if ( bp_is_my_profile() ) { 
  886. return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT group_id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND is_confirmed = 1 AND is_banned = 0 ORDER BY rand() LIMIT %d", $user_id, $total_groups ) ) ); 
  887. } else { 
  888. return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT m.group_id FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id AND g.status != 'hidden' AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0 ORDER BY rand() LIMIT %d", $user_id, $total_groups ) ) ); 
  889.  
  890. /** 
  891. * Get the IDs of all a given group's members. 
  892. * @since 1.6.0 
  893. * @param int $group_id ID of the group. 
  894. * @return array IDs of all group members. 
  895. */ 
  896. public static function get_group_member_ids( $group_id ) { 
  897. global $wpdb; 
  898.  
  899. $bp = buddypress(); 
  900.  
  901. return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 1 AND is_banned = 0", $group_id ) ) ); 
  902.  
  903. /** 
  904. * Get a list of all a given group's admins. 
  905. * @since 1.6.0 
  906. * @param int $group_id ID of the group. 
  907. * @return array Info about group admins (user_id + date_modified). 
  908. */ 
  909. public static function get_group_administrator_ids( $group_id ) { 
  910. global $wpdb; 
  911.  
  912. $group_admins = wp_cache_get( $group_id, 'bp_group_admins' ); 
  913.  
  914. if ( false === $group_admins ) { 
  915. self::prime_group_admins_mods_cache( array( $group_id ) ); 
  916. $group_admins = wp_cache_get( $group_id, 'bp_group_admins' ); 
  917.  
  918. // Integer casting. 
  919. foreach ( (array) $group_admins as $key => $data ) { 
  920. $group_admins[ $key ]->user_id = (int) $group_admins[ $key ]->user_id; 
  921.  
  922. return $group_admins; 
  923.  
  924. /** 
  925. * Prime the bp_group_admins cache for one or more groups. 
  926. * @since 2.7.0 
  927. * @param array $group_ids IDs of the groups. 
  928. * @return bool True on success. 
  929. */ 
  930. public static function prime_group_admins_mods_cache( $group_ids ) { 
  931. global $wpdb; 
  932.  
  933. $uncached = bp_get_non_cached_ids( $group_ids, 'bp_group_admins' ); 
  934.  
  935. if ( $uncached ) { 
  936. $bp = buddypress(); 
  937. $uncached_sql = implode( ', ', array_map( 'intval', $uncached ) ); 
  938. $group_admin_mods = $wpdb->get_results( "SELECT user_id, group_id, date_modified, is_admin, is_mod FROM {$bp->groups->table_name_members} WHERE group_id IN ({$uncached_sql}) AND ( is_admin = 1 OR is_mod = 1 ) AND is_banned = 0" ); 
  939.  
  940. $admins = $mods = array(); 
  941. if ( $group_admin_mods ) { 
  942. foreach ( $group_admin_mods as $group_admin_mod ) { 
  943. $obj = new stdClass(); 
  944. $obj->user_id = $group_admin_mod->user_id; 
  945. $obj->date_modified = $group_admin_mod->date_modified; 
  946.  
  947. if ( $group_admin_mod->is_admin ) { 
  948. $admins[ $group_admin_mod->group_id ][] = $obj; 
  949. } else { 
  950. $mods[ $group_admin_mod->group_id ][] = $obj; 
  951.  
  952. // Prime cache for all groups, even those with no matches. 
  953. foreach ( $uncached as $group_id ) { 
  954. $group_admins = isset( $admins[ $group_id ] ) ? $admins[ $group_id ] : array(); 
  955. wp_cache_set( $group_id, $group_admins, 'bp_group_admins' ); 
  956.  
  957. $group_mods = isset( $mods[ $group_id ] ) ? $mods[ $group_id ] : array(); 
  958. wp_cache_set( $group_id, $group_mods, 'bp_group_mods' ); 
  959.  
  960. /** 
  961. * Get a list of all a given group's moderators. 
  962. * @since 1.6.0 
  963. * @param int $group_id ID of the group. 
  964. * @return array Info about group mods (user_id + date_modified). 
  965. */ 
  966. public static function get_group_moderator_ids( $group_id ) { 
  967. global $wpdb; 
  968.  
  969. $group_mods = wp_cache_get( $group_id, 'bp_group_mods' ); 
  970.  
  971. if ( false === $group_mods ) { 
  972. self::prime_group_admins_mods_cache( array( $group_id ) ); 
  973. $group_mods = wp_cache_get( $group_id, 'bp_group_mods' ); 
  974.  
  975. // Integer casting. 
  976. foreach ( (array) $group_mods as $key => $data ) { 
  977. $group_mods[ $key ]->user_id = (int) $group_mods[ $key ]->user_id; 
  978.  
  979. return $group_mods; 
  980.  
  981. /** 
  982. * Get group membership objects by ID (or an array of IDs). 
  983. * @since 2.6.0 
  984. * @param int|string|array $membership_ids Single membership ID or comma-separated/array list of membership IDs. 
  985. * @return array 
  986. */ 
  987. public static function get_memberships_by_id( $membership_ids ) { 
  988. global $wpdb; 
  989.  
  990. $bp = buddypress(); 
  991.  
  992. $membership_ids = implode( ', ', wp_parse_id_list( $membership_ids ) ); 
  993. return $wpdb->get_results( "SELECT * FROM {$bp->groups->table_name_members} WHERE id IN ({$membership_ids})" ); 
  994.  
  995. /** 
  996. * Get the IDs users with outstanding membership requests to the group. 
  997. * @since 1.6.0 
  998. * @param int $group_id ID of the group. 
  999. * @return array IDs of users with outstanding membership requests. 
  1000. */ 
  1001. public static function get_all_membership_request_user_ids( $group_id ) { 
  1002. global $wpdb; 
  1003.  
  1004. $bp = buddypress(); 
  1005.  
  1006. return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 0 AND inviter_id = 0", $group_id ) ) ); 
  1007.  
  1008. /** 
  1009. * Get members of a group. 
  1010. * @deprecated 1.6.0 
  1011. * @param int $group_id ID of the group being queried for. 
  1012. * @param bool|int $limit Max amount to return. 
  1013. * @param bool|int $page Pagination value. 
  1014. * @param bool $exclude_admins_mods Whether or not to exclude admins and moderators. 
  1015. * @param bool $exclude_banned Whether or not to exclude banned members. 
  1016. * @param bool|array $exclude Array of user IDs to exclude. 
  1017. * @return mixed 
  1018. */ 
  1019. public static function get_all_for_group( $group_id, $limit = false, $page = false, $exclude_admins_mods = true, $exclude_banned = true, $exclude = false ) { 
  1020. global $wpdb; 
  1021.  
  1022. _deprecated_function( __METHOD__, '1.8', 'BP_Group_Member_Query' ); 
  1023.  
  1024. $pag_sql = ''; 
  1025. if ( !empty( $limit ) && !empty( $page ) ) 
  1026. $pag_sql = $wpdb->prepare( "LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 
  1027.  
  1028. $exclude_admins_sql = ''; 
  1029. if ( !empty( $exclude_admins_mods ) ) 
  1030. $exclude_admins_sql = "AND is_admin = 0 AND is_mod = 0"; 
  1031.  
  1032. $banned_sql = ''; 
  1033. if ( !empty( $exclude_banned ) ) 
  1034. $banned_sql = " AND is_banned = 0"; 
  1035.  
  1036. $exclude_sql = ''; 
  1037. if ( !empty( $exclude ) ) { 
  1038. $exclude = implode( ', ', wp_parse_id_list( $exclude ) ); 
  1039. $exclude_sql = " AND m.user_id NOT IN ({$exclude})"; 
  1040.  
  1041. $bp = buddypress(); 
  1042.  
  1043. if ( bp_is_active( 'xprofile' ) ) { 
  1044.  
  1045. /** 
  1046. * Filters the SQL prepared statement used to fetch group members. 
  1047. * @since 1.5.0 
  1048. * @param string $value SQL prepared statement for fetching group members. 
  1049. */ 
  1050. $members = $wpdb->get_results( apply_filters( 'bp_group_members_user_join_filter', $wpdb->prepare( "SELECT m.user_id, m.date_modified, m.is_banned, u.user_login, u.user_nicename, u.user_email, pd.value as display_name FROM {$bp->groups->table_name_members} m, {$wpdb->users} u, {$bp->profile->table_name_data} pd WHERE u.ID = m.user_id AND u.ID = pd.user_id AND pd.field_id = 1 AND group_id = %d AND is_confirmed = 1 {$banned_sql} {$exclude_admins_sql} {$exclude_sql} ORDER BY m.date_modified DESC {$pag_sql}", $group_id ) ) ); 
  1051. } else { 
  1052.  
  1053. /** This filter is documented in bp-groups/bp-groups-classes */ 
  1054. $members = $wpdb->get_results( apply_filters( 'bp_group_members_user_join_filter', $wpdb->prepare( "SELECT m.user_id, m.date_modified, m.is_banned, u.user_login, u.user_nicename, u.user_email, u.display_name FROM {$bp->groups->table_name_members} m, {$wpdb->users} u WHERE u.ID = m.user_id AND group_id = %d AND is_confirmed = 1 {$banned_sql} {$exclude_admins_sql} {$exclude_sql} ORDER BY m.date_modified DESC {$pag_sql}", $group_id ) ) ); 
  1055.  
  1056. if ( empty( $members ) ) { 
  1057. return false; 
  1058.  
  1059. if ( empty( $pag_sql ) ) { 
  1060. $total_member_count = count( $members ); 
  1061. } else { 
  1062.  
  1063. /** 
  1064. * Filters the SQL prepared statement used to fetch group members total count. 
  1065. * @since 1.5.0 
  1066. * @param string $value SQL prepared statement for fetching group member count. 
  1067. */ 
  1068. $total_member_count = $wpdb->get_var( apply_filters( 'bp_group_members_count_user_join_filter', $wpdb->prepare( "SELECT COUNT(user_id) FROM {$bp->groups->table_name_members} m WHERE group_id = %d AND is_confirmed = 1 {$banned_sql} {$exclude_admins_sql} {$exclude_sql}", $group_id ) ) ); 
  1069.  
  1070. // Fetch whether or not the user is a friend. 
  1071. foreach ( (array) $members as $user ) 
  1072. $user_ids[] = $user->user_id; 
  1073.  
  1074. $user_ids = implode( ', ', wp_parse_id_list( $user_ids ) ); 
  1075.  
  1076. if ( bp_is_active( 'friends' ) ) { 
  1077. $friend_status = $wpdb->get_results( $wpdb->prepare( "SELECT initiator_user_id, friend_user_id, is_confirmed FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d AND friend_user_id IN ( {$user_ids} ) ) OR (initiator_user_id IN ( {$user_ids} ) AND friend_user_id = %d )", bp_loggedin_user_id(), bp_loggedin_user_id() ) ); 
  1078. for ( $i = 0, $count = count( $members ); $i < $count; ++$i ) { 
  1079. foreach ( (array) $friend_status as $status ) { 
  1080. if ( $status->initiator_user_id == $members[$i]->user_id || $status->friend_user_id == $members[$i]->user_id ) { 
  1081. $members[$i]->is_friend = $status->is_confirmed; 
  1082.  
  1083. return array( 'members' => $members, 'count' => $total_member_count ); 
  1084.  
  1085. /** 
  1086. * Get all membership IDs for a user. 
  1087. * @since 2.6.0 
  1088. * @param int $user_id ID of the user. 
  1089. * @return array 
  1090. */ 
  1091. public static function get_membership_ids_for_user( $user_id ) { 
  1092. global $wpdb; 
  1093.  
  1094. $bp = buddypress(); 
  1095.  
  1096. $group_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d ORDER BY id ASC", $user_id ) ); 
  1097.  
  1098. return $group_ids; 
  1099.  
  1100. /** 
  1101. * Delete all memberships for a given group. 
  1102. * @since 1.6.0 
  1103. * @param int $group_id ID of the group. 
  1104. * @return int Number of records deleted. 
  1105. */ 
  1106. public static function delete_all( $group_id ) { 
  1107. global $wpdb; 
  1108.  
  1109. $bp = buddypress(); 
  1110.  
  1111. return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE group_id = %d", $group_id ) ); 
  1112.  
  1113. /** 
  1114. * Delete all group membership information for the specified user. 
  1115. * @since 1.0.0 
  1116. * @param int $user_id ID of the user. 
  1117. * @return mixed 
  1118. */ 
  1119. public static function delete_all_for_user( $user_id ) { 
  1120. global $wpdb; 
  1121.  
  1122. $bp = buddypress(); 
  1123.  
  1124. // Get all the group ids for the current user's groups and update counts. 
  1125. $group_ids = BP_Groups_Member::get_group_ids( $user_id ); 
  1126. foreach ( $group_ids['groups'] as $group_id ) { 
  1127. groups_update_groupmeta( $group_id, 'total_member_count', groups_get_total_member_count( $group_id ) - 1 ); 
  1128.  
  1129. // If current user is the creator of a group and is the sole admin, delete that group to avoid counts going out-of-sync. 
  1130. if ( groups_is_user_admin( $user_id, $group_id ) && count( groups_get_group_admins( $group_id ) ) < 2 && groups_is_user_creator( $user_id, $group_id ) ) 
  1131. groups_delete_group( $group_id ); 
  1132.  
  1133. return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d", $user_id ) );