Make WordPress Core


Ignore:
Timestamp:
09/27/2017 01:03:03 PM (7 years ago)
Author:
johnbillion
Message:

Users: Introduce the concept of a large site in order to speed up the Users screen when there are many users.

Calling the count_users() function is expensive, regardless of the counting strategy that's used, and it gets
slower the more users there are on a site. In order to speed up the Users screen in the admin area, calling
count_users() can be avoided entirely while still displaying the total count for users.

This introduces some new functions:

  • wp_is_large_user_count()
  • wp_get_active_user_count()
  • wp_update_active_user_count()

A corresponding wp_is_large_user_count filter is also introduced.

Props tharsheblows, johnbillion

Fixes #38741

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-users-list-table.php

    r41180 r41613  
    167167
    168168        $wp_roles = wp_roles();
     169        $count_users = true;
     170
     171        if ( wp_is_large_user_count() ) {
     172            $count_users = false;
     173        } elseif ( is_multisite() && wp_is_large_network( 'users' ) ) {
     174            $count_users = false;
     175        }
    169176
    170177        if ( $this->is_site_users ) {
    171178            $url = 'site-users.php?id=' . $this->site_id;
    172             switch_to_blog( $this->site_id );
    173             $users_of_blog = count_users( 'time', $this->site_id );
    174             restore_current_blog();
     179            if ( $count_users ) {
     180                switch_to_blog( $this->site_id );
     181                $users_of_blog = count_users( 'time', $this->site_id );
     182                restore_current_blog();
     183            }
    175184        } else {
    176185            $url = 'users.php';
    177             $users_of_blog = count_users();
    178         }
    179 
    180         $total_users = $users_of_blog['total_users'];
    181         $avail_roles =& $users_of_blog['avail_roles'];
    182         unset($users_of_blog);
     186            if ( $count_users ) {
     187                $users_of_blog = count_users();
     188            }
     189        }
     190
     191        if ( $count_users ) {
     192            $total_users = $users_of_blog['total_users'];
     193            $avail_roles =& $users_of_blog['avail_roles'];
     194            unset($users_of_blog);
     195        } else {
     196            $avail_roles = array();
     197        }
    183198
    184199        $class = empty($role) ? ' class="current"' : '';
    185200        $role_links = array();
    186         $role_links['all'] = "<a href='$url'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_users, 'users' ), number_format_i18n( $total_users ) ) . '</a>';
     201
     202        if ( $count_users ) {
     203            $role_links['all'] = "<a href='$url'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_users, 'users' ), number_format_i18n( $total_users ) ) . '</a>';
     204        } else {
     205            $role_links['all'] = "<a href='$url'$class>" . _x( 'All', 'users' ) . '</a>';
     206        }
    187207        foreach ( $wp_roles->get_names() as $this_role => $name ) {
    188             if ( !isset($avail_roles[$this_role]) )
     208            if ( $count_users && !isset($avail_roles[$this_role]) ) {
    189209                continue;
     210            }
    190211
    191212            $class = '';
     
    196217
    197218            $name = translate_user_role( $name );
    198             /* translators: User role name with count */
    199             $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles[$this_role] ) );
     219            if ( $count_users ) {
     220                /* translators: User role name with count */
     221                $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles[$this_role] ) );
     222            }
    200223            $role_links[$this_role] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$class>$name</a>";
    201224        }
    202225
    203         if ( ! empty( $avail_roles['none' ] ) ) {
     226        if ( ! $count_users || ! empty( $avail_roles['none' ] ) ) {
    204227
    205228            $class = '';
     
    210233
    211234            $name = __( 'No role' );
    212             /* translators: User role name with count */
    213             $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles['none' ] ) );
     235            if ( $count_users ) {
     236                /* translators: User role name with count */
     237                $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles['none' ] ) );
     238            }
    214239            $role_links['none'] = "<a href='" . esc_url( add_query_arg( 'role', 'none', $url ) ) . "'$class>$name</a>";
    215240
Note: See TracChangeset for help on using the changeset viewer.