BP_Document_Query::get_sql_for_clause( array $clause, array $parent_query )

Generate WHERE clauses for a first-order clause.

Description

Parameters

$clause

(Required) Array of arguments belonging to the clause.

$parent_query

(Required) Parent query to which the clause belongs.

Return

(array)

Source

File: bp-document/classes/class-bp-document-query.php

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
protected function get_sql_for_clause( $clause, $parent_query ) {
    global $wpdb;
 
    $sql_chunks = array(
        'where' => array(),
        'join'  => array(),
    );
 
    $column = isset( $clause['column'] ) ? $this->validate_column( $clause['column'] ) : '';
    $value  = isset( $clause['value'] ) ? $clause['value'] : '';
    if ( empty( $column ) || ! isset( $clause['value'] ) ) {
        return $sql_chunks;
    }
 
    if ( isset( $clause['compare'] ) ) {
        $clause['compare'] = strtoupper( $clause['compare'] );
    } else {
        $clause['compare'] = isset( $clause['value'] ) && is_array( $clause['value'] ) ? 'IN' : '=';
    }
 
    // Default 'compare' to '=' if no valid operator is found.
    if ( ! in_array(
        $clause['compare'],
        array(
            '=',
            '!=',
            '>',
            '>=',
            '<',
            '<=',
            'LIKE',
            'NOT LIKE',
            'IN',
            'NOT IN',
            'BETWEEN',
            'NOT BETWEEN',
            'REGEXP',
            'NOT REGEXP',
            'RLIKE',
        )
    ) ) {
        $clause['compare'] = '=';
    }
 
    $compare = $clause['compare'];
 
    $alias = ! empty( $this->table_alias ) ? "{$this->table_alias}." : '';
 
    // Next, Build the WHERE clause.
    $where = '';
 
    // Value.
    if ( isset( $clause['value'] ) ) {
        if ( in_array( $compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
            if ( ! is_array( $value ) ) {
                $value = preg_split( '/[,\s]+/', $value );
            }
        }
 
        // Tinyint.
        if ( ! empty( $column ) && true === in_array( $column, array( 'hide_sitewide', 'is_spam' ) ) ) {
            $sql_chunks['where'][] = $wpdb->prepare( "{$alias}{$column} = %d", $value );
 
        } else {
            switch ( $compare ) {
                // IN uses different syntax.
                case 'IN':
                case 'NOT IN':
                    $in_sql = BP_Document::get_in_operator_sql( "{$alias}{$column}", $value );
 
                    // 'NOT IN' operator is as easy as a string replace!
                    if ( 'NOT IN' === $compare ) {
                        $in_sql = str_replace( 'IN', 'NOT IN', $in_sql );
                    }
 
                    $sql_chunks['where'][] = $in_sql;
                    break;
 
                case 'BETWEEN':
                case 'NOT BETWEEN':
                    $value = array_slice( $value, 0, 2 );
                    $where = $wpdb->prepare( '%s AND %s', $value );
                    break;
 
                case 'LIKE':
                case 'NOT LIKE':
                    $value = '%' . bp_esc_like( $value ) . '%';
                    $where = $wpdb->prepare( '%s', $value );
                    break;
 
                default:
                    $where = $wpdb->prepare( '%s', $value );
                    break;
 
            }
        }
 
        if ( $where ) {
            $sql_chunks['where'][] = "{$alias}{$column} {$compare} {$where}";
        }
    }
 
    /*
     * Multiple WHERE clauses should be joined in parentheses.
     */
    if ( 1 < count( $sql_chunks['where'] ) ) {
        $sql_chunks['where'] = array( '( ' . implode( ' AND ', $sql_chunks['where'] ) . ' )' );
    }
 
    return $sql_chunks;
}

Changelog

Changelog
Version Description
BuddyBoss 1.4.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.