]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-wp-meta-query.php
WordPress 4.4-scripts
[autoinstalls/wordpress.git] / wp-includes / class-wp-meta-query.php
1 <?php
2 /**
3  * Meta API: WP_Meta_Query class
4  *
5  * @package WordPress
6  * @subpackage Meta
7  * @since 4.4.0
8  */
9
10 /**
11  * Core class used to implement meta queries for the Meta API.
12  *
13  * Used for generating SQL clauses that filter a primary query according to metadata keys and values.
14  *
15  * `WP_Meta_Query` is a helper that allows primary query classes, such as {@see WP_Query} and {@see WP_User_Query},
16  * to filter their results by object metadata, by generating `JOIN` and `WHERE` subclauses to be attached
17  * to the primary SQL query string.
18  *
19  * @since 3.2.0
20  * @package WordPress
21  * @subpackage Meta
22  */
23 class WP_Meta_Query {
24         /**
25          * Array of metadata queries.
26          *
27          * See {@see WP_Meta_Query::__construct()} for information on meta query arguments.
28          *
29          * @since 3.2.0
30          * @access public
31          * @var array
32          */
33         public $queries = array();
34
35         /**
36          * The relation between the queries. Can be one of 'AND' or 'OR'.
37          *
38          * @since 3.2.0
39          * @access public
40          * @var string
41          */
42         public $relation;
43
44         /**
45          * Database table to query for the metadata.
46          *
47          * @since 4.1.0
48          * @access public
49          * @var string
50          */
51         public $meta_table;
52
53         /**
54          * Column in meta_table that represents the ID of the object the metadata belongs to.
55          *
56          * @since 4.1.0
57          * @access public
58          * @var string
59          */
60         public $meta_id_column;
61
62         /**
63          * Database table that where the metadata's objects are stored (eg $wpdb->users).
64          *
65          * @since 4.1.0
66          * @access public
67          * @var string
68          */
69         public $primary_table;
70
71         /**
72          * Column in primary_table that represents the ID of the object.
73          *
74          * @since 4.1.0
75          * @access public
76          * @var string
77          */
78         public $primary_id_column;
79
80         /**
81          * A flat list of table aliases used in JOIN clauses.
82          *
83          * @since 4.1.0
84          * @access protected
85          * @var array
86          */
87         protected $table_aliases = array();
88
89         /**
90          * A flat list of clauses, keyed by clause 'name'.
91          *
92          * @since 4.2.0
93          * @access protected
94          * @var array
95          */
96         protected $clauses = array();
97
98         /**
99          * Whether the query contains any OR relations.
100          *
101          * @since 4.3.0
102          * @access protected
103          * @var bool
104          */
105         protected $has_or_relation = false;
106
107         /**
108          * Constructor.
109          *
110          * @since 3.2.0
111          * @since 4.2.0 Introduced support for naming query clauses by associative array keys.
112          *
113          * @access public
114          *
115          * @param array $meta_query {
116          *     Array of meta query clauses. When first-order clauses use strings as their array keys, they may be
117          *     referenced in the 'orderby' parameter of the parent query.
118          *
119          *     @type string $relation Optional. The MySQL keyword used to join
120          *                            the clauses of the query. Accepts 'AND', or 'OR'. Default 'AND'.
121          *     @type array {
122          *         Optional. An array of first-order clause parameters, or another fully-formed meta query.
123          *
124          *         @type string $key     Meta key to filter by.
125          *         @type string $value   Meta value to filter by.
126          *         @type string $compare MySQL operator used for comparing the $value. Accepts '=',
127          *                               '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN',
128          *                               'BETWEEN', 'NOT BETWEEN', 'REGEXP', 'NOT REGEXP', or 'RLIKE'.
129          *                               Default is 'IN' when `$value` is an array, '=' otherwise.
130          *         @type string $type    MySQL data type that the meta_value column will be CAST to for
131          *                               comparisons. Accepts 'NUMERIC', 'BINARY', 'CHAR', 'DATE',
132          *                               'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', or 'UNSIGNED'.
133          *                               Default is 'CHAR'.
134          *     }
135          * }
136          */
137         public function __construct( $meta_query = false ) {
138                 if ( !$meta_query )
139                         return;
140
141                 if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) {
142                         $this->relation = 'OR';
143                 } else {
144                         $this->relation = 'AND';
145                 }
146
147                 $this->queries = $this->sanitize_query( $meta_query );
148         }
149
150         /**
151          * Ensure the 'meta_query' argument passed to the class constructor is well-formed.
152          *
153          * Eliminates empty items and ensures that a 'relation' is set.
154          *
155          * @since 4.1.0
156          * @access public
157          *
158          * @param array $queries Array of query clauses.
159          * @return array Sanitized array of query clauses.
160          */
161         public function sanitize_query( $queries ) {
162                 $clean_queries = array();
163
164                 if ( ! is_array( $queries ) ) {
165                         return $clean_queries;
166                 }
167
168                 foreach ( $queries as $key => $query ) {
169                         if ( 'relation' === $key ) {
170                                 $relation = $query;
171
172                         } elseif ( ! is_array( $query ) ) {
173                                 continue;
174
175                         // First-order clause.
176                         } elseif ( $this->is_first_order_clause( $query ) ) {
177                                 if ( isset( $query['value'] ) && array() === $query['value'] ) {
178                                         unset( $query['value'] );
179                                 }
180
181                                 $clean_queries[ $key ] = $query;
182
183                         // Otherwise, it's a nested query, so we recurse.
184                         } else {
185                                 $cleaned_query = $this->sanitize_query( $query );
186
187                                 if ( ! empty( $cleaned_query ) ) {
188                                         $clean_queries[ $key ] = $cleaned_query;
189                                 }
190                         }
191                 }
192
193                 if ( empty( $clean_queries ) ) {
194                         return $clean_queries;
195                 }
196
197                 // Sanitize the 'relation' key provided in the query.
198                 if ( isset( $relation ) && 'OR' === strtoupper( $relation ) ) {
199                         $clean_queries['relation'] = 'OR';
200                         $this->has_or_relation = true;
201
202                 /*
203                  * If there is only a single clause, call the relation 'OR'.
204                  * This value will not actually be used to join clauses, but it
205                  * simplifies the logic around combining key-only queries.
206                  */
207                 } elseif ( 1 === count( $clean_queries ) ) {
208                         $clean_queries['relation'] = 'OR';
209
210                 // Default to AND.
211                 } else {
212                         $clean_queries['relation'] = 'AND';
213                 }
214
215                 return $clean_queries;
216         }
217
218         /**
219          * Determine whether a query clause is first-order.
220          *
221          * A first-order meta query clause is one that has either a 'key' or
222          * a 'value' array key.
223          *
224          * @since 4.1.0
225          * @access protected
226          *
227          * @param array $query Meta query arguments.
228          * @return bool Whether the query clause is a first-order clause.
229          */
230         protected function is_first_order_clause( $query ) {
231                 return isset( $query['key'] ) || isset( $query['value'] );
232         }
233
234         /**
235          * Constructs a meta query based on 'meta_*' query vars
236          *
237          * @since 3.2.0
238          * @access public
239          *
240          * @param array $qv The query variables
241          */
242         public function parse_query_vars( $qv ) {
243                 $meta_query = array();
244
245                 /*
246                  * For orderby=meta_value to work correctly, simple query needs to be
247                  * first (so that its table join is against an unaliased meta table) and
248                  * needs to be its own clause (so it doesn't interfere with the logic of
249                  * the rest of the meta_query).
250                  */
251                 $primary_meta_query = array();
252                 foreach ( array( 'key', 'compare', 'type' ) as $key ) {
253                         if ( ! empty( $qv[ "meta_$key" ] ) ) {
254                                 $primary_meta_query[ $key ] = $qv[ "meta_$key" ];
255                         }
256                 }
257
258                 // WP_Query sets 'meta_value' = '' by default.
259                 if ( isset( $qv['meta_value'] ) && '' !== $qv['meta_value'] && ( ! is_array( $qv['meta_value'] ) || $qv['meta_value'] ) ) {
260                         $primary_meta_query['value'] = $qv['meta_value'];
261                 }
262
263                 $existing_meta_query = isset( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ? $qv['meta_query'] : array();
264
265                 if ( ! empty( $primary_meta_query ) && ! empty( $existing_meta_query ) ) {
266                         $meta_query = array(
267                                 'relation' => 'AND',
268                                 $primary_meta_query,
269                                 $existing_meta_query,
270                         );
271                 } elseif ( ! empty( $primary_meta_query ) ) {
272                         $meta_query = array(
273                                 $primary_meta_query,
274                         );
275                 } elseif ( ! empty( $existing_meta_query ) ) {
276                         $meta_query = $existing_meta_query;
277                 }
278
279                 $this->__construct( $meta_query );
280         }
281
282         /**
283          * Return the appropriate alias for the given meta type if applicable.
284          *
285          * @since 3.7.0
286          * @access public
287          *
288          * @param string $type MySQL type to cast meta_value.
289          * @return string MySQL type.
290          */
291         public function get_cast_for_type( $type = '' ) {
292                 if ( empty( $type ) )
293                         return 'CHAR';
294
295                 $meta_type = strtoupper( $type );
296
297                 if ( ! preg_match( '/^(?:BINARY|CHAR|DATE|DATETIME|SIGNED|UNSIGNED|TIME|NUMERIC(?:\(\d+(?:,\s?\d+)?\))?|DECIMAL(?:\(\d+(?:,\s?\d+)?\))?)$/', $meta_type ) )
298                         return 'CHAR';
299
300                 if ( 'NUMERIC' == $meta_type )
301                         $meta_type = 'SIGNED';
302
303                 return $meta_type;
304         }
305
306         /**
307          * Generates SQL clauses to be appended to a main query.
308          *
309          * @since 3.2.0
310          * @access public
311          *
312          * @param string $type              Type of meta, eg 'user', 'post'.
313          * @param string $primary_table     Database table where the object being filtered is stored (eg wp_users).
314          * @param string $primary_id_column ID column for the filtered object in $primary_table.
315          * @param object $context           Optional. The main query object.
316          * @return false|array {
317          *     Array containing JOIN and WHERE SQL clauses to append to the main query.
318          *
319          *     @type string $join  SQL fragment to append to the main JOIN clause.
320          *     @type string $where SQL fragment to append to the main WHERE clause.
321          * }
322          */
323         public function get_sql( $type, $primary_table, $primary_id_column, $context = null ) {
324                 if ( ! $meta_table = _get_meta_table( $type ) ) {
325                         return false;
326                 }
327
328                 $this->meta_table     = $meta_table;
329                 $this->meta_id_column = sanitize_key( $type . '_id' );
330
331                 $this->primary_table     = $primary_table;
332                 $this->primary_id_column = $primary_id_column;
333
334                 $sql = $this->get_sql_clauses();
335
336                 /*
337                  * If any JOINs are LEFT JOINs (as in the case of NOT EXISTS), then all JOINs should
338                  * be LEFT. Otherwise posts with no metadata will be excluded from results.
339                  */
340                 if ( false !== strpos( $sql['join'], 'LEFT JOIN' ) ) {
341                         $sql['join'] = str_replace( 'INNER JOIN', 'LEFT JOIN', $sql['join'] );
342                 }
343
344                 /**
345                  * Filter the meta query's generated SQL.
346                  *
347                  * @since 3.1.0
348                  *
349                  * @param array $args {
350                  *     An array of meta query SQL arguments.
351                  *
352                  *     @type array  $clauses           Array containing the query's JOIN and WHERE clauses.
353                  *     @type array  $queries           Array of meta queries.
354                  *     @type string $type              Type of meta.
355                  *     @type string $primary_table     Primary table.
356                  *     @type string $primary_id_column Primary column ID.
357                  *     @type object $context           The main query object.
358                  * }
359                  */
360                 return apply_filters_ref_array( 'get_meta_sql', array( $sql, $this->queries, $type, $primary_table, $primary_id_column, $context ) );
361         }
362
363         /**
364          * Generate SQL clauses to be appended to a main query.
365          *
366          * Called by the public {@see WP_Meta_Query::get_sql()}, this method
367          * is abstracted out to maintain parity with the other Query classes.
368          *
369          * @since 4.1.0
370          * @access protected
371          *
372          * @return array {
373          *     Array containing JOIN and WHERE SQL clauses to append to the main query.
374          *
375          *     @type string $join  SQL fragment to append to the main JOIN clause.
376          *     @type string $where SQL fragment to append to the main WHERE clause.
377          * }
378          */
379         protected function get_sql_clauses() {
380                 /*
381                  * $queries are passed by reference to get_sql_for_query() for recursion.
382                  * To keep $this->queries unaltered, pass a copy.
383                  */
384                 $queries = $this->queries;
385                 $sql = $this->get_sql_for_query( $queries );
386
387                 if ( ! empty( $sql['where'] ) ) {
388                         $sql['where'] = ' AND ' . $sql['where'];
389                 }
390
391                 return $sql;
392         }
393
394         /**
395          * Generate SQL clauses for a single query array.
396          *
397          * If nested subqueries are found, this method recurses the tree to
398          * produce the properly nested SQL.
399          *
400          * @since 4.1.0
401          * @access protected
402          *
403          * @param array $query Query to parse, passed by reference.
404          * @param int   $depth Optional. Number of tree levels deep we currently are.
405          *                     Used to calculate indentation. Default 0.
406          * @return array {
407          *     Array containing JOIN and WHERE SQL clauses to append to a single query array.
408          *
409          *     @type string $join  SQL fragment to append to the main JOIN clause.
410          *     @type string $where SQL fragment to append to the main WHERE clause.
411          * }
412          */
413         protected function get_sql_for_query( &$query, $depth = 0 ) {
414                 $sql_chunks = array(
415                         'join'  => array(),
416                         'where' => array(),
417                 );
418
419                 $sql = array(
420                         'join'  => '',
421                         'where' => '',
422                 );
423
424                 $indent = '';
425                 for ( $i = 0; $i < $depth; $i++ ) {
426                         $indent .= "  ";
427                 }
428
429                 foreach ( $query as $key => &$clause ) {
430                         if ( 'relation' === $key ) {
431                                 $relation = $query['relation'];
432                         } elseif ( is_array( $clause ) ) {
433
434                                 // This is a first-order clause.
435                                 if ( $this->is_first_order_clause( $clause ) ) {
436                                         $clause_sql = $this->get_sql_for_clause( $clause, $query, $key );
437
438                                         $where_count = count( $clause_sql['where'] );
439                                         if ( ! $where_count ) {
440                                                 $sql_chunks['where'][] = '';
441                                         } elseif ( 1 === $where_count ) {
442                                                 $sql_chunks['where'][] = $clause_sql['where'][0];
443                                         } else {
444                                                 $sql_chunks['where'][] = '( ' . implode( ' AND ', $clause_sql['where'] ) . ' )';
445                                         }
446
447                                         $sql_chunks['join'] = array_merge( $sql_chunks['join'], $clause_sql['join'] );
448                                 // This is a subquery, so we recurse.
449                                 } else {
450                                         $clause_sql = $this->get_sql_for_query( $clause, $depth + 1 );
451
452                                         $sql_chunks['where'][] = $clause_sql['where'];
453                                         $sql_chunks['join'][]  = $clause_sql['join'];
454                                 }
455                         }
456                 }
457
458                 // Filter to remove empties.
459                 $sql_chunks['join']  = array_filter( $sql_chunks['join'] );
460                 $sql_chunks['where'] = array_filter( $sql_chunks['where'] );
461
462                 if ( empty( $relation ) ) {
463                         $relation = 'AND';
464                 }
465
466                 // Filter duplicate JOIN clauses and combine into a single string.
467                 if ( ! empty( $sql_chunks['join'] ) ) {
468                         $sql['join'] = implode( ' ', array_unique( $sql_chunks['join'] ) );
469                 }
470
471                 // Generate a single WHERE clause with proper brackets and indentation.
472                 if ( ! empty( $sql_chunks['where'] ) ) {
473                         $sql['where'] = '( ' . "\n  " . $indent . implode( ' ' . "\n  " . $indent . $relation . ' ' . "\n  " . $indent, $sql_chunks['where'] ) . "\n" . $indent . ')';
474                 }
475
476                 return $sql;
477         }
478
479         /**
480          * Generate SQL JOIN and WHERE clauses for a first-order query clause.
481          *
482          * "First-order" means that it's an array with a 'key' or 'value'.
483          *
484          * @since 4.1.0
485          * @access public
486          *
487          * @global wpdb $wpdb WordPress database abstraction object.
488          *
489          * @param array  $clause       Query clause, passed by reference.
490          * @param array  $parent_query Parent query array.
491          * @param string $clause_key   Optional. The array key used to name the clause in the original `$meta_query`
492          *                             parameters. If not provided, a key will be generated automatically.
493          * @return array {
494          *     Array containing JOIN and WHERE SQL clauses to append to a first-order query.
495          *
496          *     @type string $join  SQL fragment to append to the main JOIN clause.
497          *     @type string $where SQL fragment to append to the main WHERE clause.
498          * }
499          */
500         public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' ) {
501                 global $wpdb;
502
503                 $sql_chunks = array(
504                         'where' => array(),
505                         'join' => array(),
506                 );
507
508                 if ( isset( $clause['compare'] ) ) {
509                         $clause['compare'] = strtoupper( $clause['compare'] );
510                 } else {
511                         $clause['compare'] = isset( $clause['value'] ) && is_array( $clause['value'] ) ? 'IN' : '=';
512                 }
513
514                 if ( ! in_array( $clause['compare'], array(
515                         '=', '!=', '>', '>=', '<', '<=',
516                         'LIKE', 'NOT LIKE',
517                         'IN', 'NOT IN',
518                         'BETWEEN', 'NOT BETWEEN',
519                         'EXISTS', 'NOT EXISTS',
520                         'REGEXP', 'NOT REGEXP', 'RLIKE'
521                 ) ) ) {
522                         $clause['compare'] = '=';
523                 }
524
525                 $meta_compare = $clause['compare'];
526
527                 // First build the JOIN clause, if one is required.
528                 $join = '';
529
530                 // We prefer to avoid joins if possible. Look for an existing join compatible with this clause.
531                 $alias = $this->find_compatible_table_alias( $clause, $parent_query );
532                 if ( false === $alias ) {
533                         $i = count( $this->table_aliases );
534                         $alias = $i ? 'mt' . $i : $this->meta_table;
535
536                         // JOIN clauses for NOT EXISTS have their own syntax.
537                         if ( 'NOT EXISTS' === $meta_compare ) {
538                                 $join .= " LEFT JOIN $this->meta_table";
539                                 $join .= $i ? " AS $alias" : '';
540                                 $join .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] );
541
542                         // All other JOIN clauses.
543                         } else {
544                                 $join .= " INNER JOIN $this->meta_table";
545                                 $join .= $i ? " AS $alias" : '';
546                                 $join .= " ON ( $this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column )";
547                         }
548
549                         $this->table_aliases[] = $alias;
550                         $sql_chunks['join'][] = $join;
551                 }
552
553                 // Save the alias to this clause, for future siblings to find.
554                 $clause['alias'] = $alias;
555
556                 // Determine the data type.
557                 $_meta_type = isset( $clause['type'] ) ? $clause['type'] : '';
558                 $meta_type  = $this->get_cast_for_type( $_meta_type );
559                 $clause['cast'] = $meta_type;
560
561                 // Fallback for clause keys is the table alias. Key must be a string.
562                 if ( is_int( $clause_key ) || ! $clause_key ) {
563                         $clause_key = $clause['alias'];
564                 }
565
566                 // Ensure unique clause keys, so none are overwritten.
567                 $iterator = 1;
568                 $clause_key_base = $clause_key;
569                 while ( isset( $this->clauses[ $clause_key ] ) ) {
570                         $clause_key = $clause_key_base . '-' . $iterator;
571                         $iterator++;
572                 }
573
574                 // Store the clause in our flat array.
575                 $this->clauses[ $clause_key ] =& $clause;
576
577                 // Next, build the WHERE clause.
578
579                 // meta_key.
580                 if ( array_key_exists( 'key', $clause ) ) {
581                         if ( 'NOT EXISTS' === $meta_compare ) {
582                                 $sql_chunks['where'][] = $alias . '.' . $this->meta_id_column . ' IS NULL';
583                         } else {
584                                 $sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) );
585                         }
586                 }
587
588                 // meta_value.
589                 if ( array_key_exists( 'value', $clause ) ) {
590                         $meta_value = $clause['value'];
591
592                         if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
593                                 if ( ! is_array( $meta_value ) ) {
594                                         $meta_value = preg_split( '/[,\s]+/', $meta_value );
595                                 }
596                         } else {
597                                 $meta_value = trim( $meta_value );
598                         }
599
600                         switch ( $meta_compare ) {
601                                 case 'IN' :
602                                 case 'NOT IN' :
603                                         $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
604                                         $where = $wpdb->prepare( $meta_compare_string, $meta_value );
605                                         break;
606
607                                 case 'BETWEEN' :
608                                 case 'NOT BETWEEN' :
609                                         $meta_value = array_slice( $meta_value, 0, 2 );
610                                         $where = $wpdb->prepare( '%s AND %s', $meta_value );
611                                         break;
612
613                                 case 'LIKE' :
614                                 case 'NOT LIKE' :
615                                         $meta_value = '%' . $wpdb->esc_like( $meta_value ) . '%';
616                                         $where = $wpdb->prepare( '%s', $meta_value );
617                                         break;
618
619                                 // EXISTS with a value is interpreted as '='.
620                                 case 'EXISTS' :
621                                         $meta_compare = '=';
622                                         $where = $wpdb->prepare( '%s', $meta_value );
623                                         break;
624
625                                 // 'value' is ignored for NOT EXISTS.
626                                 case 'NOT EXISTS' :
627                                         $where = '';
628                                         break;
629
630                                 default :
631                                         $where = $wpdb->prepare( '%s', $meta_value );
632                                         break;
633
634                         }
635
636                         if ( $where ) {
637                                 $sql_chunks['where'][] = "CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$where}";
638                         }
639                 }
640
641                 /*
642                  * Multiple WHERE clauses (for meta_key and meta_value) should
643                  * be joined in parentheses.
644                  */
645                 if ( 1 < count( $sql_chunks['where'] ) ) {
646                         $sql_chunks['where'] = array( '( ' . implode( ' AND ', $sql_chunks['where'] ) . ' )' );
647                 }
648
649                 return $sql_chunks;
650         }
651
652         /**
653          * Get a flattened list of sanitized meta clauses.
654          *
655          * This array should be used for clause lookup, as when the table alias and CAST type must be determined for
656          * a value of 'orderby' corresponding to a meta clause.
657          *
658          * @since 4.2.0
659          * @access public
660          *
661          * @return array Meta clauses.
662          */
663         public function get_clauses() {
664                 return $this->clauses;
665         }
666
667         /**
668          * Identify an existing table alias that is compatible with the current
669          * query clause.
670          *
671          * We avoid unnecessary table joins by allowing each clause to look for
672          * an existing table alias that is compatible with the query that it
673          * needs to perform.
674          *
675          * An existing alias is compatible if (a) it is a sibling of `$clause`
676          * (ie, it's under the scope of the same relation), and (b) the combination
677          * of operator and relation between the clauses allows for a shared table join.
678          * In the case of {@see WP_Meta_Query}, this only applies to 'IN' clauses that
679          * are connected by the relation 'OR'.
680          *
681          * @since 4.1.0
682          * @access protected
683          *
684          * @param  array       $clause       Query clause.
685          * @param  array       $parent_query Parent query of $clause.
686          * @return string|bool Table alias if found, otherwise false.
687          */
688         protected function find_compatible_table_alias( $clause, $parent_query ) {
689                 $alias = false;
690
691                 foreach ( $parent_query as $sibling ) {
692                         // If the sibling has no alias yet, there's nothing to check.
693                         if ( empty( $sibling['alias'] ) ) {
694                                 continue;
695                         }
696
697                         // We're only interested in siblings that are first-order clauses.
698                         if ( ! is_array( $sibling ) || ! $this->is_first_order_clause( $sibling ) ) {
699                                 continue;
700                         }
701
702                         $compatible_compares = array();
703
704                         // Clauses connected by OR can share joins as long as they have "positive" operators.
705                         if ( 'OR' === $parent_query['relation'] ) {
706                                 $compatible_compares = array( '=', 'IN', 'BETWEEN', 'LIKE', 'REGEXP', 'RLIKE', '>', '>=', '<', '<=' );
707
708                         // Clauses joined by AND with "negative" operators share a join only if they also share a key.
709                         } elseif ( isset( $sibling['key'] ) && isset( $clause['key'] ) && $sibling['key'] === $clause['key'] ) {
710                                 $compatible_compares = array( '!=', 'NOT IN', 'NOT LIKE' );
711                         }
712
713                         $clause_compare  = strtoupper( $clause['compare'] );
714                         $sibling_compare = strtoupper( $sibling['compare'] );
715                         if ( in_array( $clause_compare, $compatible_compares ) && in_array( $sibling_compare, $compatible_compares ) ) {
716                                 $alias = $sibling['alias'];
717                                 break;
718                         }
719                 }
720
721                 /**
722                  * Filter the table alias identified as compatible with the current clause.
723                  *
724                  * @since 4.1.0
725                  *
726                  * @param string|bool $alias        Table alias, or false if none was found.
727                  * @param array       $clause       First-order query clause.
728                  * @param array       $parent_query Parent of $clause.
729                  * @param object      $this         WP_Meta_Query object.
730                  */
731                 return apply_filters( 'meta_query_find_compatible_table_alias', $alias, $clause, $parent_query, $this ) ;
732         }
733
734         /**
735          * Checks whether the current query has any OR relations.
736          *
737          * In some cases, the presence of an OR relation somewhere in the query will require
738          * the use of a `DISTINCT` or `GROUP BY` keyword in the `SELECT` clause. The current
739          * method can be used in these cases to determine whether such a clause is necessary.
740          *
741          * @since 4.3.0
742          *
743          * @return bool True if the query contains any `OR` relations, otherwise false.
744          */
745         public function has_or_relation() {
746                 return $this->has_or_relation;
747         }
748 }