/bp-groups/bp-groups-functions.php

  1. <?php 
  2. /** 
  3. * BuddyPress Groups Functions. 
  4. * 
  5. * Functions are where all the magic happens in BuddyPress. They will 
  6. * handle the actual saving or manipulation of information. Usually they will 
  7. * hand off to a database class for data access, then return 
  8. * true or false on success or failure. 
  9. * 
  10. * @package BuddyPress 
  11. * @subpackage GroupsFunctions 
  12. * @since 1.5.0 
  13. */ 
  14.  
  15. // Exit if accessed directly. 
  16. defined( 'ABSPATH' ) || exit; 
  17.  
  18. /** 
  19. * Check whether there is a Groups directory page in the $bp global. 
  20. * 
  21. * @since 1.5.0 
  22. * 
  23. * @return bool True if set, False if empty. 
  24. */ 
  25. function bp_groups_has_directory() { 
  26. $bp = buddypress(); 
  27.  
  28. return (bool) !empty( $bp->pages->groups->id ); 
  29.  
  30. /** 
  31. * Fetch a single group object. 
  32. * 
  33. * When calling up a group object, you should always use this function instead 
  34. * of instantiating BP_Groups_Group directly, so that you will inherit cache 
  35. * support and pass through the groups_get_group filter. 
  36. * 
  37. * @since 1.2.0 
  38. * @since 2.7.0 The function signature was changed to accept a group ID only,  
  39. * instead of an array containing the group ID. 
  40. * 
  41. * @param int $group_id ID of the group. 
  42. * @return BP_Groups_Group $group The group object. 
  43. */ 
  44. function groups_get_group( $group_id ) { 
  45. /** 
  46. * Backward compatibilty. 
  47. * Old-style arguments take the form of an array or a query string. 
  48. */ 
  49. if ( ! is_numeric( $group_id ) ) { 
  50. $r = wp_parse_args( $group_id, array( 
  51. 'group_id' => false,  
  52. 'load_users' => false,  
  53. 'populate_extras' => false,  
  54. ) ); 
  55.  
  56. $group_id = $r['group_id']; 
  57.  
  58. $group = new BP_Groups_Group( $group_id ); 
  59.  
  60. /** 
  61. * Filters a single group object. 
  62. * 
  63. * @since 1.2.0 
  64. * 
  65. * @param BP_Groups_Group $group Single group object. 
  66. */ 
  67. return apply_filters( 'groups_get_group', $group ); 
  68.  
  69. /** Group Creation, Editing & Deletion ****************************************/ 
  70.  
  71. /** 
  72. * Create a group. 
  73. * 
  74. * @since 1.0.0 
  75. * 
  76. * @param array|string $args { 
  77. * An array of arguments. 
  78. * @type int|bool $group_id Pass a group ID to update an existing item, or 
  79. * 0 / false to create a new group. Default: 0. 
  80. * @type int $creator_id The user ID that creates the group. 
  81. * @type string $name The group name. 
  82. * @type string $description Optional. The group's description. 
  83. * @type string $slug The group slug. 
  84. * @type string $status The group's status. Accepts 'public', 'private' or 
  85. * 'hidden'. Defaults to 'public'. 
  86. * @type int $parent_id The ID of the parent group. Default: 0. 
  87. * @type int $enable_forum Optional. Whether the group has a forum enabled. 
  88. * If the legacy forums are enabled for this group 
  89. * or if a bbPress forum is enabled for the group,  
  90. * set this to 1. Default: 0. 
  91. * @type string $date_created The GMT time, in Y-m-d h:i:s format, when the group 
  92. * was created. Defaults to the current time. 
  93. * } 
  94. * @return int|bool The ID of the group on success. False on error. 
  95. */ 
  96. function groups_create_group( $args = '' ) { 
  97.  
  98. $defaults = array( 
  99. 'group_id' => 0,  
  100. 'creator_id' => 0,  
  101. 'name' => '',  
  102. 'description' => '',  
  103. 'slug' => '',  
  104. 'status' => 'public',  
  105. 'parent_id' => 0,  
  106. 'enable_forum' => 0,  
  107. 'date_created' => bp_core_current_time() 
  108. ); 
  109.  
  110. $args = wp_parse_args( $args, $defaults ); 
  111. extract( $args, EXTR_SKIP ); 
  112.  
  113. // Pass an existing group ID. 
  114. if ( ! empty( $group_id ) ) { 
  115. $group = groups_get_group( $group_id ); 
  116. $name = ! empty( $name ) ? $name : $group->name; 
  117. $slug = ! empty( $slug ) ? $slug : $group->slug; 
  118. $description = ! empty( $description ) ? $description : $group->description; 
  119.  
  120. // Groups need at least a name. 
  121. if ( empty( $name ) ) { 
  122. return false; 
  123.  
  124. // Create a new group. 
  125. } else { 
  126. // Instantiate new group object. 
  127. $group = new BP_Groups_Group; 
  128.  
  129. // Set creator ID. 
  130. if ( $creator_id ) { 
  131. $group->creator_id = (int) $creator_id; 
  132. } elseif ( is_user_logged_in() ) { 
  133. $group->creator_id = bp_loggedin_user_id(); 
  134.  
  135. if ( ! $group->creator_id ) { 
  136. return false; 
  137.  
  138. // Validate status. 
  139. if ( ! groups_is_valid_status( $status ) ) { 
  140. return false; 
  141.  
  142. // Set group name. 
  143. $group->name = $name; 
  144. $group->description = $description; 
  145. $group->slug = $slug; 
  146. $group->status = $status; 
  147. $group->parent_id = $parent_id; 
  148. $group->enable_forum = (int) $enable_forum; 
  149. $group->date_created = $date_created; 
  150.  
  151. // Save group. 
  152. if ( ! $group->save() ) { 
  153. return false; 
  154.  
  155. // If this is a new group, set up the creator as the first member and admin. 
  156. if ( empty( $group_id ) ) { 
  157. $member = new BP_Groups_Member; 
  158. $member->group_id = $group->id; 
  159. $member->user_id = $group->creator_id; 
  160. $member->is_admin = 1; 
  161. $member->user_title = __( 'Group Admin', 'buddypress' ); 
  162. $member->is_confirmed = 1; 
  163. $member->date_modified = bp_core_current_time(); 
  164. $member->save(); 
  165.  
  166. /** 
  167. * Fires after the creation of a new group and a group creator needs to be made. 
  168. * 
  169. * @since 1.5.0 
  170. * 
  171. * @param int $id ID of the newly created group. 
  172. * @param BP_Groups_Member $member Instance of the member who is assigned 
  173. * as group creator. 
  174. * @param BP_Groups_Group $group Instance of the group being created. 
  175. */ 
  176. do_action( 'groups_create_group', $group->id, $member, $group ); 
  177.  
  178. } else { 
  179.  
  180. /** 
  181. * Fires after the update of a group. 
  182. * 
  183. * @since 1.5.0 
  184. * 
  185. * @param int $id ID of the updated group. 
  186. * @param BP_Groups_Group $group Instance of the group being updated. 
  187. */ 
  188. do_action( 'groups_update_group', $group->id, $group ); 
  189.  
  190. /** 
  191. * Fires after the creation or update of a group. 
  192. * 
  193. * @since 1.0.0 
  194. * 
  195. * @param int $id ID of the newly created group. 
  196. * @param BP_Groups_Group $group Instance of the group being updated. 
  197. */ 
  198. do_action( 'groups_created_group', $group->id, $group ); 
  199.  
  200. return $group->id; 
  201.  
  202. /** 
  203. * Edit the base details for a group. 
  204. * 
  205. * These are the settings that appear on the first page of the group's Admin 
  206. * section (Name, Description, and "Notify members..."). 
  207. * 
  208. * @since 1.0.0 
  209. * 
  210. * @param int $group_id ID of the group. 
  211. * @param string $group_name Name of the group. 
  212. * @param string $group_desc Description of the group. 
  213. * @param bool $notify_members Whether to send an email notification to group 
  214. * members about changes in these details. 
  215. * @return bool True on success, false on failure. 
  216. */ 
  217. function groups_edit_base_group_details( $group_id, $group_name, $group_desc, $notify_members ) { 
  218.  
  219. if ( empty( $group_name ) || empty( $group_desc ) ) 
  220. return false; 
  221.  
  222. $group = groups_get_group( $group_id ); 
  223. $old_group = clone $group; 
  224.  
  225. $group->name = $group_name; 
  226. $group->description = $group_desc; 
  227.  
  228. if ( !$group->save() ) 
  229. return false; 
  230.  
  231. if ( $notify_members ) { 
  232. groups_notification_group_updated( $group->id, $old_group ); 
  233.  
  234. /** 
  235. * Fired after a group's details are updated. 
  236. * 
  237. * @since 2.2.0 
  238. * 
  239. * @param int $value ID of the group. 
  240. * @param BP_Groups_Group $old_group Group object, before being modified. 
  241. * @param bool $notify_members Whether to send an email notification to members about the change. 
  242. */ 
  243. do_action( 'groups_details_updated', $group->id, $old_group, $notify_members ); 
  244.  
  245. return true; 
  246.  
  247. /** 
  248. * Edit the base details for a group. 
  249. * 
  250. * These are the settings that appear on the Settings page of the group's Admin 
  251. * section (privacy settings, "enable forum", invitation status). 
  252. * 
  253. * @since 1.0.0 
  254. * 
  255. * @param int $group_id ID of the group. 
  256. * @param bool $enable_forum Whether to enable a forum for the group. 
  257. * @param string $status Group status. 'public', 'private', 'hidden'. 
  258. * @param string|bool $invite_status Optional. Who is allowed to send invitations 
  259. * to the group. 'members', 'mods', or 'admins'. 
  260. * @return bool True on success, false on failure. 
  261. */ 
  262. function groups_edit_group_settings( $group_id, $enable_forum, $status, $invite_status = false, $parent_id = false ) { 
  263.  
  264. $group = groups_get_group( $group_id ); 
  265. $group->enable_forum = $enable_forum; 
  266.  
  267. /** 
  268. * Before we potentially switch the group status, if it has been changed to public 
  269. * from private and there are outstanding membership requests, auto-accept those requests. 
  270. */ 
  271. if ( 'private' == $group->status && 'public' == $status ) 
  272. groups_accept_all_pending_membership_requests( $group->id ); 
  273.  
  274. // Now update the status. 
  275. $group->status = $status; 
  276.  
  277. // Update the parent ID if necessary. 
  278. if ( false !== $parent_id ) { 
  279. $group->parent_id = $parent_id; 
  280.  
  281. if ( !$group->save() ) 
  282. return false; 
  283.  
  284. // If forums have been enabled, and a forum does not yet exist, we need to create one. 
  285. if ( $group->enable_forum ) { 
  286. if ( bp_is_active( 'forums' ) && !groups_get_groupmeta( $group->id, 'forum_id' ) ) { 
  287. groups_new_group_forum( $group->id, $group->name, $group->description ); 
  288.  
  289. // Set the invite status. 
  290. if ( $invite_status ) 
  291. groups_update_groupmeta( $group->id, 'invite_status', $invite_status ); 
  292.  
  293. groups_update_groupmeta( $group->id, 'last_activity', bp_core_current_time() ); 
  294.  
  295. /** 
  296. * Fires after the update of a groups settings. 
  297. * 
  298. * @since 1.0.0 
  299. * 
  300. * @param int $id ID of the group that was updated. 
  301. */ 
  302. do_action( 'groups_settings_updated', $group->id ); 
  303.  
  304. return true; 
  305.  
  306. /** 
  307. * Delete a group and all of its associated metadata. 
  308. * 
  309. * @since 1.0.0 
  310. * 
  311. * @param int $group_id ID of the group to delete. 
  312. * @return bool True on success, false on failure. 
  313. */ 
  314. function groups_delete_group( $group_id ) { 
  315.  
  316. /** 
  317. * Fires before the deletion of a group. 
  318. * 
  319. * @since 1.5.0 
  320. * 
  321. * @param int $group_id ID of the group to be deleted. 
  322. */ 
  323. do_action( 'groups_before_delete_group', $group_id ); 
  324.  
  325. // Get the group object. 
  326. $group = groups_get_group( $group_id ); 
  327.  
  328. // Bail if group cannot be deleted. 
  329. if ( ! $group->delete() ) { 
  330. return false; 
  331.  
  332. // Remove all outstanding invites for this group. 
  333. groups_delete_all_group_invites( $group_id ); 
  334.  
  335. /** 
  336. * Fires after the deletion of a group. 
  337. * 
  338. * @since 1.0.0 
  339. * 
  340. * @param int $group_id ID of the group that was deleted. 
  341. */ 
  342. do_action( 'groups_delete_group', $group_id ); 
  343.  
  344. return true; 
  345.  
  346. /** 
  347. * Check a group status (eg 'private') against the whitelist of registered statuses. 
  348. * 
  349. * @since 1.1.0 
  350. * 
  351. * @param string $status Status to check. 
  352. * @return bool True if status is allowed, otherwise false. 
  353. */ 
  354. function groups_is_valid_status( $status ) { 
  355. $bp = buddypress(); 
  356.  
  357. return in_array( $status, (array) $bp->groups->valid_status ); 
  358.  
  359. /** 
  360. * Provide a unique, sanitized version of a group slug. 
  361. * 
  362. * @since 1.0.0 
  363. * 
  364. * @param string $slug Group slug to check. 
  365. * @return string $slug A unique and sanitized slug. 
  366. */ 
  367. function groups_check_slug( $slug ) { 
  368. $bp = buddypress(); 
  369.  
  370. if ( 'wp' == substr( $slug, 0, 2 ) ) 
  371. $slug = substr( $slug, 2, strlen( $slug ) - 2 ); 
  372.  
  373. if ( in_array( $slug, (array) $bp->groups->forbidden_names ) ) 
  374. $slug = $slug . '-' . rand(); 
  375.  
  376. if ( BP_Groups_Group::check_slug( $slug ) ) { 
  377. do { 
  378. $slug = $slug . '-' . rand(); 
  379. while ( BP_Groups_Group::check_slug( $slug ) ); 
  380.  
  381. return $slug; 
  382.  
  383. /** 
  384. * Get a group slug by its ID. 
  385. * 
  386. * @since 1.0.0 
  387. * 
  388. * @param int $group_id The numeric ID of the group. 
  389. * @return string The group's slug. 
  390. */ 
  391. function groups_get_slug( $group_id ) { 
  392. $group = groups_get_group( $group_id ); 
  393. return !empty( $group->slug ) ? $group->slug : ''; 
  394.  
  395. /** 
  396. * Get a group ID by its slug. 
  397. * 
  398. * @since 1.6.0 
  399. * 
  400. * @param string $group_slug The group's slug. 
  401. * @return int|null The group ID on success; null on failure. 
  402. */ 
  403. function groups_get_id( $group_slug ) { 
  404. return BP_Groups_Group::group_exists( $group_slug ); 
  405.  
  406. /** User Actions **************************************************************/ 
  407.  
  408. /** 
  409. * Remove a user from a group. 
  410. * 
  411. * @since 1.0.0 
  412. * 
  413. * @param int $group_id ID of the group. 
  414. * @param int $user_id Optional. ID of the user. Defaults to the currently 
  415. * logged-in user. 
  416. * @return bool True on success, false on failure. 
  417. */ 
  418. function groups_leave_group( $group_id, $user_id = 0 ) { 
  419.  
  420. if ( empty( $user_id ) ) 
  421. $user_id = bp_loggedin_user_id(); 
  422.  
  423. // Don't let single admins leave the group. 
  424. if ( count( groups_get_group_admins( $group_id ) ) < 2 ) { 
  425. if ( groups_is_user_admin( $user_id, $group_id ) ) { 
  426. bp_core_add_message( __( 'As the only admin, you cannot leave the group.', 'buddypress' ), 'error' ); 
  427. return false; 
  428.  
  429. if ( ! BP_Groups_Member::delete( $user_id, $group_id ) ) { 
  430. return false; 
  431.  
  432. bp_core_add_message( __( 'You successfully left the group.', 'buddypress' ) ); 
  433.  
  434. /** 
  435. * Fires after a user leaves a group. 
  436. * 
  437. * @since 1.0.0 
  438. * 
  439. * @param int $group_id ID of the group. 
  440. * @param int $user_id ID of the user leaving the group. 
  441. */ 
  442. do_action( 'groups_leave_group', $group_id, $user_id ); 
  443.  
  444. return true; 
  445.  
  446. /** 
  447. * Add a user to a group. 
  448. * 
  449. * @since 1.0.0 
  450. * 
  451. * @param int $group_id ID of the group. 
  452. * @param int $user_id Optional. ID of the user. Defaults to the currently 
  453. * logged-in user. 
  454. * @return bool True on success, false on failure. 
  455. */ 
  456. function groups_join_group( $group_id, $user_id = 0 ) { 
  457.  
  458. if ( empty( $user_id ) ) 
  459. $user_id = bp_loggedin_user_id(); 
  460.  
  461. // Check if the user has an outstanding invite. If so, delete it. 
  462. if ( groups_check_user_has_invite( $user_id, $group_id ) ) 
  463. groups_delete_invite( $user_id, $group_id ); 
  464.  
  465. // Check if the user has an outstanding request. If so, delete it. 
  466. if ( groups_check_for_membership_request( $user_id, $group_id ) ) 
  467. groups_delete_membership_request( null, $user_id, $group_id ); 
  468.  
  469. // User is already a member, just return true. 
  470. if ( groups_is_user_member( $user_id, $group_id ) ) 
  471. return true; 
  472.  
  473. $new_member = new BP_Groups_Member; 
  474. $new_member->group_id = $group_id; 
  475. $new_member->user_id = $user_id; 
  476. $new_member->inviter_id = 0; 
  477. $new_member->is_admin = 0; 
  478. $new_member->user_title = ''; 
  479. $new_member->date_modified = bp_core_current_time(); 
  480. $new_member->is_confirmed = 1; 
  481.  
  482. if ( !$new_member->save() ) 
  483. return false; 
  484.  
  485. $bp = buddypress(); 
  486.  
  487. if ( !isset( $bp->groups->current_group ) || !$bp->groups->current_group || $group_id != $bp->groups->current_group->id ) 
  488. $group = groups_get_group( $group_id ); 
  489. else 
  490. $group = $bp->groups->current_group; 
  491.  
  492. // Record this in activity streams. 
  493. groups_record_activity( array( 
  494. 'type' => 'joined_group',  
  495. 'item_id' => $group_id,  
  496. 'user_id' => $user_id,  
  497. ) ); 
  498.  
  499. /** 
  500. * Fires after a user joins a group. 
  501. * 
  502. * @since 1.0.0 
  503. * 
  504. * @param int $group_id ID of the group. 
  505. * @param int $user_id ID of the user joining the group. 
  506. */ 
  507. do_action( 'groups_join_group', $group_id, $user_id ); 
  508.  
  509. return true; 
  510.  
  511. /** General Group Functions ***************************************************/ 
  512.  
  513. /** 
  514. * Get a list of group administrators. 
  515. * 
  516. * @since 1.0.0 
  517. * 
  518. * @param int $group_id ID of the group. 
  519. * @return array Info about group admins (user_id + date_modified). 
  520. */ 
  521. function groups_get_group_admins( $group_id ) { 
  522. return BP_Groups_Member::get_group_administrator_ids( $group_id ); 
  523.  
  524. /** 
  525. * Get a list of group moderators. 
  526. * 
  527. * @since 1.0.0 
  528. * 
  529. * @param int $group_id ID of the group. 
  530. * @return array Info about group admins (user_id + date_modified). 
  531. */ 
  532. function groups_get_group_mods( $group_id ) { 
  533. return BP_Groups_Member::get_group_moderator_ids( $group_id ); 
  534.  
  535. /** 
  536. * Fetch the members of a group. 
  537. * 
  538. * Since BuddyPress 1.8, a procedural wrapper for BP_Group_Member_Query. 
  539. * Previously called BP_Groups_Member::get_all_for_group(). 
  540. * 
  541. * To use the legacy query, filter 'bp_use_legacy_group_member_query',  
  542. * returning true. 
  543. * 
  544. * @since 1.0.0 
  545. * 
  546. * @param array $args { 
  547. * An array of optional arguments. 
  548. * @type int $group_id ID of the group whose members are being queried. 
  549. * Default: current group ID. 
  550. * @type int $page Page of results to be queried. Default: 1. 
  551. * @type int $per_page Number of items to return per page of results. 
  552. * Default: 20. 
  553. * @type int $max Optional. Max number of items to return. 
  554. * @type array $exclude Optional. Array of user IDs to exclude. 
  555. * @type bool|int $value True (or 1) to exclude admins and mods from results. 
  556. * Default: 1. 
  557. * @type bool|int $value True (or 1) to exclude banned users from results. 
  558. * Default: 1. 
  559. * @type array $group_role Optional. Array of group roles to include. 
  560. * @type string $search_terms Optional. Filter results by a search string. 
  561. * @type string $type Optional. Sort the order of results. 'last_joined',  
  562. * 'first_joined', or any of the $type params available 
  563. * in {@link BP_User_Query}. Default: 'last_joined'. 
  564. * } 
  565. * @return array Multi-d array of 'members' list and 'count'. 
  566. */ 
  567. function groups_get_group_members( $args = array() ) { 
  568.  
  569. // Backward compatibility with old method of passing arguments. 
  570. if ( ! is_array( $args ) || func_num_args() > 1 ) { 
  571. _deprecated_argument( __METHOD__, '2.0.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) ); 
  572.  
  573. $old_args_keys = array( 
  574. 0 => 'group_id',  
  575. 1 => 'per_page',  
  576. 2 => 'page',  
  577. 3 => 'exclude_admins_mods',  
  578. 4 => 'exclude_banned',  
  579. 5 => 'exclude',  
  580. 6 => 'group_role',  
  581. ); 
  582.  
  583. $func_args = func_get_args(); 
  584. $args = bp_core_parse_args_array( $old_args_keys, $func_args ); 
  585.  
  586. $r = wp_parse_args( $args, array( 
  587. 'group_id' => bp_get_current_group_id(),  
  588. 'per_page' => false,  
  589. 'page' => false,  
  590. 'exclude_admins_mods' => true,  
  591. 'exclude_banned' => true,  
  592. 'exclude' => false,  
  593. 'group_role' => array(),  
  594. 'search_terms' => false,  
  595. 'type' => 'last_joined',  
  596. ) ); 
  597.  
  598. // For legacy users. Use of BP_Groups_Member::get_all_for_group() 
  599. // is deprecated. func_get_args() can't be passed to a function in PHP 
  600. // 5.2.x, so we create a variable. 
  601. $func_args = func_get_args(); 
  602. if ( apply_filters( 'bp_use_legacy_group_member_query', false, __FUNCTION__, $func_args ) ) { 
  603. $retval = BP_Groups_Member::get_all_for_group( $r['group_id'], $r['per_page'], $r['page'], $r['exclude_admins_mods'], $r['exclude_banned'], $r['exclude'] ); 
  604. } else { 
  605.  
  606. // Both exclude_admins_mods and exclude_banned are legacy arguments. 
  607. // Convert to group_role. 
  608. if ( empty( $r['group_role'] ) ) { 
  609. $r['group_role'] = array( 'member' ); 
  610.  
  611. if ( ! $r['exclude_admins_mods'] ) { 
  612. $r['group_role'][] = 'mod'; 
  613. $r['group_role'][] = 'admin'; 
  614.  
  615. if ( ! $r['exclude_banned'] ) { 
  616. $r['group_role'][] = 'banned'; 
  617.  
  618. // Perform the group member query (extends BP_User_Query). 
  619. $members = new BP_Group_Member_Query( array( 
  620. 'group_id' => $r['group_id'],  
  621. 'per_page' => $r['per_page'],  
  622. 'page' => $r['page'],  
  623. 'group_role' => $r['group_role'],  
  624. 'exclude' => $r['exclude'],  
  625. 'search_terms' => $r['search_terms'],  
  626. 'type' => $r['type'],  
  627. ) ); 
  628.  
  629. // Structure the return value as expected by the template functions. 
  630. $retval = array( 
  631. 'members' => array_values( $members->results ),  
  632. 'count' => $members->total_users,  
  633. ); 
  634.  
  635. return $retval; 
  636.  
  637. /** 
  638. * Get the member count for a group. 
  639. * 
  640. * @since 1.2.3 
  641. * 
  642. * @param int $group_id Group ID. 
  643. * @return int Count of confirmed members for the group. 
  644. */ 
  645. function groups_get_total_member_count( $group_id ) { 
  646. return BP_Groups_Group::get_total_member_count( $group_id ); 
  647.  
  648. /** Group Fetching, Filtering & Searching ************************************/ 
  649.  
  650. /** 
  651. * Get a collection of groups, based on the parameters passed. 
  652. * 
  653. * @since 1.2.0 
  654. * @since 2.6.0 Added `$group_type`, `$group_type__in`, and `$group_type__not_in` parameters. 
  655. * @since 2.7.0 Added `$update_admin_cache` and `$parent_id` parameters. 
  656. * 
  657. * @param array|string $args { 
  658. * Array of arguments. Supports all arguments of 
  659. * {@link BP_Groups_Group::get()}. Where the default values differ, they 
  660. * have been described here. 
  661. * @type int $per_page Default: 20. 
  662. * @type int $page Default: 1. 
  663. * } 
  664. * @return array See {@link BP_Groups_Group::get()}. 
  665. */ 
  666. function groups_get_groups( $args = '' ) { 
  667.  
  668. $defaults = array( 
  669. 'type' => false, // Active, newest, alphabetical, random, popular, most-forum-topics or most-forum-posts. 
  670. 'order' => 'DESC', // 'ASC' or 'DESC' 
  671. 'orderby' => 'date_created', // date_created, last_activity, total_member_count, name, random. 
  672. 'user_id' => false, // Pass a user_id to limit to only groups that this user is a member of. 
  673. 'include' => false, // Only include these specific groups (group_ids). 
  674. 'exclude' => false, // Do not include these specific groups (group_ids). 
  675. 'parent_id' => null, // Get groups that are children of the specified group(s). 
  676. 'search_terms' => false, // Limit to groups that match these search terms. 
  677. 'group_type' => '', // Array or comma-separated list of group types to limit results to. 
  678. 'group_type__in' => '', // Array or comma-separated list of group types to limit results to. 
  679. 'group_type__not_in' => '', // Array or comma-separated list of group types that will be excluded from results. 
  680. 'meta_query' => false, // Filter by groupmeta. See WP_Meta_Query for syntax. 
  681. 'show_hidden' => false, // Show hidden groups to non-admins. 
  682. 'per_page' => 20, // The number of results to return per page. 
  683. 'page' => 1, // The page to return if limiting per page. 
  684. 'update_meta_cache' => true, // Pre-fetch groupmeta for queried groups. 
  685. 'update_admin_cache' => false,  
  686. ); 
  687.  
  688. $r = wp_parse_args( $args, $defaults ); 
  689.  
  690. $groups = BP_Groups_Group::get( array( 
  691. 'type' => $r['type'],  
  692. 'user_id' => $r['user_id'],  
  693. 'include' => $r['include'],  
  694. 'exclude' => $r['exclude'],  
  695. 'parent_id' => $r['parent_id'],  
  696. 'search_terms' => $r['search_terms'],  
  697. 'group_type' => $r['group_type'],  
  698. 'group_type__in' => $r['group_type__in'],  
  699. 'group_type__not_in' => $r['group_type__not_in'],  
  700. 'meta_query' => $r['meta_query'],  
  701. 'show_hidden' => $r['show_hidden'],  
  702. 'per_page' => $r['per_page'],  
  703. 'page' => $r['page'],  
  704. 'update_meta_cache' => $r['update_meta_cache'],  
  705. 'update_admin_cache' => $r['update_admin_cache'],  
  706. 'order' => $r['order'],  
  707. 'orderby' => $r['orderby'],  
  708. ) ); 
  709.  
  710. /** 
  711. * Filters the collection of groups based on parsed parameters. 
  712. * 
  713. * @since 1.2.0 
  714. * 
  715. * @param BP_Groups_Group $groups Object of found groups based on parameters. 
  716. * Passed by reference. 
  717. * @param array $r Array of parsed arguments used for group query. 
  718. * Passed by reference. 
  719. */ 
  720. return apply_filters_ref_array( 'groups_get_groups', array( &$groups, &$r ) ); 
  721.  
  722. /** 
  723. * Get the total group count for the site. 
  724. * 
  725. * @since 1.2.0 
  726. * 
  727. * @return int 
  728. */ 
  729. function groups_get_total_group_count() { 
  730. $count = wp_cache_get( 'bp_total_group_count', 'bp' ); 
  731.  
  732. if ( false === $count ) { 
  733. $count = BP_Groups_Group::get_total_group_count(); 
  734. wp_cache_set( 'bp_total_group_count', $count, 'bp' ); 
  735.  
  736. return $count; 
  737.  
  738. /** 
  739. * Get the IDs of the groups of which a specified user is a member. 
  740. * 
  741. * @since 1.0.0 
  742. * 
  743. * @param int $user_id ID of the user. 
  744. * @param int $pag_num Optional. Max number of results to return. 
  745. * Default: false (no limit). 
  746. * @param int $pag_page Optional. Page offset of results to return. 
  747. * Default: false (no limit). 
  748. * @return array { 
  749. * @type array $groups Array of groups returned by paginated query. 
  750. * @type int $total Count of groups matching query. 
  751. * } 
  752. */ 
  753. function groups_get_user_groups( $user_id = 0, $pag_num = 0, $pag_page = 0 ) { 
  754.  
  755. if ( empty( $user_id ) ) 
  756. $user_id = bp_displayed_user_id(); 
  757.  
  758. return BP_Groups_Member::get_group_ids( $user_id, $pag_num, $pag_page ); 
  759.  
  760. /** 
  761. * Get a list of groups of which the specified user is a member. 
  762. * 
  763. * Get a list of the groups to which this member belongs,  
  764. * filtered by group membership status and role. 
  765. * Usage examples: Used with no arguments specified,  
  766. * 
  767. * bp_get_user_groups( bp_loggedin_user_id() ); 
  768. * 
  769. * returns an array of the groups in which the logged-in user 
  770. * is an unpromoted member. To fetch an array of all groups that 
  771. * the current user belongs to, in any membership role,  
  772. * member, moderator or administrator, use 
  773. * 
  774. * bp_get_user_groups( $user_id, array( 
  775. * 'is_admin' => null,  
  776. * 'is_mod' => null,  
  777. * ) ); 
  778. * 
  779. * @since 2.6.0 
  780. * 
  781. * @param int $user_id ID of the user. 
  782. * @param array $args { 
  783. * Array of optional args. 
  784. * @param bool|null $is_confirmed Whether to return only confirmed memberships. Pass `null` to disable this 
  785. * filter. Default: true. 
  786. * @param bool|null $is_banned Whether to return only banned memberships. Pass `null` to disable this filter. 
  787. * Default: false. 
  788. * @param bool|null $is_admin Whether to return only admin memberships. Pass `null` to disable this filter. 
  789. * Default: false. 
  790. * @param bool|null $is_mod Whether to return only mod memberships. Pass `null` to disable this filter. 
  791. * Default: false. 
  792. * @param bool|null $invite_sent Whether to return only memberships with 'invite_sent'. Pass `null` to disable 
  793. * this filter. Default: false. 
  794. * @param string $orderby Field to order by. Accepts 'id' (membership ID), 'group_id', 'date_modified'. 
  795. * Default: 'group_id'. 
  796. * @param string $order Sort order. Accepts 'ASC' or 'DESC'. Default: 'ASC'. 
  797. * } 
  798. * @return array Array of matching group memberships, keyed by group ID. 
  799. */ 
  800. function bp_get_user_groups( $user_id, $args = array() ) { 
  801. $r = bp_parse_args( $args, array( 
  802. 'is_confirmed' => true,  
  803. 'is_banned' => false,  
  804. 'is_admin' => false,  
  805. 'is_mod' => false,  
  806. 'invite_sent' => null,  
  807. 'orderby' => 'group_id',  
  808. 'order' => 'ASC',  
  809. ), 'get_user_groups' ); 
  810.  
  811. $user_id = intval( $user_id ); 
  812.  
  813. $membership_ids = wp_cache_get( $user_id, 'bp_groups_memberships_for_user' ); 
  814. if ( false === $membership_ids ) { 
  815. $membership_ids = BP_Groups_Member::get_membership_ids_for_user( $user_id ); 
  816. wp_cache_set( $user_id, $membership_ids, 'bp_groups_memberships_for_user' ); 
  817.  
  818. // Prime the membership cache. 
  819. $uncached_membership_ids = bp_get_non_cached_ids( $membership_ids, 'bp_groups_memberships' ); 
  820. if ( ! empty( $uncached_membership_ids ) ) { 
  821. $uncached_memberships = BP_Groups_Member::get_memberships_by_id( $uncached_membership_ids ); 
  822.  
  823. foreach ( $uncached_memberships as $uncached_membership ) { 
  824. wp_cache_set( $uncached_membership->id, $uncached_membership, 'bp_groups_memberships' ); 
  825.  
  826. // Assemble filter array for use in `wp_list_filter()`. 
  827. $filters = wp_array_slice_assoc( $r, array( 'is_confirmed', 'is_banned', 'is_admin', 'is_mod', 'invite_sent' ) ); 
  828. foreach ( $filters as $filter_name => $filter_value ) { 
  829. if ( is_null( $filter_value ) ) { 
  830. unset( $filters[ $filter_name ] ); 
  831.  
  832. // Populate group membership array from cache, and normalize. 
  833. $groups = array(); 
  834. $int_keys = array( 'id', 'group_id', 'user_id', 'inviter_id' ); 
  835. $bool_keys = array( 'is_admin', 'is_mod', 'is_confirmed', 'is_banned', 'invite_sent' ); 
  836. foreach ( $membership_ids as $membership_id ) { 
  837. $membership = wp_cache_get( $membership_id, 'bp_groups_memberships' ); 
  838.  
  839. // Sanity check. 
  840. if ( ! isset( $membership->group_id ) ) { 
  841. continue; 
  842.  
  843. // Integer values. 
  844. foreach ( $int_keys as $index ) { 
  845. $membership->{$index} = intval( $membership->{$index} ); 
  846.  
  847. // Boolean values. 
  848. foreach ( $bool_keys as $index ) { 
  849. $membership->{$index} = (bool) $membership->{$index}; 
  850.  
  851. foreach ( $filters as $filter_name => $filter_value ) { 
  852. if ( ! isset( $membership->{$filter_name} ) || $filter_value != $membership->{$filter_name} ) { 
  853. continue 2; 
  854.  
  855. $group_id = (int) $membership->group_id; 
  856.  
  857. $groups[ $group_id ] = $membership; 
  858.  
  859. // By default, results are ordered by membership id. 
  860. if ( 'group_id' === $r['orderby'] ) { 
  861. ksort( $groups ); 
  862. } elseif ( in_array( $r['orderby'], array( 'id', 'date_modified' ) ) ) { 
  863. $groups = bp_sort_by_key( $groups, $r['orderby'] ); 
  864.  
  865. // By default, results are ordered ASC. 
  866. if ( 'DESC' === strtoupper( $r['order'] ) ) { 
  867. // `true` to preserve keys. 
  868. $groups = array_reverse( $groups, true ); 
  869.  
  870. return $groups; 
  871.  
  872. /** 
  873. * Get the count of groups of which the specified user is a member. 
  874. * 
  875. * @since 1.0.0 
  876. * 
  877. * @param int $user_id Optional. Default: ID of the displayed user. 
  878. * @return int Group count. 
  879. */ 
  880. function groups_total_groups_for_user( $user_id = 0 ) { 
  881.  
  882. if ( empty( $user_id ) ) 
  883. $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id(); 
  884.  
  885. $count = wp_cache_get( 'bp_total_groups_for_user_' . $user_id, 'bp' ); 
  886.  
  887. if ( false === $count ) { 
  888. $count = BP_Groups_Member::total_group_count( $user_id ); 
  889. wp_cache_set( 'bp_total_groups_for_user_' . $user_id, $count, 'bp' ); 
  890.  
  891. return (int) $count; 
  892.  
  893. /** 
  894. * Get the BP_Groups_Group object corresponding to the current group. 
  895. * 
  896. * @since 1.5.0 
  897. * 
  898. * @return BP_Groups_Group The current group object. 
  899. */ 
  900. function groups_get_current_group() { 
  901. $bp = buddypress(); 
  902.  
  903. $current_group = isset( $bp->groups->current_group ) 
  904. ? $bp->groups->current_group 
  905. : false; 
  906.  
  907. /** 
  908. * Filters the BP_Groups_Group object corresponding to the current group. 
  909. * 
  910. * @since 1.5.0 
  911. * 
  912. * @param BP_Groups_Group $current_group Current BP_Groups_Group object. 
  913. */ 
  914. return apply_filters( 'groups_get_current_group', $current_group ); 
  915.  
  916. /** Group Avatars *************************************************************/ 
  917.  
  918. /** 
  919. * Generate the avatar upload directory path for a given group. 
  920. * 
  921. * @since 1.1.0 
  922. * 
  923. * @param int $group_id Optional. ID of the group. Default: ID of the current group. 
  924. * @return string 
  925. */ 
  926. function groups_avatar_upload_dir( $group_id = 0 ) { 
  927.  
  928. if ( empty( $group_id ) ) { 
  929. $group_id = bp_get_current_group_id(); 
  930.  
  931. $directory = 'group-avatars'; 
  932. $path = bp_core_avatar_upload_path() . '/' . $directory . '/' . $group_id; 
  933. $newbdir = $path; 
  934. $newurl = bp_core_avatar_url() . '/' . $directory . '/' . $group_id; 
  935. $newburl = $newurl; 
  936. $newsubdir = '/' . $directory . '/' . $group_id; 
  937.  
  938. /** 
  939. * Filters the avatar upload directory path for a given group. 
  940. * 
  941. * @since 1.1.0 
  942. * 
  943. * @param array $value Array of parts related to the groups avatar upload directory. 
  944. */ 
  945. return apply_filters( 'groups_avatar_upload_dir', array( 
  946. 'path' => $path,  
  947. 'url' => $newurl,  
  948. 'subdir' => $newsubdir,  
  949. 'basedir' => $newbdir,  
  950. 'baseurl' => $newburl,  
  951. 'error' => false 
  952. ) ); 
  953.  
  954. /** Group Member Status Checks ************************************************/ 
  955.  
  956. /** 
  957. * Check whether a user is an admin of a given group. 
  958. * 
  959. * @since 1.0.0 
  960. * 
  961. * @param int $user_id ID of the user. 
  962. * @param int $group_id ID of the group. 
  963. * @return int|bool ID of the membership if the user is admin, otherwise false. 
  964. */ 
  965. function groups_is_user_admin( $user_id, $group_id ) { 
  966. $is_admin = false; 
  967.  
  968. $user_groups = bp_get_user_groups( $user_id, array( 
  969. 'is_admin' => true,  
  970. ) ); 
  971.  
  972. if ( isset( $user_groups[ $group_id ] ) ) { 
  973. $is_admin = $user_groups[ $group_id ]->id; 
  974.  
  975. return $is_admin; 
  976.  
  977. /** 
  978. * Check whether a user is a mod of a given group. 
  979. * 
  980. * @since 1.0.0 
  981. * 
  982. * @param int $user_id ID of the user. 
  983. * @param int $group_id ID of the group. 
  984. * @return int|bool ID of the membership if the user is mod, otherwise false. 
  985. */ 
  986. function groups_is_user_mod( $user_id, $group_id ) { 
  987. $is_mod = false; 
  988.  
  989. $user_groups = bp_get_user_groups( $user_id, array( 
  990. 'is_mod' => true,  
  991. ) ); 
  992.  
  993. if ( isset( $user_groups[ $group_id ] ) ) { 
  994. $is_mod = $user_groups[ $group_id ]->id; 
  995.  
  996. return $is_mod; 
  997.  
  998. /** 
  999. * Check whether a user is a member of a given group. 
  1000. * 
  1001. * @since 1.0.0 
  1002. * 
  1003. * @param int $user_id ID of the user. 
  1004. * @param int $group_id ID of the group. 
  1005. * @return int|bool ID of the membership if the user is member, otherwise false. 
  1006. */ 
  1007. function groups_is_user_member( $user_id, $group_id ) { 
  1008. $is_member = false; 
  1009.  
  1010. $user_groups = bp_get_user_groups( $user_id, array( 
  1011. 'is_admin' => null,  
  1012. 'is_mod' => null,  
  1013. ) ); 
  1014.  
  1015. if ( isset( $user_groups[ $group_id ] ) ) { 
  1016. $is_member = $user_groups[ $group_id ]->id; 
  1017.  
  1018. return $is_member; 
  1019.  
  1020. /** 
  1021. * Check whether a user is banned from a given group. 
  1022. * 
  1023. * @since 1.0.0 
  1024. * 
  1025. * @param int $user_id ID of the user. 
  1026. * @param int $group_id ID of the group. 
  1027. * @return int|bool ID of the membership if the user is banned, otherwise false. 
  1028. */ 
  1029. function groups_is_user_banned( $user_id, $group_id ) { 
  1030. $is_banned = false; 
  1031.  
  1032. $user_groups = bp_get_user_groups( $user_id, array( 
  1033. 'is_confirmed' => null,  
  1034. 'is_banned' => true,  
  1035. ) ); 
  1036.  
  1037. if ( isset( $user_groups[ $group_id ] ) ) { 
  1038. $is_banned = $user_groups[ $group_id ]->id; 
  1039.  
  1040. return $is_banned; 
  1041.  
  1042. /** 
  1043. * Check whether a user has an outstanding invitation to a group. 
  1044. * 
  1045. * @since 2.6.0 
  1046. * 
  1047. * @param int $user_id ID of the user. 
  1048. * @param int $group_id ID of the group. 
  1049. * @return int|bool ID of the membership if the user is invited, otherwise false. 
  1050. */ 
  1051. function groups_is_user_invited( $user_id, $group_id ) { 
  1052. $is_invited = false; 
  1053.  
  1054. $user_groups = bp_get_user_groups( $user_id, array( 
  1055. 'invite_sent' => true,  
  1056. 'is_confirmed' => false,  
  1057. ) ); 
  1058.  
  1059. if ( isset( $user_groups[ $group_id ] ) ) { 
  1060. $is_invited = $user_groups[ $group_id ]->id; 
  1061.  
  1062. return $is_invited; 
  1063.  
  1064. /** 
  1065. * Check whether a user has a pending membership request for a group. 
  1066. * 
  1067. * @since 2.6.0 
  1068. * 
  1069. * @param int $user_id ID of the user. 
  1070. * @param int $group_id ID of the group. 
  1071. * @return int|bool ID of the membership if the user is pending, otherwise false. 
  1072. */ 
  1073. function groups_is_user_pending( $user_id, $group_id ) { 
  1074. $is_pending = false; 
  1075.  
  1076. $user_groups = bp_get_user_groups( $user_id, array( 
  1077. 'invite_sent' => false,  
  1078. 'is_confirmed' => false,  
  1079. ) ); 
  1080.  
  1081. if ( isset( $user_groups[ $group_id ] ) ) { 
  1082. $is_pending = $user_groups[ $group_id ]->id; 
  1083.  
  1084. return $is_pending; 
  1085.  
  1086. /** 
  1087. * Is the specified user the creator of the group? 
  1088. * 
  1089. * @since 1.2.6 
  1090. * 
  1091. * @param int $user_id ID of the user. 
  1092. * @param int $group_id ID of the group. 
  1093. * @return bool 
  1094. */ 
  1095. function groups_is_user_creator( $user_id, $group_id ) { 
  1096. return BP_Groups_Member::check_is_creator( $user_id, $group_id ); 
  1097.  
  1098. /** Group Activity Posting ****************************************************/ 
  1099.  
  1100. /** 
  1101. * Post an Activity status update affiliated with a group. 
  1102. * 
  1103. * @since 1.2.0 
  1104. * @since 2.6.0 Added 'error_type' parameter to $args. 
  1105. * 
  1106. * @param array|string $args { 
  1107. * Array of arguments. 
  1108. * @type string $content The content of the update. 
  1109. * @type int $user_id Optional. ID of the user posting the update. Default: 
  1110. * ID of the logged-in user. 
  1111. * @type int $group_id Optional. ID of the group to be affiliated with the 
  1112. * update. Default: ID of the current group. 
  1113. * } 
  1114. * @return int|bool Returns the ID of the new activity item on success, or false on failure. 
  1115. */ 
  1116. function groups_post_update( $args = '' ) { 
  1117. if ( ! bp_is_active( 'activity' ) ) { 
  1118. return false; 
  1119.  
  1120. $bp = buddypress(); 
  1121.  
  1122. $defaults = array( 
  1123. 'content' => false,  
  1124. 'user_id' => bp_loggedin_user_id(),  
  1125. 'group_id' => 0,  
  1126. 'error_type' => 'bool' 
  1127. ); 
  1128.  
  1129. $r = wp_parse_args( $args, $defaults ); 
  1130. extract( $r, EXTR_SKIP ); 
  1131.  
  1132. if ( empty( $group_id ) && !empty( $bp->groups->current_group->id ) ) 
  1133. $group_id = $bp->groups->current_group->id; 
  1134.  
  1135. if ( empty( $content ) || !strlen( trim( $content ) ) || empty( $user_id ) || empty( $group_id ) ) 
  1136. return false; 
  1137.  
  1138. $bp->groups->current_group = groups_get_group( $group_id ); 
  1139.  
  1140. // Be sure the user is a member of the group before posting. 
  1141. if ( !bp_current_user_can( 'bp_moderate' ) && !groups_is_user_member( $user_id, $group_id ) ) 
  1142. return false; 
  1143.  
  1144. // Record this in activity streams. 
  1145. $activity_action = sprintf( __( '%1$s posted an update in the group %2$s', 'buddypress'), bp_core_get_userlink( $user_id ), '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . '">' . esc_attr( $bp->groups->current_group->name ) . '</a>' ); 
  1146. $activity_content = $content; 
  1147.  
  1148. /** 
  1149. * Filters the action for the new group activity update. 
  1150. * 
  1151. * @since 1.2.0 
  1152. * 
  1153. * @param string $activity_action The new group activity update. 
  1154. */ 
  1155. $action = apply_filters( 'groups_activity_new_update_action', $activity_action ); 
  1156.  
  1157. /** 
  1158. * Filters the content for the new group activity update. 
  1159. * 
  1160. * @since 1.2.0 
  1161. * 
  1162. * @param string $activity_content The content of the update. 
  1163. */ 
  1164. $content_filtered = apply_filters( 'groups_activity_new_update_content', $activity_content ); 
  1165.  
  1166. $activity_id = groups_record_activity( array( 
  1167. 'user_id' => $user_id,  
  1168. 'action' => $action,  
  1169. 'content' => $content_filtered,  
  1170. 'type' => 'activity_update',  
  1171. 'item_id' => $group_id,  
  1172. 'error_type' => $error_type 
  1173. ) ); 
  1174.  
  1175. groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() ); 
  1176.  
  1177. /** 
  1178. * Fires after posting of an Activity status update affiliated with a group. 
  1179. * 
  1180. * @since 1.2.0 
  1181. * 
  1182. * @param string $content The content of the update. 
  1183. * @param int $user_id ID of the user posting the update. 
  1184. * @param int $group_id ID of the group being posted to. 
  1185. * @param bool $activity_id Whether or not the activity recording succeeded. 
  1186. */ 
  1187. do_action( 'bp_groups_posted_update', $content, $user_id, $group_id, $activity_id ); 
  1188.  
  1189. return $activity_id; 
  1190.  
  1191. /** Group Invitations *********************************************************/ 
  1192.  
  1193. /** 
  1194. * Get IDs of users with outstanding invites to a given group from a specified user. 
  1195. * 
  1196. * @since 1.0.0 
  1197. * 
  1198. * @param int $user_id ID of the inviting user. 
  1199. * @param int|bool $limit Limit to restrict to. 
  1200. * @param int|bool $page Optional. Page offset of results to return. 
  1201. * @param string|array|bool $exclude Array of comma-separated list of group IDs 
  1202. * to exclude from results. 
  1203. * @return array $value IDs of users who have been invited to the group by the 
  1204. * user but have not yet accepted. 
  1205. */ 
  1206. function groups_get_invites_for_user( $user_id = 0, $limit = false, $page = false, $exclude = false ) { 
  1207.  
  1208. if ( empty( $user_id ) ) 
  1209. $user_id = bp_loggedin_user_id(); 
  1210.  
  1211. return BP_Groups_Member::get_invites( $user_id, $limit, $page, $exclude ); 
  1212.  
  1213. /** 
  1214. * Get the total group invite count for a user. 
  1215. * 
  1216. * @since 2.0.0 
  1217. * 
  1218. * @param int $user_id The user ID. 
  1219. * @return int 
  1220. */ 
  1221. function groups_get_invite_count_for_user( $user_id = 0 ) { 
  1222. if ( empty( $user_id ) ) { 
  1223. $user_id = bp_loggedin_user_id(); 
  1224.  
  1225. return BP_Groups_Member::get_invite_count_for_user( $user_id ); 
  1226.  
  1227. /** 
  1228. * Invite a user to a group. 
  1229. * 
  1230. * @since 1.0.0 
  1231. * 
  1232. * @param array|string $args { 
  1233. * Array of arguments. 
  1234. * @type int $user_id ID of the user being invited. 
  1235. * @type int $group_id ID of the group to which the user is being invited. 
  1236. * @type int $inviter_id Optional. ID of the inviting user. Default: 
  1237. * ID of the logged-in user. 
  1238. * @type string $date_modified Optional. Modified date for the invitation. 
  1239. * Default: current date/time. 
  1240. * @type bool $is_confirmed Optional. Whether the invitation should be 
  1241. * marked confirmed. Default: false. 
  1242. * } 
  1243. * @return bool True on success, false on failure. 
  1244. */ 
  1245. function groups_invite_user( $args = '' ) { 
  1246.  
  1247. $defaults = array( 
  1248. 'user_id' => false,  
  1249. 'group_id' => false,  
  1250. 'inviter_id' => bp_loggedin_user_id(),  
  1251. 'date_modified' => bp_core_current_time(),  
  1252. 'is_confirmed' => 0 
  1253. ); 
  1254.  
  1255. $args = wp_parse_args( $args, $defaults ); 
  1256. extract( $args, EXTR_SKIP ); 
  1257.  
  1258. if ( ! $user_id || ! $group_id || ! $inviter_id ) { 
  1259. return false; 
  1260.  
  1261. // If the user has already requested membership, accept the request. 
  1262. if ( $membership_id = groups_check_for_membership_request( $user_id, $group_id ) ) { 
  1263. groups_accept_membership_request( $membership_id, $user_id, $group_id ); 
  1264.  
  1265. // Otherwise, create a new invitation. 
  1266. } elseif ( ! groups_is_user_member( $user_id, $group_id ) && ! groups_check_user_has_invite( $user_id, $group_id, 'all' ) ) { 
  1267. $invite = new BP_Groups_Member; 
  1268. $invite->group_id = $group_id; 
  1269. $invite->user_id = $user_id; 
  1270. $invite->date_modified = $date_modified; 
  1271. $invite->inviter_id = $inviter_id; 
  1272. $invite->is_confirmed = $is_confirmed; 
  1273.  
  1274. if ( !$invite->save() ) 
  1275. return false; 
  1276.  
  1277. /** 
  1278. * Fires after the creation of a new group invite. 
  1279. * 
  1280. * @since 1.0.0 
  1281. * 
  1282. * @param array $args Array of parsed arguments for the group invite. 
  1283. */ 
  1284. do_action( 'groups_invite_user', $args ); 
  1285.  
  1286. return true; 
  1287.  
  1288. /** 
  1289. * Uninvite a user from a group. 
  1290. * 
  1291. * Functionally, this is equivalent to removing a user from a group. 
  1292. * 
  1293. * @since 1.0.0 
  1294. * 
  1295. * @param int $user_id ID of the user. 
  1296. * @param int $group_id ID of the group. 
  1297. * @return bool True on success, false on failure. 
  1298. */ 
  1299. function groups_uninvite_user( $user_id, $group_id ) { 
  1300.  
  1301. if ( ! BP_Groups_Member::delete_invite( $user_id, $group_id ) ) 
  1302. return false; 
  1303.  
  1304. /** 
  1305. * Fires after uninviting a user from a group. 
  1306. * 
  1307. * @since 1.0.0 
  1308. * 
  1309. * @param int $group_id ID of the group being uninvited from. 
  1310. * @param int $user_id ID of the user being uninvited. 
  1311. */ 
  1312. do_action( 'groups_uninvite_user', $group_id, $user_id ); 
  1313.  
  1314. return true; 
  1315.  
  1316. /** 
  1317. * Process the acceptance of a group invitation. 
  1318. * 
  1319. * Returns true if a user is already a member of the group. 
  1320. * 
  1321. * @since 1.0.0 
  1322. * 
  1323. * @param int $user_id ID of the user. 
  1324. * @param int $group_id ID of the group. 
  1325. * @return bool True when the user is a member of the group, otherwise false. 
  1326. */ 
  1327. function groups_accept_invite( $user_id, $group_id ) { 
  1328.  
  1329. // If the user is already a member (because BP at one point allowed two invitations to 
  1330. // slip through), delete all existing invitations/requests and return true. 
  1331. if ( groups_is_user_member( $user_id, $group_id ) ) { 
  1332. if ( groups_check_user_has_invite( $user_id, $group_id ) ) { 
  1333. groups_delete_invite( $user_id, $group_id ); 
  1334.  
  1335. if ( groups_check_for_membership_request( $user_id, $group_id ) ) { 
  1336. groups_delete_membership_request( null, $user_id, $group_id ); 
  1337.  
  1338. return true; 
  1339.  
  1340. $member = new BP_Groups_Member( $user_id, $group_id ); 
  1341. $member->accept_invite(); 
  1342.  
  1343. if ( !$member->save() ) { 
  1344. return false; 
  1345.  
  1346. // Remove request to join. 
  1347. if ( $member->check_for_membership_request( $user_id, $group_id ) ) { 
  1348. $member->delete_request( $user_id, $group_id ); 
  1349.  
  1350. // Modify group meta. 
  1351. groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() ); 
  1352.  
  1353. /** 
  1354. * Fires after a user has accepted a group invite. 
  1355. * 
  1356. * @since 1.0.0 
  1357. * 
  1358. * @param int $user_id ID of the user who accepted the group invite. 
  1359. * @param int $group_id ID of the group being accepted to. 
  1360. */ 
  1361. do_action( 'groups_accept_invite', $user_id, $group_id ); 
  1362.  
  1363. return true; 
  1364.  
  1365. /** 
  1366. * Reject a group invitation. 
  1367. * 
  1368. * @since 1.0.0 
  1369. * 
  1370. * @param int $user_id ID of the user. 
  1371. * @param int $group_id ID of the group. 
  1372. * @return bool True on success, false on failure. 
  1373. */ 
  1374. function groups_reject_invite( $user_id, $group_id ) { 
  1375. if ( ! BP_Groups_Member::delete_invite( $user_id, $group_id ) ) 
  1376. return false; 
  1377.  
  1378. /** 
  1379. * Fires after a user rejects a group invitation. 
  1380. * 
  1381. * @since 1.0.0 
  1382. * 
  1383. * @param int $user_id ID of the user rejecting the invite. 
  1384. * @param int $group_id ID of the group being rejected. 
  1385. */ 
  1386. do_action( 'groups_reject_invite', $user_id, $group_id ); 
  1387.  
  1388. return true; 
  1389.  
  1390. /** 
  1391. * Delete a group invitation. 
  1392. * 
  1393. * @since 1.0.0 
  1394. * 
  1395. * @param int $user_id ID of the invited user. 
  1396. * @param int $group_id ID of the group. 
  1397. * @return bool True on success, false on failure. 
  1398. */ 
  1399. function groups_delete_invite( $user_id, $group_id ) { 
  1400. if ( ! BP_Groups_Member::delete_invite( $user_id, $group_id ) ) 
  1401. return false; 
  1402.  
  1403. /** 
  1404. * Fires after the deletion of a group invitation. 
  1405. * 
  1406. * @since 1.9.0 
  1407. * 
  1408. * @param int $user_id ID of the user whose invitation is being deleted. 
  1409. * @param int $group_id ID of the group whose invitation is being deleted. 
  1410. */ 
  1411. do_action( 'groups_delete_invite', $user_id, $group_id ); 
  1412.  
  1413. return true; 
  1414.  
  1415. /** 
  1416. * Send all pending invites by a single user to a specific group. 
  1417. * 
  1418. * @since 1.0.0 
  1419. * 
  1420. * @param int $user_id ID of the inviting user. 
  1421. * @param int $group_id ID of the group. 
  1422. */ 
  1423. function groups_send_invites( $user_id, $group_id ) { 
  1424.  
  1425. if ( empty( $user_id ) ) 
  1426. $user_id = bp_loggedin_user_id(); 
  1427.  
  1428. // Send friend invites. 
  1429. $invited_users = groups_get_invites_for_group( $user_id, $group_id ); 
  1430. $group = groups_get_group( $group_id ); 
  1431.  
  1432. for ( $i = 0, $count = count( $invited_users ); $i < $count; ++$i ) { 
  1433. $member = new BP_Groups_Member( $invited_users[$i], $group_id ); 
  1434.  
  1435. // Send the actual invite. 
  1436. groups_notification_group_invites( $group, $member, $user_id ); 
  1437.  
  1438. $member->invite_sent = 1; 
  1439. $member->save(); 
  1440.  
  1441. /** 
  1442. * Fires after the sending of invites for a group. 
  1443. * 
  1444. * @since 1.0.0 
  1445. * @since 2.5.0 Added $user_id to passed parameters. 
  1446. * 
  1447. * @param int $group_id ID of the group who's being invited to. 
  1448. * @param array $invited_users Array of users being invited to the group. 
  1449. * @param int $user_id ID of the inviting user. 
  1450. */ 
  1451. do_action( 'groups_send_invites', $group_id, $invited_users, $user_id ); 
  1452.  
  1453. /** 
  1454. * Get IDs of users with outstanding invites to a given group from a specified user. 
  1455. * 
  1456. * @since 1.0.0 
  1457. * 
  1458. * @param int $user_id ID of the inviting user. 
  1459. * @param int $group_id ID of the group. 
  1460. * @return array $value IDs of users who have been invited to the group by the 
  1461. * user but have not yet accepted. 
  1462. */ 
  1463. function groups_get_invites_for_group( $user_id, $group_id ) { 
  1464. return BP_Groups_Group::get_invites( $user_id, $group_id ); 
  1465.  
  1466. /** 
  1467. * Check to see whether a user has already been invited to a group. 
  1468. * 
  1469. * By default, the function checks for invitations that have been sent. 
  1470. * Entering 'all' as the $type parameter will return unsent invitations as 
  1471. * well (useful to make sure AJAX requests are not duplicated). 
  1472. * 
  1473. * @since 1.0.0 
  1474. * 
  1475. * @param int $user_id ID of potential group member. 
  1476. * @param int $group_id ID of potential group. 
  1477. * @param string $type Optional. Use 'sent' to check for sent invites,  
  1478. * 'all' to check for all. Default: 'sent'. 
  1479. * @return int|bool ID of the membership if found, otherwise false. 
  1480. */ 
  1481. function groups_check_user_has_invite( $user_id, $group_id, $type = 'sent' ) { 
  1482. $invite = false; 
  1483.  
  1484. $args = array( 
  1485. 'is_confirmed' => false,  
  1486. 'is_banned' => null,  
  1487. 'is_admin' => null,  
  1488. 'is_mod' => null,  
  1489. ); 
  1490.  
  1491. if ( 'sent' === $type ) { 
  1492. $args['invite_sent'] = true; 
  1493.  
  1494. $user_groups = bp_get_user_groups( $user_id, $args ); 
  1495.  
  1496. if ( isset( $user_groups[ $group_id ] ) && 0 !== $user_groups[ $group_id ]->inviter_id ) { 
  1497. $invite = $user_groups[ $group_id ]->id; 
  1498.  
  1499. return $invite; 
  1500.  
  1501. /** 
  1502. * Delete all invitations to a given group. 
  1503. * 
  1504. * @since 1.0.0 
  1505. * 
  1506. * @param int $group_id ID of the group whose invitations are being deleted. 
  1507. * @return int|null Number of rows records deleted on success, null on failure. 
  1508. */ 
  1509. function groups_delete_all_group_invites( $group_id ) { 
  1510. return BP_Groups_Group::delete_all_invites( $group_id ); 
  1511.  
  1512. /** Group Promotion & Banning *************************************************/ 
  1513.  
  1514. /** 
  1515. * Promote a member to a new status within a group. 
  1516. * 
  1517. * @since 1.0.0 
  1518. * 
  1519. * @param int $user_id ID of the user. 
  1520. * @param int $group_id ID of the group. 
  1521. * @param string $status The new status. 'mod' or 'admin'. 
  1522. * @return bool True on success, false on failure. 
  1523. */ 
  1524. function groups_promote_member( $user_id, $group_id, $status ) { 
  1525.  
  1526. if ( ! bp_is_item_admin() ) 
  1527. return false; 
  1528.  
  1529. $member = new BP_Groups_Member( $user_id, $group_id ); 
  1530.  
  1531. // Don't use this action. It's deprecated as of BuddyPress 1.6. 
  1532. do_action( 'groups_premote_member', $group_id, $user_id, $status ); 
  1533.  
  1534. /** 
  1535. * Fires before the promotion of a user to a new status. 
  1536. * 
  1537. * @since 1.6.0 
  1538. * 
  1539. * @param int $group_id ID of the group being promoted in. 
  1540. * @param int $user_id ID of the user being promoted. 
  1541. * @param string $status New status being promoted to. 
  1542. */ 
  1543. do_action( 'groups_promote_member', $group_id, $user_id, $status ); 
  1544.  
  1545. return $member->promote( $status ); 
  1546.  
  1547. /** 
  1548. * Demote a user to 'member' status within a group. 
  1549. * 
  1550. * @since 1.0.0 
  1551. * 
  1552. * @param int $user_id ID of the user. 
  1553. * @param int $group_id ID of the group. 
  1554. * @return bool True on success, false on failure. 
  1555. */ 
  1556. function groups_demote_member( $user_id, $group_id ) { 
  1557.  
  1558. if ( ! bp_is_item_admin() ) 
  1559. return false; 
  1560.  
  1561. $member = new BP_Groups_Member( $user_id, $group_id ); 
  1562.  
  1563. /** 
  1564. * Fires before the demotion of a user to 'member'. 
  1565. * 
  1566. * @since 1.0.0 
  1567. * 
  1568. * @param int $group_id ID of the group being demoted in. 
  1569. * @param int $user_id ID of the user being demoted. 
  1570. */ 
  1571. do_action( 'groups_demote_member', $group_id, $user_id ); 
  1572.  
  1573. return $member->demote(); 
  1574.  
  1575. /** 
  1576. * Ban a member from a group. 
  1577. * 
  1578. * @since 1.0.0 
  1579. * 
  1580. * @param int $user_id ID of the user. 
  1581. * @param int $group_id ID of the group. 
  1582. * @return bool True on success, false on failure. 
  1583. */ 
  1584. function groups_ban_member( $user_id, $group_id ) { 
  1585.  
  1586. if ( ! bp_is_item_admin() ) 
  1587. return false; 
  1588.  
  1589. $member = new BP_Groups_Member( $user_id, $group_id ); 
  1590.  
  1591. /** 
  1592. * Fires before the banning of a member from a group. 
  1593. * 
  1594. * @since 1.0.0 
  1595. * 
  1596. * @param int $group_id ID of the group being banned from. 
  1597. * @param int $user_id ID of the user being banned. 
  1598. */ 
  1599. do_action( 'groups_ban_member', $group_id, $user_id ); 
  1600.  
  1601. return $member->ban(); 
  1602.  
  1603. /** 
  1604. * Unban a member from a group. 
  1605. * 
  1606. * @since 1.0.0 
  1607. * 
  1608. * @param int $user_id ID of the user. 
  1609. * @param int $group_id ID of the group. 
  1610. * @return bool True on success, false on failure. 
  1611. */ 
  1612. function groups_unban_member( $user_id, $group_id ) { 
  1613.  
  1614. if ( ! bp_is_item_admin() ) 
  1615. return false; 
  1616.  
  1617. $member = new BP_Groups_Member( $user_id, $group_id ); 
  1618.  
  1619. /** 
  1620. * Fires before the unbanning of a member from a group. 
  1621. * 
  1622. * @since 1.0.0 
  1623. * 
  1624. * @param int $group_id ID of the group being unbanned from. 
  1625. * @param int $user_id ID of the user being unbanned. 
  1626. */ 
  1627. do_action( 'groups_unban_member', $group_id, $user_id ); 
  1628.  
  1629. return $member->unban(); 
  1630.  
  1631. /** Group Removal *************************************************************/ 
  1632.  
  1633. /** 
  1634. * Remove a member from a group. 
  1635. * 
  1636. * @since 1.2.6 
  1637. * 
  1638. * @param int $user_id ID of the user. 
  1639. * @param int $group_id ID of the group. 
  1640. * @return bool True on success, false on failure. 
  1641. */ 
  1642. function groups_remove_member( $user_id, $group_id ) { 
  1643.  
  1644. if ( ! bp_is_item_admin() ) { 
  1645. return false; 
  1646.  
  1647. $member = new BP_Groups_Member( $user_id, $group_id ); 
  1648.  
  1649. /** 
  1650. * Fires before the removal of a member from a group. 
  1651. * 
  1652. * @since 1.2.6 
  1653. * 
  1654. * @param int $group_id ID of the group being removed from. 
  1655. * @param int $user_id ID of the user being removed. 
  1656. */ 
  1657. do_action( 'groups_remove_member', $group_id, $user_id ); 
  1658.  
  1659. return $member->remove(); 
  1660.  
  1661. /** Group Membership **********************************************************/ 
  1662.  
  1663. /** 
  1664. * Create a group membership request. 
  1665. * 
  1666. * @since 1.0.0 
  1667. * 
  1668. * @param int $requesting_user_id ID of the user requesting membership. 
  1669. * @param int $group_id ID of the group. 
  1670. * @return bool True on success, false on failure. 
  1671. */ 
  1672. function groups_send_membership_request( $requesting_user_id, $group_id ) { 
  1673.  
  1674. // Prevent duplicate requests. 
  1675. if ( groups_check_for_membership_request( $requesting_user_id, $group_id ) ) 
  1676. return false; 
  1677.  
  1678. // Check if the user is already a member or is banned. 
  1679. if ( groups_is_user_member( $requesting_user_id, $group_id ) || groups_is_user_banned( $requesting_user_id, $group_id ) ) 
  1680. return false; 
  1681.  
  1682. // Check if the user is already invited - if so, simply accept invite. 
  1683. if ( groups_check_user_has_invite( $requesting_user_id, $group_id ) ) { 
  1684. groups_accept_invite( $requesting_user_id, $group_id ); 
  1685. return true; 
  1686.  
  1687. $requesting_user = new BP_Groups_Member; 
  1688. $requesting_user->group_id = $group_id; 
  1689. $requesting_user->user_id = $requesting_user_id; 
  1690. $requesting_user->inviter_id = 0; 
  1691. $requesting_user->is_admin = 0; 
  1692. $requesting_user->user_title = ''; 
  1693. $requesting_user->date_modified = bp_core_current_time(); 
  1694. $requesting_user->is_confirmed = 0; 
  1695. $requesting_user->comments = isset( $_POST['group-request-membership-comments'] ) ? $_POST['group-request-membership-comments'] : ''; 
  1696.  
  1697. if ( $requesting_user->save() ) { 
  1698. $admins = groups_get_group_admins( $group_id ); 
  1699.  
  1700. // Saved okay, now send the email notification. 
  1701. for ( $i = 0, $count = count( $admins ); $i < $count; ++$i ) 
  1702. groups_notification_new_membership_request( $requesting_user_id, $admins[$i]->user_id, $group_id, $requesting_user->id ); 
  1703.  
  1704. /** 
  1705. * Fires after the creation of a new membership request. 
  1706. * 
  1707. * @since 1.0.0 
  1708. * 
  1709. * @param int $requesting_user_id ID of the user requesting membership. 
  1710. * @param array $admins Array of group admins. 
  1711. * @param int $group_id ID of the group being requested to. 
  1712. * @param int $requesting_user->id ID of the user requesting membership. 
  1713. */ 
  1714. do_action( 'groups_membership_requested', $requesting_user_id, $admins, $group_id, $requesting_user->id ); 
  1715.  
  1716. return true; 
  1717.  
  1718. return false; 
  1719.  
  1720. /** 
  1721. * Accept a pending group membership request. 
  1722. * 
  1723. * @since 1.0.0 
  1724. * 
  1725. * @param int $membership_id ID of the membership object. 
  1726. * @param int $user_id Optional. ID of the user who requested membership. 
  1727. * Provide this value along with $group_id to override 
  1728. * $membership_id. 
  1729. * @param int $group_id Optional. ID of the group to which membership is being 
  1730. * requested. Provide this value along with $user_id to 
  1731. * override $membership_id. 
  1732. * @return bool True on success, false on failure. 
  1733. */ 
  1734. function groups_accept_membership_request( $membership_id, $user_id = 0, $group_id = 0 ) { 
  1735.  
  1736. if ( !empty( $user_id ) && !empty( $group_id ) ) { 
  1737. $membership = new BP_Groups_Member( $user_id, $group_id ); 
  1738. } else { 
  1739. $membership = new BP_Groups_Member( false, false, $membership_id ); 
  1740.  
  1741. $membership->accept_request(); 
  1742.  
  1743. if ( !$membership->save() ) { 
  1744. return false; 
  1745.  
  1746. // Check if the user has an outstanding invite, if so delete it. 
  1747. if ( groups_check_user_has_invite( $membership->user_id, $membership->group_id ) ) { 
  1748. groups_delete_invite( $membership->user_id, $membership->group_id ); 
  1749.  
  1750. /** 
  1751. * Fires after a group membership request has been accepted. 
  1752. * 
  1753. * @since 1.0.0 
  1754. * 
  1755. * @param int $user_id ID of the user who accepted membership. 
  1756. * @param int $group_id ID of the group that was accepted membership to. 
  1757. * @param bool $value If membership was accepted. 
  1758. */ 
  1759. do_action( 'groups_membership_accepted', $membership->user_id, $membership->group_id, true ); 
  1760.  
  1761. return true; 
  1762.  
  1763. /** 
  1764. * Reject a pending group membership request. 
  1765. * 
  1766. * @since 1.0.0 
  1767. * 
  1768. * @param int $membership_id ID of the membership object. 
  1769. * @param int $user_id Optional. ID of the user who requested membership. 
  1770. * Provide this value along with $group_id to override 
  1771. * $membership_id. 
  1772. * @param int $group_id Optional. ID of the group to which membership is being 
  1773. * requested. Provide this value along with $user_id to 
  1774. * override $membership_id. 
  1775. * @return bool True on success, false on failure. 
  1776. */ 
  1777. function groups_reject_membership_request( $membership_id, $user_id = 0, $group_id = 0 ) { 
  1778. if ( !$membership = groups_delete_membership_request( $membership_id, $user_id, $group_id ) ) { 
  1779. return false; 
  1780.  
  1781. /** 
  1782. * Fires after a group membership request has been rejected. 
  1783. * 
  1784. * @since 1.0.0 
  1785. * 
  1786. * @param int $user_id ID of the user who rejected membership. 
  1787. * @param int $group_id ID of the group that was rejected membership to. 
  1788. * @param bool $value If membership was accepted. 
  1789. */ 
  1790. do_action( 'groups_membership_rejected', $membership->user_id, $membership->group_id, false ); 
  1791.  
  1792. return true; 
  1793.  
  1794. /** 
  1795. * Delete a pending group membership request. 
  1796. * 
  1797. * @since 1.2.0 
  1798. * 
  1799. * @param int $membership_id ID of the membership object. 
  1800. * @param int $user_id Optional. ID of the user who requested membership. 
  1801. * Provide this value along with $group_id to override 
  1802. * $membership_id. 
  1803. * @param int $group_id Optional. ID of the group to which membership is being 
  1804. * requested. Provide this value along with $user_id to 
  1805. * override $membership_id. 
  1806. * @return bool True on success, false on failure. 
  1807. */ 
  1808. function groups_delete_membership_request( $membership_id, $user_id = 0, $group_id = 0 ) { 
  1809. if ( !empty( $user_id ) && !empty( $group_id ) ) 
  1810. $membership = new BP_Groups_Member( $user_id, $group_id ); 
  1811. else 
  1812. $membership = new BP_Groups_Member( false, false, $membership_id ); 
  1813.  
  1814. if ( ! BP_Groups_Member::delete_request( $membership->user_id, $membership->group_id ) ) 
  1815. return false; 
  1816.  
  1817. return $membership; 
  1818.  
  1819. /** 
  1820. * Check whether a user has an outstanding membership request for a given group. 
  1821. * 
  1822. * @since 1.0.0 
  1823. * 
  1824. * @param int $user_id ID of the user. 
  1825. * @param int $group_id ID of the group. 
  1826. * @return int|bool ID of the membership if found, otherwise false. 
  1827. */ 
  1828. function groups_check_for_membership_request( $user_id, $group_id ) { 
  1829. $request = false; 
  1830.  
  1831. $user_groups = bp_get_user_groups( $user_id, array( 
  1832. 'is_confirmed' => false,  
  1833. 'is_banned' => false,  
  1834. 'is_admin' => null,  
  1835. 'is_mod' => null 
  1836. ) ); 
  1837.  
  1838. if ( isset( $user_groups[ $group_id ] ) && 0 === $user_groups[ $group_id ]->inviter_id ) { 
  1839. $request = $user_groups[ $group_id ]->id; 
  1840.  
  1841. return $request; 
  1842.  
  1843. /** 
  1844. * Accept all pending membership requests to a group. 
  1845. * 
  1846. * @since 1.0.2 
  1847. * 
  1848. * @param int $group_id ID of the group. 
  1849. * @return bool True on success, false on failure. 
  1850. */ 
  1851. function groups_accept_all_pending_membership_requests( $group_id ) { 
  1852. $user_ids = BP_Groups_Member::get_all_membership_request_user_ids( $group_id ); 
  1853.  
  1854. if ( !$user_ids ) 
  1855. return false; 
  1856.  
  1857. foreach ( (array) $user_ids as $user_id ) 
  1858. groups_accept_membership_request( false, $user_id, $group_id ); 
  1859.  
  1860. /** 
  1861. * Fires after the acceptance of all pending membership requests to a group. 
  1862. * 
  1863. * @since 1.0.2 
  1864. * 
  1865. * @param int $group_id ID of the group whose pending memberships were accepted. 
  1866. */ 
  1867. do_action( 'groups_accept_all_pending_membership_requests', $group_id ); 
  1868.  
  1869. return true; 
  1870.  
  1871. /** Group Meta ****************************************************************/ 
  1872.  
  1873. /** 
  1874. * Delete metadata for a group. 
  1875. * 
  1876. * @since 1.0.0 
  1877. * 
  1878. * @param int $group_id ID of the group. 
  1879. * @param string|bool $meta_key The key of the row to delete. 
  1880. * @param string|bool $meta_value Optional. Metadata value. If specified, only delete 
  1881. * metadata entries with this value. 
  1882. * @param bool $delete_all Optional. If true, delete matching metadata entries 
  1883. * for all groups. Otherwise, only delete matching 
  1884. * metadata entries for the specified group. 
  1885. * Default: false. 
  1886. * @return bool True on success, false on failure. 
  1887. */ 
  1888. function groups_delete_groupmeta( $group_id, $meta_key = false, $meta_value = false, $delete_all = false ) { 
  1889. global $wpdb; 
  1890.  
  1891. // Legacy - if no meta_key is passed, delete all for the item. 
  1892. if ( empty( $meta_key ) ) { 
  1893. $keys = $wpdb->get_col( $wpdb->prepare( "SELECT meta_key FROM {$wpdb->groupmeta} WHERE group_id = %d", $group_id ) ); 
  1894.  
  1895. // With no meta_key, ignore $delete_all. 
  1896. $delete_all = false; 
  1897. } else { 
  1898. $keys = array( $meta_key ); 
  1899.  
  1900. add_filter( 'query', 'bp_filter_metaid_column_name' ); 
  1901.  
  1902. $retval = true; 
  1903. foreach ( $keys as $key ) { 
  1904. $retval = delete_metadata( 'group', $group_id, $key, $meta_value, $delete_all ); 
  1905.  
  1906. remove_filter( 'query', 'bp_filter_metaid_column_name' ); 
  1907.  
  1908. return $retval; 
  1909.  
  1910. /** 
  1911. * Get a piece of group metadata. 
  1912. * 
  1913. * @since 1.0.0 
  1914. * 
  1915. * @param int $group_id ID of the group. 
  1916. * @param string $meta_key Metadata key. 
  1917. * @param bool $single Optional. If true, return only the first value of the 
  1918. * specified meta_key. This parameter has no effect if 
  1919. * meta_key is empty. 
  1920. * @return mixed Metadata value. 
  1921. */ 
  1922. function groups_get_groupmeta( $group_id, $meta_key = '', $single = true ) { 
  1923. add_filter( 'query', 'bp_filter_metaid_column_name' ); 
  1924. $retval = get_metadata( 'group', $group_id, $meta_key, $single ); 
  1925. remove_filter( 'query', 'bp_filter_metaid_column_name' ); 
  1926.  
  1927. return $retval; 
  1928.  
  1929. /** 
  1930. * Update a piece of group metadata. 
  1931. * 
  1932. * @since 1.0.0 
  1933. * 
  1934. * @param int $group_id ID of the group. 
  1935. * @param string $meta_key Metadata key. 
  1936. * @param mixed $meta_value Value to store. 
  1937. * @param mixed $prev_value Optional. If specified, only update existing 
  1938. * metadata entries with the specified value. 
  1939. * Otherwise, update all entries. 
  1940. * @return bool|int $retval Returns false on failure. On successful update of existing 
  1941. * metadata, returns true. On successful creation of new metadata,  
  1942. * returns the integer ID of the new metadata row. 
  1943. */ 
  1944. function groups_update_groupmeta( $group_id, $meta_key, $meta_value, $prev_value = '' ) { 
  1945. add_filter( 'query', 'bp_filter_metaid_column_name' ); 
  1946. $retval = update_metadata( 'group', $group_id, $meta_key, $meta_value, $prev_value ); 
  1947. remove_filter( 'query', 'bp_filter_metaid_column_name' ); 
  1948.  
  1949. return $retval; 
  1950.  
  1951. /** 
  1952. * Add a piece of group metadata. 
  1953. * 
  1954. * @since 2.0.0 
  1955. * 
  1956. * @param int $group_id ID of the group. 
  1957. * @param string $meta_key Metadata key. 
  1958. * @param mixed $meta_value Metadata value. 
  1959. * @param bool $unique Optional. Whether to enforce a single metadata value 
  1960. * for the given key. If true, and the object already 
  1961. * has a value for the key, no change will be made. 
  1962. * Default: false. 
  1963. * @return int|bool The meta ID on successful update, false on failure. 
  1964. */ 
  1965. function groups_add_groupmeta( $group_id, $meta_key, $meta_value, $unique = false ) { 
  1966. add_filter( 'query', 'bp_filter_metaid_column_name' ); 
  1967. $retval = add_metadata( 'group', $group_id, $meta_key, $meta_value, $unique ); 
  1968. remove_filter( 'query', 'bp_filter_metaid_column_name' ); 
  1969.  
  1970. return $retval; 
  1971.  
  1972. /** Group Cleanup Functions ***************************************************/ 
  1973.  
  1974. /** 
  1975. * Delete all group membership information for the specified user. 
  1976. * 
  1977. * @since 1.0.0 
  1978. * 
  1979. * @param int $user_id ID of the user. 
  1980. */ 
  1981. function groups_remove_data_for_user( $user_id ) { 
  1982. BP_Groups_Member::delete_all_for_user( $user_id ); 
  1983.  
  1984. /** 
  1985. * Fires after the deletion of all data for a user. 
  1986. * 
  1987. * @since 1.1.0 
  1988. * 
  1989. * @param int $user_id ID of the user whose data is being deleted. 
  1990. */ 
  1991. do_action( 'groups_remove_data_for_user', $user_id ); 
  1992. add_action( 'wpmu_delete_user', 'groups_remove_data_for_user' ); 
  1993. add_action( 'delete_user', 'groups_remove_data_for_user' ); 
  1994. add_action( 'bp_make_spam_user', 'groups_remove_data_for_user' ); 
  1995.  
  1996. /** Group Types ***************************************************************/ 
  1997.  
  1998. /** 
  1999. * Register a group type. 
  2000. * 
  2001. * @since 2.6.0 
  2002. * @since 2.7.0 Introduce $has_directory, $show_in_create_screen, $show_in_list, and 
  2003. * $description, $create_screen_checked as $args parameters. 
  2004. * 
  2005. * @param string $group_type Unique string identifier for the group type. 
  2006. * @param array $args { 
  2007. * Array of arguments describing the group type. 
  2008. * 
  2009. * @type string|bool $has_directory Set the slug to be used for custom group directory page. eg. 
  2010. * example.com/groups/type/MY_SLUG. Default: false. 
  2011. * @type bool $show_in_create_screen Whether this group type is allowed to be selected on the group creation 
  2012. * page. Default: false. 
  2013. * @type bool|null $show_in_list Whether this group type should be shown in lists rendered by 
  2014. * bp_group_type_list(). Default: null. If $show_in_create_screen is true,  
  2015. * this will default to true, unless this is set explicitly to false. 
  2016. * @type string $description A short descriptive summary of what the group type is. Currently shown 
  2017. * on a group's "Manage > Settings" page when selecting group types. 
  2018. * @type bool $create_screen_checked If $show_in_create_screen is true, whether we should have our group type 
  2019. * checkbox checked by default. Handy if you want to imply that the group 
  2020. * type should be enforced, but decision lies with the group creator. 
  2021. * Default: false. 
  2022. * @type array $labels { 
  2023. * Array of labels to use in various parts of the interface. 
  2024. * 
  2025. * @type string $name Default name. Should typically be plural. 
  2026. * @type string $singular_name Singular name. 
  2027. * } 
  2028. * } 
  2029. * @return object|WP_Error Group type object on success, WP_Error object on failure. 
  2030. */ 
  2031. function bp_groups_register_group_type( $group_type, $args = array() ) { 
  2032. $bp = buddypress(); 
  2033.  
  2034. if ( isset( $bp->groups->types[ $group_type ] ) ) { 
  2035. return new WP_Error( 'bp_group_type_exists', __( 'Group type already exists.', 'buddypress' ), $group_type ); 
  2036.  
  2037. $r = bp_parse_args( $args, array( 
  2038. 'has_directory' => false,  
  2039. 'show_in_create_screen' => false,  
  2040. 'show_in_list' => null,  
  2041. 'description' => '',  
  2042. 'create_screen_checked' => false,  
  2043. 'labels' => array(),  
  2044. ), 'register_group_type' ); 
  2045.  
  2046. $group_type = sanitize_key( $group_type ); 
  2047.  
  2048. /** 
  2049. * Filters the list of illegal group type names. 
  2050. * 
  2051. * - 'any' is a special pseudo-type, representing items unassociated with any group type. 
  2052. * - 'null' is a special pseudo-type, representing users without any type. 
  2053. * - '_none' is used internally to denote an item that should not apply to any group types. 
  2054. * 
  2055. * @since 2.6.0 
  2056. * 
  2057. * @param array $illegal_names Array of illegal names. 
  2058. */ 
  2059. $illegal_names = apply_filters( 'bp_group_type_illegal_names', array( 'any', 'null', '_none' ) ); 
  2060. if ( in_array( $group_type, $illegal_names, true ) ) { 
  2061. return new WP_Error( 'bp_group_type_illegal_name', __( 'You may not register a group type with this name.', 'buddypress' ), $group_type ); 
  2062.  
  2063. // Store the group type name as data in the object (not just as the array key). 
  2064. $r['name'] = $group_type; 
  2065.  
  2066. // Make sure the relevant labels have been filled in. 
  2067. $default_name = isset( $r['labels']['name'] ) ? $r['labels']['name'] : ucfirst( $r['name'] ); 
  2068. $r['labels'] = array_merge( array( 
  2069. 'name' => $default_name,  
  2070. 'singular_name' => $default_name,  
  2071. ), $r['labels'] ); 
  2072.  
  2073. // Directory slug. 
  2074. if ( ! empty( $r['has_directory'] ) ) { 
  2075. // A string value is intepreted as the directory slug. 
  2076. if ( is_string( $r['has_directory'] ) ) { 
  2077. $directory_slug = $r['has_directory']; 
  2078.  
  2079. // Otherwise fall back on group type. 
  2080. } else { 
  2081. $directory_slug = $group_type; 
  2082.  
  2083. // Sanitize for use in URLs. 
  2084. $r['directory_slug'] = sanitize_title( $directory_slug ); 
  2085. $r['has_directory'] = true; 
  2086. } else { 
  2087. $r['directory_slug'] = ''; 
  2088. $r['has_directory'] = false; 
  2089.  
  2090. // Type lists. 
  2091. if ( true === $r['show_in_create_screen'] && is_null( $r['show_in_list'] ) ) { 
  2092. $r['show_in_list'] = true; 
  2093. } else { 
  2094. $r['show_in_list'] = (bool) $r['show_in_list']; 
  2095.  
  2096. $bp->groups->types[ $group_type ] = $type = (object) $r; 
  2097.  
  2098. /** 
  2099. * Fires after a group type is registered. 
  2100. * 
  2101. * @since 2.6.0 
  2102. * 
  2103. * @param string $group_type Group type identifier. 
  2104. * @param object $type Group type object. 
  2105. */ 
  2106. do_action( 'bp_groups_register_group_type', $group_type, $type ); 
  2107.  
  2108. return $type; 
  2109.  
  2110. /** 
  2111. * Get a list of all registered group type objects. 
  2112. * 
  2113. * @since 2.6.0 
  2114. * 
  2115. * @see bp_groups_register_group_type() for accepted arguments. 
  2116. * 
  2117. * @param array|string $args Optional. An array of key => value arguments to match against 
  2118. * the group type objects. Default empty array. 
  2119. * @param string $output Optional. The type of output to return. Accepts 'names' 
  2120. * or 'objects'. Default 'names'. 
  2121. * @param string $operator Optional. The logical operation to perform. 'or' means only one 
  2122. * element from the array needs to match; 'and' means all elements 
  2123. * must match. Accepts 'or' or 'and'. Default 'and'. 
  2124. * @return array $types A list of groups type names or objects. 
  2125. */ 
  2126. function bp_groups_get_group_types( $args = array(), $output = 'names', $operator = 'and' ) { 
  2127. $types = buddypress()->groups->types; 
  2128.  
  2129. $types = wp_filter_object_list( $types, $args, $operator ); 
  2130.  
  2131. /** 
  2132. * Filters the array of group type objects. 
  2133. * 
  2134. * This filter is run before the $output filter has been applied, so that 
  2135. * filtering functions have access to the entire group type objects. 
  2136. * 
  2137. * @since 2.6.0 
  2138. * 
  2139. * @param array $types group type objects, keyed by name. 
  2140. * @param array $args Array of key=>value arguments for filtering. 
  2141. * @param string $operator 'or' to match any of $args, 'and' to require all. 
  2142. */ 
  2143. $types = apply_filters( 'bp_groups_get_group_types', $types, $args, $operator ); 
  2144.  
  2145. if ( 'names' === $output ) { 
  2146. $types = wp_list_pluck( $types, 'name' ); 
  2147.  
  2148. return $types; 
  2149.  
  2150. /** 
  2151. * Retrieve a group type object by name. 
  2152. * 
  2153. * @since 2.6.0 
  2154. * 
  2155. * @param string $group_type The name of the group type. 
  2156. * @return object A group type object. 
  2157. */ 
  2158. function bp_groups_get_group_type_object( $group_type ) { 
  2159. $types = bp_groups_get_group_types( array(), 'objects' ); 
  2160.  
  2161. if ( empty( $types[ $group_type ] ) ) { 
  2162. return null; 
  2163.  
  2164. return $types[ $group_type ]; 
  2165.  
  2166. /** 
  2167. * Set type for a group. 
  2168. * 
  2169. * @since 2.6.0 
  2170. * @since 2.7.0 $group_type parameter also accepts an array of group types now. 
  2171. * 
  2172. * @param int $group ID of the group. 
  2173. * @param string|array $group_type Group type or array of group types to set. 
  2174. * @param bool $append Optional. True to append this to existing types for group,  
  2175. * false to replace. Default: false. 
  2176. * @return array $retval See bp_set_object_terms(). 
  2177. */ 
  2178. function bp_groups_set_group_type( $group_id, $group_type, $append = false ) { 
  2179. // Pass an empty group type to remove group's type. 
  2180. if ( ! empty( $group_type ) && is_string( $group_type ) && ! bp_groups_get_group_type_object( $group_type ) ) { 
  2181. return false; 
  2182.  
  2183. // Cast as array. 
  2184. $group_type = (array) $group_type; 
  2185.  
  2186. // Validate group types. 
  2187. foreach ( $group_type as $type ) { 
  2188. // Remove any invalid group types. 
  2189. if ( is_null( bp_groups_get_group_type_object( $type ) ) ) { 
  2190. unset( $group_type[ $type ] ); 
  2191.  
  2192. $retval = bp_set_object_terms( $group_id, $group_type, 'bp_group_type', $append ); 
  2193.  
  2194. // Bust the cache if the type has been updated. 
  2195. if ( ! is_wp_error( $retval ) ) { 
  2196. wp_cache_delete( $group_id, 'bp_groups_group_type' ); 
  2197.  
  2198. /** 
  2199. * Fires just after a group type has been changed. 
  2200. * 
  2201. * @since 2.6.0 
  2202. * 
  2203. * @param int $group_id ID of the group whose group type has been updated. 
  2204. * @param string|array $group_type Group type or array of group types. 
  2205. * @param bool $append Whether the type is being appended to existing types. 
  2206. */ 
  2207. do_action( 'bp_groups_set_group_type', $group_id, $group_type, $append ); 
  2208.  
  2209. return $retval; 
  2210.  
  2211. /** 
  2212. * Get type for a group. 
  2213. * 
  2214. * @since 2.6.0 
  2215. * 
  2216. * @param int $group_id ID of the group. 
  2217. * @param bool $single Optional. Whether to return a single type string. If multiple types are found 
  2218. * for the group, the oldest one will be returned. Default: true. 
  2219. * @return string|array|bool On success, returns a single group type (if `$single` is true) or an array of group 
  2220. * types (if `$single` is false). Returns false on failure. 
  2221. */ 
  2222. function bp_groups_get_group_type( $group_id, $single = true ) { 
  2223. $types = wp_cache_get( $group_id, 'bp_groups_group_type' ); 
  2224.  
  2225. if ( false === $types ) { 
  2226. $raw_types = bp_get_object_terms( $group_id, 'bp_group_type' ); 
  2227.  
  2228. if ( ! is_wp_error( $raw_types ) ) { 
  2229. $types = array(); 
  2230.  
  2231. // Only include currently registered group types. 
  2232. foreach ( $raw_types as $gtype ) { 
  2233. if ( bp_groups_get_group_type_object( $gtype->name ) ) { 
  2234. $types[] = $gtype->name; 
  2235.  
  2236. wp_cache_set( $group_id, $types, 'bp_groups_group_type' ); 
  2237.  
  2238. $type = false; 
  2239. if ( ! empty( $types ) ) { 
  2240. if ( $single ) { 
  2241. $type = end( $types ); 
  2242. } else { 
  2243. $type = $types; 
  2244.  
  2245. /** 
  2246. * Filters a groups's group type(s). 
  2247. * 
  2248. * @since 2.6.0 
  2249. * 
  2250. * @param string|array $type Group type. 
  2251. * @param int $group_id ID of the group. 
  2252. * @param bool $single Whether to return a single type string, or an array. 
  2253. */ 
  2254. return apply_filters( 'bp_groups_get_group_type', $type, $group_id, $single ); 
  2255.  
  2256. /** 
  2257. * Remove type for a group. 
  2258. * 
  2259. * @since 2.6.0 
  2260. * 
  2261. * @param int $group ID of the user. 
  2262. * @param string $group_type Group type. 
  2263. * @return bool|WP_Error $deleted True on success. False or WP_Error on failure. 
  2264. */ 
  2265. function bp_groups_remove_group_type( $group_id, $group_type ) { 
  2266. if ( empty( $group_type ) || ! bp_groups_get_group_type_object( $group_type ) ) { 
  2267. return false; 
  2268.  
  2269. $deleted = bp_remove_object_terms( $group_id, $group_type, 'bp_group_type' ); 
  2270.  
  2271. // Bust the case, if the type has been removed. 
  2272. if ( ! is_wp_error( $deleted ) ) { 
  2273. wp_cache_delete( $group_id, 'bp_groups_group_type' ); 
  2274.  
  2275. /** 
  2276. * Fires just after a group's group type has been removed. 
  2277. * 
  2278. * @since 2.6.0 
  2279. * 
  2280. * @param int $group ID of the group whose group type has been removed. 
  2281. * @param string $group_type Group type. 
  2282. */ 
  2283. do_action( 'bp_groups_remove_group_type', $group_id, $group_type ); 
  2284.  
  2285. return $deleted; 
  2286.  
  2287. /** 
  2288. * Check whether the given group has a certain group type. 
  2289. * 
  2290. * @since 2.6.0 
  2291. * 
  2292. * @param int $group_id ID of the group. 
  2293. * @param string $group_type Group type. 
  2294. * @return bool Whether the group has the give group type. 
  2295. */ 
  2296. function bp_groups_has_group_type( $group_id, $group_type ) { 
  2297. if ( empty( $group_type ) || ! bp_groups_get_group_type_object( $group_type ) ) { 
  2298. return false; 
  2299.  
  2300. // Get all group's group types. 
  2301. $types = bp_groups_get_group_type( $group_id, false ); 
  2302.  
  2303. if ( ! is_array( $types ) ) { 
  2304. return false; 
  2305.  
  2306. return in_array( $group_type, $types ); 
  2307.  
  2308. /** 
  2309. * Get the "current" group type, if one is provided, in group directories. 
  2310. * 
  2311. * @since 2.7.0 
  2312. * 
  2313. * @return string 
  2314. */ 
  2315. function bp_get_current_group_directory_type() { 
  2316.  
  2317. /** 
  2318. * Filters the "current" group type, if one is provided, in group directories. 
  2319. * 
  2320. * @since 2.7.0 
  2321. * 
  2322. * @param string $value "Current" group type. 
  2323. */ 
  2324. return apply_filters( 'bp_get_current_group_directory_type', buddypress()->groups->current_directory_type ); 
  2325.  
  2326. /** 
  2327. * Delete a group's type when the group is deleted. 
  2328. * 
  2329. * @since 2.6.0 
  2330. * 
  2331. * @param int $group_id ID of the group. 
  2332. * @return array $value See {@see bp_groups_set_group_type()}. 
  2333. */ 
  2334. function bp_remove_group_type_on_group_delete( $group_id = 0 ) { 
  2335. bp_groups_set_group_type( $group_id, '' ); 
  2336. add_action( 'groups_delete_group', 'bp_remove_group_type_on_group_delete' ); 
.