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/src/wp-includes/ms-functions.php

    r52737 r53011  
    9999        return $primary;
    100100    }
    101 }
    102 
    103 /**
    104  * The number of active users in your installation.
    105  *
    106  * The count is cached and updated twice daily. This is not a live count.
    107  *
    108  * @since MU (3.0.0)
    109  * @since 4.8.0 The `$network_id` parameter has been added.
    110  *
    111  * @param int|null $network_id ID of the network. Default is the current network.
    112  * @return int Number of active users on the network.
    113  */
    114 function get_user_count( $network_id = null ) {
    115     return get_network_option( $network_id, 'user_count' );
    116101}
    117102
     
    26122597 * @since 3.7.0
    26132598 * @since 4.8.0 The `$network_id` parameter has been added.
    2614  *
    2615  * @global wpdb $wpdb WordPress database abstraction object.
     2599 * @since 6.0.0 This function is now a wrapper for wp_update_user_counts().
    26162600 *
    26172601 * @param int|null $network_id ID of the network. Default is the current network.
    26182602 */
    26192603function wp_update_network_user_counts( $network_id = null ) {
    2620     global $wpdb;
    2621 
    2622     $count = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
    2623     update_network_option( $network_id, 'user_count', $count );
     2604    wp_update_user_counts( $network_id );
    26242605}
    26252606
     
    27552736    if ( 'users' === $using ) {
    27562737        $count = get_user_count( $network_id );
     2738
     2739        $is_large_network = wp_is_large_user_count( $network_id );
     2740
    27572741        /**
    27582742         * Filters whether the network is considered large.
     
    27662750         * @param int    $network_id       The ID of the network being checked.
    27672751         */
    2768         return apply_filters( 'wp_is_large_network', $count > 10000, 'users', $count, $network_id );
     2752        return apply_filters( 'wp_is_large_network', $is_large_network, 'users', $count, $network_id );
    27692753    }
    27702754
Note: See TracChangeset for help on using the changeset viewer.