Bp_Search_Members

BuddyPress Global Search – search members class

Description

Source

File: bp-search/classes/class-bp-search-members.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
    class Bp_Search_Members extends Bp_Search_Type {
        private $type = 'members';
 
        /**
         * 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_Members
         */
        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_Members();
 
                add_action( 'bp_search_settings_item_members', array( $instance, 'print_search_options' ) );
            }
 
            // 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 */
        }
 
        /**
         * Generates sql for members search.
         *
         * @todo: if Mr.X has set privacy of xprofile field 'location' data as 'private'
         * then, location of Mr.X shouldn't be checked in searched.
         *
         * @since BuddyBoss 1.0.0
         * @param string $search_term
         * @param boolean $only_totalrow_count
         *
         * @return string sql query
         */
        public function sql( $search_term, $only_totalrow_count=false ){
            global $wpdb, $bp;
 
            $bp_prefix = bp_core_get_table_prefix();
 
            $query_placeholder = array();
 
            $COLUMNS = " SELECT ";
 
            if( $only_totalrow_count ){
                $COLUMNS .= " COUNT( DISTINCT u.id ) ";
            } else {
                $COLUMNS .= " DISTINCT u.id, 'members' as type, u.display_name LIKE %s AS relevance, a.date_recorded as entry_date ";
                $query_placeholder[] = '%'. $search_term .'%';
            }
 
            $FROM = " {$wpdb->users} u LEFT JOIN {$bp->members->table_name_last_activity} a ON a.user_id=u.id AND a.component = 'members' AND a.type = 'last_activity'";
 
            $WHERE = array();
            $WHERE[] = "1=1";
            $WHERE[] = "u.user_status = 0";
            $where_fields = array();
 
            /* ++++++++++++++++++++++++++++++++
             * wp_users table fields
             +++++++++++++++++++++++++++++++ */
            $user_fields = bp_get_search_user_fields();
            if ( ! empty( $user_fields ) ) {
                $conditions_wp_user_table = array();
                foreach ( $user_fields as $user_field => $field_label ) {
 
 
                    if ( ! bp_is_search_user_field_enable( $user_field ) ) {
                        continue;
                    }
 
                    if ( 'user_meta' === $user_field ) {
                        //Search in user meta table for terms
                        $conditions_wp_user_table[] = " ID IN ( SELECT user_id FROM {$wpdb->usermeta} WHERE {$bp_prefix}bp_strip_tags(meta_value) LIKE %s ) ";
                        $query_placeholder[]        = '%' . $search_term . '%';
                    } else {
                        $conditions_wp_user_table[] = $user_field . " LIKE %s ";
                        $query_placeholder[]        = '%' . $search_term . '%';
                    }
 
                }
 
                if ( ! empty( $conditions_wp_user_table ) ) {
 
                    $clause_wp_user_table = "u.id IN ( SELECT ID FROM {$wpdb->users}  WHERE ( ";
                    $clause_wp_user_table .= implode( ' OR ', $conditions_wp_user_table );
                    $clause_wp_user_table .= " ) ) ";
 
                    $where_fields[] = $clause_wp_user_table;
                }
 
            }
            /* _____________________________ */
 
            /* ++++++++++++++++++++++++++++++++
             * xprofile fields
             +++++++++++++++++++++++++++++++ */
            //get all selected xprofile fields
            if( function_exists( 'bp_is_active' ) && bp_is_active( 'xprofile' ) ){
                $groups = bp_xprofile_get_groups( array(
                    'fetch_fields' => true
                ) );
 
                if ( !empty( $groups ) ){
                    $selected_xprofile_fields = array(
                        'word_search'   => array(0), //Search for whole word in field of type checkbox and radio
                        'char_search'   => array(0), //Search for character in field of type textbox, textarea and etc
                    );
 
                    $word_search_field_type = array( 'radio', 'checkbox' );
 
                    foreach ( $groups as $group ){
                        if ( !empty( $group->fields ) ){
                            foreach ( $group->fields as $field ) {
                                if ( bp_is_search_xprofile_enable( $field->id ) ) {
 
                                    if( in_array( $field->type, $word_search_field_type ) ) {
                                        $selected_xprofile_fields['word_search'][] = $field->id;
                                    } else {
                                        $selected_xprofile_fields['char_search'][] = $field->id;
                                    }
                                }
                            }
                        }
                    }
 
                    if( !empty( $selected_xprofile_fields ) ){
                        //u.id IN ( SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE %s )
                        $clause_xprofile_table = "u.id IN ( SELECT user_id FROM {$bp->profile->table_name_data} WHERE ( {$bp_prefix}bp_strip_tags(value) LIKE %s AND field_id IN ( ";
                        $clause_xprofile_table .= implode( ',', $selected_xprofile_fields['char_search'] );
                        $clause_xprofile_table .= ") ) OR ( value REGEXP '[[:<:]]{$search_term}[[:>:]]' AND field_id IN ( ";
                        $clause_xprofile_table .= implode( ',', $selected_xprofile_fields['word_search'] );
                        $clause_xprofile_table .= ") ) ) ";
 
                        $where_fields[] = $clause_xprofile_table;
                        $query_placeholder[] = '%'. $search_term .'%';
                    }
                }
            }
            /* _____________________________ */
 
                        /* ++++++++++++++++++++++++++++++++
             * Search from search string
             +++++++++++++++++++++++++++++++ */
 
                            $split_search_term = explode(' ', $search_term);
 
                            if (count($split_search_term) > 1 ) {
 
                                $clause_search_string_table = "u.id IN ( SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = 'bbgs_search_string' AND (";
 
                                foreach ( $split_search_term as $k => $sterm ) {
 
                                    if ( $k == 0 ) {
                                        $clause_search_string_table .= " meta_value LIKE %s ";
                                        $query_placeholder[] = '%'. $sterm .'%';
                                    } else {
                                        $clause_search_string_table .= " OR meta_value LIKE %s ";
                                        $query_placeholder[] = '%'. $sterm .'%';
                                    }
                                }
 
                                $clause_search_string_table .= ") ) ";
 
                                $where_fields[] = $clause_search_string_table;
 
                            }
 
                        /* _____________________________ */
 
            if( !empty( $where_fields ) )
                $WHERE[] = '(' . implode ( ' OR ', $where_fields ) . ')';
 
            // other conditions
//          $WHERE[] = " a.component = 'members' ";
//          $WHERE[] = " a.type = 'last_activity' ";
 
            $sql = $COLUMNS . ' FROM ' . $FROM . ' WHERE ' . implode( ' AND ', $WHERE );
            if( !$only_totalrow_count ){
                $sql .= " GROUP BY u.id ";
            }
 
            $sql = $wpdb->prepare( $sql, $query_placeholder );
 
 
                        return apply_filters(
                            'Bp_Search_Members_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 ){
                $group_ids[] = $item_id;
            }
 
            do_action( 'bp_before_search_members_html' );
 
            //now we have all the posts
            //lets do a groups loop
            if ( bp_has_members( array(
                'search_terms' => '',
                'include'      => $group_ids,
                'per_page'     => count( $group_ids )
            ) ) ) {
                while ( bp_members() ) {
                    bp_the_member();
 
                    $result_item = array(
                        'id'    => bp_get_member_user_id(),
                        'type'  => $this->type,
                        'title' => bp_get_member_name(),
                        'html'  => bp_search_buffer_template_part( 'loop/member', $template_type, false ),
                    );
 
                    $this->search_results['items'][ bp_get_member_user_id() ] = $result_item;
                }
            }
 
            do_action( 'bp_after_search_members_html' );
        }
 
        /**
         * What fields members should be searched on?
         * Prints options to search through username, email, nicename/displayname.
         * Prints xprofile fields, if xprofile component is active.
         *
         * @since BuddyBoss 1.0.0
         */
        function print_search_options( $items_to_search ){
            echo "<div class='wp-user-fields' style='margin: 10px 0 0 30px'>";
            echo "<p class='xprofile-group-name' style='margin: 5px 0'><strong>" . __('Account','buddyboss') . "</strong></p>";
 
            $fields = array(
                'user_login'    => __( 'Username/Login', 'buddyboss' ),
                'display_name'  => __( 'Display Name', 'buddyboss' ),
                'user_email'    => __( 'Email', 'buddyboss' ),
                'user_meta'     => __( 'User Meta', 'buddyboss' )
            );
            foreach( $fields as $field=>$label ){
                $item = 'member_field_' . $field;
                $checked = !empty( $items_to_search ) && in_array( $item, $items_to_search ) ? ' checked' : '';
                echo "<label><input type='checkbox' value='{$item}' name='bp_search_plugin_options[items-to-search][]' {$checked}>{$label}</label><br>";
            }
 
            echo "</div><!-- .wp-user-fields -->";
 
            if( !function_exists( 'bp_is_active' ) || !bp_is_active( 'xprofile' ) )
                return;
 
            $groups = bp_xprofile_get_groups( array(
                'fetch_fields' => true
            ) );
 
            if ( !empty( $groups ) ){
                echo "<div class='xprofile-fields' style='margin: 0 0 10px 30px'>";
                foreach ( $groups as $group ){
                    echo "<p class='xprofile-group-name' style='margin: 5px 0'><strong>" . $group->name . "</strong></p>";
 
                    if ( !empty( $group->fields ) ){
                        foreach ( $group->fields as $field ) {
                            //lets save these as xprofile_field_{field_id}
                            $item = 'xprofile_field_' . $field->id;
                            $checked = !empty( $items_to_search ) && in_array( $item, $items_to_search ) ? ' checked' : '';
                            echo "<label><input type='checkbox' value='{$item}' name='bp_search_plugin_options[items-to-search][]' {$checked}>{$field->name}</label><br>";
                        }
                    }
                }
                echo "</div><!-- .xprofile-fields -->";
            }
        }
    }

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.
  • print_search_options — What fields members should be searched on? Prints options to search through username, email, nicename/displayname.
  • sql — Generates sql for members search.

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.