Make WordPress Core

Ticket #33216: #33216

File #33216, 4.8 KB (added by emirpprime, 9 years ago)

Patch for bug described in ticket.

Line 
1From 8e94268c1ea99aaf36b59a405ca186e9ed361edd Mon Sep 17 00:00:00 2001
2From: Phil Banks <phil@helpfultechnology.com>
3Date: Fri, 31 Jul 2015 12:34:22 +0100
4Subject: [PATCH] Show true post count on users screen
5
6Include draft posts in count of users' posts on users.php
7---
8 wp-admin/includes/class-wp-users-list-table.php |  2 +-
9 wp-includes/post.php                            |  6 +++++-
10 wp-includes/user.php                            | 12 +++++++-----
11 3 files changed, 13 insertions(+), 7 deletions(-)
12
13diff --git a/wp-admin/includes/class-wp-users-list-table.php b/wp-admin/includes/class-wp-users-list-table.php
14index 4035c58..472c97d 100644
15--- a/wp-admin/includes/class-wp-users-list-table.php
16+++ b/wp-admin/includes/class-wp-users-list-table.php
17@@ -311,7 +311,7 @@ class WP_Users_List_Table extends WP_List_Table {
18        public function display_rows() {
19                // Query the post counts for this page
20                if ( ! $this->is_site_users )
21-                       $post_counts = count_many_users_posts( array_keys( $this->items ) );
22+                       $post_counts = count_many_users_posts( array_keys( $this->items ), 'post', false, true );
23 
24                $editable_roles = array_keys( get_editable_roles() );
25 
26diff --git a/wp-includes/post.php b/wp-includes/post.php
27index ff332e2..26b16e1 100644
28--- a/wp-includes/post.php
29+++ b/wp-includes/post.php
30@@ -5415,9 +5415,10 @@ function get_private_posts_cap_sql( $post_type ) {
31  * @param int            $post_author Optional. Query posts having a single author ID. Default null.
32  * @param bool           $public_only Optional. Only return public posts. Skips cap checks for
33  *                                    $current_user.  Default false.
34+ * @param bool           $drafts         Optional. Include draft posts. Default false.
35  * @return string SQL WHERE code that can be added to a query.
36  */
37-function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) {
38+function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false, $drafts = false ) {
39        global $wpdb;
40 
41        if ( is_array( $post_type ) ) {
42@@ -5448,6 +5449,9 @@ function get_posts_by_author_sql( $post_type, $full = true, $post_author = null,
43 
44                // Only need to check the cap if $public_only is false.
45                $post_status_sql = "post_status = 'publish'";
46+               if( $drafts ) {
47+                       $post_status_sql .= " OR post_status = 'draft'";
48+               }
49                if ( false === $public_only ) {
50                        if ( $cap ) {
51                                // Does the user have the capability to view private posts? Guess so.
52diff --git a/wp-includes/user.php b/wp-includes/user.php
53index 54b09ff..572667c 100644
54--- a/wp-includes/user.php
55+++ b/wp-includes/user.php
56@@ -263,12 +263,13 @@ function wp_validate_logged_in_cookie( $user_id ) {
57  * @param int          $userid      User ID.
58  * @param array|string $post_type   Optional. Post type(s) to count the number of posts for. Default 'post'.
59  * @param bool         $public_only Optional. Whether to only return counts for public posts. Default false.
60- * @return int Number of posts the user has written in this post type.
61+ * @param bool         $drafts                 Optional. Include draft posts. Default false.
62+* @return int Number of posts the user has written in this post type.
63  */
64-function count_user_posts( $userid, $post_type = 'post', $public_only = false ) {
65+function count_user_posts( $userid, $post_type = 'post', $public_only = false, $drafts = false ) {
66        global $wpdb;
67 
68-       $where = get_posts_by_author_sql( $post_type, true, $userid, $public_only );
69+       $where = get_posts_by_author_sql( $post_type, true, $userid, $public_only, $drafts );
70 
71        $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
72 
73@@ -297,9 +298,10 @@ function count_user_posts( $userid, $post_type = 'post', $public_only = false )
74  * @param array        $users       Array of user IDs.
75  * @param string|array $post_type   Optional. Single post type or array of post types to check. Defaults to 'post'.
76  * @param bool         $public_only Optional. Only return counts for public posts.  Defaults to false.
77+ * @param bool         $drafts                 Optional. Include draft posts. Default false.
78  * @return array Amount of posts each user has written.
79  */
80-function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) {
81+function count_many_users_posts( $users, $post_type = 'post', $public_only = false, $drafts = false ) {
82        global $wpdb;
83 
84        $count = array();
85@@ -307,7 +309,7 @@ function count_many_users_posts( $users, $post_type = 'post', $public_only = fal
86                return $count;
87 
88        $userlist = implode( ',', array_map( 'absint', $users ) );
89-       $where = get_posts_by_author_sql( $post_type, true, null, $public_only );
90+       $where = get_posts_by_author_sql( $post_type, true, null, $public_only, $drafts );
91 
92        $result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N );
93        foreach ( $result as $row ) {
94--
952.2.1
96