Make WordPress Core


Ignore:
Timestamp:
09/27/2017 01:03:03 PM (9 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/src/wp-includes/functions.php

    r41388 r41613  
    58345834    ), $email_change_email['message'], $email_change_email['headers'] );
    58355835}
     5836
     5837/**
     5838 * Whether or not we have a large site, based on its number of users.
     5839 *
     5840 * The default criteria for a large site is more than 10,000 users.
     5841 *
     5842 * @since 4.9.0
     5843 *
     5844 * @return bool True if the site meets the criteria for large. False otherwise.
     5845 */
     5846function wp_is_large_user_count() {
     5847    $count = wp_get_active_user_count();
     5848
     5849    /**
     5850     * Filters whether the site is considered large, based on its number of users.
     5851     *
     5852     * The default criteria for a large site is more than 10,000 users.
     5853     *
     5854     * @since 4.9.0
     5855     *
     5856     * @param bool $is_large_user_count Whether the site is considered large.
     5857     * @param int  $count               The number of users on the site.
     5858     */
     5859    return apply_filters( 'wp_is_large_user_count', $count > 10000, $count );
     5860}
     5861
     5862/**
     5863 * Update the active user count.
     5864 *
     5865 * @since 4.9.0
     5866 * @global wpdb $wpdb WordPress database abstraction object.
     5867 *
     5868 * @return int The active user count.
     5869 */
     5870function wp_update_active_user_count() {
     5871    global $wpdb;
     5872
     5873    $count = $wpdb->get_var( "
     5874        SELECT COUNT(ID) as c
     5875        FROM {$wpdb->users}
     5876    " );
     5877    $count=12345;
     5878    update_option( 'active_user_count', $count );
     5879
     5880    return (int) $count;
     5881}
     5882
     5883/**
     5884 * The number of active users.
     5885 *
     5886 * @since 4.9.0
     5887 *
     5888 * @return int The active user count.
     5889 */
     5890function wp_get_active_user_count() {
     5891    $count = get_option( 'active_user_count', false );
     5892
     5893    if ( false === $count ) {
     5894        $count = wp_update_active_user_count();
     5895    }
     5896    return (int) $count;
     5897}
Note: See TracChangeset for help on using the changeset viewer.