Make WordPress Core


Ignore:
Timestamp:
09/27/2017 01:03:03 PM (8 years ago)
Author:
johnbillion
Message:

Users: Introduce the concept of a large site in order to speed up the Users screen when there are many users.

Calling the count_users() function is expensive, regardless of the counting strategy that's used, and it gets
slower the more users there are on a site. In order to speed up the Users screen in the admin area, calling
count_users() can be avoided entirely while still displaying the total count for users.

This introduces some new functions:

  • wp_is_large_user_count()
  • wp_get_active_user_count()
  • wp_update_active_user_count()

A corresponding wp_is_large_user_count filter is also introduced.

Props tharsheblows, johnbillion

Fixes #38741

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/functions.php

    r41388 r41613  
    11611161        return $data;
    11621162    }
     1163
     1164    /**
     1165     * @ticket 38741
     1166     */
     1167    public function test_wp_get_active_user_count() {
     1168        $user_count = wp_get_active_user_count();
     1169        $this->assertSame( $user_count, 1 );
     1170
     1171        // make twenty additional users to make 21 users
     1172        self::factory()->user->create_many( 20 );
     1173
     1174        $user_count = wp_get_active_user_count();
     1175        $this->assertSame( $user_count, 21 );
     1176    }
     1177
     1178    /**
     1179     * @ticket 38741
     1180     */
     1181    public function test_wp_get_active_user_count_caching() {
     1182        global $wpdb;
     1183
     1184        // Ensure we start with 1 user
     1185        $user_count = wp_get_active_user_count();
     1186        $this->assertSame( $user_count, 1 );
     1187
     1188        self::factory()->user->create();
     1189
     1190        delete_option( 'active_user_count' );
     1191
     1192        // Ensure we now have 2 users
     1193        $user_count = wp_get_active_user_count();
     1194        $this->assertSame( $user_count, 2 );
     1195
     1196        $queries = $wpdb->num_queries;
     1197
     1198        // Ensure that caching inside wp_get_active_user_count() works as expected
     1199        $user_count = wp_get_active_user_count();
     1200        $this->assertSame( $user_count, 2 );
     1201        $this->assertSame( $queries, $wpdb->num_queries );
     1202    }
     1203
     1204    /**
     1205     * @ticket 38741
     1206     */
     1207    public function test_wp_is_large_user_count() {
     1208        // set the 'active_user_count' value to over 10000 to emulate a large site
     1209        update_option( 'active_user_count', 10001 );
     1210        $user_count = wp_get_active_user_count();
     1211        $this->assertSame( $user_count, 10001 );
     1212        $this->assertTrue( wp_is_large_user_count() );
     1213
     1214        delete_option( 'active_user_count' );
     1215        $this->assertFalse( wp_is_large_user_count() );
     1216    }
    11631217}
Note: See TracChangeset for help on using the changeset viewer.