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

@@ -0,0 +1,171 @@
<?php
/**
* @group user
* @covers ::get_user_count
*/
class Tests_User_GetUserCount extends WP_UnitTestCase {
/**
* @ticket 40386
* @group multisite
* @group ms-required
*/
public function test_wp_update_network_counts_on_different_network() {
$this->skipWithoutMultisite();
$different_network_id = self::factory()->network->create(
array(
'domain' => 'wordpress.org',
'path' => '/',
)
);
delete_network_option( $different_network_id, 'user_count' );
wp_update_network_counts( $different_network_id );
$user_count = get_user_count( $different_network_id );
$this->assertGreaterThan( 0, $user_count );
}
/**
* @ticket 37866
* @group multisite
* @group ms-required
*/
public function test_get_user_count_on_different_network() {
$this->skipWithoutMultisite();
$different_network_id = self::factory()->network->create(
array(
'domain' => 'wordpress.org',
'path' => '/',
)
);
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( $different_network_id );
$user_count = get_user_count( $different_network_id );
$this->assertSame( $current_network_user_count + 1, $user_count );
}
/**
* @ticket 22917
* @group multisite
* @group ms-required
*/
public function test_enable_live_network_user_counts_filter() {
$this->skipWithoutMultisite();
// 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' );
self::factory()->user->create( array( 'role' => 'administrator' ) );
$count = get_user_count();
$this->assertSame( $start_count + 1, $count );
}
/**
* @ticket 38741
*/
public function test_get_user_count_update() {
wp_update_user_counts();
$current_network_user_count = get_user_count();
self::factory()->user->create( array( 'role' => 'administrator' ) );
$user_count = get_user_count();
$this->assertSame( $current_network_user_count + 1, $user_count );
}
/**
* @group ms-excluded
* @ticket 38741
*/
public function test_get_user_count_update_on_delete() {
$this->skipWithMultisite();
wp_update_user_counts();
$current_network_user_count = get_user_count();
$u1 = self::factory()->user->create( array( 'role' => 'administrator' ) );
$user_count = get_user_count();
$this->assertSame( $current_network_user_count + 1, $user_count );
wp_delete_user( $u1 );
$user_count_after_delete = get_user_count();
$this->assertSame( $user_count - 1, $user_count_after_delete );
}
/**
* @group ms-required
* @ticket 38741
*/
public function test_get_user_count_update_on_delete_multisite() {
$this->skipWithoutMultisite();
wp_update_user_counts();
$current_network_user_count = get_user_count();
$u1 = wpmu_create_user( 'user', 'pass', 'email' );
$user_count = get_user_count();
$this->assertSame( $current_network_user_count + 1, $user_count );
wpmu_delete_user( $u1 );
$user_count_after_delete = get_user_count();
$this->assertSame( $user_count - 1, $user_count_after_delete );
}
/**
* @group multisite
* @group ms-required
* @ticket 38741
*/
public function test_get_user_count() {
$this->skipWithoutMultisite();
// 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->assertSame( $start_count + 1, $count );
}
}