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