bp_ps_xprofile_search( $f )

Return results from BuddyBoss Profile Search Extended.

Description

Source

File: bp-core/profile-search/bps-xprofile.php

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
function bp_ps_xprofile_search ($f)
{  
    global $bp, $wpdb;
     
    $value = $f->value;
    $filter = $f->format. '_'.  ($f->filter == ''? 'is': $f->filter);
     
    $sql = array ('select' => '', 'where' => array ());
    $sql['select'] = "SELECT user_id FROM {$bp->profile->table_name_data}";
    $sql['where']['field_id'] = $wpdb->prepare ("field_id = %d", $f->id);
 
    switch ($filter)
    {
    case 'integer_range':
        if (isset ($value['min']))  $sql['where']['min'] = $wpdb->prepare ("value >= %d", $value['min']);
        if (isset ($value['max']))  $sql['where']['max'] = $wpdb->prepare ("value <= %d", $value['max']);
        break;
 
    case 'decimal_range':
        if (isset ($value['min']))  $sql['where']['min'] = $wpdb->prepare ("value >= %f", $value['min']);
        if (isset ($value['max']))  $sql['where']['max'] = $wpdb->prepare ("value <= %f", $value['max']);
        break;
 
    case 'date_date_range':
        $range_types = array( 'min', 'max' );
        foreach ( $range_types as $range_type ) {
            if ( isset( $value[ $range_type ]['year'] ) && !empty( $value[ $range_type ]['year'] ) ) {
                $year = $f->value[ $range_type ]['year'];
                $month  = !empty( $f->value[ $range_type ]['month'] ) ? $f->value[ $range_type ]['month'] : '00';
                $day    = !empty( $f->value[ $range_type ]['day'] ) ? $f->value[ $range_type ]['day'] : '00';
                $date = $year . '-' . $month . '-' . $day;
                 
                $operator = 'min' == $range_type ? '>=' : '<=';
                 
                $sql['where'][ $range_type ] = $wpdb->prepare ( "DATE(value) $operator %s", $date );
            }
        }
        break;
 
    case 'date_age_range':
        $day = date ('j');
        $month = date ('n');
        $year = date ('Y');
 
        if (isset ($value['max']))
        {
            $ymin = $year - $value['max'] - 1;
            $sql['where']['age_min'] = $wpdb->prepare ("DATE(value) > %s", "$ymin-$month-$day");
        }
        if (isset ($value['min']))
        {
            $ymax = $year - $value['min'];
            $sql['where']['age_max'] = $wpdb->prepare ("DATE(value) <= %s", "$ymax-$month-$day");
        }
        break;
 
    case 'text_contains':
    case 'location_contains':
        if ( is_array( $value ) ) {
            $values = (array)$value;
            $parts = array ();
            foreach ( $values as $v ) {
                $v = str_replace ( '&', '&amp;', $v );
                $escaped = '%'. bp_ps_esc_like ( $v ). '%';
                $parts[] = $wpdb->prepare ( "value LIKE %s", $escaped);
            }
            $match = ' OR ';
            $sql['where'][$filter] = '('. implode ($match, $parts). ')';
        } else {
            $value = str_replace ('&', '&amp;', $value);
            $escaped = '%'. bp_ps_esc_like ($value). '%';
            $sql['where'][$filter] = $wpdb->prepare ("value LIKE %s", $escaped);
        }
        break;
 
    case 'text_like':
    case 'location_like':
        $value = str_replace ('&', '&amp;', $value);
        $value = str_replace ('\\\\%', '\\%', $value);
        $value = str_replace ('\\\\_', '\\_', $value);
        $sql['where'][$filter] = $wpdb->prepare ("value LIKE %s", $value);
        break;
 
    case 'text_is':
    case 'location_is':
        $value = str_replace ('&', '&amp;', $value);
        $sql['where'][$filter] = $wpdb->prepare ("value = %s", $value);
        break;
 
    case 'integer_is':
        $sql['where'][$filter] = $wpdb->prepare ("value = %d", $value);
        break;
 
    case 'decimal_is':
        $sql['where'][$filter] = $wpdb->prepare ("value = %f", $value);
        break;
 
    case 'date_is':
        $sql['where'][$filter] = $wpdb->prepare ("DATE(value) = %s", $value);
        break;
 
    case 'text_one_of':
        $values = (array)$value;
        $parts = array ();
        foreach ($values as $value)
        {
            $value = str_replace ('&', '&amp;', $value);
            $parts[] = $wpdb->prepare ("value = %s", $value);
        }
        $sql['where'][$filter] = '('. implode (' OR ', $parts). ')';
        break;
 
    case 'set_match_any':
    case 'set_match_all':
        $values = (array)$value;
        $parts = array ();
        foreach ($values as $value)
        {
            $value = str_replace ('&', '&amp;', $value);
            $escaped = '%:"'. bp_ps_esc_like ($value). '";%';
            $parts[] = $wpdb->prepare ("value LIKE %s", $escaped);
        }
        $match = ($filter == 'set_match_any')? ' OR ': ' AND ';
        $sql['where'][$filter] = '('. implode ($match, $parts). ')';
        break;
 
    default:
        return array ();
    }
 
    $sql = apply_filters ('bp_ps_field_sql', $sql, $f);
    $query = $sql['select']. ' WHERE '. implode (' AND ', $sql['where']);
     
    $results = $wpdb->get_col ($query);
    return $results;
}

Changelog

Changelog
Version Description
BuddyBoss 1.0.0 Introduced.

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.