Bp_Search_Groups

BuddyPress Global Search – search groups class

Description

Source

File: bp-search/classes/class-bp-search-groups.php

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
class Bp_Search_Groups extends Bp_Search_Type {
    private $type = 'groups';
 
    /**
     * Insures that only one instance of Class exists in memory at any
     * one time. Also prevents needing to define globals all over the place.
     * @since BuddyBoss 1.0.0
     *
     * @return object Bp_Search_Groups
     */
    public static function instance() {
        // Store the instance locally to avoid private static replication
        static $instance = null;
 
        // Only run these methods if they haven't been run previously
        if (null === $instance) {
            $instance = new Bp_Search_Groups();
        }
 
        // Always return the instance
        return $instance;
    }
 
    /**
     * A dummy constructor to prevent this class from being loaded more than once.
     *
     * @since BuddyBoss 1.0.0
     */
    private function __construct() { /* Do nothing here */
    }
 
    public function sql( $search_term, $only_totalrow_count=false ){
        /* an example UNION query :-
        -----------------------------------------------------
        (
            SELECT
                DISTINCT g.id, 'groups' as type, g.name LIKE '%ho%' AS relevance, gm2.meta_value as entry_date
            FROM
                wp_bp_groups_groupmeta gm1, wp_bp_groups_groupmeta gm2, wp_bp_groups g
            WHERE
                1=1
                AND g.id = gm1.group_id
                AND g.id = gm2.group_id
                AND gm2.meta_key = 'last_activity'
                AND gm1.meta_key = 'total_member_count'
                AND ( g.name LIKE '%ho%' OR g.description LIKE '%ho%' )
        )
        ----------------------------------------------------
        */
        global $wpdb, $bp;
        $query_placeholder = array();
 
        $sql = " SELECT ";
 
        if ( $only_totalrow_count ) {
            $sql .= " COUNT( DISTINCT g.id ) ";
        } else {
            $sql .= " DISTINCT g.id, 'groups' as type, g.name LIKE %s AS relevance, gm2.meta_value as entry_date ";
            $query_placeholder[] = '%'.$wpdb->esc_like( $search_term ).'%';
        }
 
        $sql .= " FROM
                    {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name} g
                WHERE
                    1=1
                    AND g.id = gm1.group_id
                    AND g.id = gm2.group_id
                    AND gm2.meta_key = 'last_activity'
                    AND gm1.meta_key = 'total_member_count'
                    AND ( g.name LIKE %s OR g.description LIKE %s )
            ";
        $query_placeholder[] = '%'.$wpdb->esc_like( $search_term ).'%';
        $query_placeholder[] = '%'.$wpdb->esc_like( $search_term ).'%';
 
        /** LOCATION AUTOCOMPLETE SEARCH ************************************************/
 
        if (function_exists('bp_bpla') && 'yes' == bp_bpla()->option('enable-for-groups') ) {
 
                $split_search_term = explode(' ', $search_term );
 
                $sql .= "OR g.id IN ( SELECT group_id FROM {$bp->groups->table_name_groupmeta} WHERE meta_key = 'bbgs_group_search_string' AND ";
 
                foreach ( $split_search_term as $k => $sterm ) {
 
                    if ( $k == 0 ) {
                        $sql .= "meta_value LIKE %s";
                        $query_placeholder[] = '%'.$wpdb->esc_like( $sterm ) . '%';
                    } else {
                        $sql .= "AND meta_value LIKE %s";
                        $query_placeholder[] = '%'.$wpdb->esc_like( $sterm ) .'%';
                    }
 
                }
                $sql .= " ) ";
 
        }
 
        /**
         * Properly handle hidden groups.
         * For guest users - exclude all hidden groups.
         * For members - include only those hidden groups where current user is a member.
         * For admins - include all hidden groups ( do nothing extra ).
         * @since 1.1.0
         */
        if( is_user_logged_in() ){
            if( !current_user_can( 'level_10' ) ){
                //get all hidden groups where i am a member of
                $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() );
                $hidden_groups_ids = $wpdb->get_col( $hidden_groups_sql );
                if( empty( $hidden_groups_ids ) ){
                    $hidden_groups_ids = array( 99999999 );//arbitrarily large number
                }
 
                $hidden_groups_ids_csv = implode( ',', $hidden_groups_ids );
 
                //either gruops which are not hidden,
                //or if hidden, only those where i am a member.
                $sql .= " AND ( g.status != 'hidden' OR g.id IN ( {$hidden_groups_ids_csv} ) ) ";
            }
        } else {
            $sql .= " AND g.status != 'hidden' ";
        }
 
        $sql = $wpdb->prepare( $sql, $query_placeholder );
 
        return apply_filters(
            'Bp_Search_Groups_sql',
            $sql,
            array(
                'search_term'           => $search_term,
                'only_totalrow_count'   => $only_totalrow_count,
            )
        );
    }
 
    protected function generate_html( $template_type='' ){
        $group_ids = array();
        foreach( $this->search_results['items'] as $item_id=>$item_html ){
            $group_ids[] = $item_id;
        }
 
        //now we have all the posts
        //lets do a groups loop
        $args = array( 'include'=>$group_ids, 'per_page'=>count($group_ids), 'search_terms' => false );
        if( is_user_logged_in() ){
            $args['show_hidden'] = true;
        }
 
        if (function_exists('bp_bpla') ) {
            $args['search_terms'] = ' ';
        }
 
        do_action( 'bp_before_search_groups_html' );
 
        if( bp_has_groups( $args ) ){
            while ( bp_groups() ){
                bp_the_group();
 
                $result = array(
                    'id'    => bp_get_group_id(),
                    'type'  => $this->type,
                    'title' => bp_get_group_name(),
                    'html'  => bp_search_buffer_template_part( 'loop/group', $template_type, false ),
                );
 
                $this->search_results['items'][bp_get_group_id()] = $result;
            }
        }
 
        do_action( 'bp_after_search_groups_html' );
    }
}

Methods

  • __construct — A dummy constructor to prevent this class from being loaded more than once.
  • generate_html
  • instance — Insures that only one instance of Class exists in memory at any one time. Also prevents needing to define globals all over the place.
  • sql

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.