<!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" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { 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 #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#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>
<title>[15715] trunk/wp-includes: Introduce WP_Object_Query.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/15715">15715</a></dd>
<dt>Author</dt> <dd>scribu</dd>
<dt>Date</dt> <dd>2010-10-04 18:57:13 +0000 (Mon, 04 Oct 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Introduce WP_Object_Query. See <a href="http://trac.wordpress.org/ticket/15032">#15032</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesclassesphp">trunk/wp-includes/classes.php</a></li>
<li><a href="#trunkwpincludesfunctionsphp">trunk/wp-includes/functions.php</a></li>
<li><a href="#trunkwpincludesqueryphp">trunk/wp-includes/query.php</a></li>
<li><a href="#trunkwpincludesuserphp">trunk/wp-includes/user.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/classes.php (15714 => 15715)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/classes.php        2010-10-04 18:26:26 UTC (rev 15714)
+++ trunk/wp-includes/classes.php        2010-10-04 18:57:13 UTC (rev 15715)
</span><span class="lines">@@ -525,6 +525,105 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><ins>+ * WordPress Query class.
+ *
+ * Abstract class for handling advanced queries
+ *
+ * @package WordPress
+ * @since 3.1.0
+ */
+class WP_Object_Query {
+
+        /**
+         * Metadata query
+         *
+         * @since 3.1.0
+         * @access public
+         * @var array
+         */
+        var $meta_query = array();
+
+        /*
+         * Populates the $meta_query property
+         *
+         * @access private
+         * @since 3.1.0
+         *
+         * @param array $qv The query variables
+         */
+        function parse_meta_query( $qv ) {
+                if ( !empty( $qv['meta_query'] ) &amp;&amp; is_array( $qv['meta_query'] ) ) {
+                        $this-&gt;meta_query = $qv['meta_query'];
+                }
+
+                $meta_query = array();
+                foreach ( array( 'key', 'value', 'compare' ) as $key ) {
+                        if ( !empty( $qv[ &quot;meta_$key&quot; ] ) )
+                                $meta_query[ $key ] = $qv[ &quot;meta_$key&quot; ];
+                }
+
+                if ( !empty( $meta_query ) ) {
+                        $this-&gt;meta_query[] = $meta_query;
+                }
+        }
+
+        /*
+         * Used internally to generate an SQL string for searching across multiple meta key = value pairs
+         *
+         * @access private
+         * @since 3.1.0
+         *
+         * @param string $primary_table
+         * @param string $primary_id_column
+         * @param string $meta_table
+         * @param string $meta_id_column
+         * @return array( $join_sql, $where_sql )
+         */
+        function get_meta_sql( $primary_table, $primary_id_column, $meta_table, $meta_id_column ) {
+                global $wpdb;
+
+                $clauses = array();
+
+                $join = '';
+                $where = '';
+                $i = 0;
+                foreach ( $this-&gt;meta_query as $q ) {
+                        $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
+                        $meta_value = isset( $q['value'] ) ? trim( $q['value'] ) : '';
+                        $meta_compare = isset( $q['compare'] ) ? $q['compare'] : '=';
+
+                        if ( !in_array( $meta_compare, array( '=', '!=', '&gt;', '&gt;=', '&lt;', '&lt;=', 'like' ) ) )
+                                $meta_compare = '=';
+
+                        if ( empty( $meta_key ) &amp;&amp; empty( $meta_value ) )
+                                continue;
+
+                        $alias = $i ? 'mt' . $i : $meta_table;
+
+                        $join .= &quot;\nINNER JOIN $meta_table&quot;;
+                        $join .= $i ? &quot; AS $alias&quot; : '';
+                        $join .= &quot; ON ($primary_table.$primary_id_column = $alias.$meta_id_column)&quot;;
+
+                        $i++;
+
+                        if ( !empty( $meta_key ) )
+                                $where .= $wpdb-&gt;prepare( &quot; AND $alias.meta_key = %s&quot;, $meta_key );
+
+                        if ( empty( $meta_value ) )
+                                continue;
+
+                        if ( 'like' == $meta_compare ) {
+                                $where .= $wpdb-&gt;prepare( &quot; AND $alias.meta_value LIKE %s&quot;, '%' . like_escape( $meta_value ) . '%' );
+                        } else {
+                                $where .= $wpdb-&gt;prepare( &quot; AND $alias.meta_value $meta_compare %s&quot;, $meta_value );
+                        }
+                }
+
+                return array( $join, $where );
+        }
+}
+
+/**
</ins><span class="cx">  * WordPress Error class.
</span><span class="cx">  *
</span><span class="cx">  * Container for checking for WordPress errors and error messages. Return
</span></span></pre></div>
<a id="trunkwpincludesfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/functions.php (15714 => 15715)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/functions.php        2010-10-04 18:26:26 UTC (rev 15714)
+++ trunk/wp-includes/functions.php        2010-10-04 18:57:13 UTC (rev 15715)
</span><span class="lines">@@ -4257,62 +4257,6 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /*
</span><del>- * Used internally to generate an SQL string for searching across multiple meta key = value pairs
- *
- * @access private
- * @since 3.1.0
- *
- * @param array $queries An array of queries
- * @param string $primary_table
- * @param string $primary_id_column
- * @param string $meta_table
- * @param string $meta_id_column
- * @return array( $join_sql, $where_sql )
- */
-function _wp_meta_sql( $queries, $primary_table, $primary_id_column, $meta_table, $meta_id_column ) {
-        global $wpdb;
-
-        $clauses = array();
-
-        $join = '';
-        $where = '';
-        $i = 0;
-        foreach ( $queries as $q ) {
-                $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
-                $meta_value = isset( $q['value'] ) ? trim( $q['value'] ) : '';
-                $meta_compare = isset( $q['compare'] ) ? $q['compare'] : '=';
-
-                if ( !in_array( $meta_compare, array( '=', '!=', '&gt;', '&gt;=', '&lt;', '&lt;=', 'like' ) ) )
-                        $meta_compare = '=';
-
-                if ( empty( $meta_key ) &amp;&amp; empty( $meta_value ) )
-                        continue;
-
-                $alias = $i ? 'mt' . $i : $meta_table;
-
-                $join .= &quot;\nINNER JOIN $meta_table&quot;;
-                $join .= $i ? &quot; AS $alias&quot; : '';
-                $join .= &quot; ON ($primary_table.$primary_id_column = $alias.$meta_id_column)&quot;;
-
-                $i++;
-
-                if ( !empty( $meta_key ) )
-                        $where .= $wpdb-&gt;prepare( &quot; AND $alias.meta_key = %s&quot;, $meta_key );
-
-                if ( empty( $meta_value ) )
-                        continue;
-
-                if ( 'like' == $meta_compare ) {
-                        $where .= $wpdb-&gt;prepare( &quot; AND $alias.meta_value LIKE %s&quot;, '%' . like_escape( $meta_value ) . '%' );
-                } else {
-                        $where .= $wpdb-&gt;prepare( &quot; AND $alias.meta_value $meta_compare %s&quot;, $meta_value );
-                }
-        }
-
-        return array( $join, $where );
-}
-
-/*
</del><span class="cx">  * Used internally to tidy up the search terms
</span><span class="cx">  *
</span><span class="cx">  * @access private
</span></span></pre></div>
<a id="trunkwpincludesqueryphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/query.php (15714 => 15715)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/query.php        2010-10-04 18:26:26 UTC (rev 15714)
+++ trunk/wp-includes/query.php        2010-10-04 18:57:13 UTC (rev 15715)
</span><span class="lines">@@ -641,7 +641,7 @@
</span><span class="cx">  *
</span><span class="cx">  * @since 1.5.0
</span><span class="cx">  */
</span><del>-class WP_Query {
</del><ins>+class WP_Query extends WP_Object_Query {
</ins><span class="cx"> 
</span><span class="cx">         /**
</span><span class="cx">          * Query vars set by the user
</span><span class="lines">@@ -671,15 +671,6 @@
</span><span class="cx">         var $tax_query = array();
</span><span class="cx"> 
</span><span class="cx">         /**
</span><del>-         * Metadata query
-         *
-         * @since 3.1.0
-         * @access public
-         * @var array
-         */
-        var $meta_query = array();
-
-        /**
</del><span class="cx">          * Holds the data for a single object that is queried.
</span><span class="cx">          *
</span><span class="cx">          * Holds the contents of a post, page, category, attachment.
</span><span class="lines">@@ -1389,20 +1380,8 @@
</span><span class="cx">                                 $this-&gt;is_tax = true;
</span><span class="cx">                         }
</span><span class="cx"> 
</span><del>-                        if ( !empty( $qv['meta_query'] ) &amp;&amp; is_array( $qv['meta_query'] ) ) {
-                                $this-&gt;meta_query = $qv['meta_query'];
-                        }
</del><ins>+                        $this-&gt;parse_meta_query( $qv );
</ins><span class="cx"> 
</span><del>-                        $meta_query = array();
-                        foreach ( array( 'key', 'value', 'compare' ) as $key ) {
-                                if ( !empty( $qv[ &quot;meta_$key&quot; ] ) )
-                                        $meta_query[ $key ] = $qv[ &quot;meta_$key&quot; ];
-                        }
-
-                        if ( !empty( $meta_query ) ) {
-                                $this-&gt;meta_query[] = $meta_query;
-                        }
-
</del><span class="cx">                         if ( empty($qv['author']) || ($qv['author'] == '0') ) {
</span><span class="cx">                                 $this-&gt;is_author = false;
</span><span class="cx">                         } else {
</span><span class="lines">@@ -2211,7 +2190,7 @@
</span><span class="cx">                         $where .= ')';
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                list( $meta_join, $meta_where ) = _wp_meta_sql( $this-&gt;meta_query, $wpdb-&gt;posts, 'ID', $wpdb-&gt;postmeta, 'post_id' );
</del><ins>+                list( $meta_join, $meta_where ) = $this-&gt;get_meta_sql( $wpdb-&gt;posts, 'ID', $wpdb-&gt;postmeta, 'post_id' );
</ins><span class="cx">                 $join .= $meta_join;
</span><span class="cx">                 $where .= $meta_where;
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpincludesuserphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/user.php (15714 => 15715)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/user.php        2010-10-04 18:26:26 UTC (rev 15714)
+++ trunk/wp-includes/user.php        2010-10-04 18:57:13 UTC (rev 15715)
</span><span class="lines">@@ -330,7 +330,7 @@
</span><span class="cx">  *
</span><span class="cx">  * @since 3.1.0
</span><span class="cx">  */
</span><del>-class WP_User_Query {
</del><ins>+class WP_User_Query extends WP_Object_Query {
</ins><span class="cx"> 
</span><span class="cx">         /**
</span><span class="cx">          * List of found user ids
</span><span class="lines">@@ -444,11 +444,11 @@
</span><span class="cx">                         $this-&gt;query_where .= _wp_search_sql( $search, array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') );
</span><span class="cx">                 }
</span><span class="cx"> 
</span><ins>+                $this-&gt;parse_meta_query( $qv );
+
</ins><span class="cx">                 $role = trim( $qv['role'] );
</span><span class="cx">                 $blog_id = absint( $qv['blog_id'] );
</span><span class="cx"> 
</span><del>-                $meta_queries = array();
-
</del><span class="cx">                 if ( $blog_id ) {
</span><span class="cx">                         $cap_meta_query = array();
</span><span class="cx">                         $cap_meta_query['key'] = $wpdb-&gt;get_blog_prefix( $blog_id ) . 'capabilities';
</span><span class="lines">@@ -458,20 +458,10 @@
</span><span class="cx">                                 $cap_meta_query['compare'] = 'like';
</span><span class="cx">                         }
</span><span class="cx"> 
</span><del>-                        $meta_queries[] = $cap_meta_query;
</del><ins>+                        $this-&gt;meta_query[] = $cap_meta_query;
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                $meta_query = array();
-                foreach ( array( 'key', 'value', 'compare' ) as $key ) {
-                        if ( !empty( $qv[ &quot;meta_$key&quot; ] ) )
-                                $meta_query[ $key ] = $qv[ &quot;meta_$key&quot; ];
-                }
-
-                if ( !empty( $meta_query ) ) {
-                        $meta_queries[] = $meta_query;
-                }
-
-                list( $meta_join, $meta_where ) = _wp_meta_sql( $meta_queries, $wpdb-&gt;users, 'ID', $wpdb-&gt;usermeta, 'user_id' );
</del><ins>+                list( $meta_join, $meta_where ) = $this-&gt;get_meta_sql( $wpdb-&gt;users, 'ID', $wpdb-&gt;usermeta, 'user_id' );
</ins><span class="cx">                 $this-&gt;query_from .= $meta_join;
</span><span class="cx">                 $this-&gt;query_where .= $meta_where;
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>