<!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>[15723] trunk/wp-includes: Move _wp_search_sql() into WP_Object_Query.</title>
</head>
<body>

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

<h3>Log Message</h3>
<pre>Move _wp_search_sql() into WP_Object_Query. Introduce WP_Comment_Search. 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="#trunkwpincludescommentphp">trunk/wp-includes/comment.php</a></li>
<li><a href="#trunkwpincludesfunctionsphp">trunk/wp-includes/functions.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 (15722 => 15723)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/classes.php        2010-10-04 20:53:04 UTC (rev 15722)
+++ trunk/wp-includes/classes.php        2010-10-04 21:05:31 UTC (rev 15723)
</span><span class="lines">@@ -546,7 +546,7 @@
</span><span class="cx">         /*
</span><span class="cx">          * Populates the $meta_query property
</span><span class="cx">          *
</span><del>-         * @access private
</del><ins>+         * @access protected
</ins><span class="cx">          * @since 3.1.0
</span><span class="cx">          *
</span><span class="cx">          * @param array $qv The query variables
</span><span class="lines">@@ -570,7 +570,7 @@
</span><span class="cx">         /*
</span><span class="cx">          * Used internally to generate an SQL string for searching across multiple meta key = value pairs
</span><span class="cx">          *
</span><del>-         * @access private
</del><ins>+         * @access protected
</ins><span class="cx">          * @since 3.1.0
</span><span class="cx">          *
</span><span class="cx">          * @param string $primary_table
</span><span class="lines">@@ -621,6 +621,26 @@
</span><span class="cx"> 
</span><span class="cx">                 return array( $join, $where );
</span><span class="cx">         }
</span><ins>+
+        /*
+         * Used internally to generate an SQL string for searching across multiple columns
+         *
+         * @access protected
+         * @since 3.1.0
+         *
+         * @param string $string
+         * @param array $cols
+         * @return string
+         */
+        function get_search_sql( $string, $cols ) {
+                $string = esc_sql( $string );
+
+                $searches = array();
+                foreach ( $cols as $col )
+                        $searches[] = &quot;$col LIKE '%$string%'&quot;;
+
+                return ' AND (' . implode(' OR ', $searches) . ')';
+        }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span></span></pre></div>
<a id="trunkwpincludescommentphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/comment.php (15722 => 15723)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/comment.php        2010-10-04 20:53:04 UTC (rev 15722)
+++ trunk/wp-includes/comment.php        2010-10-04 21:05:31 UTC (rev 15723)
</span><span class="lines">@@ -188,123 +188,131 @@
</span><span class="cx">  * @return array List of comments.
</span><span class="cx">  */
</span><span class="cx"> function get_comments( $args = '' ) {
</span><del>-        global $wpdb;
</del><ins>+        $query = new WP_Comment_Query;
+        return $query-&gt;query( $args );
+}
</ins><span class="cx"> 
</span><del>-        $defaults = array(
-                'author_email' =&gt; '',
-                'ID' =&gt; '',
-                'karma' =&gt; '',
-                'number' =&gt; '',
-                'offset' =&gt; '',
-                'orderby' =&gt; '',
-                'order' =&gt; 'DESC',
-                'parent' =&gt; '',
-                'post_ID' =&gt; '',
-                'post_id' =&gt; 0,
-                'status' =&gt; '',
-                'type' =&gt; '',
-                'user_id' =&gt; '',
-                'search' =&gt; '',
-                'count' =&gt; false
-        );
</del><ins>+class WP_Comment_Query extends WP_Object_Query {
</ins><span class="cx"> 
</span><del>-        $args = wp_parse_args( $args, $defaults );
-        extract( $args, EXTR_SKIP );
</del><ins>+        function query( $args ) {
+                global $wpdb;
</ins><span class="cx"> 
</span><del>-        // $args can be whatever, only use the args defined in defaults to compute the key
-        $key = md5( serialize( compact(array_keys($defaults)) )  );
-        $last_changed = wp_cache_get('last_changed', 'comment');
-        if ( !$last_changed ) {
-                $last_changed = time();
-                wp_cache_set('last_changed', $last_changed, 'comment');
-        }
-        $cache_key = &quot;get_comments:$key:$last_changed&quot;;
</del><ins>+                $defaults = array(
+                        'author_email' =&gt; '',
+                        'ID' =&gt; '',
+                        'karma' =&gt; '',
+                        'number' =&gt; '',
+                        'offset' =&gt; '',
+                        'orderby' =&gt; '',
+                        'order' =&gt; 'DESC',
+                        'parent' =&gt; '',
+                        'post_ID' =&gt; '',
+                        'post_id' =&gt; 0,
+                        'status' =&gt; '',
+                        'type' =&gt; '',
+                        'user_id' =&gt; '',
+                        'search' =&gt; '',
+                        'count' =&gt; false
+                );
</ins><span class="cx"> 
</span><del>-        if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
-                return $cache;
-        }
</del><ins>+                $args = wp_parse_args( $args, $defaults );
+                extract( $args, EXTR_SKIP );
</ins><span class="cx"> 
</span><del>-        $post_id = absint($post_id);
</del><ins>+                // $args can be whatever, only use the args defined in defaults to compute the key
+                $key = md5( serialize( compact(array_keys($defaults)) )  );
+                $last_changed = wp_cache_get('last_changed', 'comment');
+                if ( !$last_changed ) {
+                        $last_changed = time();
+                        wp_cache_set('last_changed', $last_changed, 'comment');
+                }
+                $cache_key = &quot;get_comments:$key:$last_changed&quot;;
</ins><span class="cx"> 
</span><del>-        if ( 'hold' == $status )
-                $approved = &quot;comment_approved = '0'&quot;;
-        elseif ( 'approve' == $status )
-                $approved = &quot;comment_approved = '1'&quot;;
-        elseif ( 'spam' == $status )
-                $approved = &quot;comment_approved = 'spam'&quot;;
-        elseif ( 'trash' == $status )
-                $approved = &quot;comment_approved = 'trash'&quot;;
-        else
-                $approved = &quot;( comment_approved = '0' OR comment_approved = '1' )&quot;;
</del><ins>+                if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
+                        return $cache;
+                }
</ins><span class="cx"> 
</span><del>-        $order = ( 'ASC' == strtoupper($order) ) ? 'ASC' : 'DESC';
</del><ins>+                $post_id = absint($post_id);
</ins><span class="cx"> 
</span><del>-        if ( ! empty( $orderby ) ) {
-                $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
-                $ordersby = array_intersect(
-                        $ordersby,
-                        array(
-                                'comment_agent',
-                                'comment_approved',
-                                'comment_author',
-                                'comment_author_email',
-                                'comment_author_IP',
-                                'comment_author_url',
-                                'comment_content',
-                                'comment_date',
-                                'comment_date_gmt',
-                                'comment_ID',
-                                'comment_karma',
-                                'comment_parent',
-                                'comment_post_ID',
-                                'comment_type',
-                                'user_id',
-                        )
-                );
-                $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
-        } else {
-                $orderby = 'comment_date_gmt';
-        }
</del><ins>+                if ( 'hold' == $status )
+                        $approved = &quot;comment_approved = '0'&quot;;
+                elseif ( 'approve' == $status )
+                        $approved = &quot;comment_approved = '1'&quot;;
+                elseif ( 'spam' == $status )
+                        $approved = &quot;comment_approved = 'spam'&quot;;
+                elseif ( 'trash' == $status )
+                        $approved = &quot;comment_approved = 'trash'&quot;;
+                else
+                        $approved = &quot;( comment_approved = '0' OR comment_approved = '1' )&quot;;
</ins><span class="cx"> 
</span><del>-        $number = absint($number);
-        $offset = absint($offset);
</del><ins>+                $order = ( 'ASC' == strtoupper($order) ) ? 'ASC' : 'DESC';
</ins><span class="cx"> 
</span><del>-        if ( !empty($number) ) {
-                if ( $offset )
-                        $limit = 'LIMIT ' . $offset . ',' . $number;
-                else
-                        $limit = 'LIMIT ' . $number;
-        } else {
-                $limit = '';
-        }
</del><ins>+                if ( ! empty( $orderby ) ) {
+                        $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
+                        $ordersby = array_intersect(
+                                $ordersby,
+                                array(
+                                        'comment_agent',
+                                        'comment_approved',
+                                        'comment_author',
+                                        'comment_author_email',
+                                        'comment_author_IP',
+                                        'comment_author_url',
+                                        'comment_content',
+                                        'comment_date',
+                                        'comment_date_gmt',
+                                        'comment_ID',
+                                        'comment_karma',
+                                        'comment_parent',
+                                        'comment_post_ID',
+                                        'comment_type',
+                                        'user_id',
+                                )
+                        );
+                        $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
+                } else {
+                        $orderby = 'comment_date_gmt';
+                }
</ins><span class="cx"> 
</span><del>-        $post_where = &quot;WHERE $approved&quot;;
</del><ins>+                $number = absint($number);
+                $offset = absint($offset);
</ins><span class="cx"> 
</span><del>-        if ( ! empty($post_id) )
-                $post_where .= $wpdb-&gt;prepare( ' AND comment_post_ID = %d', $post_id );
-        if ( '' !== $author_email )
-                $post_where .= $wpdb-&gt;prepare( 'AND comment_author_email = %s', $author_email );
-        if ( '' !== $karma )
-                $post_where .= $wpdb-&gt;prepare( 'AND comment_karma = %d', $karma );
-        if ( 'comment' == $type )
-                $post_where .= &quot; AND comment_type = ''&quot;;
-        elseif ( ! empty( $type ) )
-                $post_where .= $wpdb-&gt;prepare( ' AND comment_type = %s', $type );
-        if ( '' !== $parent )
-                $post_where .= $wpdb-&gt;prepare( ' AND comment_parent = %d', $parent );
-        if ( '' !== $user_id )
-                $post_where .= $wpdb-&gt;prepare( ' AND user_id = %d', $user_id );
-        if ( '' !== $search )
-                $post_where .= _wp_search_sql($search, array('comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content'));
</del><ins>+                if ( !empty($number) ) {
+                        if ( $offset )
+                                $limit = 'LIMIT ' . $offset . ',' . $number;
+                        else
+                                $limit = 'LIMIT ' . $number;
+                } else {
+                        $limit = '';
+                }
</ins><span class="cx"> 
</span><del>-        if ( $count )
-                return $wpdb-&gt;get_var( &quot;SELECT COUNT(*) FROM $wpdb-&gt;comments $post_where ORDER BY $orderby $order $limit&quot; );
</del><ins>+                $post_where = &quot;WHERE $approved&quot;;
</ins><span class="cx"> 
</span><del>-        $comments = $wpdb-&gt;get_results( &quot;SELECT * FROM $wpdb-&gt;comments $post_where ORDER BY $orderby $order $limit&quot; );
</del><ins>+                if ( ! empty($post_id) )
+                        $post_where .= $wpdb-&gt;prepare( ' AND comment_post_ID = %d', $post_id );
+                if ( '' !== $author_email )
+                        $post_where .= $wpdb-&gt;prepare( 'AND comment_author_email = %s', $author_email );
+                if ( '' !== $karma )
+                        $post_where .= $wpdb-&gt;prepare( 'AND comment_karma = %d', $karma );
+                if ( 'comment' == $type )
+                        $post_where .= &quot; AND comment_type = ''&quot;;
+                elseif ( ! empty( $type ) )
+                        $post_where .= $wpdb-&gt;prepare( ' AND comment_type = %s', $type );
+                if ( '' !== $parent )
+                        $post_where .= $wpdb-&gt;prepare( ' AND comment_parent = %d', $parent );
+                if ( '' !== $user_id )
+                        $post_where .= $wpdb-&gt;prepare( ' AND user_id = %d', $user_id );
+                if ( '' !== $search )
+                        $post_where .= $this-&gt;get_search_sql( $search, array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) );
</ins><span class="cx"> 
</span><del>-        wp_cache_add( $cache_key, $comments, 'comment' );
</del><ins>+                if ( $count )
+                        return $wpdb-&gt;get_var( &quot;SELECT COUNT(*) FROM $wpdb-&gt;comments $post_where ORDER BY $orderby $order $limit&quot; );
</ins><span class="cx"> 
</span><del>-        return $comments;
</del><ins>+                $comments = $wpdb-&gt;get_results( &quot;SELECT * FROM $wpdb-&gt;comments $post_where ORDER BY $orderby $order $limit&quot; );
+
+                wp_cache_add( $cache_key, $comments, 'comment' );
+
+                return $comments;        
+        }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span></span></pre></div>
<a id="trunkwpincludesfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/functions.php (15722 => 15723)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/functions.php        2010-10-04 20:53:04 UTC (rev 15722)
+++ trunk/wp-includes/functions.php        2010-10-04 21:05:31 UTC (rev 15723)
</span><span class="lines">@@ -4237,26 +4237,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 columns
- *
- * @access private
- * @since 3.1.0
- *
- * @param string $string
- * @param array $cols
- * @return string
- */
-function _wp_search_sql($string, $cols) {
-        $string = esc_sql($string);
-
-        $searches = array();
-        foreach ( $cols as $col )
-                $searches[] = &quot;$col LIKE '%$string%'&quot;;
-
-        return ' AND (' . implode(' OR ', $searches) . ')';
-}
-
-/*
</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="trunkwpincludesuserphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/user.php (15722 => 15723)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/user.php        2010-10-04 20:53:04 UTC (rev 15722)
+++ trunk/wp-includes/user.php        2010-10-04 21:05:31 UTC (rev 15723)
</span><span class="lines">@@ -441,7 +441,7 @@
</span><span class="cx"> 
</span><span class="cx">                 $search = trim( $qv['search'] );
</span><span class="cx">                 if ( $search ) {
</span><del>-                        $this-&gt;query_where .= _wp_search_sql( $search, array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') );
</del><ins>+                        $this-&gt;query_where .= $this-&gt;get_search_sql( $search, array( 'user_login', 'user_nicename', 'user_email', 'user_url', 'display_name' ) );
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 $this-&gt;parse_meta_query( $qv );
</span></span></pre>
</div>
</div>

</body>
</html>