Make WordPress Core


Ignore:
Timestamp:
03/29/2022 12:41:00 PM (2 years 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/tests/phpunit/tests/multisite/network.php

    r52010 r53011  
    206206        }
    207207
    208         /**
    209          * @ticket 37866
    210          */
    211         public function test_get_user_count_on_different_network() {
    212             wp_update_network_user_counts();
    213             $current_network_user_count = get_user_count();
    214 
    215             // Add another user to fake the network user count to be different.
    216             wpmu_create_user( 'user', 'pass', 'email' );
    217 
    218             wp_update_network_user_counts( self::$different_network_id );
    219 
    220             $user_count = get_user_count( self::$different_network_id );
    221 
    222             $this->assertEquals( $current_network_user_count + 1, $user_count );
    223         }
    224 
    225         /**
    226          * @ticket 22917
    227          */
    228         public function test_enable_live_network_user_counts_filter() {
    229             // False for large networks by default.
    230             add_filter( 'enable_live_network_counts', '__return_false' );
    231 
    232             // Refresh the cache.
    233             wp_update_network_counts();
    234             $start_count = get_user_count();
    235 
    236             wpmu_create_user( 'user', 'pass', 'email' );
    237 
    238             // No change, cache not refreshed.
    239             $count = get_user_count();
    240 
    241             $this->assertSame( $start_count, $count );
    242 
    243             wp_update_network_counts();
    244             $start_count = get_user_count();
    245 
    246             add_filter( 'enable_live_network_counts', '__return_true' );
    247 
    248             wpmu_create_user( 'user2', 'pass2', 'email2' );
    249 
    250             $count = get_user_count();
    251             $this->assertEquals( $start_count + 1, $count );
    252 
    253             remove_filter( 'enable_live_network_counts', '__return_false' );
    254             remove_filter( 'enable_live_network_counts', '__return_true' );
    255         }
     208
    256209
    257210        public function test_active_network_plugins() {
     
    318271        public function helper_deactivate_hook() {
    319272            $this->plugin_hook_count++;
    320         }
    321 
    322         public function test_get_user_count() {
    323             // Refresh the cache.
    324             wp_update_network_counts();
    325             $start_count = get_user_count();
    326 
    327             // Only false for large networks as of 3.7.
    328             add_filter( 'enable_live_network_counts', '__return_false' );
    329             self::factory()->user->create( array( 'role' => 'administrator' ) );
    330 
    331             $count = get_user_count(); // No change, cache not refreshed.
    332             $this->assertSame( $start_count, $count );
    333 
    334             wp_update_network_counts(); // Magic happens here.
    335 
    336             $count = get_user_count();
    337             $this->assertEquals( $start_count + 1, $count );
    338             remove_filter( 'enable_live_network_counts', '__return_false' );
    339273        }
    340274
     
    408342            update_network_option( null, 'user_count', 40 );
    409343
    410             $expected = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
     344            $expected = (int) $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
    411345
    412346            wp_update_network_user_counts();
     
    424358            update_network_option( self::$different_network_id, 'user_count', 40 );
    425359
    426             $expected = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
     360            $expected = (int) $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
    427361
    428362            wp_update_network_user_counts( self::$different_network_id );
Note: See TracChangeset for help on using the changeset viewer.