BP_Activity_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)

  • 'where'
    (array) Array of subclauses for the WHERE statement.
  • 'join'
    (array) Empty array. Not used.

Source

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

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
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_Activity_Activity::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
BuddyPress 2.2.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.