BBoss_Global_Search_Groups

BuddyPress Global Search - search groups **************************************.

Defined (1)

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

/includes/search-types/class.BBoss_Global_Search_Groups.php  
  1. class BBoss_Global_Search_Groups extends BBoss_Global_Search_Type { 
  2. private $type = 'groups'; 
  3.  
  4. /** 
  5. * Insures that only one instance of Class exists in memory at any 
  6. * one time. Also prevents needing to define globals all over the place. 
  7. * @since 1.0.0 
  8. * @return object BBoss_Global_Search_Groups 
  9. */ 
  10. public static function instance() { 
  11. // Store the instance locally to avoid private static replication 
  12. static $instance = null; 
  13.  
  14. // Only run these methods if they haven't been run previously 
  15. if (null === $instance) { 
  16. $instance = new BBoss_Global_Search_Groups(); 
  17.  
  18. // Always return the instance 
  19. return $instance; 
  20.  
  21. /** 
  22. * A dummy constructor to prevent this class from being loaded more than once. 
  23. * @since 1.0.0 
  24. */ 
  25. private function __construct() { /** Do nothing here */ 
  26.  
  27. public function sql( $search_term, $only_totalrow_count=false ) { 
  28. /** an example UNION query :-  
  29. ----------------------------------------------------- 
  30. SELECT  
  31. DISTINCT g.id, 'groups' as type, g.name LIKE '%ho%' AS relevance, gm2.meta_value as entry_date  
  32. FROM  
  33. wp_bp_groups_groupmeta gm1, wp_bp_groups_groupmeta gm2, wp_bp_groups g  
  34. WHERE  
  35. 1=1  
  36. AND g.id = gm1.group_id  
  37. AND g.id = gm2.group_id  
  38. AND gm2.meta_key = 'last_activity'  
  39. AND gm1.meta_key = 'total_member_count'  
  40. AND ( g.name LIKE '%ho%' OR g.description LIKE '%ho%' ) 
  41. ---------------------------------------------------- 
  42. */ 
  43. global $wpdb, $bp; 
  44. $query_placeholder = array(); 
  45.  
  46. $sql = " SELECT "; 
  47.  
  48. if( $only_totalrow_count ) { 
  49. $sql .= " COUNT( DISTINCT g.id ) "; 
  50. } else { 
  51. $sql .= " DISTINCT g.id, 'groups' as type, g.name LIKE '%%%s%%' AS relevance, gm2.meta_value as entry_date "; 
  52. $query_placeholder[] = $search_term; 
  53.  
  54. $sql .= " FROM  
  55. {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name} g  
  56. WHERE  
  57. 1=1  
  58. AND g.id = gm1.group_id  
  59. AND g.id = gm2.group_id  
  60. AND gm2.meta_key = 'last_activity'  
  61. AND gm1.meta_key = 'total_member_count'  
  62. AND ( g.name LIKE '%%%s%%' OR g.description LIKE '%%%s%%' ) 
  63. "; 
  64. $query_placeholder[] = $search_term; 
  65. $query_placeholder[] = $search_term; 
  66.  
  67. /** 
  68. * Properly handle hidden groups. 
  69. * For guest users - exclude all hidden groups. 
  70. * For members - include only those hidden groups where current user is a member. 
  71. * For admins - include all hidden groups ( do nothing extra ). 
  72. * @since 1.1.0 
  73. */ 
  74. if( is_user_logged_in() ) { 
  75. if( !current_user_can( 'level_10' ) ) { 
  76. //get all hidden groups where i am a member of 
  77. $hidden_groups_sql = $wpdb->prepare( "SELECT DISTINCT gm.group_id FROM {$bp->groups->table_name_members} gm JOIN {$bp->groups->table_name} g ON gm.group_id = g.id WHERE gm.user_id = %d AND gm.is_confirmed = 1 AND gm.is_banned = 0 AND g.status='hidden' ", bp_loggedin_user_id() ); 
  78. $hidden_groups_ids = $wpdb->get_col( $hidden_groups_sql ); 
  79. if( empty( $hidden_groups_ids ) ) { 
  80. $hidden_groups_ids = array( 99999999 );//arbitrarily large number 
  81.  
  82. $hidden_groups_ids_csv = implode( ', ', $hidden_groups_ids ); 
  83.  
  84. //either gruops which are not hidden,  
  85. //or if hidden, only those where i am a member. 
  86. $sql .= " AND ( g.status != 'hidden' OR g.id IN ( {$hidden_groups_ids_csv} ) ) "; 
  87. } else { 
  88. $sql .= " AND g.status != 'hidden' "; 
  89.  
  90. $sql = $wpdb->prepare( $sql, $query_placeholder ); 
  91.  
  92. return apply_filters(  
  93. 'BBoss_Global_Search_Groups_sql',  
  94. $sql,  
  95. array(  
  96. 'search_term' => $search_term,  
  97. 'only_totalrow_count' => $only_totalrow_count,  
  98. )  
  99. ); 
  100.  
  101. protected function generate_html( $template_type='' ) { 
  102. $group_ids = array(); 
  103. foreach( $this->search_results['items'] as $item_id=>$item_html ) { 
  104. $group_ids[] = $item_id; 
  105.  
  106. //now we have all the posts 
  107. //lets do a groups loop 
  108. $args = array( 'include'=>$group_ids, 'per_page'=>count($group_ids) ); 
  109. if( is_user_logged_in() ) { 
  110. $args['show_hidden'] = true; 
  111.  
  112. if( bp_has_groups( $args ) ) { 
  113. while ( bp_groups() ) { 
  114. bp_the_group(); 
  115.  
  116. $result = array( 
  117. 'id' => bp_get_group_id(),  
  118. 'type' => $this->type,  
  119. 'title' => bp_get_group_name(),  
  120. 'html' => buddyboss_global_search_buffer_template_part( 'loop/group', $template_type, false ),  
  121. ); 
  122.  
  123. $this->search_results['items'][bp_get_group_id()] = $result; 
  124.  
  125. //var_dump( $this->search_results['items'] );