Make WordPress Core


Ignore:
Timestamp:
03/29/2022 12:41:00 PM (18 months ago)
Author:
spacedmonkey
Message:

Users: Introduce the concept of a large site to single site installations.

Currently in WordPress multisite there is a concept of large networks. The function wp_is_large_network is used to determine if a network has a large number of sites or users. If a network is marked as large, then
expensive queries to calculate user counts are not run on page load but deferred to scheduled events. However there are a number of places in a single site installation where this functionality would also be useful, as
expensive calls to count users and roles can make screens in the admin extremely slow.

In this change, the get_user_count function and related functionality around it is ported to be available in a single site context. This means that expensive calls to the count_users function are replaced with
calls to get_user_count. This change also includes a new function called wp_is_large_user_count and a filter of the same name, to mark if a site is large.

Props johnbillion, Spacedmonkey, Mista-Flo, lumpysimon, tharsheblows, obenland, miss_jwo, jrchamp, flixos90, macbookandrew, pento, desrosj, johnjamesjacoby, jb510, davidbaumwald, costdev.
Fixes #38741.

File:
1 edited

Legend:

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

    r52581 r53011  
    178178        $wp_roles = wp_roles();
    179179
     180        $count_users = ! wp_is_large_user_count();
     181
    180182        if ( $this->is_site_users ) {
    181183            $url = 'site-users.php?id=' . $this->site_id;
    182             switch_to_blog( $this->site_id );
    183             $users_of_blog = count_users( 'time', $this->site_id );
    184             restore_current_blog();
    185184        } else {
    186             $url           = 'users.php';
    187             $users_of_blog = count_users();
    188         }
    189 
    190         $total_users = $users_of_blog['total_users'];
    191         $avail_roles =& $users_of_blog['avail_roles'];
    192         unset( $users_of_blog );
    193 
     185            $url = 'users.php';
     186        }
     187
     188        $role_links              = array();
     189        $avail_roles             = array();
     190        $all_text                = __( 'All' );
    194191        $current_link_attributes = empty( $role ) ? ' class="current" aria-current="page"' : '';
    195192
    196         $role_links        = array();
    197         $role_links['all'] = sprintf(
    198             '<a href="%s"%s>%s</a>',
    199             $url,
    200             $current_link_attributes,
    201             sprintf(
     193        if ( $count_users ) {
     194            if ( $this->is_site_users ) {
     195                switch_to_blog( $this->site_id );
     196                $users_of_blog = count_users( 'time', $this->site_id );
     197                restore_current_blog();
     198            } else {
     199                $users_of_blog = count_users();
     200            }
     201
     202            $total_users = $users_of_blog['total_users'];
     203            $avail_roles =& $users_of_blog['avail_roles'];
     204            unset( $users_of_blog );
     205
     206            $all_text = sprintf(
    202207                /* translators: %s: Number of users. */
    203208                _nx(
     
    208213                ),
    209214                number_format_i18n( $total_users )
    210             )
    211         );
     215            );
     216        }
     217
     218        $role_links['all'] = sprintf( '<a href="%s"%s>%s</a>', $url, $current_link_attributes, $all_text );
    212219
    213220        foreach ( $wp_roles->get_names() as $this_role => $name ) {
    214             if ( ! isset( $avail_roles[ $this_role ] ) ) {
     221            if ( $count_users && ! isset( $avail_roles[ $this_role ] ) ) {
    215222                continue;
    216223            }
     
    223230
    224231            $name = translate_user_role( $name );
    225             $name = sprintf(
    226                 /* translators: 1: User role name, 2: Number of users. */
    227                 __( '%1$s <span class="count">(%2$s)</span>' ),
    228                 $name,
    229                 number_format_i18n( $avail_roles[ $this_role ] )
    230             );
     232            if ( $count_users ) {
     233                $name = sprintf(
     234                    /* translators: 1: User role name, 2: Number of users. */
     235                    __( '%1$s <span class="count">(%2$s)</span>' ),
     236                    $name,
     237                    number_format_i18n( $avail_roles[ $this_role ] )
     238                );
     239            }
    231240
    232241            $role_links[ $this_role ] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$current_link_attributes>$name</a>";
Note: See TracChangeset for help on using the changeset viewer.