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