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.



git-svn-id: https://develop.svn.wordpress.org/trunk@53011 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris
2022-03-29 12:41:00 +00:00
parent 556492ad88
commit e19b7ead2c
12 changed files with 363 additions and 132 deletions
@@ -177,28 +177,33 @@ class WP_Users_List_Table extends WP_List_Table {
$wp_roles = wp_roles();
$count_users = ! wp_is_large_user_count();
if ( $this->is_site_users ) {
$url = 'site-users.php?id=' . $this->site_id;
switch_to_blog( $this->site_id );
$users_of_blog = count_users( 'time', $this->site_id );
restore_current_blog();
} else {
$url = 'users.php';
$users_of_blog = count_users();
$url = 'users.php';
}
$total_users = $users_of_blog['total_users'];
$avail_roles =& $users_of_blog['avail_roles'];
unset( $users_of_blog );
$role_links = array();
$avail_roles = array();
$all_text = __( 'All' );
$current_link_attributes = empty( $role ) ? ' class="current" aria-current="page"' : '';
$role_links = array();
$role_links['all'] = sprintf(
'<a href="%s"%s>%s</a>',
$url,
$current_link_attributes,
sprintf(
if ( $count_users ) {
if ( $this->is_site_users ) {
switch_to_blog( $this->site_id );
$users_of_blog = count_users( 'time', $this->site_id );
restore_current_blog();
} else {
$users_of_blog = count_users();
}
$total_users = $users_of_blog['total_users'];
$avail_roles =& $users_of_blog['avail_roles'];
unset( $users_of_blog );
$all_text = sprintf(
/* translators: %s: Number of users. */
_nx(
'All <span class="count">(%s)</span>',
@@ -207,11 +212,13 @@ class WP_Users_List_Table extends WP_List_Table {
'users'
),
number_format_i18n( $total_users )
)
);
);
}
$role_links['all'] = sprintf( '<a href="%s"%s>%s</a>', $url, $current_link_attributes, $all_text );
foreach ( $wp_roles->get_names() as $this_role => $name ) {
if ( ! isset( $avail_roles[ $this_role ] ) ) {
if ( $count_users && ! isset( $avail_roles[ $this_role ] ) ) {
continue;
}
@@ -222,12 +229,14 @@ class WP_Users_List_Table extends WP_List_Table {
}
$name = translate_user_role( $name );
$name = sprintf(
/* translators: 1: User role name, 2: Number of users. */
__( '%1$s <span class="count">(%2$s)</span>' ),
$name,
number_format_i18n( $avail_roles[ $this_role ] )
);
if ( $count_users ) {
$name = sprintf(
/* translators: 1: User role name, 2: Number of users. */
__( '%1$s <span class="count">(%2$s)</span>' ),
$name,
number_format_i18n( $avail_roles[ $this_role ] )
);
}
$role_links[ $this_role ] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$current_link_attributes>$name</a>";
}