<!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>[12989] trunk/wp-admin/includes/user.php:
  Allow plugins to modify the query run by WP_User_Search.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/12989">12989</a></dd>
<dt>Author</dt> <dd>westi</dd>
<dt>Date</dt> <dd>2010-02-06 12:35:15 +0000 (Sat, 06 Feb 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Allow plugins to modify the query run by WP_User_Search. Fixes <a href="http://trac.wordpress.org/ticket/10781">#10781</a> props scribu.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminincludesuserphp">trunk/wp-admin/includes/user.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminincludesuserphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/user.php (12988 => 12989)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/user.php        2010-02-06 11:42:20 UTC (rev 12988)
+++ trunk/wp-admin/includes/user.php        2010-02-06 12:35:15 UTC (rev 12989)
</span><span class="lines">@@ -566,31 +566,40 @@
</span><span class="cx">          *
</span><span class="cx">          * @since unknown
</span><span class="cx">          * @access private
</span><del>-         * @var unknown_type
</del><ins>+         * @var string
</ins><span class="cx">          */
</span><span class="cx">         var $query_limit;
</span><span class="cx"> 
</span><span class="cx">         /**
</span><span class="cx">          * {@internal Missing Description}}
</span><span class="cx">          *
</span><del>-         * @since unknown
</del><ins>+         * @since 3.0
</ins><span class="cx">          * @access private
</span><del>-         * @var unknown_type
</del><ins>+         * @var string
</ins><span class="cx">          */
</span><del>-        var $query_sort;
</del><ins>+        var $query_orderby;
</ins><span class="cx"> 
</span><span class="cx">         /**
</span><span class="cx">          * {@internal Missing Description}}
</span><span class="cx">          *
</span><del>-         * @since unknown
</del><ins>+         * @since 3.0
</ins><span class="cx">          * @access private
</span><del>-         * @var unknown_type
</del><ins>+         * @var string
</ins><span class="cx">          */
</span><del>-        var $query_from_where;
</del><ins>+        var $query_from;
</ins><span class="cx"> 
</span><span class="cx">         /**
</span><span class="cx">          * {@internal Missing Description}}
</span><span class="cx">          *
</span><ins>+         * @since 3.0
+         * @access private
+         * @var string
+         */
+        var $query_where;
+
+        /**
+         * {@internal Missing Description}}
+         *
</ins><span class="cx">          * @since unknown
</span><span class="cx">          * @access private
</span><span class="cx">          * @var int
</span><span class="lines">@@ -657,8 +666,10 @@
</span><span class="cx">         function prepare_query() {
</span><span class="cx">                 global $wpdb;
</span><span class="cx">                 $this-&gt;first_user = ($this-&gt;page - 1) * $this-&gt;users_per_page;
</span><ins>+
</ins><span class="cx">                 $this-&gt;query_limit = $wpdb-&gt;prepare(&quot; LIMIT %d, %d&quot;, $this-&gt;first_user, $this-&gt;users_per_page);
</span><del>-                $this-&gt;query_sort = ' ORDER BY user_login';
</del><ins>+                $this-&gt;query_orderby = ' ORDER BY user_login';
+
</ins><span class="cx">                 $search_sql = '';
</span><span class="cx">                 if ( $this-&gt;search_term ) {
</span><span class="cx">                         $searches = array();
</span><span class="lines">@@ -669,17 +680,19 @@
</span><span class="cx">                         $search_sql .= ')';
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                $this-&gt;query_from_where = &quot;FROM $wpdb-&gt;users&quot;;
</del><ins>+                $this-&gt;query_from = &quot; FROM $wpdb-&gt;users&quot;;
+                $this-&gt;query_where = &quot; WHERE 1=1 $search_sql&quot;;
+
</ins><span class="cx">                 if ( $this-&gt;role ) {
</span><del>-                        $this-&gt;query_from_where .= $wpdb-&gt;prepare(&quot; INNER JOIN $wpdb-&gt;usermeta ON $wpdb-&gt;users.ID = $wpdb-&gt;usermeta.user_id WHERE $wpdb-&gt;usermeta.meta_key = '{$wpdb-&gt;prefix}capabilities' AND $wpdb-&gt;usermeta.meta_value LIKE %s&quot;, '%' . $this-&gt;role . '%');
-                } elseif ( !is_multisite() ) {
-                        $this-&gt;query_from_where .= &quot; WHERE 1=1&quot;;
-                } else {
</del><ins>+                        $this-&gt;query_from .= &quot; INNER JOIN $wpdb-&gt;usermeta ON $wpdb-&gt;users.ID = $wpdb-&gt;usermeta.user_id&quot;;
+                        $this-&gt;query_where .= $wpdb-&gt;prepare(&quot; AND $wpdb-&gt;usermeta.meta_key = '{$wpdb-&gt;prefix}capabilities' AND $wpdb-&gt;usermeta.meta_value LIKE %s&quot;, '%' . $this-&gt;role . '%');
+                } elseif ( is_multisite() ) {
</ins><span class="cx">                         $level_key = $wpdb-&gt;get_blog_prefix() . 'capabilities'; // wpmu site admins don't have user_levels
</span><del>-                        $this-&gt;query_from_where .= &quot;, $wpdb-&gt;usermeta WHERE $wpdb-&gt;users.ID = $wpdb-&gt;usermeta.user_id AND meta_key = '{$level_key}'&quot;;
</del><ins>+                        $this-&gt;query_from .= &quot;, $wpdb-&gt;usermeta&quot;;
+                        $this-&gt;query_where .= &quot; AND $wpdb-&gt;users.ID = $wpdb-&gt;usermeta.user_id AND meta_key = '{$level_key}'&quot;;
</ins><span class="cx">                 }
</span><del>-                $this-&gt;query_from_where .= &quot; $search_sql&quot;;
</del><span class="cx"> 
</span><ins>+                do_action_ref_array( 'pre_user_search', array( &amp;$this ) );
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span><span class="lines">@@ -692,10 +705,11 @@
</span><span class="cx">          */
</span><span class="cx">         function query() {
</span><span class="cx">                 global $wpdb;
</span><del>-                $this-&gt;results = $wpdb-&gt;get_col('SELECT ID ' . $this-&gt;query_from_where . $this-&gt;query_sort . $this-&gt;query_limit);
</del><span class="cx"> 
</span><ins>+                $this-&gt;results = $wpdb-&gt;get_col(&quot;SELECT DISTINCT($wpdb-&gt;users.ID)&quot; . $this-&gt;query_from . $this-&gt;query_where . $this-&gt;query_orderby . $this-&gt;query_limit);
+
</ins><span class="cx">                 if ( $this-&gt;results )
</span><del>-                        $this-&gt;total_users_for_query = $wpdb-&gt;get_var('SELECT COUNT(ID) ' . $this-&gt;query_from_where); // no limit
</del><ins>+                        $this-&gt;total_users_for_query = $wpdb-&gt;get_var(&quot;SELECT COUNT(DISTINCT($wpdb-&gt;users.ID))&quot; . $this-&gt;query_from . $this-&gt;query_where); // no limit
</ins><span class="cx">                 else
</span><span class="cx">                         $this-&gt;search_errors = new WP_Error('no_matching_users_found', __('No matching users were found!'));
</span><span class="cx">         }
</span></span></pre>
</div>
</div>

</body>
</html>