<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[29887] trunk: Introduce support for nested queries in WP_Meta_Query.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/29887">29887</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/29887","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2014-10-13 22:02:18 +0000 (Mon, 13 Oct 2014)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Introduce support for nested queries in WP_Meta_Query.
Previously, meta query arguments could be joined by a single AND or OR relation.
Now, these queries can be arbitrarily nested, allowing clauses to be linked
together with multiple relations.
Adds unit tests for the new nesting syntax. Modifies a few existing unit tests
that were overly specific for the old SQL syntax. Backward compatibility with
existing syntax is fully maintained.
Props boonebgorges, DrewAPicture.
See <a href="https://core.trac.wordpress.org/ticket/29642">#29642</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesmetaphp">trunk/src/wp-includes/meta.php</a></li>
<li><a href="#trunktestsphpunittestsmetaqueryphp">trunk/tests/phpunit/tests/meta/query.php</a></li>
<li><a href="#trunktestsphpunittestspostqueryphp">trunk/tests/phpunit/tests/post/query.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesmetaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/meta.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/meta.php 2014-10-13 21:14:50 UTC (rev 29886)
+++ trunk/src/wp-includes/meta.php 2014-10-13 22:02:18 UTC (rev 29887)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -849,27 +849,24 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Container class for a multiple metadata query
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Class for generating SQL clauses that filter a primary query according to metadata keys and values.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * `WP_Meta_Query` is a helper that allows primary query classes, such as {@see WP_Query} and {@see WP_User_Query},
+ * to filter their results by object metadata, by generating `JOIN` and `WHERE` subclauses to be attached
+ * to the primary SQL query string.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @since 3.2.0
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> class WP_Meta_Query {
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * List of metadata queries. A single query is an associative array:
- * - 'key' string The meta key
- * - 'value' string|array The meta value
- * - 'compare' (optional) string How to compare the key to the value.
- * Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN',
- * 'BETWEEN', 'NOT BETWEEN', 'REGEXP', 'NOT REGEXP', 'RLIKE'.
- * Default: '='
- * - 'type' string (optional) The type of the value.
- * Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.
- * Default: 'CHAR'
- *
- * @since 3.2.0
- * @access public
- * @var array
- */
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Array of metadata queries.
+ *
+ * See {@see WP_Meta_Query::__construct()} for information on meta query arguments.
+ *
+ * @since 3.2.0
+ * @access public
+ * @var array
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> public $queries = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -882,10 +879,77 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public $relation;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Constructor
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Database table to query for the metadata.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param array $meta_query (optional) A meta query
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.1.0
+ * @access public
+ * @var string
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public $meta_table;
+
+ /**
+ * Column in meta_table that represents the ID of the object the metadata belongs to.
+ *
+ * @since 4.1.0
+ * @access public
+ * @var string
+ */
+ public $meta_id_column;
+
+ /**
+ * Database table that where the metadata's objects are stored (eg $wpdb->users).
+ *
+ * @since 4.1.0
+ * @access public
+ * @var string
+ */
+ public $primary_table;
+
+ /**
+ * Column in primary_table that represents the ID of the object.
+ *
+ * @since 4.1.0
+ * @access public
+ * @var string
+ */
+ public $primary_id_column;
+
+ /**
+ * A flat list of table aliases used in JOIN clauses.
+ *
+ * @since 4.1.0
+ * @access protected
+ * @var array
+ */
+ protected $table_aliases = array();
+
+ /**
+ * Constructor.
+ *
+ * @since 3.2.0
+ * @access public
+ *
+ * @param array $meta_query {
+ * Array of meta query clauses.
+ *
+ * @type string $relation Optional. The MySQL keyword used to join
+ * the clauses of the query. Accepts 'AND', or 'OR'. Default 'AND'.
+ * @type array {
+ * Optional. An array of first-order clause parameters, or another fully-formed meta query.
+ *
+ * @type string $key Meta key to filter by.
+ * @type string $value Meta value to filter by.
+ * @type string $compare MySQL operator used for comparing the $value. Accepts '=',
+ * '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN',
+ * 'BETWEEN', 'NOT BETWEEN', 'REGEXP', 'NOT REGEXP', or 'RLIKE'.
+ * Default is 'IN' when `$value` is an array, '=' otherwise.
+ * @type string $type MySQL data type that the meta_value column will be CAST to for
+ * comparisons. Accepts 'NUMERIC', 'BINARY', 'CHAR', 'DATE',
+ * 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', or 'UNSIGNED'.
+ * Default is 'CHAR'.
+ * }
+ * }
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> public function __construct( $meta_query = false ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( !$meta_query )
</span><span class="cx" style="display: block; padding: 0 10px"> return;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -896,17 +960,86 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->relation = 'AND';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->queries = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->queries = $this->sanitize_query( $meta_query );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( $meta_query as $key => $query ) {
- if ( ! is_array( $query ) )
- continue;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Ensure the `meta_query` argument passed to the class constructor is well-formed.
+ *
+ * Eliminates empty items and ensures that a 'relation' is set.
+ *
+ * @since 4.1.0
+ * @access public
+ *
+ * @param array $queries Array of query clauses.
+ * @return array Sanitized array of query clauses.
+ */
+ public function sanitize_query( $queries ) {
+ $clean_queries = array();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->queries[] = $query;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! is_array( $queries ) ) {
+ return $clean_queries;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ foreach ( $queries as $key => $query ) {
+ if ( 'relation' === $key ) {
+ $relation = $query;
+
+ // First-order clause.
+ } else if ( $this->is_first_order_clause( $query ) ) {
+ $clean_queries[] = $query;
+
+ // Otherwise, it's a nested query, so we recurse.
+ } else {
+ $cleaned_query = $this->sanitize_query( $query );
+
+ if ( ! empty( $cleaned_query ) ) {
+ $clean_queries[] = $cleaned_query;
+ }
+ }
+ }
+
+ if ( empty( $clean_queries ) ) {
+ return $clean_queries;
+ }
+
+ // Sanitize the 'relation' key provided in the query.
+ if ( isset( $relation ) && 'OR' === strtoupper( $relation ) ) {
+ $clean_queries['relation'] = 'OR';
+
+ /*
+ * If there is only a single clause, call the relation 'OR'.
+ * This value will not actually be used to join clauses, but it
+ * simplifies the logic around combining key-only queries.
+ */
+ } else if ( 1 === count( $clean_queries ) ) {
+ $clean_queries['relation'] = 'OR';
+
+ // Default to AND.
+ } else {
+ $clean_queries['relation'] = 'AND';
+ }
+
+ return $clean_queries;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Determine whether a query clause is first-order.
+ *
+ * A first-order meta query clause is one that has either a 'key' or
+ * a 'value' array key.
+ *
+ * @since 4.1.0
+ * @access protected
+ *
+ * @param array $query Meta query arguments.
+ * @return bool Whether the query clause is a first-order clause.
+ */
+ protected function is_first_order_clause( $query ) {
+ return isset( $query['key'] ) || isset( $query['value'] );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Constructs a meta query based on 'meta_*' query vars
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.2.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -917,13 +1050,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public function parse_query_vars( $qv ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $meta_query = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Simple query needs to be first for orderby=meta_value to work correctly
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Simple query needs to be first for orderby=meta_value to work correctly.
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( array( 'key', 'compare', 'type' ) as $key ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( !empty( $qv[ "meta_$key" ] ) )
</span><span class="cx" style="display: block; padding: 0 10px"> $meta_query[0][ $key ] = $qv[ "meta_$key" ];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // WP_Query sets 'meta_value' = '' by default
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // WP_Query sets 'meta_value' = '' by default.
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $qv[ 'meta_value' ] ) && '' !== $qv[ 'meta_value' ] && ( ! is_array( $qv[ 'meta_value' ] ) || $qv[ 'meta_value' ] ) )
</span><span class="cx" style="display: block; padding: 0 10px"> $meta_query[0]['value'] = $qv[ 'meta_value' ];
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -935,12 +1068,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Given a meta type, return the appropriate alias if applicable
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Return the appropriate alias for the given meta type if applicable.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.7.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @access public
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $type MySQL type to cast meta_value
- * @return string MySQL type
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string $type MySQL type to cast meta_value.
+ * @return string MySQL type.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_cast_for_type( $type = '' ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $type ) )
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -963,120 +1097,268 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.2.0
</span><span class="cx" style="display: block; padding: 0 10px"> * @access public
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $type Type of meta
- * @param string $primary_table
- * @param string $primary_id_column
- * @param object $context (optional) The main query object
- * @return array( 'join' => $join_sql, 'where' => $where_sql )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string $type Type of meta, eg 'user', 'post'.
+ * @param string $primary_table Database table where the object being filtered is stored (eg wp_users).
+ * @param string $primary_id_column ID column for the filtered object in $primary_table.
+ * @param object $context Optional. The main query object.
+ * @return array {
+ * Array containing JOIN and WHERE SQL clauses to append to the main query.
+ *
+ * @type string $join SQL fragment to append to the main JOIN clause.
+ * @type string $where SQL fragment to append to the main WHERE clause.
+ * }
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_sql( $type, $primary_table, $primary_id_column, $context = null ) {
</span><span class="cx" style="display: block; padding: 0 10px"> global $wpdb;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! $meta_table = _get_meta_table( $type ) )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! $meta_table = _get_meta_table( $type ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $meta_id_column = sanitize_key( $type . '_id' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->meta_table = $meta_table;
+ $this->meta_id_column = sanitize_key( $type . '_id' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $join = array();
- $where = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->primary_table = $primary_table;
+ $this->primary_id_column = $primary_id_column;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $key_only_queries = array();
- $queries = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $sql = $this->get_sql_clauses();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Split out the queries with empty arrays as value
- foreach ( $this->queries as $k => $q ) {
- if ( isset( $q['value'] ) && is_array( $q['value'] ) && empty( $q['value'] ) ) {
- $key_only_queries[$k] = $q;
- unset( $this->queries[$k] );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Filter the meta query's generated SQL.
+ *
+ * @since 3.1.0
+ *
+ * @param array $args {
+ * An array of meta query SQL arguments.
+ *
+ * @type array $clauses Array containing the query's JOIN and WHERE clauses.
+ * @type array $queries Array of meta queries.
+ * @type string $type Type of meta.
+ * @type string $primary_table Primary table.
+ * @type string $primary_id_column Primary column ID.
+ * @type object $context The main query object.
+ * }
+ */
+ return apply_filters_ref_array( 'get_meta_sql', array( $sql, $this->queries, $type, $primary_table, $primary_id_column, $context ) );
+ }
+
+ /**
+ * Generate SQL clauses to be appended to a main query.
+ *
+ * Called by the public {@see WP_Meta_Query::get_sql()}, this method
+ * is abstracted out to maintain parity with the other Query classes.
+ *
+ * @since 4.1.0
+ * @access protected
+ *
+ * @return array {
+ * Array containing JOIN and WHERE SQL clauses to append to the main query.
+ *
+ * @type string $join SQL fragment to append to the main JOIN clause.
+ * @type string $where SQL fragment to append to the main WHERE clause.
+ * }
+ */
+ protected function get_sql_clauses() {
+ $sql = $this->get_sql_for_query( $this->queries );
+
+ if ( ! empty( $sql['where'] ) ) {
+ $sql['where'] = ' AND ' . $sql['where'];
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Split out the meta_key only queries (we can only do this for OR)
- if ( 'OR' == $this->relation ) {
- foreach ( $this->queries as $k => $q ) {
- if ( ( empty( $q['compare'] ) || 'NOT EXISTS' != $q['compare'] ) && ! array_key_exists( 'value', $q ) && ! empty( $q['key'] ) )
- $key_only_queries[$k] = $q;
- else
- $queries[$k] = $q;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $sql;
+ }
+
+ /**
+ * Generate SQL clauses for a single query array.
+ *
+ * If nested subqueries are found, this method recurses the tree to
+ * produce the properly nested SQL.
+ *
+ * @since 4.1.0
+ * @access protected
+ *
+ * @param array $query Query to parse.
+ * @param int $depth Optional. Number of tree levels deep we currently are.
+ * Used to calculate indentation.
+ * @return array {
+ * Array containing JOIN and WHERE SQL clauses to append to a single query array.
+ *
+ * @type string $join SQL fragment to append to the main JOIN clause.
+ * @type string $where SQL fragment to append to the main WHERE clause.
+ * }
+ */
+ protected function get_sql_for_query( $query, $depth = 0 ) {
+ $sql_chunks = array(
+ 'join' => array(),
+ 'where' => array(),
+ );
+
+ $sql = array(
+ 'join' => '',
+ 'where' => '',
+ );
+
+ $indent = '';
+ for ( $i = 0; $i < $depth; $i++ ) {
+ $indent .= " ";
+ }
+
+ foreach ( $query as $key => $clause ) {
+ if ( 'relation' === $key ) {
+ $relation = $query['relation'];
+ } else if ( is_array( $clause ) ) {
+
+ // This is a first-order clause.
+ if ( $this->is_first_order_clause( $clause ) ) {
+ $clause_sql = $this->get_sql_for_clause( $clause, $query );
+
+ $where_count = count( $clause_sql['where'] );
+ if ( ! $where_count ) {
+ $sql_chunks['where'][] = '';
+ } else if ( 1 === $where_count ) {
+ $sql_chunks['where'][] = $clause_sql['where'][0];
+ } else {
+ $sql_chunks['where'][] = '( ' . implode( ' AND ', $clause_sql['where'] ) . ' )';
+ }
+
+ $sql_chunks['join'] = array_merge( $sql_chunks['join'], $clause_sql['join'] );
+ // This is a subquery, so we recurse.
+ } else {
+ $clause_sql = $this->get_sql_for_query( $clause, $depth + 1 );
+
+ $sql_chunks['where'][] = $clause_sql['where'];
+ $sql_chunks['join'][] = $clause_sql['join'];
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- } else {
- $queries = $this->queries;
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Specify all the meta_key only queries in one go
- if ( $key_only_queries ) {
- $join[] = "INNER JOIN $meta_table ON $primary_table.$primary_id_column = $meta_table.$meta_id_column";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Filter to remove empties.
+ $sql_chunks['join'] = array_filter( $sql_chunks['join'] );
+ $sql_chunks['where'] = array_filter( $sql_chunks['where'] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( $key_only_queries as $key => $q )
- $where["key-only-$key"] = $wpdb->prepare( "$meta_table.meta_key = %s", trim( $q['key'] ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $relation ) ) {
+ $relation = 'AND';
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( $queries as $k => $q ) {
- $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
- $meta_type = $this->get_cast_for_type( isset( $q['type'] ) ? $q['type'] : '' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Filter duplicate JOIN clauses and combine into a single string.
+ if ( ! empty( $sql_chunks['join'] ) ) {
+ $sql['join'] = implode( ' ', array_unique( $sql_chunks['join'] ) );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( array_key_exists( 'value', $q ) && is_null( $q['value'] ) )
- $q['value'] = '';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Generate a single WHERE clause with proper brackets and indentation.
+ if ( ! empty( $sql_chunks['where'] ) ) {
+ $sql['where'] = '( ' . "\n " . $indent . implode( ' ' . "\n " . $indent . $relation . ' ' . "\n " . $indent, $sql_chunks['where'] ) . "\n" . $indent . ')';
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $meta_value = isset( $q['value'] ) ? $q['value'] : null;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $sql;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $q['compare'] ) )
- $meta_compare = strtoupper( $q['compare'] );
- else
- $meta_compare = is_array( $meta_value ) ? 'IN' : '=';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Generate SQL JOIN and WHERE clauses for a first-order query clause.
+ *
+ * "First-order" means that it's an array with a 'key' or 'value'.
+ *
+ * @since 4.1.0
+ * @access public
+ *
+ * @param array $clause Query clause.
+ * @param array $parent_query Parent query array.
+ * @return array {
+ * Array containing JOIN and WHERE SQL clauses to append to a first-order query.
+ *
+ * @type string $join SQL fragment to append to the main JOIN clause.
+ * @type string $where SQL fragment to append to the main WHERE clause.
+ * }
+ */
+ public function get_sql_for_clause( $clause, $parent_query ) {
+ global $wpdb;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! in_array( $meta_compare, array(
- '=', '!=', '>', '>=', '<', '<=',
- 'LIKE', 'NOT LIKE',
- 'IN', 'NOT IN',
- 'BETWEEN', 'NOT BETWEEN',
- 'NOT EXISTS',
- 'REGEXP', 'NOT REGEXP', 'RLIKE'
- ) ) )
- $meta_compare = '=';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $sql_chunks = array(
+ 'where' => array(),
+ 'join' => array(),
+ );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $i = count( $join );
- $alias = $i ? 'mt' . $i : $meta_table;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $i = count( $this->table_aliases );
+ $alias = $i ? 'mt' . $i : $this->meta_table;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( 'NOT EXISTS' == $meta_compare ) {
- $join[$i] = "LEFT JOIN $meta_table";
- $join[$i] .= $i ? " AS $alias" : '';
- $join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column AND $alias.meta_key = '$meta_key')";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $clause['compare'] ) ) {
+ $meta_compare = strtoupper( $clause['compare'] );
+ } else {
+ $meta_compare = isset( $clause['value'] ) && is_array( $clause['value'] ) ? 'IN' : '=';
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $where[$k] = ' ' . $alias . '.' . $meta_id_column . ' IS NULL';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! in_array( $meta_compare, array(
+ '=', '!=', '>', '>=', '<', '<=',
+ 'LIKE', 'NOT LIKE',
+ 'IN', 'NOT IN',
+ 'BETWEEN', 'NOT BETWEEN',
+ 'EXISTS', 'NOT EXISTS',
+ 'REGEXP', 'NOT REGEXP', 'RLIKE'
+ ) ) ) {
+ $meta_compare = '=';
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- continue;
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+ * There are a number of different query structures that get
+ * built in different ways.
+ * 1. Key-only clauses - (a) clauses without a 'value' key that
+ * appear in the context of an OR relation and do not use
+ * 'NOT EXISTS' as the 'compare', or (b) clauses with an
+ * empty array for 'value'.
+ */
+ if ( ! empty( $clause['key'] ) && (
+ ( ! array_key_exists( 'value', $clause ) && 'NOT EXISTS' !== $meta_compare && 'OR' === $parent_query['relation'] ) ||
+ ( isset( $clause['value'] ) && is_array( $clause['value'] ) && empty( $clause['value'] ) )
+ ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $join[$i] = "INNER JOIN $meta_table";
- $join[$i] .= $i ? " AS $alias" : '';
- $join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $alias = $this->meta_table;
+ $sql_chunks['join'][] = " INNER JOIN $this->meta_table ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column)";
+ $sql_chunks['where'][] = $wpdb->prepare( "$this->meta_table.meta_key = %s", trim( $clause['key'] ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $where[$k] = '';
- if ( !empty( $meta_key ) )
- $where[$k] = $wpdb->prepare( "$alias.meta_key = %s", $meta_key );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // 2. NOT EXISTS.
+ } else if ( 'NOT EXISTS' === $meta_compare ) {
+ $join = " LEFT JOIN $this->meta_table";
+ $join .= $i ? " AS $alias" : '';
+ $join .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] );
+ $sql_chunks['join'][] = $join;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( is_null( $meta_value ) ) {
- if ( empty( $where[$k] ) )
- unset( $join[$i] );
- continue;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $sql_chunks['where'][] = $alias . '.' . $this->meta_id_column . ' IS NULL';
+
+ // 3. EXISTS and other key-only queries.
+ } else if ( 'EXISTS' === $meta_compare || ( ! empty( $clause['key'] ) && ! array_key_exists( 'value', $clause ) ) ) {
+ $join = " INNER JOIN $this->meta_table";
+ $join .= $i ? " AS $alias" : '';
+ $join .= " ON ( $this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column )";
+ $sql_chunks['join'][] = $join;
+
+ $sql_chunks['where'][] = $wpdb->prepare( $alias . '.meta_key = %s', trim( $clause['key'] ) );
+
+ // 4. Clauses that have a value.
+ } else if ( array_key_exists( 'value', $clause ) ) {
+ $join = " INNER JOIN $this->meta_table";
+ $join .= $i ? " AS $alias" : '';
+ $join .= " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column)";
+ $sql_chunks['join'][] = $join;
+
+ if ( ! empty( $clause['key'] ) ) {
+ $sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $meta_type = $this->get_cast_for_type( isset( $clause['type'] ) ? $clause['type'] : '' );
+
+ $meta_value = isset( $clause['value'] ) ? $clause['value'] : '';
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! is_array( $meta_value ) )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! is_array( $meta_value ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $meta_value = preg_split( '/[,\s]+/', $meta_value );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
- if ( empty( $meta_value ) ) {
- unset( $join[$i] );
- continue;
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> $meta_value = trim( $meta_value );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( 'IN' == substr( $meta_compare, -2) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 'IN' == substr( $meta_compare, -2 ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ } elseif ( 'BETWEEN' == substr( $meta_compare, -7 ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $meta_value = array_slice( $meta_value, 0, 2 );
</span><span class="cx" style="display: block; padding: 0 10px"> $meta_compare_string = '%s AND %s';
</span><span class="cx" style="display: block; padding: 0 10px"> } elseif ( 'LIKE' == $meta_compare || 'NOT LIKE' == $meta_compare ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1086,40 +1368,20 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $meta_compare_string = '%s';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! empty( $where[$k] ) )
- $where[$k] .= ' AND ';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $sql_chunks['where'][] = $wpdb->prepare( "CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $where[$k] = ' (' . $where[$k] . $wpdb->prepare( "CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string})", $meta_value );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+ * Multiple WHERE clauses (for meta_key and meta_value) should
+ * be joined in parentheses.
+ */
+ if ( 1 < count( $sql_chunks['where'] ) ) {
+ $sql_chunks['where'] = array( '( ' . implode( ' AND ', $sql_chunks['where'] ) . ' )' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $where = array_filter( $where );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->table_aliases[] = $alias;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( empty( $where ) )
- $where = '';
- else
- $where = ' AND (' . implode( "\n{$this->relation} ", $where ) . ' )';
-
- $join = implode( "\n", $join );
- if ( ! empty( $join ) )
- $join = ' ' . $join;
-
- /**
- * Filter the meta query's generated SQL.
- *
- * @since 3.1.0
- *
- * @param array $args {
- * An array of arguments.
- *
- * @type array $clauses Array containing the query's JOIN and WHERE clauses.
- * @type array $queries Array of meta queries.
- * @type string $type Type of meta.
- * @type string $primary_table Primary table.
- * @type string $primary_id_column Primary column ID.
- * @type object $context The main query object.
- * }
- */
- return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $this->queries, $type, $primary_table, $primary_id_column, $context ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $sql_chunks;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunittestsmetaqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/meta/query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/meta/query.php 2014-10-13 21:14:50 UTC (rev 29886)
+++ trunk/tests/phpunit/tests/meta/query.php 2014-10-13 22:02:18 UTC (rev 29887)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -40,7 +40,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> array(),
</span><span class="cx" style="display: block; padding: 0 10px"> ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( array( array() ), $query->queries );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( array(), $query->queries );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -153,19 +153,44 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $query = new WP_Meta_Query();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // just meta_value
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $query->parse_query_vars( array( 'meta_key' => 'abc' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $expected = array(
+ 'relation' => 'OR',
+ array(
+ 'key' => 'abc',
+ ),
+ );
+ $query->parse_query_vars( array(
+ 'meta_key' => 'abc',
+ ) );
+ $this->assertEquals( $expected, $query->queries );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( array( array( 'key' => 'abc' ) ), $query->queries );
-
</del><span class="cx" style="display: block; padding: 0 10px"> // meta_key & meta_value
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $query->parse_query_vars( array( 'meta_key' => 'abc', 'meta_value' => 'def' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $expected = array(
+ 'relation' => 'OR',
+ array(
+ 'key' => 'abc',
+ 'value' => 'def',
+ ),
+ );
+ $query->parse_query_vars( array(
+ 'meta_key' => 'abc',
+ 'meta_value' => 'def',
+ ) );
+ $this->assertEquals( $expected, $query->queries );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( array( array( 'key' => 'abc', 'value' => 'def' ) ), $query->queries );
-
</del><span class="cx" style="display: block; padding: 0 10px"> // meta_compare
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $query->parse_query_vars( array( 'meta_key' => 'abc', 'meta_compare' => '=>' ) );
-
- $this->assertEquals( array( array( 'key' => 'abc', 'compare' => '=>' ) ), $query->queries );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $expected = array(
+ 'relation' => 'OR',
+ array(
+ 'key' => 'abc',
+ 'compare' => '=>',
+ ),
+ );
+ $query->parse_query_vars( array(
+ 'meta_key' => 'abc',
+ 'meta_compare' => '=>',
+ ) );
+ $this->assertEquals( $expected, $query->queries );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -202,6 +227,229 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'ANYTHING ELSE' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_sanitize_query_single_query() {
+ $expected = array(
+ 'relation' => 'OR',
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ );
+
+ $q = new WP_Meta_Query();
+ $found = $q->sanitize_query( array(
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ ) );
+
+ $this->assertEquals( $expected, $found );
+ }
+
+ public function test_sanitize_query_multiple_first_order_queries_relation_default() {
+ $expected = array(
+ 'relation' => 'AND',
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ array(
+ 'key' => 'foo2',
+ 'value' => 'bar2',
+ ),
+ );
+
+ $q = new WP_Meta_Query();
+ $found = $q->sanitize_query( array(
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ array(
+ 'key' => 'foo2',
+ 'value' => 'bar2',
+ ),
+ ) );
+
+ $this->assertEquals( $expected, $found );
+ }
+
+ public function test_sanitize_query_multiple_first_order_queries_relation_or() {
+ $expected = array(
+ 'relation' => 'OR',
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ array(
+ 'key' => 'foo2',
+ 'value' => 'bar2',
+ ),
+ );
+
+ $q = new WP_Meta_Query();
+ $found = $q->sanitize_query( array(
+ 'relation' => 'OR',
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ array(
+ 'key' => 'foo2',
+ 'value' => 'bar2',
+ ),
+ ) );
+
+ $this->assertEquals( $expected, $found );
+ }
+
+ public function test_sanitize_query_multiple_first_order_queries_relation_or_lowercase() {
+ $expected = array(
+ 'relation' => 'OR',
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ array(
+ 'key' => 'foo2',
+ 'value' => 'bar2',
+ ),
+ );
+
+ $q = new WP_Meta_Query();
+ $found = $q->sanitize_query( array(
+ 'relation' => 'or',
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ array(
+ 'key' => 'foo2',
+ 'value' => 'bar2',
+ ),
+ ) );
+
+ $this->assertEquals( $expected, $found );
+ }
+
+ public function test_sanitize_query_multiple_first_order_queries_invalid_relation() {
+ $expected = array(
+ 'relation' => 'AND',
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ array(
+ 'key' => 'foo2',
+ 'value' => 'bar2',
+ ),
+ );
+
+ $q = new WP_Meta_Query();
+ $found = $q->sanitize_query( array(
+ 'relation' => 'FOO',
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ array(
+ 'key' => 'foo2',
+ 'value' => 'bar2',
+ ),
+ ) );
+
+ $this->assertEquals( $expected, $found );
+ }
+
+ public function test_sanitize_query_single_query_which_is_a_nested_query() {
+ $expected = array(
+ 'relation' => 'OR',
+ array(
+ 'relation' => 'AND',
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ array(
+ 'key' => 'foo2',
+ 'value' => 'bar2',
+ ),
+ )
+ );
+
+ $q = new WP_Meta_Query();
+ $found = $q->sanitize_query( array(
+ array(
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ array(
+ 'key' => 'foo2',
+ 'value' => 'bar2',
+ ),
+ ),
+ ) );
+
+ $this->assertEquals( $expected, $found );
+ }
+
+ public function test_sanitize_query_multiple_nested_queries() {
+ $expected = array(
+ 'relation' => 'OR',
+ array(
+ 'relation' => 'AND',
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ array(
+ 'key' => 'foo2',
+ 'value' => 'bar2',
+ ),
+ ),
+ array(
+ 'relation' => 'AND',
+ array(
+ 'key' => 'foo3',
+ 'value' => 'bar3',
+ ),
+ array(
+ 'key' => 'foo4',
+ 'value' => 'bar4',
+ ),
+ ),
+ );
+
+ $q = new WP_Meta_Query();
+ $found = $q->sanitize_query( array(
+ 'relation' => 'OR',
+ array(
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ array(
+ 'key' => 'foo2',
+ 'value' => 'bar2',
+ ),
+ ),
+ array(
+ array(
+ 'key' => 'foo3',
+ 'value' => 'bar3',
+ ),
+ array(
+ 'key' => 'foo4',
+ 'value' => 'bar4',
+ ),
+ ),
+ ) );
+
+ $this->assertEquals( $expected, $found );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Invalid $type will fail to get a table from _get_meta_table()
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -229,7 +477,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // We should have 2 joins - one for my_first_key and one for my_second_key
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( 2, substr_count( $sql['join'], 'INNER JOIN' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( 2, substr_count( $sql['join'], 'JOIN' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // The WHERE should check my_third_key against an unaliased table
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'my_third_key'" ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -247,7 +495,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = '')" ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = ''" ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -558,7 +806,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertContains( "{$wpdb->postmeta}.meta_key = 'exclude'\nOR", $sql['where'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Use regex because we don't care about the whitespace before OR.
+ $this->assertRegExp( "/{$wpdb->postmeta}\.meta_key = \'exclude\'\s+OR/", $sql['where'] );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertNotContains( "{$wpdb->postmeta}.post_id IS NULL", $sql['where'] );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestspostqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/post/query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/post/query.php 2014-10-13 21:14:50 UTC (rev 29886)
+++ trunk/tests/phpunit/tests/post/query.php 2014-10-13 22:02:18 UTC (rev 29887)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -772,6 +772,100 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEqualSets( array( $post_4, $post_3, $post_2, $post_1 ), $query->posts );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @ticket 29642
+ * @group meta
+ */
+ public function test_meta_query_nested() {
+ $p1 = $this->factory->post->create();
+ $p2 = $this->factory->post->create();
+ $p3 = $this->factory->post->create();
+
+ add_post_meta( $p1, 'foo', 'bar' );
+ add_post_meta( $p2, 'foo2', 'bar' );
+ add_post_meta( $p3, 'foo2', 'bar' );
+ add_post_meta( $p3, 'foo3', 'bar' );
+
+ $query = new WP_Query( array(
+ 'update_post_meta_cache' => false,
+ 'update_term_meta_cache' => false,
+ 'fields' => 'ids',
+ 'meta_query' => array(
+ 'relation' => 'OR',
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ array(
+ 'relation' => 'AND',
+ array(
+ 'key' => 'foo2',
+ 'value' => 'bar',
+ ),
+ array(
+ 'key' => 'foo3',
+ 'value' => 'bar',
+ ),
+ ),
+ ),
+ ) );
+
+ $expected = array( $p1, $p3 );
+ $this->assertEqualSets( $expected, $query->posts );
+ }
+
+ /**
+ * @ticket 29642
+ * @group meta
+ */
+ public function test_meta_query_nested_two_levels_deep() {
+ $p1 = $this->factory->post->create();
+ $p2 = $this->factory->post->create();
+ $p3 = $this->factory->post->create();
+
+ add_post_meta( $p1, 'foo', 'bar' );
+ add_post_meta( $p3, 'foo2', 'bar' );
+ add_post_meta( $p3, 'foo3', 'bar' );
+ add_post_meta( $p3, 'foo4', 'bar' );
+
+ $query = new WP_Query( array(
+ 'update_post_meta_cache' => false,
+ 'update_term_meta_cache' => false,
+ 'fields' => 'ids',
+ 'meta_query' => array(
+ 'relation' => 'OR',
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ array(
+ 'relation' => 'OR',
+ array(
+ 'key' => 'foo2',
+ 'value' => 'bar',
+ ),
+ array(
+ 'relation' => 'AND',
+ array(
+ 'key' => 'foo3',
+ 'value' => 'bar',
+ ),
+ array(
+ 'key' => 'foo4',
+ 'value' => 'bar',
+ ),
+ ),
+ ),
+ ),
+ ) );
+
+ $expected = array( $p1, $p3 );
+ $this->assertEqualSets( $expected, $query->posts );
+ }
+
+ /**
+ * @group meta
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> function test_meta_between_not_between() {
</span><span class="cx" style="display: block; padding: 0 10px"> $post_id = $this->factory->post->create();
</span><span class="cx" style="display: block; padding: 0 10px"> add_post_meta( $post_id, 'time', 500 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -819,6 +913,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 16829
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @group meta
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function test_meta_default_compare() {
</span><span class="cx" style="display: block; padding: 0 10px"> // compare should default to IN when meta_value is an array
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -859,6 +954,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 17264
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @group meta
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function test_duplicate_posts_when_no_key() {
</span><span class="cx" style="display: block; padding: 0 10px"> $post_id = $this->factory->post->create();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -890,6 +986,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 15292
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @group meta
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function test_empty_meta_value() {
</span><span class="cx" style="display: block; padding: 0 10px"> $post_id = $this->factory->post->create();
</span></span></pre>
</div>
</div>
</body>
</html>