Ticket #17165: 17165.relation.2.diff

File 17165.relation.2.diff, 12.6 KB (added by scribu, 2 years ago)
Line 
1Index: wp-includes/user.php
2===================================================================
3--- wp-includes/user.php        (revision 17693)
4+++ wp-includes/user.php        (working copy)
5@@ -501,8 +501,6 @@
6                        $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query
7                }
8 
9-               _parse_meta_query( $qv );
10-
11                $role = trim( $qv['role'] );
12 
13                if ( $blog_id && ( $role || is_multisite() ) ) {
14@@ -517,8 +515,11 @@
15                        $qv['meta_query'][] = $cap_meta_query;
16                }
17 
18-               if ( !empty( $qv['meta_query'] ) ) {
19-                       $clauses = call_user_func_array( '_get_meta_sql', array( $qv['meta_query'], 'user', $wpdb->users, 'ID', &$this ) );
20+               $meta_query = new WP_Meta_Query();
21+               $meta_query->parse_query_vars( $qv );
22+
23+               if ( !empty( $meta_query->queries ) ) {
24+                       $clauses = $meta_query->get_sql( 'user', $wpdb->users, 'ID', $this );
25                        $this->query_from .= $clauses['join'];
26                        $this->query_where .= $clauses['where'];
27                }
28Index: wp-includes/query.php
29===================================================================
30--- wp-includes/query.php       (revision 17693)
31+++ wp-includes/query.php       (working copy)
32@@ -849,6 +849,15 @@
33        var $tax_query;
34 
35        /**
36+        * Metadata query container
37+        *
38+        * @since 3.2
39+        * @access public
40+        * @var object WP_Meta_Query
41+        */
42+       var $meta_query = false;
43+
44+       /**
45         * Holds the data for a single object that is queried.
46         *
47         * Holds the contents of a post, page, category, attachment.
48@@ -1525,8 +1534,6 @@
49                        }
50                        unset( $tax_query );
51 
52-                       _parse_meta_query( $qv );
53-
54                        if ( empty($qv['author']) || ($qv['author'] == '0') ) {
55                                $this->is_author = false;
56                        } else {
57@@ -1900,6 +1907,10 @@
58                // Fill again in case pre_get_posts unset some vars.
59                $q = $this->fill_query_vars($q);
60 
61+               // Parse meta query
62+               $this->meta_query = new WP_Meta_Query();
63+               $this->meta_query->parse_query_vars( $q );
64+
65                // Set a flag if a pre_get_posts hook changed the query vars.
66                $hash = md5( serialize( $this->query_vars ) );
67                if ( $hash != $this->query_vars_hash ) {
68@@ -2235,7 +2246,7 @@
69                        }
70                }
71 
72-               if ( !empty( $this->tax_query->queries ) || !empty( $q['meta_key'] ) ) {
73+               if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
74                        $groupby = "{$wpdb->posts}.ID";
75                }
76 
77@@ -2468,18 +2479,8 @@
78                        $where .= ')';
79                }
80 
81-               // Parse the meta query again if query vars have changed.
82-               if ( $this->query_vars_changed ) {
83-                       $meta_query_hash = md5( serialize( $q['meta_query'] ) );
84-                       $_meta_query = $q['meta_query'];
85-                       unset( $q['meta_query'] );
86-                       _parse_meta_query( $q );
87-                       if ( md5( serialize( $q['meta_query'] ) ) != $meta_query_hash && is_array( $_meta_query ) )
88-                               $q['meta_query'] = array_merge( $_meta_query, $q['meta_query'] );
89-               }
90-
91-               if ( !empty( $q['meta_query'] ) ) {
92-                       $clauses = call_user_func_array( '_get_meta_sql', array( $q['meta_query'], 'post', $wpdb->posts, 'ID', &$this) );
93+               if ( !empty( $this->meta_query->queries ) ) {
94+                       $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
95                        $join .= $clauses['join'];
96                        $where .= $clauses['where'];
97                }
98Index: wp-includes/meta.php
99===================================================================
100--- wp-includes/meta.php        (revision 17693)
101+++ wp-includes/meta.php        (working copy)
102@@ -355,121 +355,190 @@
103 /**
104  * Given a meta query, generates SQL clauses to be appended to a main query
105  *
106- * @since 3.1.0
107- * @access private
108+ * @since 3.2.0
109  *
110- * @param array $meta_query List of metadata queries. A single query is an associative array:
111- * - 'key' string The meta key
112- * - 'value' string|array The meta value
113- * - 'compare' (optional) string How to compare the key to the value.
114- *             Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
115- *             Default: '='
116- * - 'type' string (optional) The type of the value.
117- *             Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.
118- *             Default: 'CHAR'
119+ * @see WP_Meta_Query
120  *
121+ * @param array (optional) $meta_query A meta query
122  * @param string $type Type of meta
123  * @param string $primary_table
124  * @param string $primary_id_column
125  * @param object $context (optional) The main query object
126  * @return array( 'join' => $join_sql, 'where' => $where_sql )
127  */
128-function _get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) {
129-       global $wpdb;
130+function get_meta_sql( $meta_query = false, $type, $primary_table, $primary_id_column, $context = null ) {
131+       $meta_query_obj = new WP_Meta_Query( $meta_query );
132+       return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context );
133+}
134 
135-       if ( ! $meta_table = _get_meta_table( $type ) )
136-               return false;
137+/**
138+ * Container class for a multiple metadata query
139+ *
140+ * @since 3.2
141+ */
142+class WP_Meta_Query {
143+       /**
144+       * List of metadata queries. A single query is an associative array:
145+       * - 'key' string The meta key
146+       * - 'value' string|array The meta value
147+       * - 'compare' (optional) string How to compare the key to the value.
148+       *              Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
149+       *              Default: '='
150+       * - 'type' string (optional) The type of the value.
151+       *              Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.
152+       *              Default: 'CHAR'
153+       *
154+       * @since 3.2
155+       * @access public
156+       * @var array
157+       */
158+       public $queries = array();
159 
160-       $meta_id_column = esc_sql( $type . '_id' );
161+       /**
162+        * The relation between the queries. Can be one of 'AND' or 'OR'.
163+        *
164+        * @since 3.2
165+        * @access public
166+        * @var string
167+        */     
168+       public $relation;
169 
170-       $join = '';
171-       $where = '';
172-       $i = 0;
173-       foreach ( $meta_query as $q ) {
174-               $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
175-               $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
176-               $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
177+       /**
178+        * Constructor
179+        *
180+        * @param array (optional) $meta_query A meta query
181+        */
182+       function __construct( $meta_query = false ) {
183+               if ( !$meta_query )
184+                       return;
185 
186-               if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
187-                       $meta_compare = '=';
188+               if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) {
189+                       $this->relation = 'OR';
190+               } else {
191+                       $this->relation = 'AND';
192+               }
193 
194-               if ( 'NUMERIC' == $meta_type )
195-                       $meta_type = 'SIGNED';
196-               elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
197-                       $meta_type = 'CHAR';
198+               $this->queries = array();
199 
200-               if ( empty( $meta_key ) && empty( $meta_value ) )
201-                       continue;
202+               foreach ( $meta_query as $key => $query ) {
203+                       if ( ! is_array( $query ) )
204+                               continue;
205 
206-               $alias = $i ? 'mt' . $i : $meta_table;
207+                       $this->queries[] = $query;
208+               }
209+       }
210 
211-               $join .= "\nINNER JOIN $meta_table";
212-               $join .= $i ? " AS $alias" : '';
213-               $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
214+       /**
215+        * Constructs a meta query based on 'meta_*' query vars
216+        *
217+        * @since 3.2
218+        * @access public
219+        *
220+        * @param array $qv The query variables
221+        */
222+       function parse_query_vars( $qv ) {
223+               $meta_query = array();
224 
225-               $i++;
226+               // Simple query needs to be first for orderby=meta_value to work correctly
227+               foreach ( array( 'key', 'compare', 'type' ) as $key ) {
228+                       if ( !empty( $qv[ "meta_$key" ] ) )
229+                               $meta_query[0][ $key ] = $qv[ "meta_$key" ];
230+               }
231 
232-               if ( !empty( $meta_key ) )
233-                       $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
234+               // WP_Query sets 'meta_value' = '' by default
235+               if ( isset( $qv[ 'meta_value' ] ) && '' !== $qv[ 'meta_value' ] )
236+                       $meta_query[0]['value'] = $qv[ 'meta_value' ];
237 
238-               if ( !isset( $q['value'] ) )
239-                       continue;
240-               $meta_value = $q['value'];
241+               if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
242+                       $meta_query = array_merge( $meta_query, $qv['meta_query'] );
243+               }
244 
245-               if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
246-                       if ( ! is_array( $meta_value ) )
247-                               $meta_value = preg_split( '/[,\s]+/', $meta_value );
248+               $this->__construct( $meta_query );
249+       }
250 
251-                       if ( empty( $meta_value ) )
252+       /**
253+        * Generates SQL clauses to be appended to a main query.
254+        *
255+        * @since 3.2
256+        * @access public
257+        *
258+        * @param string $type Type of meta
259+        * @param string $primary_table
260+        * @param string $primary_id_column
261+        * @param object $context (optional) The main query object
262+        * @return array( 'join' => $join_sql, 'where' => $where_sql )
263+        */
264+       function get_sql( $type, $primary_table, $primary_id_column, $context = null ) {
265+               global $wpdb;
266+
267+               if ( ! $meta_table = _get_meta_table( $type ) )
268+                       return false;
269+
270+               $meta_id_column = esc_sql( $type . '_id' );
271+
272+               $join = '';
273+               $where = array();
274+               $i = 0;
275+               foreach ( $this->queries as $k => $q ) {
276+                       $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
277+                       $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
278+                       $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
279+
280+                       if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
281+                               $meta_compare = '=';
282+
283+                       if ( 'NUMERIC' == $meta_type )
284+                               $meta_type = 'SIGNED';
285+                       elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
286+                               $meta_type = 'CHAR';
287+
288+                       if ( empty( $meta_key ) && empty( $meta_value ) )
289                                continue;
290-               } else {
291-                       $meta_value = trim( $meta_value );
292-               }
293 
294-               if ( 'IN' == substr( $meta_compare, -2) ) {
295-                       $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
296-               } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
297-                       $meta_value = array_slice( $meta_value, 0, 2 );
298-                       $meta_compare_string = '%s AND %s';
299-               } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {
300-                       $meta_value = '%' . like_escape( $meta_value ) . '%';
301-                       $meta_compare_string = '%s';
302-               } else {
303-                       $meta_compare_string = '%s';
304-               }
305+                       $alias = $i ? 'mt' . $i : $meta_table;
306 
307-               $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
308-       }
309+                       $join .= "\nINNER JOIN $meta_table";
310+                       $join .= $i ? " AS $alias" : '';
311+                       $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
312 
313-       return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $meta_query, $type, $primary_table, $primary_id_column, &$context ) );
314-}
315+                       $i++;
316 
317-/**
318- * Populates the $meta_query property
319- *
320- * @access private
321- * @since 3.1.0
322- *
323- * @param array $qv The query variables
324- */
325-function _parse_meta_query( &$qv ) {
326-       $meta_query = array();
327+                       if ( !empty( $meta_key ) )
328+                               $where[$k] = $wpdb->prepare( "$alias.meta_key = %s", $meta_key );
329 
330-       // Simple query needs to be first for orderby=meta_value to work correctly
331-       foreach ( array( 'key', 'compare', 'type' ) as $key ) {
332-               if ( !empty( $qv[ "meta_$key" ] ) )
333-                       $meta_query[0][ $key ] = $qv[ "meta_$key" ];
334-       }
335+                       if ( !isset( $q['value'] ) )
336+                               continue;
337+                       $meta_value = $q['value'];
338 
339-       // WP_Query sets 'meta_value' = '' by default
340-       if ( isset( $qv[ 'meta_value' ] ) && '' !== $qv[ 'meta_value' ] )
341-               $meta_query[0]['value'] = $qv[ 'meta_value' ];
342+                       if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
343+                               if ( ! is_array( $meta_value ) )
344+                                       $meta_value = preg_split( '/[,\s]+/', $meta_value );
345 
346-       if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
347-               $meta_query = array_merge( $meta_query, $qv['meta_query'] );
348+                               if ( empty( $meta_value ) )
349+                                       continue;
350+                       } else {
351+                               $meta_value = trim( $meta_value );
352+                       }
353+
354+                       if ( 'IN' == substr( $meta_compare, -2) ) {
355+                               $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
356+                       } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
357+                               $meta_value = array_slice( $meta_value, 0, 2 );
358+                               $meta_compare_string = '%s AND %s';
359+                       } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {
360+                               $meta_value = '%' . like_escape( $meta_value ) . '%';
361+                               $meta_compare_string = '%s';
362+                       } else {
363+                               $meta_compare_string = '%s';
364+                       }
365+
366+                       $where[$k] = ' (' . $where[$k] . $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string})", $meta_value );
367+               }
368+               $where = ' AND (' . implode( " {$this->relation} ", $where ) . ' )';
369+
370+               return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $this->queries, $type, $primary_table, $primary_id_column, $context ) );
371        }
372 
373-       $qv['meta_query'] = $meta_query;
374 }
375 
376 /**