[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> class-wp-meta-query.php (source)

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


Generated: Thu May 19 01:00:03 2022 Cross-referenced by PHPXref 0.7.1