WordPress.org

Make WordPress Core

Ticket #11914: 11914.4.diff

File 11914.4.diff, 11.8 KB (added by miqrogroove, 8 years ago)
Added missing term
!$full in get_posts_by_author_sql
  • wp-admin/includes/schema.php

     
    145145  PRIMARY KEY  (ID),
    146146  KEY post_name (post_name),
    147147  KEY type_status_date (post_type,post_status,post_date,ID),
    148   KEY post_parent (post_parent)
     148  KEY post_parent (post_parent),
     149  KEY post_author (post_author)
    149150) $charset_collate;
    150151CREATE TABLE $wpdb->users (
    151152  ID bigint(20) unsigned NOT NULL auto_increment,
  • wp-admin/includes/template.php

     
    17891789}
    17901790
    17911791/**
    1792  * {@internal Missing Short Description}}
     1792 * Generate HTML for a single row on the users.php admin panel.
    17931793 *
    17941794 * @since unknown
    17951795 *
    1796  * @param unknown_type $user_object
    1797  * @param unknown_type $style
    1798  * @param unknown_type $role
    1799  * @return unknown
     1796 * @param object $user_object
     1797 * @param string $style Optional. Attributes added to the TR element.  Must be sanitized.
     1798 * @param string $role Key for the $wp_roles array.
     1799 * @param int $numposts Optional. Post count to display for this user.  Defaults to zero, as in, a new user has made zero posts.
     1800 * @return string
    18001801 */
    1801 function user_row( $user_object, $style = '', $role = '' ) {
     1802function user_row( $user_object, $style = '', $role = '', $numposts = 0 ) {
    18021803        global $wp_roles;
    18031804
    18041805        $current_user = wp_get_current_user();
     
    18141815                $short_url = substr( $short_url, 0, -1 );
    18151816        if ( strlen( $short_url ) > 35 )
    18161817                $short_url = substr( $short_url, 0, 32 ).'...';
    1817         $numposts = get_usernumposts( $user_object->ID );
    18181818        $checkbox = '';
    18191819        // Check if the user for this row is editable
    18201820        if ( current_user_can( 'edit_user', $user_object->ID ) ) {
  • wp-admin/users.php

     
    208208        $userspage = isset($_GET['userspage']) ? $_GET['userspage'] : null;
    209209        $role = isset($_GET['role']) ? $_GET['role'] : null;
    210210
    211         // Query the users
     211        // Query the user IDs for this page
    212212        $wp_user_search = new WP_User_Search($usersearch, $userspage, $role);
    213213
     214        // Query the post counts for this page
     215        $post_counts = get_users_numposts($wp_user_search->get_results());
     216       
     217        // Query the users for this page
     218        cache_users($wp_user_search->get_results());
     219
    214220        $messages = array();
    215221        if ( isset($_GET['update']) ) :
    216222                switch($_GET['update']) {
     
    263269<form id="list-filter" action="" method="get">
    264270<ul class="subsubsub">
    265271<?php
    266 $role_links = array();
    267 $avail_roles = array();
    268 $users_of_blog = get_users_of_blog();
    269 $total_users = count( $users_of_blog );
    270 foreach ( (array) $users_of_blog as $b_user ) {
    271         $b_roles = unserialize($b_user->meta_value);
    272         foreach ( (array) $b_roles as $b_role => $val ) {
    273                 if ( !isset($avail_roles[$b_role]) )
    274                         $avail_roles[$b_role] = 0;
    275                 $avail_roles[$b_role]++;
    276         }
    277 }
     272$users_of_blog = count_blog_users_by_role_intime();
     273$total_users = $users_of_blog['total_users'];
     274$avail_roles =& $users_of_blog['avail_roles'];
    278275unset($users_of_blog);
    279276
    280277$current_role = false;
    281278$class = empty($role) ? ' class="current"' : '';
     279$role_links = array();
    282280$role_links[] = "<li><a href='users.php'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_users, 'users' ), number_format_i18n( $total_users ) ) . '</a>';
    283281foreach ( $wp_roles->get_names() as $this_role => $name ) {
    284282        if ( !isset($avail_roles[$this_role]) )
     
    372370        $role = array_shift($roles);
    373371
    374372        $style = ( ' class="alternate"' == $style ) ? '' : ' class="alternate"';
    375         echo "\n\t" . user_row($user_object, $style, $role);
     373        echo "\n\t", user_row($user_object, $style, $role, $post_counts[(string)$userid]);
    376374}
    377375?>
    378376</tbody>
  • wp-includes/pluggable.php

     
    139139}
    140140endif;
    141141
     142if ( !function_exists('cache_users') ) :
     143/**
     144 * Retrieve info for user lists to prevent multiple queries by get_userdata()
     145 *
     146 * @since 3.0.0
     147 *
     148 * @param array $users User ID numbers list
     149 */
     150function cache_users( $users ) {
     151        global $wpdb;
     152
     153        $clean = array();
     154        foreach($users as $id) {
     155                $id = (int) $id;
     156                if (wp_cache_get($id, 'users')) {
     157                        // seems to be cached already
     158                } else {
     159                        $clean[] = $id;
     160                }
     161        }
     162
     163        if ( 0 == count($clean) )
     164                return;
     165
     166        $list = implode(',', $clean);
     167
     168        $result = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE ID IN ($list)", OBJECT_K);
     169
     170        foreach($result as $user_object) {
     171                wp_cache_add($user_object->ID, $user_object, 'users');
     172        }
     173}
     174endif;
     175
    142176if ( !function_exists('get_user_by') ) :
    143177/**
    144178 * Retrieve user info by a given field
  • wp-includes/post.php

     
    36343634 * @return string SQL code that can be added to a where clause.
    36353635 */
    36363636function get_private_posts_cap_sql($post_type) {
    3637         global $user_ID;
    3638         $cap = '';
     3637        return get_posts_by_author_sql($post_type, FALSE);
     3638}
    36393639
     3640/**
     3641 * Retrieve the post SQL based on capability, author, and type.
     3642 *
     3643 * See above for full description.
     3644 *
     3645 * @since 3.0.0
     3646 * @param string $post_type currently only supports 'post' or 'page'.
     3647 * @param bool $full Optional.  Returns a full WHERE statement instead of just an 'andalso' term.
     3648 * @param int $post_author Optional.  Query posts having a single author ID.
     3649 * @return string SQL WHERE code that can be added to a query.
     3650 */
     3651function get_posts_by_author_sql($post_type, $full = TRUE, $post_author = NULL) {
     3652        global $user_ID, $wpdb;
     3653
    36403654        // Private posts
    36413655        if ($post_type == 'post') {
    36423656                $cap = 'read_private_posts';
     
    36453659                $cap = 'read_private_pages';
    36463660        // Dunno what it is, maybe plugins have their own post type?
    36473661        } else {
     3662                $cap = '';
    36483663                $cap = apply_filters('pub_priv_sql_capability', $cap);
    36493664
    36503665                if (empty($cap)) {
    36513666                        // We don't know what it is, filters don't change anything,
    36523667                        // so set the SQL up to return nothing.
    3653                         return '1 = 0';
     3668                        return ' 1 = 0 ';
    36543669                }
    36553670        }
    36563671
    3657         $sql = '(post_status = \'publish\'';
     3672        if ($full) {
     3673                if (is_null($post_author)) {
     3674                        $sql = $wpdb->prepare('WHERE post_type = %s AND ', $post_type);
     3675                } else {
     3676                        $sql = $wpdb->prepare('WHERE post_author = %d AND post_type = %s AND ', $post_author, $post_type);
     3677                }
     3678        } else {
     3679                $sql = '';
     3680        }
    36583681
     3682        $sql .= "(post_status = 'publish'";
     3683
    36593684        if (current_user_can($cap)) {
    36603685                // Does the user have the capability to view private posts? Guess so.
    3661                 $sql .= ' OR post_status = \'private\'';
     3686                $sql .= " OR post_status = 'private'";
    36623687        } elseif (is_user_logged_in()) {
    36633688                // Users can view their own private posts.
    3664                 $sql .= ' OR post_status = \'private\' AND post_author = \'' . $user_ID . '\'';
    3665         }
     3689                $id = (int) $user_ID;
     3690                if (is_null($post_author) || !$full) {
     3691                        $sql .= " OR post_status = 'private' AND post_author = $id";
     3692                } elseif ($id == (int)$post_author) {
     3693                        $sql .= " OR post_status = 'private'";
     3694                } // else none
     3695        } // else none
    36663696
    36673697        $sql .= ')';
    36683698
     
    44544484
    44554485                add_filter('the_preview', '_set_preview');
    44564486        }
    4457 }
    4458  No newline at end of file
     4487}
  • wp-includes/user.php

     
    178178 */
    179179function get_usernumposts($userid) {
    180180        global $wpdb;
    181         $userid = (int) $userid;
    182         $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = %d AND post_type = 'post' AND ", $userid) . get_private_posts_cap_sql('post'));
     181
     182        $where = get_posts_by_author_sql('post', TRUE, $userid);
     183
     184        $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
     185
    183186        return apply_filters('get_usernumposts', $count, $userid);
    184187}
    185188
    186189/**
     190 * Number of posts written by a list of users.
     191 *
     192 * @since 3.0.0
     193 * @param array $userid User ID number list.
     194 * @return array Amount of posts each user has written.
     195 */
     196function get_users_numposts($users) {
     197        global $wpdb;
     198       
     199        if (0 == count($users))
     200                return array();
     201           
     202        $userlist = implode(',', $users);
     203        $where = get_posts_by_author_sql('post');
     204
     205        $result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N );
     206
     207        $count = array();
     208        foreach($result as $row) {
     209                $count[$row[0]] = $row[1];
     210        }
     211
     212        foreach($users as $id) {
     213                $id = (string) $id;
     214                if (!isset($count[$id]))
     215                        $count[$id] = 0;
     216        }
     217
     218        return $count;
     219}
     220
     221/**
    187222 * Check that the user login name and password is correct.
    188223 *
    189224 * @since 0.71
     
    440475        return true;
    441476}
    442477
     478/**
     479 * Count number of users who have each of the user roles.
     480 *
     481 * Assumes there are neither duplicated nor orphaned capabilities meta_values.
     482 * Assumes the serialized array values are meaningless, only array keys are used.
     483 * Intended scale for this version is 10^5 users.  It is not quite capable of that yet due to WP Bug #12257
     484 * Memory exhaustion is expected at higher orders.
     485 *
     486 * @since 3.0.0
     487 * @return array Includes a grand total and an array of counts indexed by role strings.
     488 */
     489function count_blog_users_by_role_inmem() {
     490        global $wpdb, $blog_id;
     491
     492        $id = (int) $blog_id;
     493        $blog_prefix = $wpdb->get_blog_prefix($id);
     494        $avail_roles = array();
     495
     496        $users_of_blog = $wpdb->get_col( "SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'" );
     497
     498        foreach ( $users_of_blog as $caps_meta ) {
     499                $b_roles = unserialize($caps_meta);
     500                if ( is_array($b_roles) ) {
     501                        foreach ( $b_roles as $b_role => $val ) {
     502                                if ( isset($avail_roles[$b_role]) )
     503                                        $avail_roles[$b_role]++;
     504                                else
     505                                        $avail_roles[$b_role] = 1;
     506                        }
     507                }
     508        }
     509
     510        $result = array();
     511        $result['total_users'] = count( $users_of_blog );
     512        $result['avail_roles'] =& $avail_roles;
     513       
     514        return $result;
     515}
     516
     517/**
     518 * Count number of users who have each of the user roles.
     519 *
     520 * Assumes there are neither duplicated nor orphaned capabilities meta_values.
     521 * Assumes role names are unique phrases.  Same assumption made by WP_User_Search::prepare_query()
     522 * Intended scale for this version is 10^7 users.
     523 * CPU exhaustion is expected at higher orders.
     524 *
     525 * @since 3.0.0
     526 * @return array Includes a grand total and an array of counts indexed by role strings.
     527 */
     528function count_blog_users_by_role_intime() {
     529        global $wpdb, $blog_id, $wp_roles;
     530
     531        // Initialize
     532        $id = (int) $blog_id;
     533        $blog_prefix = $wpdb->get_blog_prefix($id);
     534        $avail_roles = $wp_roles->get_names();
     535
     536        // Build a CPU-intensive query that will return concise information.
     537        $select_count = array();
     538        foreach ( $avail_roles as $this_role => $name ) {
     539                $select_count[] = "COUNT(NULLIF(`meta_value` LIKE '%" . like_escape($this_role) . "%', FALSE))";
     540        }
     541        $select_count = implode(', ', $select_count);
     542
     543        // Add the meta_value index to the selection list, then run the query.
     544        $row = $wpdb->get_row( "SELECT $select_count, COUNT(*) FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'", ARRAY_N );
     545
     546        // Run the previous loop again to associate results with role names.
     547        $col = 0;
     548        $role_counts = array();
     549        foreach ( $avail_roles as $this_role => $name ) {
     550                $count = (int) $row[$col++];
     551                if ($count > 0)
     552                        $role_counts[$this_role] = $count;
     553        }
     554
     555        // Get the meta_value index from the end of the result set.
     556        $total_users = (int) $row[$col];
     557
     558        $result = array();
     559        $result['total_users'] = $total_users;
     560        $result['avail_roles'] =& $role_counts;
     561
     562        return $result;
     563}
     564
    443565//
    444566// Private helper functions
    445567//