BP_XProfile_Query::sanitize_query( array $queries )

Ensure the xprofile_query argument passed to the class constructor is well-formed.


Description Description

Eliminates empty items and ensures that a ‘relation’ is set.


Parameters Parameters

$queries

(Required) Array of query clauses.


Top ↑

Return Return

(array) Sanitized array of query clauses.


Top ↑

Source Source

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

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
public 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;
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.2.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.