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

View File

@@ -205,54 +205,7 @@ if ( is_multisite() ) :
$this->assertEquals( count( self::$different_site_ids ), $site_count );
}
/**
* @ticket 37866
*/
public function test_get_user_count_on_different_network() {
wp_update_network_user_counts();
$current_network_user_count = get_user_count();
// Add another user to fake the network user count to be different.
wpmu_create_user( 'user', 'pass', 'email' );
wp_update_network_user_counts( self::$different_network_id );
$user_count = get_user_count( self::$different_network_id );
$this->assertEquals( $current_network_user_count + 1, $user_count );
}
/**
* @ticket 22917
*/
public function test_enable_live_network_user_counts_filter() {
// False for large networks by default.
add_filter( 'enable_live_network_counts', '__return_false' );
// Refresh the cache.
wp_update_network_counts();
$start_count = get_user_count();
wpmu_create_user( 'user', 'pass', 'email' );
// No change, cache not refreshed.
$count = get_user_count();
$this->assertSame( $start_count, $count );
wp_update_network_counts();
$start_count = get_user_count();
add_filter( 'enable_live_network_counts', '__return_true' );
wpmu_create_user( 'user2', 'pass2', 'email2' );
$count = get_user_count();
$this->assertEquals( $start_count + 1, $count );
remove_filter( 'enable_live_network_counts', '__return_false' );
remove_filter( 'enable_live_network_counts', '__return_true' );
}
public function test_active_network_plugins() {
$path = 'hello.php';
@@ -319,25 +272,6 @@ if ( is_multisite() ) :
$this->plugin_hook_count++;
}
public function test_get_user_count() {
// Refresh the cache.
wp_update_network_counts();
$start_count = get_user_count();
// Only false for large networks as of 3.7.
add_filter( 'enable_live_network_counts', '__return_false' );
self::factory()->user->create( array( 'role' => 'administrator' ) );
$count = get_user_count(); // No change, cache not refreshed.
$this->assertSame( $start_count, $count );
wp_update_network_counts(); // Magic happens here.
$count = get_user_count();
$this->assertEquals( $start_count + 1, $count );
remove_filter( 'enable_live_network_counts', '__return_false' );
}
public function test_wp_schedule_update_network_counts() {
$this->assertFalse( wp_next_scheduled( 'update_network_counts' ) );
@@ -407,7 +341,7 @@ if ( is_multisite() ) :
update_network_option( null, 'user_count', 40 );
$expected = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
$expected = (int) $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
wp_update_network_user_counts();
@@ -423,7 +357,7 @@ if ( is_multisite() ) :
update_network_option( self::$different_network_id, 'user_count', 40 );
$expected = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
$expected = (int) $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
wp_update_network_user_counts( self::$different_network_id );