<!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>[17742] trunk/wp-includes:
Use the post type object in get_posts_by_author_sql()
and add a post type parameter to count_many_users_posts().</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/17742">17742</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2011-04-28 11:27:39 +0000 (Thu, 28 Apr 2011)</dd>
</dl>
<h3>Log Message</h3>
<pre>Use the post type object in get_posts_by_author_sql() and add a post type parameter to count_many_users_posts(). The formerly somewhat useless and now totally useless pub_priv_sql_capability filter is considered deprecated. fixes <a href="http://trac.wordpress.org/ticket/17220">#17220</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludespostphp">trunk/wp-includes/post.php</a></li>
<li><a href="#trunkwpincludesuserphp">trunk/wp-includes/user.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/post.php (17741 => 17742)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/post.php        2011-04-28 11:17:09 UTC (rev 17741)
+++ trunk/wp-includes/post.php        2011-04-28 11:27:39 UTC (rev 17742)
</span><span class="lines">@@ -4047,63 +4047,49 @@
</span><span class="cx"> * Retrieve the private post SQL based on capability.
</span><span class="cx"> *
</span><span class="cx"> * This function provides a standardized way to appropriately select on the
</span><del>- * post_status of posts/pages. The function will return a piece of SQL code that
- * can be added to a WHERE clause; this SQL is constructed to allow all
</del><ins>+ * post_status of a post type. The function will return a piece of SQL code
+ * that can be added to a WHERE clause; this SQL is constructed to allow all
</ins><span class="cx"> * published posts, and all private posts to which the user has access.
</span><span class="cx"> *
</span><del>- * It also allows plugins that define their own post type to control the cap by
- * using the hook 'pub_priv_sql_capability'. The plugin is expected to return
- * the capability the user must have to read the private post type.
- *
</del><span class="cx"> * @since 2.2.0
</span><span class="cx"> *
</span><span class="cx"> * @uses $user_ID
</span><del>- * @uses apply_filters() Call 'pub_priv_sql_capability' filter for plugins with different post types.
</del><span class="cx"> *
</span><span class="cx"> * @param string $post_type currently only supports 'post' or 'page'.
</span><span class="cx"> * @return string SQL code that can be added to a where clause.
</span><span class="cx"> */
</span><del>-function get_private_posts_cap_sql($post_type) {
-        return get_posts_by_author_sql($post_type, FALSE);
</del><ins>+function get_private_posts_cap_sql( $post_type ) {
+        return get_posts_by_author_sql( $post_type, false );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="cx"> * Retrieve the post SQL based on capability, author, and type.
</span><span class="cx"> *
</span><del>- * See above for full description.
</del><ins>+ * @see get_private_posts_cap_sql() for full description.
</ins><span class="cx"> *
</span><span class="cx"> * @since 3.0.0
</span><del>- * @param string $post_type currently only supports 'post' or 'page'.
</del><ins>+ * @param string $post_type Post type.
</ins><span class="cx"> * @param bool $full Optional. Returns a full WHERE statement instead of just an 'andalso' term.
</span><span class="cx"> * @param int $post_author Optional. Query posts having a single author ID.
</span><span class="cx"> * @return string SQL WHERE code that can be added to a query.
</span><span class="cx"> */
</span><del>-function get_posts_by_author_sql($post_type, $full = TRUE, $post_author = NULL) {
</del><ins>+function get_posts_by_author_sql( $post_type, $full = true, $post_author = null ) {
</ins><span class="cx">         global $user_ID, $wpdb;
</span><span class="cx">
</span><span class="cx">         // Private posts
</span><del>-        if ($post_type == 'post') {
-                $cap = 'read_private_posts';
-        // Private pages
-        } elseif ($post_type == 'page') {
-                $cap = 'read_private_pages';
-        // Dunno what it is, maybe plugins have their own post type?
-        } else {
-                $cap = '';
-                $cap = apply_filters('pub_priv_sql_capability', $cap);
</del><ins>+        $post_type_obj = get_post_type_object( $post_type );
+        if ( ! $post_type_obj )
+                return ' 1 = 0 ';
</ins><span class="cx">
</span><del>-                if (empty($cap)) {
-                        // We don't know what it is, filters don't change anything,
-                        // so set the SQL up to return nothing.
-                        return ' 1 = 0 ';
-                }
-        }
</del><ins>+        // This hook is deprecated. Why you'd want to use it, I dunno.
+        if ( ! $cap = apply_filters( 'pub_priv_sql_capability', '' ) )
+                $cap = $post_type_obj->cap->read_private_posts;
</ins><span class="cx">
</span><del>-        if ($full) {
-                if (is_null($post_author)) {
-                        $sql = $wpdb->prepare('WHERE post_type = %s AND ', $post_type);
</del><ins>+        if ( $full ) {
+                if ( null === $post_author ) {
+                        $sql = $wpdb->prepare( 'WHERE post_type = %s AND ', $post_type );
</ins><span class="cx">                 } else {
</span><del>-                        $sql = $wpdb->prepare('WHERE post_author = %d AND post_type = %s AND ', $post_author, $post_type);
</del><ins>+                        $sql = $wpdb->prepare( 'WHERE post_author = %d AND post_type = %s AND ', $post_author, $post_type );
</ins><span class="cx">                 }
</span><span class="cx">         } else {
</span><span class="cx">                 $sql = '';
</span><span class="lines">@@ -4111,15 +4097,15 @@
</span><span class="cx">
</span><span class="cx">         $sql .= "(post_status = 'publish'";
</span><span class="cx">
</span><del>-        if (current_user_can($cap)) {
</del><ins>+        if ( current_user_can( $cap ) ) {
</ins><span class="cx">                 // Does the user have the capability to view private posts? Guess so.
</span><span class="cx">                 $sql .= " OR post_status = 'private'";
</span><del>-        } elseif (is_user_logged_in()) {
</del><ins>+        } elseif ( is_user_logged_in() ) {
</ins><span class="cx">                 // Users can view their own private posts.
</span><span class="cx">                 $id = (int) $user_ID;
</span><del>-                if (is_null($post_author) || !$full) {
</del><ins>+                if ( null === $post_author || ! $full ) {
</ins><span class="cx">                         $sql .= " OR post_status = 'private' AND post_author = $id";
</span><del>-                } elseif ($id == (int)$post_author) {
</del><ins>+                } elseif ( $id == (int) $post_author ) {
</ins><span class="cx">                         $sql .= " OR post_status = 'private'";
</span><span class="cx">                 } // else none
</span><span class="cx">         } // else none
</span></span></pre></div>
<a id="trunkwpincludesuserphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/user.php (17741 => 17742)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/user.php        2011-04-28 11:17:09 UTC (rev 17741)
+++ trunk/wp-includes/user.php        2011-04-28 11:27:39 UTC (rev 17742)
</span><span class="lines">@@ -165,18 +165,19 @@
</span><span class="cx"> * Number of posts written by a list of users.
</span><span class="cx"> *
</span><span class="cx"> * @since 3.0.0
</span><del>- * @param array $users User ID number list.
</del><ins>+ * @param array $user_ids Array of user IDs.
+ * @param string|array $post_type Optional. Post type to check. Defaults to post.
</ins><span class="cx"> * @return array Amount of posts each user has written.
</span><span class="cx"> */
</span><del>-function count_many_users_posts($users) {
</del><ins>+function count_many_users_posts($users, $post_type = 'post' ) {
</ins><span class="cx">         global $wpdb;
</span><span class="cx">
</span><span class="cx">         $count = array();
</span><del>-        if ( ! is_array($users) || empty( $users ) )
</del><ins>+        if ( empty( $users ) || ! is_array( $users ) )
</ins><span class="cx">                 return $count;
</span><span class="cx">
</span><del>-        $userlist = implode( ',', $users );
-        $where = get_posts_by_author_sql( 'post' );
</del><ins>+        $userlist = implode( ',', array_map( 'absint', $users ) );
+        $where = get_posts_by_author_sql( $post_type );
</ins><span class="cx">
</span><span class="cx">         $result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N );
</span><span class="cx">         foreach ( $result as $row ) {
</span></span></pre>
</div>
</div>
</body>
</html>