BP_Groups_Group

BuddyPress Group object.

Defined (1)

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

/bp-groups/classes/class-bp-groups-group.php  
  1. class BP_Groups_Group { 
  2.  
  3. /** 
  4. * ID of the group. 
  5. * @since 1.6.0 
  6. * @var int 
  7. */ 
  8. public $id; 
  9.  
  10. /** 
  11. * User ID of the group's creator. 
  12. * @since 1.6.0 
  13. * @var int 
  14. */ 
  15. public $creator_id; 
  16.  
  17. /** 
  18. * Name of the group. 
  19. * @since 1.6.0 
  20. * @var string 
  21. */ 
  22. public $name; 
  23.  
  24. /** 
  25. * Group slug. 
  26. * @since 1.6.0 
  27. * @var string 
  28. */ 
  29. public $slug; 
  30.  
  31. /** 
  32. * Group description. 
  33. * @since 1.6.0 
  34. * @var string 
  35. */ 
  36. public $description; 
  37.  
  38. /** 
  39. * Group status. 
  40. * Core statuses are 'public', 'private', and 'hidden'. 
  41. * @since 1.6.0 
  42. * @var string 
  43. */ 
  44. public $status; 
  45.  
  46. /** 
  47. * Parent ID. 
  48. * ID of parent group, if applicable. 
  49. * @since 2.7.0 
  50. * @var int 
  51. */ 
  52. public $parent_id; 
  53.  
  54. /** 
  55. * Should (legacy) bbPress forums be enabled for this group? 
  56. * @since 1.6.0 
  57. * @var int 
  58. */ 
  59. public $enable_forum; 
  60.  
  61. /** 
  62. * Date the group was created. 
  63. * @since 1.6.0 
  64. * @var string 
  65. */ 
  66. public $date_created; 
  67.  
  68. /** 
  69. * Data about the group's admins. 
  70. * @since 1.6.0 
  71. * @var array 
  72. */ 
  73. protected $admins; 
  74.  
  75. /** 
  76. * Data about the group's moderators. 
  77. * @since 1.6.0 
  78. * @var array 
  79. */ 
  80. protected $mods; 
  81.  
  82. /** 
  83. * Total count of group members. 
  84. * @since 1.6.0 
  85. * @var int 
  86. */ 
  87. protected $total_member_count; 
  88.  
  89. /** 
  90. * Is the current user a member of this group? 
  91. * @since 1.2.0 
  92. * @var bool 
  93. */ 
  94. protected $is_member; 
  95.  
  96. /** 
  97. * Does the current user have an outstanding invitation to this group? 
  98. * @since 1.9.0 
  99. * @var bool 
  100. */ 
  101. protected $is_invited; 
  102.  
  103. /** 
  104. * Does the current user have a pending membership request to this group? 
  105. * @since 1.9.0 
  106. * @var bool 
  107. */ 
  108. protected $is_pending; 
  109.  
  110. /** 
  111. * Timestamp of the last activity that happened in this group. 
  112. * @since 1.2.0 
  113. * @var string 
  114. */ 
  115. protected $last_activity; 
  116.  
  117. /** 
  118. * If this is a private or hidden group, does the current user have access? 
  119. * @since 1.6.0 
  120. * @var bool 
  121. */ 
  122. protected $user_has_access; 
  123.  
  124. /** 
  125. * Raw arguments passed to the constructor. 
  126. * @since 2.0.0 
  127. * @var array 
  128. */ 
  129. public $args; 
  130.  
  131. /** 
  132. * Constructor method. 
  133. * @since 1.6.0 
  134. * @param int|null $id Optional. If the ID of an existing group is provided,  
  135. * the object will be pre-populated with info about that group. 
  136. * @param array $args { 
  137. * Array of optional arguments. 
  138. * @type bool $populate_extras Deprecated. 
  139. * } 
  140. */ 
  141. public function __construct( $id = null, $args = array() ) { 
  142. if ( !empty( $id ) ) { 
  143. $this->id = (int) $id; 
  144. $this->populate(); 
  145.  
  146. /** 
  147. * Set up data about the current group. 
  148. * @since 1.6.0 
  149. */ 
  150. public function populate() { 
  151. global $wpdb; 
  152.  
  153. // Get BuddyPress. 
  154. $bp = buddypress(); 
  155.  
  156. // Check cache for group data. 
  157. $group = wp_cache_get( $this->id, 'bp_groups' ); 
  158.  
  159. // Cache missed, so query the DB. 
  160. if ( false === $group ) { 
  161. $group = $wpdb->get_row( $wpdb->prepare( "SELECT g.* FROM {$bp->groups->table_name} g WHERE g.id = %d", $this->id ) ); 
  162.  
  163. wp_cache_set( $this->id, $group, 'bp_groups' ); 
  164.  
  165. // No group found so set the ID and bail. 
  166. if ( empty( $group ) || is_wp_error( $group ) ) { 
  167. $this->id = 0; 
  168. return; 
  169.  
  170. // Group found so setup the object variables. 
  171. $this->id = (int) $group->id; 
  172. $this->creator_id = (int) $group->creator_id; 
  173. $this->name = stripslashes( $group->name ); 
  174. $this->slug = $group->slug; 
  175. $this->description = stripslashes( $group->description ); 
  176. $this->status = $group->status; 
  177. $this->parent_id = (int) $group->parent_id; 
  178. $this->enable_forum = (int) $group->enable_forum; 
  179. $this->date_created = $group->date_created; 
  180.  
  181. /** 
  182. * Save the current group to the database. 
  183. * @since 1.6.0 
  184. * @return bool True on success, false on failure. 
  185. */ 
  186. public function save() { 
  187. global $wpdb; 
  188.  
  189. $bp = buddypress(); 
  190.  
  191. $this->creator_id = apply_filters( 'groups_group_creator_id_before_save', $this->creator_id, $this->id ); 
  192. $this->name = apply_filters( 'groups_group_name_before_save', $this->name, $this->id ); 
  193. $this->slug = apply_filters( 'groups_group_slug_before_save', $this->slug, $this->id ); 
  194. $this->description = apply_filters( 'groups_group_description_before_save', $this->description, $this->id ); 
  195. $this->status = apply_filters( 'groups_group_status_before_save', $this->status, $this->id ); 
  196. $this->parent_id = apply_filters( 'groups_group_parent_id_before_save', $this->parent_id, $this->id ); 
  197. $this->enable_forum = apply_filters( 'groups_group_enable_forum_before_save', $this->enable_forum, $this->id ); 
  198. $this->date_created = apply_filters( 'groups_group_date_created_before_save', $this->date_created, $this->id ); 
  199.  
  200. /** 
  201. * Fires before the current group item gets saved. 
  202. * Please use this hook to filter the properties above. Each part will be passed in. 
  203. * @since 1.0.0 
  204. * @param BP_Groups_Group $this Current instance of the group item being saved. Passed by reference. 
  205. */ 
  206. do_action_ref_array( 'groups_group_before_save', array( &$this ) ); 
  207.  
  208. // Groups need at least a name. 
  209. if ( empty( $this->name ) ) { 
  210. return false; 
  211.  
  212. // Set slug with group title if not passed. 
  213. if ( empty( $this->slug ) ) { 
  214. $this->slug = sanitize_title( $this->name ); 
  215.  
  216. // Sanity check. 
  217. if ( empty( $this->slug ) ) { 
  218. return false; 
  219.  
  220. // Check for slug conflicts if creating new group. 
  221. if ( empty( $this->id ) ) { 
  222. $this->slug = groups_check_slug( $this->slug ); 
  223.  
  224. if ( !empty( $this->id ) ) { 
  225. $sql = $wpdb->prepare( 
  226. "UPDATE {$bp->groups->table_name} SET 
  227. creator_id = %d,  
  228. name = %s,  
  229. slug = %s,  
  230. description = %s,  
  231. status = %s,  
  232. parent_id = %d,  
  233. enable_forum = %d,  
  234. date_created = %s 
  235. WHERE 
  236. id = %d 
  237. ",  
  238. $this->creator_id,  
  239. $this->name,  
  240. $this->slug,  
  241. $this->description,  
  242. $this->status,  
  243. $this->parent_id,  
  244. $this->enable_forum,  
  245. $this->date_created,  
  246. $this->id 
  247. ); 
  248. } else { 
  249. $sql = $wpdb->prepare( 
  250. "INSERT INTO {$bp->groups->table_name} ( 
  251. creator_id,  
  252. name,  
  253. slug,  
  254. description,  
  255. status,  
  256. parent_id,  
  257. enable_forum,  
  258. date_created 
  259. ) VALUES ( 
  260. %d, %s, %s, %s, %s, %d, %d, %s 
  261. )",  
  262. $this->creator_id,  
  263. $this->name,  
  264. $this->slug,  
  265. $this->description,  
  266. $this->status,  
  267. $this->parent_id,  
  268. $this->enable_forum,  
  269. $this->date_created 
  270. ); 
  271.  
  272. if ( false === $wpdb->query($sql) ) 
  273. return false; 
  274.  
  275. if ( empty( $this->id ) ) 
  276. $this->id = $wpdb->insert_id; 
  277.  
  278. /** 
  279. * Fires after the current group item has been saved. 
  280. * @since 1.0.0 
  281. * @param BP_Groups_Group $this Current instance of the group item that was saved. Passed by reference. 
  282. */ 
  283. do_action_ref_array( 'groups_group_after_save', array( &$this ) ); 
  284.  
  285. wp_cache_delete( $this->id, 'bp_groups' ); 
  286.  
  287. return true; 
  288.  
  289. /** 
  290. * Delete the current group. 
  291. * @since 1.6.0 
  292. * @return bool True on success, false on failure. 
  293. */ 
  294. public function delete() { 
  295. global $wpdb; 
  296.  
  297. // Delete groupmeta for the group. 
  298. groups_delete_groupmeta( $this->id ); 
  299.  
  300. // Fetch the user IDs of all the members of the group. 
  301. $user_ids = BP_Groups_Member::get_group_member_ids( $this->id ); 
  302. $user_id_str = esc_sql( implode( ', ', wp_parse_id_list( $user_ids ) ) ); 
  303.  
  304. // Modify group count usermeta for members. 
  305. $wpdb->query( "UPDATE {$wpdb->usermeta} SET meta_value = meta_value - 1 WHERE meta_key = 'total_group_count' AND user_id IN ( {$user_id_str} )" ); 
  306.  
  307. // Now delete all group member entries. 
  308. BP_Groups_Member::delete_all( $this->id ); 
  309.  
  310. /** 
  311. * Fires before the deletion of a group. 
  312. * @since 1.2.0 
  313. * @param BP_Groups_Group $this Current instance of the group item being deleted. Passed by reference. 
  314. * @param array $user_ids Array of user IDs that were members of the group. 
  315. */ 
  316. do_action_ref_array( 'bp_groups_delete_group', array( &$this, $user_ids ) ); 
  317.  
  318. wp_cache_delete( $this->id, 'bp_groups' ); 
  319.  
  320. $bp = buddypress(); 
  321.  
  322. // Finally remove the group entry from the DB. 
  323. if ( !$wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name} WHERE id = %d", $this->id ) ) ) 
  324. return false; 
  325.  
  326. return true; 
  327.  
  328. /** 
  329. * Magic getter. 
  330. * @since 2.7.0 
  331. * @param string $key Property name. 
  332. * @return mixed 
  333. */ 
  334. public function __get( $key ) { 
  335. switch ( $key ) { 
  336. case 'last_activity' : 
  337. case 'total_member_count' : 
  338. case 'forum_id' : 
  339. $retval = groups_get_groupmeta( $this->id, $key ); 
  340.  
  341. if ( 'last_activity' !== $key ) { 
  342. $retval = (int) $retval; 
  343.  
  344. return $retval; 
  345.  
  346. case 'admins' : 
  347. return $this->get_admins(); 
  348.  
  349. case 'mods' : 
  350. return $this->get_mods(); 
  351.  
  352. case 'is_member' : 
  353. return $this->get_is_member(); 
  354.  
  355. case 'is_invited' : 
  356. return groups_check_user_has_invite( bp_loggedin_user_id(), $this->id ); 
  357.  
  358. case 'is_pending' : 
  359. return groups_check_for_membership_request( bp_loggedin_user_id(), $this->id ); 
  360.  
  361. case 'user_has_access' : 
  362. return $this->get_user_has_access(); 
  363.  
  364. default : 
  365. return isset( $this->{$key} ) ? $this->{$key} : null; 
  366.  
  367. /** 
  368. * Magic issetter. 
  369. * Used to maintain backward compatibility for properties that are now 
  370. * accessible only via magic method. 
  371. * @since 2.7.0 
  372. * @param string $key Property name. 
  373. * @return bool 
  374. */ 
  375. public function __isset( $key ) { 
  376. switch ( $key ) { 
  377. case 'admins' : 
  378. case 'is_invited' : 
  379. case 'is_member' : 
  380. case 'is_pending' : 
  381. case 'last_activity' : 
  382. case 'mods' : 
  383. case 'total_member_count' : 
  384. case 'user_has_access' : 
  385. case 'forum_id' : 
  386. return true; 
  387.  
  388. default : 
  389. return isset( $this->{$key} ); 
  390.  
  391. /** 
  392. * Magic setter. 
  393. * Used to maintain backward compatibility for properties that are now 
  394. * accessible only via magic method. 
  395. * @since 2.7.0 
  396. * @param string $key Property name. 
  397. */ 
  398. public function __set( $key, $value ) { 
  399. switch ( $key ) { 
  400. case 'user_has_access' : 
  401. return $this->user_has_access = (bool) $value; 
  402.  
  403. default : 
  404. $this->{$key} = $value; 
  405.  
  406. /** 
  407. * Get a list of the group's admins. 
  408. * Used to provide cache-friendly access to the 'admins' property of 
  409. * the group object. 
  410. * @since 2.7.0 
  411. * @return array 
  412. */ 
  413. protected function get_admins() { 
  414. if ( isset( $this->admins ) ) { 
  415. return $this->admins; 
  416.  
  417. $this->set_up_admins_and_mods(); 
  418. return $this->admins; 
  419.  
  420. /** 
  421. * Get a list of the group's mods. 
  422. * Used to provide cache-friendly access to the 'mods' property of 
  423. * the group object. 
  424. * @since 2.7.0 
  425. * @return array 
  426. */ 
  427. protected function get_mods() { 
  428. if ( isset( $this->mods ) ) { 
  429. return $this->mods; 
  430.  
  431. $this->set_up_admins_and_mods(); 
  432. return $this->mods; 
  433.  
  434. /** 
  435. * Set up admins and mods for the current group object. 
  436. * Called only when the 'admins' or 'mods' property is accessed. 
  437. * @since 2.7.0 
  438. */ 
  439. protected function set_up_admins_and_mods() { 
  440. $admin_ids = BP_Groups_Member::get_group_administrator_ids( $this->id ); 
  441. $admin_ids_plucked = wp_list_pluck( $admin_ids, 'user_id' ); 
  442.  
  443. $mod_ids = BP_Groups_Member::get_group_moderator_ids( $this->id ); 
  444. $mod_ids_plucked = wp_list_pluck( $mod_ids, 'user_id' ); 
  445.  
  446. $admin_mod_users = get_users( array( 
  447. 'include' => array_merge( $admin_ids_plucked, $mod_ids_plucked ),  
  448. ) ); 
  449.  
  450. $admin_objects = $mod_objects = array(); 
  451. foreach ( $admin_mod_users as $admin_mod_user ) { 
  452. $obj = new stdClass(); 
  453. $obj->user_id = $admin_mod_user->ID; 
  454. $obj->user_login = $admin_mod_user->user_login; 
  455. $obj->user_email = $admin_mod_user->user_email; 
  456. $obj->user_nicename = $admin_mod_user->user_nicename; 
  457.  
  458. if ( in_array( $admin_mod_user->ID, $admin_ids_plucked, true ) ) { 
  459. $obj->is_admin = 1; 
  460. $obj->is_mod = 0; 
  461. $admin_objects[] = $obj; 
  462. } else { 
  463. $obj->is_admin = 0; 
  464. $obj->is_mod = 1; 
  465. $mod_objects[] = $obj; 
  466.  
  467. $this->admins = $admin_objects; 
  468. $this->mods = $mod_objects; 
  469.  
  470. /** 
  471. * Checks whether the logged-in user is a member of the group. 
  472. * @since 2.7.0 
  473. * @return bool 
  474. */ 
  475. protected function get_is_member() { 
  476. if ( isset( $this->is_member ) ) { 
  477. return $this->is_member; 
  478.  
  479. $this->is_member = groups_is_user_member( bp_loggedin_user_id(), $this->id ); 
  480. return $this->is_member; 
  481.  
  482. /** 
  483. * Checks whether the logged-in user has access to the group. 
  484. * @since 2.7.0 
  485. * @return bool 
  486. */ 
  487. protected function get_user_has_access() { 
  488. if ( isset( $this->user_has_access ) ) { 
  489. return $this->user_has_access; 
  490.  
  491. if ( ( 'private' === $this->status ) || ( 'hidden' === $this->status ) ) { 
  492.  
  493. // Assume user does not have access to hidden/private groups. 
  494. $this->user_has_access = false; 
  495.  
  496. // Group members or community moderators have access. 
  497. if ( ( is_user_logged_in() && $this->get_is_member() ) || bp_current_user_can( 'bp_moderate' ) ) { 
  498. $this->user_has_access = true; 
  499. } else { 
  500. $this->user_has_access = true; 
  501.  
  502. return $this->user_has_access; 
  503.  
  504. /** Static Methods ****************************************************/ 
  505.  
  506. /** 
  507. * Get whether a group exists for a given slug. 
  508. * @since 1.6.0 
  509. * @param string $slug Slug to check. 
  510. * @param string|bool $table_name Optional. Name of the table to check 
  511. * against. Default: $bp->groups->table_name. 
  512. * @return int|null Group ID if found; null if not. 
  513. */ 
  514. public static function group_exists( $slug, $table_name = false ) { 
  515. global $wpdb; 
  516.  
  517. if ( empty( $table_name ) ) 
  518. $table_name = buddypress()->groups->table_name; 
  519.  
  520. if ( empty( $slug ) ) 
  521. return false; 
  522.  
  523. $query = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$table_name} WHERE slug = %s", strtolower( $slug ) ) ); 
  524.  
  525. return is_numeric( $query ) ? (int) $query : $query; 
  526.  
  527. /** 
  528. * Get the ID of a group by the group's slug. 
  529. * Alias of {@link BP_Groups_Group::group_exists()}. 
  530. * @since 1.6.0 
  531. * @param string $slug See {@link BP_Groups_Group::group_exists()}. 
  532. * @return string|null See {@link BP_Groups_Group::group_exists()}. 
  533. */ 
  534. public static function get_id_from_slug( $slug ) { 
  535. return BP_Groups_Group::group_exists( $slug ); 
  536.  
  537. /** 
  538. * Get IDs of users with outstanding invites to a given group from a specified user. 
  539. * @since 1.6.0 
  540. * @param int $user_id ID of the inviting user. 
  541. * @param int $group_id ID of the group. 
  542. * @return array IDs of users who have been invited to the group by the 
  543. * user but have not yet accepted. 
  544. */ 
  545. public static function get_invites( $user_id, $group_id ) { 
  546. global $wpdb; 
  547.  
  548. $bp = buddypress(); 
  549.  
  550. return $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 = %d", $group_id, $user_id ) ); 
  551.  
  552. /** 
  553. * Get a list of a user's groups, filtered by a search string. 
  554. * @since 1.6.0 
  555. * @param string $filter Search term. Matches against 'name' and 
  556. * 'description' fields. 
  557. * @param int $user_id ID of the user whose groups are being searched. 
  558. * Default: the displayed user. 
  559. * @param mixed $order Not used. 
  560. * @param int|null $limit Optional. The max number of results to return. 
  561. * Default: null (no limit). 
  562. * @param int|null $page Optional. The page offset of results to return. 
  563. * Default: null (no limit). 
  564. * @return false|array { 
  565. * @type array $groups Array of matched and paginated group IDs. 
  566. * @type int $total Total count of groups matching the query. 
  567. * } 
  568. */ 
  569. public static function filter_user_groups( $filter, $user_id = 0, $order = false, $limit = null, $page = null ) { 
  570. if ( empty( $user_id ) ) { 
  571. $user_id = bp_displayed_user_id(); 
  572.  
  573. $args = array( 
  574. 'search_terms' => $filter,  
  575. 'user_id' => $user_id,  
  576. 'per_page' => $limit,  
  577. 'page' => $page,  
  578. 'order' => $order,  
  579. ); 
  580.  
  581. $groups = BP_Groups_Group::get( $args ); 
  582.  
  583. // Modify the results to match the old format. 
  584. $paged_groups = array(); 
  585. $i = 0; 
  586. foreach ( $groups['groups'] as $group ) { 
  587. $paged_groups[ $i ] = new stdClass; 
  588. $paged_groups[ $i ]->group_id = $group->id; 
  589. $i++; 
  590.  
  591. return array( 'groups' => $paged_groups, 'total' => $groups['total'] ); 
  592.  
  593. /** 
  594. * Get a list of groups, filtered by a search string. 
  595. * @since 1.6.0 
  596. * @param string $filter Search term. Matches against 'name' and 
  597. * 'description' fields. 
  598. * @param int|null $limit Optional. The max number of results to return. 
  599. * Default: null (no limit). 
  600. * @param int|null $page Optional. The page offset of results to return. 
  601. * Default: null (no limit). 
  602. * @param string|bool $sort_by Column to sort by. Default: false (default 
  603. * sort). 
  604. * @param string|bool $order ASC or DESC. Default: false (default sort). 
  605. * @return array { 
  606. * @type array $groups Array of matched and paginated group IDs. 
  607. * @type int $total Total count of groups matching the query. 
  608. * } 
  609. */ 
  610. public static function search_groups( $filter, $limit = null, $page = null, $sort_by = false, $order = false ) { 
  611. $args = array( 
  612. 'search_terms' => $filter,  
  613. 'per_page' => $limit,  
  614. 'page' => $page,  
  615. 'orderby' => $sort_by,  
  616. 'order' => $order,  
  617. ); 
  618.  
  619. $groups = BP_Groups_Group::get( $args ); 
  620.  
  621. // Modify the results to match the old format. 
  622. $paged_groups = array(); 
  623. $i = 0; 
  624. foreach ( $groups['groups'] as $group ) { 
  625. $paged_groups[ $i ] = new stdClass; 
  626. $paged_groups[ $i ]->group_id = $group->id; 
  627. $i++; 
  628.  
  629. return array( 'groups' => $paged_groups, 'total' => $groups['total'] ); 
  630.  
  631. /** 
  632. * Check for the existence of a slug. 
  633. * @since 1.6.0 
  634. * @param string $slug Slug to check. 
  635. * @return string|null The slug, if found. Otherwise null. 
  636. */ 
  637. public static function check_slug( $slug ) { 
  638. global $wpdb; 
  639.  
  640. $bp = buddypress(); 
  641.  
  642. return $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM {$bp->groups->table_name} WHERE slug = %s", $slug ) ); 
  643.  
  644. /** 
  645. * Get the slug for a given group ID. 
  646. * @since 1.6.0 
  647. * @param int $group_id ID of the group. 
  648. * @return string|null The slug, if found. Otherwise null. 
  649. */ 
  650. public static function get_slug( $group_id ) { 
  651. global $wpdb; 
  652.  
  653. $bp = buddypress(); 
  654.  
  655. return $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM {$bp->groups->table_name} WHERE id = %d", $group_id ) ); 
  656.  
  657. /** 
  658. * Check whether a given group has any members. 
  659. * @since 1.6.0 
  660. * @param int $group_id ID of the group. 
  661. * @return bool True if the group has members, otherwise false. 
  662. */ 
  663. public static function has_members( $group_id ) { 
  664. global $wpdb; 
  665.  
  666. $bp = buddypress(); 
  667.  
  668. $members = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->groups->table_name_members} WHERE group_id = %d", $group_id ) ); 
  669.  
  670. if ( empty( $members ) ) 
  671. return false; 
  672.  
  673. return true; 
  674.  
  675. /** 
  676. * Check whether a group has outstanding membership requests. 
  677. * @since 1.6.0 
  678. * @param int $group_id ID of the group. 
  679. * @return int|null The number of outstanding requests, or null if 
  680. * none are found. 
  681. */ 
  682. public static function has_membership_requests( $group_id ) { 
  683. global $wpdb; 
  684.  
  685. $bp = buddypress(); 
  686.  
  687. return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 0", $group_id ) ); 
  688.  
  689. /** 
  690. * Get outstanding membership requests for a group. 
  691. * @since 1.6.0 
  692. * @param int $group_id ID of the group. 
  693. * @param int|null $limit Optional. Max number of results to return. 
  694. * Default: null (no limit). 
  695. * @param int|null $page Optional. Page offset of results returned. Default: 
  696. * null (no limit). 
  697. * @return array { 
  698. * @type array $requests The requested page of located requests. 
  699. * @type int $total Total number of requests outstanding for the 
  700. * group. 
  701. * } 
  702. */ 
  703. public static function get_membership_requests( $group_id, $limit = null, $page = null ) { 
  704. global $wpdb; 
  705.  
  706. if ( !empty( $limit ) && !empty( $page ) ) { 
  707. $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 
  708.  
  709. $bp = buddypress(); 
  710.  
  711. $paged_requests = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 0 AND inviter_id = 0{$pag_sql}", $group_id ) ); 
  712. $total_requests = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 0 AND inviter_id = 0", $group_id ) ); 
  713.  
  714. return array( 'requests' => $paged_requests, 'total' => $total_requests ); 
  715.  
  716. /** 
  717. * Query for groups. 
  718. * @see WP_Meta_Query::queries for a description of the 'meta_query' 
  719. * parameter format. 
  720. * @since 1.6.0 
  721. * @since 2.6.0 Added `$group_type`, `$group_type__in`, and `$group_type__not_in` parameters. 
  722. * @since 2.7.0 Added `$update_admin_cache` and `$parent_id` parameters. 
  723. * @param array $args { 
  724. * Array of parameters. All items are optional. 
  725. * @type string $type Optional. Shorthand for certain orderby/order combinations. 
  726. * 'newest', 'active', 'popular', 'alphabetical', 'random'. 
  727. * When present, will override orderby and order params. 
  728. * Default: null. 
  729. * @type string $orderby Optional. Property to sort by. 'date_created', 'last_activity',  
  730. * 'total_member_count', 'name', 'random'. Default: 'date_created'. 
  731. * @type string $order Optional. Sort order. 'ASC' or 'DESC'. Default: 'DESC'. 
  732. * @type int $per_page Optional. Number of items to return per page of results. 
  733. * Default: null (no limit). 
  734. * @type int $page Optional. Page offset of results to return. 
  735. * Default: null (no limit). 
  736. * @type int $user_id Optional. If provided, results will be limited to groups 
  737. * of which the specified user is a member. Default: null. 
  738. * @type string $search_terms Optional. If provided, only groups whose names or descriptions 
  739. * match the search terms will be returned. Allows specifying the 
  740. * wildcard position using a '*' character before or after the 
  741. * string or both. Works in concert with $search_columns. 
  742. * Default: false. 
  743. * @type string $search_columns Optional. If provided, only apply the search terms to the 
  744. * specified columns. Works in concert with $search_terms. 
  745. * Default: empty array. 
  746. * @type array|string $group_type Array or comma-separated list of group types to limit results to. 
  747. * @type array|string $group_type__in Array or comma-separated list of group types to limit results to. 
  748. * @type array|string $group_type__not_in Array or comma-separated list of group types that will be 
  749. * excluded from results. 
  750. * @type array $meta_query Optional. An array of meta_query conditions. 
  751. * See {@link WP_Meta_Query::queries} for description. 
  752. * @type array|string $value Optional. Array or comma-separated list of group IDs. Results 
  753. * will be limited to groups within the list. Default: false. 
  754. * @type array|string $parent_id Optional. Array or comma-separated list of group IDs. Results 
  755. * will be limited to children of the specified groups. Default: null. 
  756. * @type array|string $exclude Optional. Array or comma-separated list of group IDs. 
  757. * Results will exclude the listed groups. Default: false. 
  758. * @type bool $update_meta_cache Whether to pre-fetch groupmeta for the returned groups. 
  759. * Default: true. 
  760. * @type bool $update_admin_cache Whether to pre-fetch administrator IDs for the returned 
  761. * groups. Default: false. 
  762. * @type bool $show_hidden Whether to include hidden groups in results. Default: false. 
  763. * } 
  764. * @return array { 
  765. * @type array $groups Array of group objects returned by the 
  766. * paginated query. 
  767. * @type int $total Total count of all groups matching non- 
  768. * paginated query params. 
  769. * } 
  770. */ 
  771. public static function get( $args = array() ) { 
  772. global $wpdb; 
  773.  
  774. // Backward compatibility with old method of passing arguments. 
  775. if ( ! is_array( $args ) || func_num_args() > 1 ) { 
  776. _deprecated_argument( __METHOD__, '1.7', 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__ ) ); 
  777.  
  778. $old_args_keys = array( 
  779. 0 => 'type',  
  780. 1 => 'per_page',  
  781. 2 => 'page',  
  782. 3 => 'user_id',  
  783. 4 => 'search_terms',  
  784. 5 => 'include',  
  785. 6 => 'populate_extras',  
  786. 7 => 'exclude',  
  787. 8 => 'show_hidden',  
  788. ); 
  789.  
  790. $args = bp_core_parse_args_array( $old_args_keys, func_get_args() ); 
  791.  
  792. $defaults = array( 
  793. 'type' => null,  
  794. 'orderby' => 'date_created',  
  795. 'order' => 'DESC',  
  796. 'per_page' => null,  
  797. 'page' => null,  
  798. 'user_id' => 0,  
  799. 'search_terms' => false,  
  800. 'search_columns' => array(),  
  801. 'group_type' => '',  
  802. 'group_type__in' => '',  
  803. 'group_type__not_in' => '',  
  804. 'meta_query' => false,  
  805. 'include' => false,  
  806. 'parent_id' => null,  
  807. 'update_meta_cache' => true,  
  808. 'update_admin_cache' => false,  
  809. 'exclude' => false,  
  810. 'show_hidden' => false,  
  811. ); 
  812.  
  813. $r = wp_parse_args( $args, $defaults ); 
  814.  
  815. $bp = buddypress(); 
  816.  
  817. $sql = array( 
  818. 'select' => "SELECT DISTINCT g.id",  
  819. 'from' => "{$bp->groups->table_name} g",  
  820. 'where' => '',  
  821. 'orderby' => '',  
  822. 'pagination' => '',  
  823. ); 
  824.  
  825. if ( ! empty( $r['user_id'] ) ) { 
  826. $sql['from'] .= " JOIN {$bp->groups->table_name_members} m ON ( g.id = m.group_id )"; 
  827.  
  828. $where_conditions = array(); 
  829.  
  830. if ( empty( $r['show_hidden'] ) ) { 
  831. $where_conditions['hidden'] = "g.status != 'hidden'"; 
  832.  
  833. $search = ''; 
  834. if ( isset( $r['search_terms'] ) ) { 
  835. $search = trim( $r['search_terms'] ); 
  836.  
  837. if ( $search ) { 
  838. $leading_wild = ( ltrim( $search, '*' ) != $search ); 
  839. $trailing_wild = ( rtrim( $search, '*' ) != $search ); 
  840. if ( $leading_wild && $trailing_wild ) { 
  841. $wild = 'both'; 
  842. } elseif ( $leading_wild ) { 
  843. $wild = 'leading'; 
  844. } elseif ( $trailing_wild ) { 
  845. $wild = 'trailing'; 
  846. } else { 
  847. // Default is to wrap in wildcard characters. 
  848. $wild = 'both'; 
  849. $search = trim( $search, '*' ); 
  850.  
  851. $searches = array(); 
  852. $leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : ''; 
  853. $trailing_wild = ( 'trailing' == $wild || 'both' == $wild ) ? '%' : ''; 
  854. $wildcarded = $leading_wild . bp_esc_like( $search ) . $trailing_wild; 
  855.  
  856. $search_columns = array( 'name', 'description' ); 
  857. if ( $r['search_columns'] ) { 
  858. $search_columns = array_intersect( $r['search_columns'], $search_columns ); 
  859.  
  860. foreach ( $search_columns as $search_column ) { 
  861. $searches[] = $wpdb->prepare( "$search_column LIKE %s", $wildcarded ); 
  862.  
  863. $where_conditions['search'] = '(' . implode(' OR ', $searches) . ')'; 
  864.  
  865. $meta_query_sql = self::get_meta_query_sql( $r['meta_query'] ); 
  866.  
  867. if ( ! empty( $meta_query_sql['join'] ) ) { 
  868. $sql['from'] .= $meta_query_sql['join']; 
  869.  
  870. if ( ! empty( $meta_query_sql['where'] ) ) { 
  871. $where_conditions['meta'] = $meta_query_sql['where']; 
  872.  
  873. // Only use 'group_type__in', if 'group_type' is not set. 
  874. if ( empty( $r['group_type'] ) && ! empty( $r['group_type__in']) ) { 
  875. $r['group_type'] = $r['group_type__in']; 
  876.  
  877. // Group types to exclude. This has priority over inclusions. 
  878. if ( ! empty( $r['group_type__not_in'] ) ) { 
  879. $group_type_clause = self::get_sql_clause_for_group_types( $r['group_type__not_in'], 'NOT IN' ); 
  880.  
  881. // Group types to include. 
  882. } elseif ( ! empty( $r['group_type'] ) ) { 
  883. $group_type_clause = self::get_sql_clause_for_group_types( $r['group_type'], 'IN' ); 
  884.  
  885. if ( ! empty( $group_type_clause ) ) { 
  886. $where_conditions['group_type'] = $group_type_clause; 
  887.  
  888. if ( ! empty( $r['user_id'] ) ) { 
  889. $where_conditions['user'] = $wpdb->prepare( "m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0", $r['user_id'] ); 
  890.  
  891. if ( ! empty( $r['include'] ) ) { 
  892. $include = implode( ', ', wp_parse_id_list( $r['include'] ) ); 
  893. $where_conditions['include'] = "g.id IN ({$include})"; 
  894.  
  895. if ( ! is_null( $r['parent_id'] ) ) { 
  896. // Note that `wp_parse_id_list()` converts `false` to 0. 
  897. $parent_id = implode( ', ', wp_parse_id_list( $r['parent_id'] ) ); 
  898. $where_conditions['parent_id'] = "g.parent_id IN ({$parent_id})"; 
  899.  
  900. if ( ! empty( $r['exclude'] ) ) { 
  901. $exclude = implode( ', ', wp_parse_id_list( $r['exclude'] ) ); 
  902. $where_conditions['exclude'] = "g.id NOT IN ({$exclude})"; 
  903.  
  904. /** Order/orderby ********************************************/ 
  905.  
  906. $order = $r['order']; 
  907. $orderby = $r['orderby']; 
  908.  
  909. // If a 'type' parameter was passed, parse it and overwrite 
  910. // 'order' and 'orderby' params passed to the function. 
  911. if ( ! empty( $r['type'] ) ) { 
  912.  
  913. /** 
  914. * Filters the 'type' parameter used to overwrite 'order' and 'orderby' values. 
  915. * @since 2.1.0 
  916. * @param array $value Converted 'type' value for order and orderby. 
  917. * @param string $value Parsed 'type' value for the get method. 
  918. */ 
  919. $order_orderby = apply_filters( 'bp_groups_get_orderby', self::convert_type_to_order_orderby( $r['type'] ), $r['type'] ); 
  920.  
  921. // If an invalid type is passed, $order_orderby will be 
  922. // an array with empty values. In this case, we stick 
  923. // with the default values of $order and $orderby. 
  924. if ( ! empty( $order_orderby['order'] ) ) { 
  925. $order = $order_orderby['order']; 
  926.  
  927. if ( ! empty( $order_orderby['orderby'] ) ) { 
  928. $orderby = $order_orderby['orderby']; 
  929.  
  930. // 'total_member_count' and 'last_activity' sorts require additional table joins. 
  931. if ( 'total_member_count' === $orderby ) { 
  932. $sql['from'] .= " JOIN {$bp->groups->table_name_groupmeta} gm_total_member_count ON ( g.id = gm_total_member_count.group_id )"; 
  933. $where_conditions['total_member_count'] = "gm_total_member_count.meta_key = 'total_member_count'"; 
  934. } elseif ( 'last_activity' === $orderby ) { 
  935.  
  936. $sql['from'] .= " JOIN {$bp->groups->table_name_groupmeta} gm_last_activity on ( g.id = gm_last_activity.group_id )"; 
  937. $where_conditions['last_activity'] = "gm_last_activity.meta_key = 'last_activity'"; 
  938.  
  939. // Sanitize 'order'. 
  940. $order = bp_esc_sql_order( $order ); 
  941.  
  942. /** 
  943. * Filters the converted 'orderby' term. 
  944. * @since 2.1.0 
  945. * @param string $value Converted 'orderby' term. 
  946. * @param string $orderby Original orderby value. 
  947. * @param string $value Parsed 'type' value for the get method. 
  948. */ 
  949. $orderby = apply_filters( 'bp_groups_get_orderby_converted_by_term', self::convert_orderby_to_order_by_term( $orderby ), $orderby, $r['type'] ); 
  950.  
  951. // Random order is a special case. 
  952. if ( 'rand()' === $orderby ) { 
  953. $sql['orderby'] = "ORDER BY rand()"; 
  954. } else { 
  955. $sql['orderby'] = "ORDER BY {$orderby} {$order}"; 
  956.  
  957. if ( ! empty( $r['per_page'] ) && ! empty( $r['page'] ) && $r['per_page'] != -1 ) { 
  958. $sql['pagination'] = $wpdb->prepare( "LIMIT %d, %d", intval( ( $r['page'] - 1 ) * $r['per_page']), intval( $r['per_page'] ) ); 
  959.  
  960. $where = ''; 
  961. if ( ! empty( $where_conditions ) ) { 
  962. $sql['where'] = implode( ' AND ', $where_conditions ); 
  963. $where = "WHERE {$sql['where']}"; 
  964.  
  965. $paged_groups_sql = "{$sql['select']} FROM {$sql['from']} {$where} {$sql['orderby']} {$sql['pagination']}"; 
  966.  
  967. /** 
  968. * Filters the pagination SQL statement. 
  969. * @since 1.5.0 
  970. * @param string $value Concatenated SQL statement. 
  971. * @param array $sql Array of SQL parts before concatenation. 
  972. * @param array $r Array of parsed arguments for the get method. 
  973. */ 
  974. $paged_groups_sql = apply_filters( 'bp_groups_get_paged_groups_sql', $paged_groups_sql, $sql, $r ); 
  975.  
  976. $cached = bp_core_get_incremented_cache( $paged_groups_sql, 'bp_groups' ); 
  977. if ( false === $cached ) { 
  978. $paged_group_ids = $wpdb->get_col( $paged_groups_sql ); 
  979. bp_core_set_incremented_cache( $paged_groups_sql, 'bp_groups', $paged_group_ids ); 
  980. } else { 
  981. $paged_group_ids = $cached; 
  982.  
  983. $uncached_group_ids = bp_get_non_cached_ids( $paged_group_ids, 'bp_groups' ); 
  984. if ( $uncached_group_ids ) { 
  985. $group_ids_sql = implode( ', ', array_map( 'intval', $uncached_group_ids ) ); 
  986. $group_data_objects = $wpdb->get_results( "SELECT g.* FROM {$bp->groups->table_name} g WHERE g.id IN ({$group_ids_sql})" ); 
  987. foreach ( $group_data_objects as $group_data_object ) { 
  988. wp_cache_set( $group_data_object->id, $group_data_object, 'bp_groups' ); 
  989.  
  990. $paged_groups = array(); 
  991. foreach ( $paged_group_ids as $paged_group_id ) { 
  992. $paged_groups[] = new BP_Groups_Group( $paged_group_id ); 
  993.  
  994. $total_groups_sql = "SELECT COUNT(DISTINCT g.id) FROM {$sql['from']} $where"; 
  995.  
  996. /** 
  997. * Filters the SQL used to retrieve total group results. 
  998. * @since 1.5.0 
  999. * @param string $t_sql Concatenated SQL statement used for retrieving total group results. 
  1000. * @param array $total_sql Array of SQL parts for the query. 
  1001. * @param array $r Array of parsed arguments for the get method. 
  1002. */ 
  1003. $total_groups_sql = apply_filters( 'bp_groups_get_total_groups_sql', $total_groups_sql, $sql, $r ); 
  1004.  
  1005. $cached = bp_core_get_incremented_cache( $total_groups_sql, 'bp_groups' ); 
  1006. if ( false === $cached ) { 
  1007. $total_groups = (int) $wpdb->get_var( $total_groups_sql ); 
  1008. bp_core_set_incremented_cache( $total_groups_sql, 'bp_groups', $total_groups ); 
  1009. } else { 
  1010. $total_groups = (int) $cached; 
  1011.  
  1012. $group_ids = array(); 
  1013. foreach ( (array) $paged_groups as $group ) { 
  1014. $group_ids[] = $group->id; 
  1015.  
  1016. // Grab all groupmeta. 
  1017. if ( ! empty( $r['update_meta_cache'] ) ) { 
  1018. bp_groups_update_meta_cache( $group_ids ); 
  1019.  
  1020. // Prefetch all administrator IDs, if requested. 
  1021. if ( $r['update_admin_cache'] ) { 
  1022. BP_Groups_Member::prime_group_admins_mods_cache( $group_ids ); 
  1023.  
  1024. // Set up integer properties needing casting. 
  1025. $int_props = array( 
  1026. 'id', 'creator_id', 'enable_forum' 
  1027. ); 
  1028.  
  1029. // Integer casting. 
  1030. foreach ( $paged_groups as $key => $g ) { 
  1031. foreach ( $int_props as $int_prop ) { 
  1032. $paged_groups[ $key ]->{$int_prop} = (int) $paged_groups[ $key ]->{$int_prop}; 
  1033.  
  1034. unset( $sql, $total_sql ); 
  1035.  
  1036. return array( 'groups' => $paged_groups, 'total' => $total_groups ); 
  1037.  
  1038. /** 
  1039. * Get the SQL for the 'meta_query' param in BP_Activity_Activity::get() 
  1040. * We use WP_Meta_Query to do the heavy lifting of parsing the 
  1041. * meta_query array and creating the necessary SQL clauses. 
  1042. * @since 1.8.0 
  1043. * @param array $meta_query An array of meta_query filters. See the 
  1044. * documentation for {@link WP_Meta_Query} for details. 
  1045. * @return array $sql_array 'join' and 'where' clauses. 
  1046. */ 
  1047. protected static function get_meta_query_sql( $meta_query = array() ) { 
  1048. global $wpdb; 
  1049.  
  1050. $sql_array = array( 
  1051. 'join' => '',  
  1052. 'where' => '',  
  1053. ); 
  1054.  
  1055. if ( ! empty( $meta_query ) ) { 
  1056. $groups_meta_query = new WP_Meta_Query( $meta_query ); 
  1057.  
  1058. // WP_Meta_Query expects the table name at 
  1059. // $wpdb->group. 
  1060. $wpdb->groupmeta = buddypress()->groups->table_name_groupmeta; 
  1061.  
  1062. $meta_sql = $groups_meta_query->get_sql( 'group', 'g', 'id' ); 
  1063. $sql_array['join'] = $meta_sql['join']; 
  1064. $sql_array['where'] = self::strip_leading_and( $meta_sql['where'] ); 
  1065.  
  1066. return $sql_array; 
  1067.  
  1068. /** 
  1069. * Convert the 'type' parameter to 'order' and 'orderby'. 
  1070. * @since 1.8.0 
  1071. * @param string $type The 'type' shorthand param. 
  1072. * @return array { 
  1073. * @type string $order SQL-friendly order string. 
  1074. * @type string $orderby SQL-friendly orderby column name. 
  1075. * } 
  1076. */ 
  1077. protected static function convert_type_to_order_orderby( $type = '' ) { 
  1078. $order = $orderby = ''; 
  1079.  
  1080. switch ( $type ) { 
  1081. case 'newest' : 
  1082. $order = 'DESC'; 
  1083. $orderby = 'date_created'; 
  1084. break; 
  1085.  
  1086. case 'active' : 
  1087. $order = 'DESC'; 
  1088. $orderby = 'last_activity'; 
  1089. break; 
  1090.  
  1091. case 'popular' : 
  1092. $order = 'DESC'; 
  1093. $orderby = 'total_member_count'; 
  1094. break; 
  1095.  
  1096. case 'alphabetical' : 
  1097. $order = 'ASC'; 
  1098. $orderby = 'name'; 
  1099. break; 
  1100.  
  1101. case 'random' : 
  1102. $order = ''; 
  1103. $orderby = 'random'; 
  1104. break; 
  1105.  
  1106. return array( 'order' => $order, 'orderby' => $orderby ); 
  1107.  
  1108. /** 
  1109. * Get a list of groups, sorted by those that have the most legacy forum topics. 
  1110. * @since 1.6.0 
  1111. * @param int|null $limit Optional. The max number of results to return. 
  1112. * Default: null (no limit). 
  1113. * @param int|null $page Optional. The page offset of results to return. 
  1114. * Default: null (no limit). 
  1115. * @param int $user_id Optional. If present, groups will be limited to 
  1116. * those of which the specified user is a member. 
  1117. * @param string|bool $search_terms Optional. Limit groups to those whose name 
  1118. * or description field contain the search string. 
  1119. * @param bool $populate_extras Optional. Whether to fetch extra 
  1120. * information about the groups. Default: true. 
  1121. * @param string|array|bool $exclude Optional. Array or comma-separated list of group 
  1122. * IDs to exclude from results. 
  1123. * @return array { 
  1124. * @type array $groups Array of group objects returned by the 
  1125. * paginated query. 
  1126. * @type int $total Total count of all groups matching non- 
  1127. * paginated query params. 
  1128. * } 
  1129. */ 
  1130. public static function get_by_most_forum_topics( $limit = null, $page = null, $user_id = 0, $search_terms = false, $populate_extras = true, $exclude = false ) { 
  1131. global $wpdb, $bbdb; 
  1132.  
  1133. if ( empty( $bbdb ) ) { 
  1134.  
  1135. /** This action is documented in bp-forums/bp-forums-screens */ 
  1136. do_action( 'bbpress_init' ); 
  1137.  
  1138. if ( !empty( $limit ) && !empty( $page ) ) { 
  1139. $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 
  1140.  
  1141. if ( !is_user_logged_in() || ( !bp_current_user_can( 'bp_moderate' ) && ( $user_id != bp_loggedin_user_id() ) ) ) 
  1142. $hidden_sql = " AND g.status != 'hidden'"; 
  1143.  
  1144. if ( !empty( $search_terms ) ) { 
  1145. $search_terms_like = '%' . bp_esc_like( $search_terms ) . '%'; 
  1146. $search_sql = $wpdb->prepare( ' AND ( g.name LIKE %s OR g.description LIKE %s ) ', $search_terms_like, $search_terms_like ); 
  1147.  
  1148. if ( !empty( $exclude ) ) { 
  1149. $exclude = implode( ', ', wp_parse_id_list( $exclude ) ); 
  1150. $exclude_sql = " AND g.id NOT IN ({$exclude})"; 
  1151.  
  1152. $bp = buddypress(); 
  1153.  
  1154. if ( !empty( $user_id ) ) { 
  1155. $user_id = absint( esc_sql( $user_id ) ); 
  1156. $paged_groups = $wpdb->get_results( "SELECT DISTINCT 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_groupmeta} gm3, {$bp->groups->table_name_members} m, {$bbdb->forums} f, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND g.id = gm3.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' AND (gm3.meta_key = 'forum_id' AND gm3.meta_value = f.forum_id) AND f.topics > 0 {$hidden_sql} {$search_sql} AND m.user_id = {$user_id} AND m.is_confirmed = 1 AND m.is_banned = 0 {$exclude_sql} ORDER BY f.topics DESC {$pag_sql}" ); 
  1157. $total_groups = $wpdb->get_var( "SELECT COUNT(DISTINCT g.id) FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_groupmeta} gm3, {$bbdb->forums} f, {$bp->groups->table_name} g WHERE g.id = gm1.group_id AND g.id = gm2.group_id AND g.id = gm3.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' AND (gm3.meta_key = 'forum_id' AND gm3.meta_value = f.forum_id) AND f.topics > 0 {$hidden_sql} {$search_sql} AND m.user_id = {$user_id} AND m.is_confirmed = 1 AND m.is_banned = 0 {$exclude_sql}" ); 
  1158. } else { 
  1159. $paged_groups = $wpdb->get_results( "SELECT DISTINCT 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_groupmeta} gm3, {$bbdb->forums} f, {$bp->groups->table_name} g WHERE g.id = gm1.group_id AND g.id = gm2.group_id AND g.id = gm3.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' AND (gm3.meta_key = 'forum_id' AND gm3.meta_value = f.forum_id) AND f.topics > 0 {$hidden_sql} {$search_sql} {$exclude_sql} ORDER BY f.topics DESC {$pag_sql}" ); 
  1160. $total_groups = $wpdb->get_var( "SELECT COUNT(DISTINCT g.id) FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_groupmeta} gm3, {$bbdb->forums} f, {$bp->groups->table_name} g WHERE g.id = gm1.group_id AND g.id = gm2.group_id AND g.id = gm3.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' AND (gm3.meta_key = 'forum_id' AND gm3.meta_value = f.forum_id) AND f.topics > 0 {$hidden_sql} {$search_sql} {$exclude_sql}" ); 
  1161.  
  1162. if ( !empty( $populate_extras ) ) { 
  1163. foreach ( (array) $paged_groups as $group ) { 
  1164. $group_ids[] = $group->id; 
  1165. $paged_groups = BP_Groups_Group::get_group_extras( $paged_groups, $group_ids, 'newest' ); 
  1166.  
  1167. return array( 'groups' => $paged_groups, 'total' => $total_groups ); 
  1168.  
  1169. /** 
  1170. * Convert the 'orderby' param into a proper SQL term/column. 
  1171. * @since 1.8.0 
  1172. * @param string $orderby Orderby term as passed to get(). 
  1173. * @return string $order_by_term SQL-friendly orderby term. 
  1174. */ 
  1175. protected static function convert_orderby_to_order_by_term( $orderby ) { 
  1176. $order_by_term = ''; 
  1177.  
  1178. switch ( $orderby ) { 
  1179. case 'date_created' : 
  1180. default : 
  1181. $order_by_term = 'g.date_created'; 
  1182. break; 
  1183.  
  1184. case 'last_activity' : 
  1185. $order_by_term = 'gm_last_activity.meta_value'; 
  1186. break; 
  1187.  
  1188. case 'total_member_count' : 
  1189. $order_by_term = 'CONVERT(gm_total_member_count.meta_value, SIGNED)'; 
  1190. break; 
  1191.  
  1192. case 'name' : 
  1193. $order_by_term = 'g.name'; 
  1194. break; 
  1195.  
  1196. case 'random' : 
  1197. $order_by_term = 'rand()'; 
  1198. break; 
  1199.  
  1200. return $order_by_term; 
  1201.  
  1202. /** 
  1203. * Get a list of groups, sorted by those that have the most legacy forum posts. 
  1204. * @since 1.6.0 
  1205. * @param int|null $limit Optional. The max number of results to return. 
  1206. * Default: null (no limit). 
  1207. * @param int|null $page Optional. The page offset of results to return. 
  1208. * Default: null (no limit). 
  1209. * @param string|bool $search_terms Optional. Limit groups to those whose name 
  1210. * or description field contain the search string. 
  1211. * @param bool $populate_extras Optional. Whether to fetch extra 
  1212. * information about the groups. Default: true. 
  1213. * @param string|array|bool $exclude Optional. Array or comma-separated list of group 
  1214. * IDs to exclude from results. 
  1215. * @return array { 
  1216. * @type array $groups Array of group objects returned by the 
  1217. * paginated query. 
  1218. * @type int $total Total count of all groups matching non- 
  1219. * paginated query params. 
  1220. * } 
  1221. */ 
  1222. public static function get_by_most_forum_posts( $limit = null, $page = null, $search_terms = false, $populate_extras = true, $exclude = false ) { 
  1223. global $wpdb, $bbdb; 
  1224.  
  1225. if ( empty( $bbdb ) ) { 
  1226.  
  1227. /** This action is documented in bp-forums/bp-forums-screens */ 
  1228. do_action( 'bbpress_init' ); 
  1229.  
  1230. if ( !empty( $limit ) && !empty( $page ) ) { 
  1231. $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 
  1232.  
  1233. if ( !is_user_logged_in() || ( !bp_current_user_can( 'bp_moderate' ) && ( $user_id != bp_loggedin_user_id() ) ) ) 
  1234. $hidden_sql = " AND g.status != 'hidden'"; 
  1235.  
  1236. if ( !empty( $search_terms ) ) { 
  1237. $search_terms_like = '%' . bp_esc_like( $search_terms ) . '%'; 
  1238. $search_sql = $wpdb->prepare( ' AND ( g.name LIKE %s OR g.description LIKE %s ) ', $search_terms_like, $search_terms_like ); 
  1239.  
  1240. if ( !empty( $exclude ) ) { 
  1241. $exclude = implode( ', ', wp_parse_id_list( $exclude ) ); 
  1242. $exclude_sql = " AND g.id NOT IN ({$exclude})"; 
  1243.  
  1244. $bp = buddypress(); 
  1245.  
  1246. if ( !empty( $user_id ) ) { 
  1247. $user_id = esc_sql( $user_id ); 
  1248. $paged_groups = $wpdb->get_results( "SELECT DISTINCT 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_groupmeta} gm3, {$bp->groups->table_name_members} m, {$bbdb->forums} f, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND g.id = gm3.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' AND (gm3.meta_key = 'forum_id' AND gm3.meta_value = f.forum_id) {$hidden_sql} {$search_sql} AND m.user_id = {$user_id} AND m.is_confirmed = 1 AND m.is_banned = 0 {$exclude_sql} ORDER BY f.posts ASC {$pag_sql}" ); 
  1249. $total_groups = $wpdb->get_results( "SELECT COUNT(DISTINCT g.id) FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_groupmeta} gm3, {$bp->groups->table_name_members} m, {$bbdb->forums} f, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND g.id = gm3.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' AND (gm3.meta_key = 'forum_id' AND gm3.meta_value = f.forum_id) AND f.posts > 0 {$hidden_sql} {$search_sql} AND m.user_id = {$user_id} AND m.is_confirmed = 1 AND m.is_banned = 0 {$exclude_sql} " ); 
  1250. } else { 
  1251. $paged_groups = $wpdb->get_results( "SELECT DISTINCT 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_groupmeta} gm3, {$bbdb->forums} f, {$bp->groups->table_name} g WHERE g.id = gm1.group_id AND g.id = gm2.group_id AND g.id = gm3.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' AND (gm3.meta_key = 'forum_id' AND gm3.meta_value = f.forum_id) AND f.posts > 0 {$hidden_sql} {$search_sql} {$exclude_sql} ORDER BY f.posts ASC {$pag_sql}" ); 
  1252. $total_groups = $wpdb->get_var( "SELECT COUNT(DISTINCT g.id) FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_groupmeta} gm3, {$bbdb->forums} f, {$bp->groups->table_name} g WHERE g.id = gm1.group_id AND g.id = gm2.group_id AND g.id = gm3.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' AND (gm3.meta_key = 'forum_id' AND gm3.meta_value = f.forum_id) {$hidden_sql} {$search_sql} {$exclude_sql}" ); 
  1253.  
  1254. if ( !empty( $populate_extras ) ) { 
  1255. foreach ( (array) $paged_groups as $group ) { 
  1256. $group_ids[] = $group->id; 
  1257. $paged_groups = BP_Groups_Group::get_group_extras( $paged_groups, $group_ids, 'newest' ); 
  1258.  
  1259. return array( 'groups' => $paged_groups, 'total' => $total_groups ); 
  1260.  
  1261. /** 
  1262. * Get a list of groups whose names start with a given letter. 
  1263. * @since 1.6.0 
  1264. * @param string $letter The letter. 
  1265. * @param int|null $limit Optional. The max number of results to return. 
  1266. * Default: null (no limit). 
  1267. * @param int|null $page Optional. The page offset of results to return. 
  1268. * Default: null (no limit). 
  1269. * @param bool $populate_extras Deprecated. 
  1270. * @param string|array|bool $exclude Optional. Array or comma-separated list of group 
  1271. * IDs to exclude from results. 
  1272. * @return false|array { 
  1273. * @type array $groups Array of group objects returned by the 
  1274. * paginated query. 
  1275. * @type int $total Total count of all groups matching non- 
  1276. * paginated query params. 
  1277. * } 
  1278. */ 
  1279. public static function get_by_letter( $letter, $limit = null, $page = null, $populate_extras = true, $exclude = false ) { 
  1280. global $wpdb; 
  1281.  
  1282. $pag_sql = $hidden_sql = $exclude_sql = ''; 
  1283.  
  1284. // Multibyte compliance. 
  1285. if ( function_exists( 'mb_strlen' ) ) { 
  1286. if ( mb_strlen( $letter, 'UTF-8' ) > 1 || is_numeric( $letter ) || !$letter ) { 
  1287. return false; 
  1288. } else { 
  1289. if ( strlen( $letter ) > 1 || is_numeric( $letter ) || !$letter ) { 
  1290. return false; 
  1291.  
  1292. $args = array( 
  1293. 'per_page' => $limit,  
  1294. 'page' => $page,  
  1295. 'search_terms' => $letter . '*',  
  1296. 'search_columns' => array( 'name' ),  
  1297. 'exclude' => $exclude,  
  1298. ); 
  1299.  
  1300. return BP_Groups_Group::get( $args ); 
  1301.  
  1302. /** 
  1303. * Get a list of random groups. 
  1304. * Use BP_Groups_Group::get() with 'type' = 'random' instead. 
  1305. * @since 1.6.0 
  1306. * @param int|null $limit Optional. The max number of results to return. 
  1307. * Default: null (no limit). 
  1308. * @param int|null $page Optional. The page offset of results to return. 
  1309. * Default: null (no limit). 
  1310. * @param int $user_id Optional. If present, groups will be limited to 
  1311. * those of which the specified user is a member. 
  1312. * @param string|bool $search_terms Optional. Limit groups to those whose name 
  1313. * or description field contain the search string. 
  1314. * @param bool $populate_extras Optional. Whether to fetch extra 
  1315. * information about the groups. Default: true. 
  1316. * @param string|array|bool $exclude Optional. Array or comma-separated list of group 
  1317. * IDs to exclude from results. 
  1318. * @return array { 
  1319. * @type array $groups Array of group objects returned by the 
  1320. * paginated query. 
  1321. * @type int $total Total count of all groups matching non- 
  1322. * paginated query params. 
  1323. * } 
  1324. */ 
  1325. public static function get_random( $limit = null, $page = null, $user_id = 0, $search_terms = false, $populate_extras = true, $exclude = false ) { 
  1326. $args = array( 
  1327. 'type' => 'random',  
  1328. 'per_page' => $limit,  
  1329. 'page' => $page,  
  1330. 'user_id' => $user_id,  
  1331. 'search_terms' => $search_terms,  
  1332. 'exclude' => $exclude,  
  1333. ); 
  1334.  
  1335. return BP_Groups_Group::get( $args ); 
  1336.  
  1337. /** 
  1338. * Fetch extra data for a list of groups. 
  1339. * This method is used throughout the class, by methods that take a 
  1340. * $populate_extras parameter. 
  1341. * Data fetched: 
  1342. * - Logged-in user's status within each group (is_member,  
  1343. * is_confirmed, is_pending, is_banned) 
  1344. * @since 1.6.0 
  1345. * @param array $paged_groups Array of groups. 
  1346. * @param string|array $group_ids Array or comma-separated list of IDs matching 
  1347. * $paged_groups. 
  1348. * @param string|bool $type Not used. 
  1349. * @return array $paged_groups 
  1350. */ 
  1351. public static function get_group_extras( &$paged_groups, &$group_ids, $type = false ) { 
  1352. $user_id = bp_loggedin_user_id(); 
  1353.  
  1354. foreach ( $paged_groups as &$group ) { 
  1355. $group->is_member = groups_is_user_member( $user_id, $group->id ) ? 1 : 0; 
  1356. $group->is_invited = groups_is_user_invited( $user_id, $group->id ) ? 1 : 0; 
  1357. $group->is_pending = groups_is_user_pending( $user_id, $group->id ) ? 1 : 0; 
  1358. $group->is_banned = (bool) groups_is_user_banned( $user_id, $group->id ); 
  1359.  
  1360. return $paged_groups; 
  1361.  
  1362. /** 
  1363. * Delete all invitations to a given group. 
  1364. * @since 1.6.0 
  1365. * @param int $group_id ID of the group whose invitations are being deleted. 
  1366. * @return int|null Number of rows records deleted on success, null on 
  1367. * failure. 
  1368. */ 
  1369. public static function delete_all_invites( $group_id ) { 
  1370. global $wpdb; 
  1371.  
  1372. $bp = buddypress(); 
  1373.  
  1374. return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE group_id = %d AND invite_sent = 1", $group_id ) ); 
  1375.  
  1376. /** 
  1377. * Get a total group count for the site. 
  1378. * Will include hidden groups in the count only if 
  1379. * current_user_can( 'bp_moderate' ). 
  1380. * @since 1.6.0 
  1381. * @return int Group count. 
  1382. */ 
  1383. public static function get_total_group_count() { 
  1384. global $wpdb; 
  1385.  
  1386. $hidden_sql = ''; 
  1387. if ( !bp_current_user_can( 'bp_moderate' ) ) 
  1388. $hidden_sql = "WHERE status != 'hidden'"; 
  1389.  
  1390. $bp = buddypress(); 
  1391.  
  1392. return $wpdb->get_var( "SELECT COUNT(id) FROM {$bp->groups->table_name} {$hidden_sql}" ); 
  1393.  
  1394. /** 
  1395. * Get global count of forum topics in public groups (legacy forums). 
  1396. * @since 1.6.0 
  1397. * @param string $type Optional. If 'unreplied', count will be limited to 
  1398. * those topics that have received no replies. 
  1399. * @return int Forum topic count. 
  1400. */ 
  1401. public static function get_global_forum_topic_count( $type ) { 
  1402. global $bbdb, $wpdb; 
  1403.  
  1404. $bp = buddypress(); 
  1405.  
  1406. if ( 'unreplied' == $type ) 
  1407. $bp->groups->filter_sql = ' AND t.topic_posts = 1'; 
  1408.  
  1409. /** 
  1410. * Filters the portion of the SQL related to global count of forum topics in public groups. 
  1411. * See https://buddypress.trac.wordpress.org/ticket/4306. 
  1412. * @since 1.6.0 
  1413. * @param string $filter_sql SQL portion for the query. 
  1414. * @param string $type Type of forum topics to query for. 
  1415. */ 
  1416. $extra_sql = apply_filters( 'get_global_forum_topic_count_extra_sql', $bp->groups->filter_sql, $type ); 
  1417.  
  1418. // Make sure the $extra_sql begins with an AND. 
  1419. if ( 'AND' != substr( trim( strtoupper( $extra_sql ) ), 0, 3 ) ) 
  1420. $extra_sql = ' AND ' . $extra_sql; 
  1421.  
  1422. return $wpdb->get_var( "SELECT COUNT(t.topic_id) FROM {$bbdb->topics} AS t, {$bp->groups->table_name} AS g LEFT JOIN {$bp->groups->table_name_groupmeta} AS gm ON g.id = gm.group_id WHERE (gm.meta_key = 'forum_id' AND gm.meta_value = t.forum_id) AND g.status = 'public' AND t.topic_status = '0' AND t.topic_sticky != '2' {$extra_sql} " ); 
  1423.  
  1424. /** 
  1425. * Get the member count for a group. 
  1426. * @since 1.6.0 
  1427. * @param int $group_id Group ID. 
  1428. * @return int Count of confirmed members for the group. 
  1429. */ 
  1430. public static function get_total_member_count( $group_id ) { 
  1431. global $wpdb; 
  1432.  
  1433. $bp = buddypress(); 
  1434.  
  1435. return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 1 AND is_banned = 0", $group_id ) ); 
  1436.  
  1437. /** 
  1438. * Get a total count of all topics of a given status, across groups/forums. 
  1439. * @since 1.5.0 
  1440. * @param string $status Which group type to count. 'public', 'private',  
  1441. * 'hidden', or 'all'. Default: 'public'. 
  1442. * @param string|bool $search_terms Provided search terms. 
  1443. * @return int The topic count 
  1444. */ 
  1445. public static function get_global_topic_count( $status = 'public', $search_terms = false ) { 
  1446. global $bbdb, $wpdb; 
  1447.  
  1448. switch ( $status ) { 
  1449. case 'all' : 
  1450. $status_sql = ''; 
  1451. break; 
  1452.  
  1453. case 'hidden' : 
  1454. $status_sql = "AND g.status = 'hidden'"; 
  1455. break; 
  1456.  
  1457. case 'private' : 
  1458. $status_sql = "AND g.status = 'private'"; 
  1459. break; 
  1460.  
  1461. case 'public' : 
  1462. default : 
  1463. $status_sql = "AND g.status = 'public'"; 
  1464. break; 
  1465.  
  1466. $bp = buddypress(); 
  1467.  
  1468. $sql = array(); 
  1469.  
  1470. $sql['select'] = "SELECT COUNT(t.topic_id)"; 
  1471. $sql['from'] = "FROM {$bbdb->topics} AS t INNER JOIN {$bp->groups->table_name_groupmeta} AS gm ON t.forum_id = gm.meta_value INNER JOIN {$bp->groups->table_name} AS g ON gm.group_id = g.id"; 
  1472. $sql['where'] = "WHERE gm.meta_key = 'forum_id' {$status_sql} AND t.topic_status = '0' AND t.topic_sticky != '2'"; 
  1473.  
  1474. if ( !empty( $search_terms ) ) { 
  1475. $search_terms_like = '%' . bp_esc_like( $search_terms ) . '%'; 
  1476. $sql['where'] .= $wpdb->prepare( " AND ( t.topic_title LIKE %s )", $search_terms_like ); 
  1477.  
  1478. return $wpdb->get_var( implode( ' ', $sql ) ); 
  1479.  
  1480. /** 
  1481. * Get an array containing ids for each group type. 
  1482. * A bit of a kludge workaround for some issues 
  1483. * with bp_has_groups(). 
  1484. * @since 1.7.0 
  1485. * @return array 
  1486. */ 
  1487. public static function get_group_type_ids() { 
  1488. global $wpdb; 
  1489.  
  1490. $bp = buddypress(); 
  1491. $ids = array(); 
  1492.  
  1493. $ids['all'] = $wpdb->get_col( "SELECT id FROM {$bp->groups->table_name}" ); 
  1494. $ids['public'] = $wpdb->get_col( "SELECT id FROM {$bp->groups->table_name} WHERE status = 'public'" ); 
  1495. $ids['private'] = $wpdb->get_col( "SELECT id FROM {$bp->groups->table_name} WHERE status = 'private'" ); 
  1496. $ids['hidden'] = $wpdb->get_col( "SELECT id FROM {$bp->groups->table_name} WHERE status = 'hidden'" ); 
  1497.  
  1498. return $ids; 
  1499.  
  1500. /** 
  1501. * Get SQL clause for group type(s). 
  1502. * @since 2.6.0 
  1503. * @param string|array $group_types Group type(s). 
  1504. * @param string $operator 'IN' or 'NOT IN'. 
  1505. * @return string $clause SQL clause. 
  1506. */ 
  1507. protected static function get_sql_clause_for_group_types( $group_types, $operator ) { 
  1508. global $wpdb; 
  1509.  
  1510. // Sanitize operator. 
  1511. if ( 'NOT IN' !== $operator ) { 
  1512. $operator = 'IN'; 
  1513.  
  1514. // Parse and sanitize types. 
  1515. if ( ! is_array( $group_types ) ) { 
  1516. $group_types = preg_split( '/[, \s+]/', $group_types ); 
  1517.  
  1518. $types = array(); 
  1519. foreach ( $group_types as $gt ) { 
  1520. if ( bp_groups_get_group_type_object( $gt ) ) { 
  1521. $types[] = $gt; 
  1522.  
  1523. $tax_query = new WP_Tax_Query( array( 
  1524. array( 
  1525. 'taxonomy' => 'bp_group_type',  
  1526. 'field' => 'name',  
  1527. 'operator' => $operator,  
  1528. 'terms' => $types,  
  1529. ),  
  1530. ) ); 
  1531.  
  1532. $site_id = bp_get_taxonomy_term_site_id( 'bp_group_type' ); 
  1533. $switched = false; 
  1534. if ( $site_id !== get_current_blog_id() ) { 
  1535. switch_to_blog( $site_id ); 
  1536. $switched = true; 
  1537.  
  1538. $sql_clauses = $tax_query->get_sql( 'g', 'id' ); 
  1539.  
  1540. $clause = ''; 
  1541.  
  1542. // The no_results clauses are the same between IN and NOT IN. 
  1543. if ( false !== strpos( $sql_clauses['where'], '0 = 1' ) ) { 
  1544. $clause = self::strip_leading_and( $sql_clauses['where'] ); 
  1545.  
  1546. // The tax_query clause generated for NOT IN can be used almost as-is. 
  1547. } elseif ( 'NOT IN' === $operator ) { 
  1548. $clause = self::strip_leading_and( $sql_clauses['where'] ); 
  1549.  
  1550. // IN clauses must be converted to a subquery. 
  1551. } elseif ( preg_match( '/' . $wpdb->term_relationships . '\.term_taxonomy_id IN \([0-9, ]+\)/', $sql_clauses['where'], $matches ) ) { 
  1552. $clause = " g.id IN ( SELECT object_id FROM $wpdb->term_relationships WHERE {$matches[0]} )"; 
  1553.  
  1554. if ( $switched ) { 
  1555. restore_current_blog(); 
  1556.  
  1557. return $clause; 
  1558.  
  1559. /** 
  1560. * Strips the leading AND and any surrounding whitespace from a string. 
  1561. * Used here to normalize SQL fragments generated by `WP_Meta_Query` and 
  1562. * other utility classes. 
  1563. * @since 2.7.0 
  1564. * @param string $s String. 
  1565. * @return string 
  1566. */ 
  1567. protected static function strip_leading_and( $s ) { 
  1568. return preg_replace( '/^\s*AND\s*/', '', $s );