/bp-groups/classes/class-bp-groups-member.php

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