Ticket #17165: 17165.5.diff

File 17165.5.diff, 12.2 KB (added by greuben, 2 years ago)
Line 
1Index: wp-includes/user.php
2===================================================================
3--- wp-includes/user.php        (revision 17678)
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 = call_user_func_array( array( $meta_query, 'get_sql' ), array( '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 17678)
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,7 +1534,7 @@
49                        }
50                        unset( $tax_query );
51 
52-                       _parse_meta_query( $qv );
53+                       $this->meta_query->parse_query_vars( $qv );
54 
55                        if ( empty($qv['author']) || ($qv['author'] == '0') ) {
56                                $this->is_author = false;
57@@ -2231,7 +2240,10 @@
58                        }
59                }
60 
61-               if ( !empty( $this->tax_query->queries ) || !empty( $q['meta_key'] ) ) {
62+               // Parse the meta query again if query vars have changed.
63+               $this->meta_query->parse_query_vars( $q );
64+
65+               if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
66                        $groupby = "{$wpdb->posts}.ID";
67                }
68 
69@@ -2462,18 +2474,8 @@
70                        $where .= ')';
71                }
72 
73-               // Parse the meta query again if query vars have changed.
74-               if ( $this->query_vars_changed ) {
75-                       $meta_query_hash = md5( serialize( $q['meta_query'] ) );
76-                       $_meta_query = $q['meta_query'];
77-                       unset( $q['meta_query'] );
78-                       _parse_meta_query( $q );
79-                       if ( md5( serialize( $q['meta_query'] ) ) != $meta_query_hash && is_array( $_meta_query ) )
80-                               $q['meta_query'] = array_merge( $_meta_query, $q['meta_query'] );
81-               }
82-
83-               if ( !empty( $q['meta_query'] ) ) {
84-                       $clauses = call_user_func_array( '_get_meta_sql', array( $q['meta_query'], 'post', $wpdb->posts, 'ID', &$this) );
85+               if ( !empty( $this->meta_query->queries ) ) {
86+                       $clauses = call_user_func_array( array( $this->meta_query, 'get_sql' ), array( 'post', $wpdb->posts, 'ID', &$this) );
87                        $join .= $clauses['join'];
88                        $where .= $clauses['where'];
89                }
90@@ -2887,6 +2889,7 @@
91         */
92        function &query( $query ) {
93                $this->init();
94+               $this->meta_query = new WP_Meta_Query();
95                $this->query = $this->query_vars = wp_parse_args( $query );
96                return $this->get_posts();
97        }
98Index: wp-includes/meta.php
99===================================================================
100--- wp-includes/meta.php        (revision 17678)
101+++ wp-includes/meta.php        (working copy)
102@@ -355,121 +355,166 @@
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+        * Constructor
163+        *
164+        * @param array (optional) $meta_query A meta query
165+        */
166+       function __construct( $meta_query = false ) {
167+               if( $meta_query )
168+                       $this->queries = $meta_query;
169+       }
170 
171-       $join = '';
172-       $where = '';
173-       $i = 0;
174-       foreach ( $meta_query as $q ) {
175-               $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
176-               $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
177-               $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
178+       /**
179+        * Populates the $queries property by looking for 'meta_*' query variables
180+        *
181+        * @since 3.2
182+        * @access public
183+        *
184+        * @param array $qv The query variables
185+        */
186+       function parse_query_vars( $qv ) {
187+               $this->queries = array();
188 
189-               if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
190-                       $meta_compare = '=';
191+               // Simple query needs to be first for orderby=meta_value to work correctly
192+               foreach ( array( 'key', 'compare', 'type' ) as $key ) {
193+                       if ( !empty( $qv[ "meta_$key" ] ) )
194+                               $this->queries[0][ $key ] = $qv[ "meta_$key" ];
195+               }
196 
197-               if ( 'NUMERIC' == $meta_type )
198-                       $meta_type = 'SIGNED';
199-               elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
200-                       $meta_type = 'CHAR';
201+               // WP_Query sets 'meta_value' = '' by default
202+               if ( isset( $qv[ 'meta_value' ] ) && '' !== $qv[ 'meta_value' ] )
203+                       $this->queries[0]['value'] = $qv[ 'meta_value' ];
204 
205-               if ( empty( $meta_key ) && empty( $meta_value ) )
206-                       continue;
207+               if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
208+                       $this->queries = array_merge( $this->queries, $qv['meta_query'] );
209+               }
210+       }
211 
212-               $alias = $i ? 'mt' . $i : $meta_table;
213+       /**
214+        * Generates SQL clauses to be appended to a main query.
215+        *
216+        * @since 3.2
217+        * @access public
218+        *
219+        * @param string $type Type of meta
220+        * @param string $primary_table
221+        * @param string $primary_id_column
222+        * @param object $context (optional) The main query object
223+        * @return array( 'join' => $join_sql, 'where' => $where_sql )
224+        */
225+       function get_sql( $type, $primary_table, $primary_id_column, $context = null ) {
226+               global $wpdb;
227 
228-               $join .= "\nINNER JOIN $meta_table";
229-               $join .= $i ? " AS $alias" : '';
230-               $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
231+               if ( ! $meta_table = _get_meta_table( $type ) )
232+                       return false;
233 
234-               $i++;
235+               $meta_id_column = esc_sql( $type . '_id' );
236 
237-               if ( !empty( $meta_key ) )
238-                       $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
239+               $join = '';
240+               $where = '';
241+               $i = 0;
242+               foreach ( $this->queries as $q ) {
243+                       if( ! is_array( $q ) )
244+                               continue;
245 
246-               if ( !isset( $q['value'] ) )
247-                       continue;
248-               $meta_value = $q['value'];
249+                       $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
250+                       $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
251+                       $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
252 
253-               if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
254-                       if ( ! is_array( $meta_value ) )
255-                               $meta_value = preg_split( '/[,\s]+/', $meta_value );
256+                       if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
257+                               $meta_compare = '=';
258 
259-                       if ( empty( $meta_value ) )
260+                       if ( 'NUMERIC' == $meta_type )
261+                               $meta_type = 'SIGNED';
262+                       elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
263+                               $meta_type = 'CHAR';
264+
265+                       if ( empty( $meta_key ) && empty( $meta_value ) )
266                                continue;
267-               } else {
268-                       $meta_value = trim( $meta_value );
269-               }
270 
271-               if ( 'IN' == substr( $meta_compare, -2) ) {
272-                       $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
273-               } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
274-                       $meta_value = array_slice( $meta_value, 0, 2 );
275-                       $meta_compare_string = '%s AND %s';
276-               } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {
277-                       $meta_value = '%' . like_escape( $meta_value ) . '%';
278-                       $meta_compare_string = '%s';
279-               } else {
280-                       $meta_compare_string = '%s';
281-               }
282+                       $alias = $i ? 'mt' . $i : $meta_table;
283 
284-               $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
285-       }
286+                       $join .= "\nINNER JOIN $meta_table";
287+                       $join .= $i ? " AS $alias" : '';
288+                       $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
289 
290-       return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $meta_query, $type, $primary_table, $primary_id_column, &$context ) );
291-}
292+                       $i++;
293 
294-/**
295- * Populates the $meta_query property
296- *
297- * @access private
298- * @since 3.1.0
299- *
300- * @param array $qv The query variables
301- */
302-function _parse_meta_query( &$qv ) {
303-       $meta_query = array();
304+                       if ( !empty( $meta_key ) )
305+                               $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
306 
307-       // Simple query needs to be first for orderby=meta_value to work correctly
308-       foreach ( array( 'key', 'compare', 'type' ) as $key ) {
309-               if ( !empty( $qv[ "meta_$key" ] ) )
310-                       $meta_query[0][ $key ] = $qv[ "meta_$key" ];
311-       }
312+                       if ( !isset( $q['value'] ) )
313+                               continue;
314+                       $meta_value = $q['value'];
315 
316-       // WP_Query sets 'meta_value' = '' by default
317-       if ( isset( $qv[ 'meta_value' ] ) && '' !== $qv[ 'meta_value' ] )
318-               $meta_query[0]['value'] = $qv[ 'meta_value' ];
319+                       if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
320+                               if ( ! is_array( $meta_value ) )
321+                                       $meta_value = preg_split( '/[,\s]+/', $meta_value );
322 
323-       if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
324-               $meta_query = array_merge( $meta_query, $qv['meta_query'] );
325+                               if ( empty( $meta_value ) )
326+                                       continue;
327+                       } else {
328+                               $meta_value = trim( $meta_value );
329+                       }
330+
331+                       if ( 'IN' == substr( $meta_compare, -2) ) {
332+                               $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
333+                       } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
334+                               $meta_value = array_slice( $meta_value, 0, 2 );
335+                               $meta_compare_string = '%s AND %s';
336+                       } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {
337+                               $meta_value = '%' . like_escape( $meta_value ) . '%';
338+                               $meta_compare_string = '%s';
339+                       } else {
340+                               $meta_compare_string = '%s';
341+                       }
342+
343+                       $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
344+               }
345+
346+               return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $this->queries, $type, $primary_table, $primary_id_column, $context ) );
347        }
348 
349-       $qv['meta_query'] = $meta_query;
350 }
351 
352 /**