WordPress.org

Make WordPress Core

Ticket #11914: 11914.diff

File 11914.diff, 7.3 KB (added by miqrogroove, 11 years ago)
  • 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
    214217        $messages = array();
    215218        if ( isset($_GET['update']) ) :
    216219                switch($_GET['update']) {
     
    265268<?php
    266269$role_links = array();
    267270$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 }
     271
     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;
     
    372369        $role = array_shift($roles);
    373370
    374371        $style = ( ' class="alternate"' == $style ) ? '' : ' class="alternate"';
    375         echo "\n\t" . user_row($user_object, $style, $role);
     372        echo "\n\t", user_row($user_object, $style, $role, $post_counts[(string)$userid]);
    376373}
    377374?>
    378375</tbody>
  • wp-includes/user.php

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