mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 14:20:15 +00:00
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:
@@ -8418,3 +8418,113 @@ function is_php_version_compatible( $required ) {
|
||||
function wp_fuzzy_number_match( $expected, $actual, $precision = 1 ) {
|
||||
return abs( (float) $expected - (float) $actual ) <= $precision;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of active users in your installation.
|
||||
*
|
||||
* Note that on a large site the count may be cached and only updated twice daily.
|
||||
*
|
||||
* @since MU (3.0.0)
|
||||
* @since 4.8.0 The `$network_id` parameter has been added.
|
||||
* @since 6.0.0 Move to wp-includes/functions.php.
|
||||
*
|
||||
* @param int|null $network_id ID of the network. Default is the current network.
|
||||
* @return int Number of active users on the network.
|
||||
*/
|
||||
function get_user_count( $network_id = null ) {
|
||||
if ( ! is_multisite() && null !== $network_id ) {
|
||||
_doing_it_wrong( __FUNCTION__, __( 'Unable to pass $network_id if not using multisite.' ), '6.0.0' );
|
||||
}
|
||||
return (int) get_network_option( $network_id, 'user_count', -1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the total count of users on the site if live user counting is enabled.
|
||||
*
|
||||
* @since 6.0.0
|
||||
*
|
||||
* @param int|null $network_id ID of the network. Default is the current network.
|
||||
* @return bool Whether the update was successful.
|
||||
*/
|
||||
function wp_maybe_update_user_counts( $network_id = null ) {
|
||||
if ( ! is_multisite() && null !== $network_id ) {
|
||||
_doing_it_wrong( __FUNCTION__, __( 'Unable to pass $network_id if not using multisite.' ), '6.0.0' );
|
||||
}
|
||||
|
||||
$is_small_network = ! wp_is_large_user_count( $network_id );
|
||||
/** This filter is documented in wp-includes/ms-functions.php */
|
||||
if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'users' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return wp_update_user_counts( $network_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the total count of users on the site.
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
* @since 6.0.0
|
||||
*
|
||||
* @param int|null $network_id ID of the network. Default is the current network.
|
||||
* @return bool Whether the update was successful.
|
||||
*/
|
||||
function wp_update_user_counts( $network_id = null ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( ! is_multisite() && null !== $network_id ) {
|
||||
_doing_it_wrong( __FUNCTION__, __( 'Unable to pass $network_id if not using multisite.' ), '6.0.0' );
|
||||
}
|
||||
|
||||
$query = "SELECT COUNT(ID) as c FROM $wpdb->users";
|
||||
if ( is_multisite() ) {
|
||||
$query .= " WHERE spam = '0' AND deleted = '0'";
|
||||
}
|
||||
|
||||
$count = $wpdb->get_var( $query );
|
||||
|
||||
return update_network_option( $network_id, 'user_count', $count );
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules a recurring recalculation of the total count of users.
|
||||
*
|
||||
* @since 6.0.0
|
||||
*/
|
||||
function wp_schedule_update_user_counts() {
|
||||
if ( ! is_main_site() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! wp_next_scheduled( 'wp_update_user_counts' ) && ! wp_installing() ) {
|
||||
wp_schedule_event( time(), 'twicedaily', 'wp_update_user_counts' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether the site has a large number of users.
|
||||
*
|
||||
* The default criteria for a large site is more than 10,000 users.
|
||||
*
|
||||
* @since 6.0.0
|
||||
*
|
||||
* @param int|null $network_id ID of the network. Default is the current network.
|
||||
* @return bool Whether the site has a large number of users.
|
||||
*/
|
||||
function wp_is_large_user_count( $network_id = null ) {
|
||||
if ( ! is_multisite() && null !== $network_id ) {
|
||||
_doing_it_wrong( __FUNCTION__, __( 'Unable to pass $network_id if not using multisite.' ), '6.0.0' );
|
||||
}
|
||||
$count = get_user_count( $network_id );
|
||||
|
||||
/**
|
||||
* Filters whether the site is considered large, based on its number of users.
|
||||
*
|
||||
* @since 6.0.0
|
||||
*
|
||||
* @param bool $is_large_user_count Whether the site has a large number of users.
|
||||
* @param int $count The total number of users.
|
||||
* @param int|null $network_id ID of the network. `null` represents the current network.
|
||||
*/
|
||||
return apply_filters( 'wp_is_large_user_count', $count > 10000, $count, $network_id );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user