BP_Recursive_Query::sanitize_query( array $queries )

Recursive-friendly query sanitizer.

Description

Ensures that each query-level clause has a ‘relation’ key, and that each first-order clause contains all the necessary keys from $defaults.

Extend this method if your class uses different sanitizing logic.

Parameters

$queries

(Required) Array of query clauses.

Return

(array) Sanitized array of query clauses.

Source

File: bp-core/classes/class-bp-recursive-query.php

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
protected function sanitize_query( $queries ) {
    $clean_queries = array();
 
    if ( ! is_array( $queries ) ) {
        return $clean_queries;
    }
 
    foreach ( $queries as $key => $query ) {
        if ( 'relation' === $key ) {
            $relation = $query;
 
        } elseif ( ! is_array( $query ) ) {
            continue;
 
        // First-order clause.
        } elseif ( $this->is_first_order_clause( $query ) ) {
            if ( isset( $query['value'] ) && array() === $query['value'] ) {
                unset( $query['value'] );
            }
 
            $clean_queries[] = $query;
 
        // Otherwise, it's a nested query, so we recurse.
        } else {
            $cleaned_query = $this->sanitize_query( $query );
 
            if ( ! empty( $cleaned_query ) ) {
                $clean_queries[] = $cleaned_query;
            }
        }
    }
 
    if ( empty( $clean_queries ) ) {
        return $clean_queries;
    }
 
    // Sanitize the 'relation' key provided in the query.
    if ( isset( $relation ) && 'OR' === strtoupper( $relation ) ) {
        $clean_queries['relation'] = 'OR';
 
    /*
     * If there is only a single clause, call the relation 'OR'.
     * This value will not actually be used to join clauses, but it
     * simplifies the logic around combining key-only queries.
     */
    } elseif ( 1 === count( $clean_queries ) ) {
        $clean_queries['relation'] = 'OR';
 
    // Default to AND.
    } else {
        $clean_queries['relation'] = 'AND';
    }
 
    return $clean_queries;
}

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.