Users: Move get_user_count() and related functions to wp-includes/user.php.

The new location is next to the pre-existing `count_users()` function, along with other user-specific functions, and should be a more appropriate place in terms of consistency.

This affects:
* `get_user_count()`
* `wp_maybe_update_user_counts()`
* `wp_update_user_counts()`
* `wp_schedule_update_user_counts()`
* `wp_is_large_user_count()`

Follow-up to [53011], [53016].

See #38741.
Built from https://develop.svn.wordpress.org/trunk@53018


git-svn-id: http://core.svn.wordpress.org/trunk@52607 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2022-03-29 13:53:14 +00:00
parent cd8db5e05f
commit 8dc9fafcb6
3 changed files with 145 additions and 145 deletions

View File

@ -8418,147 +8418,3 @@ 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__,
sprintf(
/* translators: %s: $network_id */
__( 'Unable to pass %s if not using multisite.' ),
'<code>$network_id</code>'
),
'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__,
sprintf(
/* translators: %s: $network_id */
__( 'Unable to pass %s if not using multisite.' ),
'<code>$network_id</code>'
),
'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__,
sprintf(
/* translators: %s: $network_id */
__( 'Unable to pass %s if not using multisite.' ),
'<code>$network_id</code>'
),
'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__,
sprintf(
/* translators: %s: $network_id */
__( 'Unable to pass %s if not using multisite.' ),
'<code>$network_id</code>'
),
'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 );
}

View File

@ -1305,6 +1305,150 @@ function count_users( $strategy = 'time', $site_id = null ) {
return $result;
}
/**
* 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 Moved to wp-includes/user.php.
*
* @param int|null $network_id ID of the network. Defaults to 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__,
sprintf(
/* translators: %s: $network_id */
__( 'Unable to pass %s if not using multisite.' ),
'<code>$network_id</code>'
),
'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. Defaults to 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__,
sprintf(
/* translators: %s: $network_id */
__( 'Unable to pass %s if not using multisite.' ),
'<code>$network_id</code>'
),
'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. Defaults to 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__,
sprintf(
/* translators: %s: $network_id */
__( 'Unable to pass %s if not using multisite.' ),
'<code>$network_id</code>'
),
'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. Defaults to 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__,
sprintf(
/* translators: %s: $network_id */
__( 'Unable to pass %s if not using multisite.' ),
'<code>$network_id</code>'
),
'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 );
}
//
// Private helper functions.
//

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.0-alpha-53016';
$wp_version = '6.0-alpha-53018';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.