Make WordPress Core


Ignore:
Timestamp:
03/29/2022 01:51:52 PM (3 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user.php

    r52978 r53018  
    13041304
    13051305    return $result;
     1306}
     1307
     1308/**
     1309 * Returns the number of active users in your installation.
     1310 *
     1311 * Note that on a large site the count may be cached and only updated twice daily.
     1312 *
     1313 * @since MU (3.0.0)
     1314 * @since 4.8.0 The `$network_id` parameter has been added.
     1315 * @since 6.0.0 Moved to wp-includes/user.php.
     1316 *
     1317 * @param int|null $network_id ID of the network. Defaults to the current network.
     1318 * @return int Number of active users on the network.
     1319 */
     1320function get_user_count( $network_id = null ) {
     1321    if ( ! is_multisite() && null !== $network_id ) {
     1322        _doing_it_wrong(
     1323            __FUNCTION__,
     1324            sprintf(
     1325                /* translators: %s: $network_id */
     1326                __( 'Unable to pass %s if not using multisite.' ),
     1327                '<code>$network_id</code>'
     1328            ),
     1329            '6.0.0'
     1330        );
     1331    }
     1332
     1333    return (int) get_network_option( $network_id, 'user_count', -1 );
     1334}
     1335
     1336/**
     1337 * Updates the total count of users on the site if live user counting is enabled.
     1338 *
     1339 * @since 6.0.0
     1340 *
     1341 * @param int|null $network_id ID of the network. Defaults to the current network.
     1342 * @return bool Whether the update was successful.
     1343 */
     1344function wp_maybe_update_user_counts( $network_id = null ) {
     1345    if ( ! is_multisite() && null !== $network_id ) {
     1346        _doing_it_wrong(
     1347            __FUNCTION__,
     1348            sprintf(
     1349                /* translators: %s: $network_id */
     1350                __( 'Unable to pass %s if not using multisite.' ),
     1351                '<code>$network_id</code>'
     1352            ),
     1353            '6.0.0'
     1354        );
     1355    }
     1356
     1357    $is_small_network = ! wp_is_large_user_count( $network_id );
     1358    /** This filter is documented in wp-includes/ms-functions.php */
     1359    if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'users' ) ) {
     1360        return false;
     1361    }
     1362
     1363    return wp_update_user_counts( $network_id );
     1364}
     1365
     1366/**
     1367 * Updates the total count of users on the site.
     1368 *
     1369 * @global wpdb $wpdb WordPress database abstraction object.
     1370 * @since 6.0.0
     1371 *
     1372 * @param int|null $network_id ID of the network. Defaults to the current network.
     1373 * @return bool Whether the update was successful.
     1374 */
     1375function wp_update_user_counts( $network_id = null ) {
     1376    global $wpdb;
     1377
     1378    if ( ! is_multisite() && null !== $network_id ) {
     1379        _doing_it_wrong(
     1380            __FUNCTION__,
     1381            sprintf(
     1382                /* translators: %s: $network_id */
     1383                __( 'Unable to pass %s if not using multisite.' ),
     1384                '<code>$network_id</code>'
     1385            ),
     1386            '6.0.0'
     1387        );
     1388    }
     1389
     1390    $query = "SELECT COUNT(ID) as c FROM $wpdb->users";
     1391    if ( is_multisite() ) {
     1392        $query .= " WHERE spam = '0' AND deleted = '0'";
     1393    }
     1394
     1395    $count = $wpdb->get_var( $query );
     1396
     1397    return update_network_option( $network_id, 'user_count', $count );
     1398}
     1399
     1400/**
     1401 * Schedules a recurring recalculation of the total count of users.
     1402 *
     1403 * @since 6.0.0
     1404 */
     1405function wp_schedule_update_user_counts() {
     1406    if ( ! is_main_site() ) {
     1407        return;
     1408    }
     1409
     1410    if ( ! wp_next_scheduled( 'wp_update_user_counts' ) && ! wp_installing() ) {
     1411        wp_schedule_event( time(), 'twicedaily', 'wp_update_user_counts' );
     1412    }
     1413}
     1414
     1415/**
     1416 * Determines whether the site has a large number of users.
     1417 *
     1418 * The default criteria for a large site is more than 10,000 users.
     1419 *
     1420 * @since 6.0.0
     1421 *
     1422 * @param int|null $network_id ID of the network. Defaults to the current network.
     1423 * @return bool Whether the site has a large number of users.
     1424 */
     1425function wp_is_large_user_count( $network_id = null ) {
     1426    if ( ! is_multisite() && null !== $network_id ) {
     1427        _doing_it_wrong(
     1428            __FUNCTION__,
     1429            sprintf(
     1430                /* translators: %s: $network_id */
     1431                __( 'Unable to pass %s if not using multisite.' ),
     1432                '<code>$network_id</code>'
     1433            ),
     1434            '6.0.0'
     1435        );
     1436    }
     1437
     1438    $count = get_user_count( $network_id );
     1439
     1440    /**
     1441     * Filters whether the site is considered large, based on its number of users.
     1442     *
     1443     * @since 6.0.0
     1444     *
     1445     * @param bool     $is_large_user_count Whether the site has a large number of users.
     1446     * @param int      $count               The total number of users.
     1447     * @param int|null $network_id          ID of the network. `null` represents the current network.
     1448     */
     1449    return apply_filters( 'wp_is_large_user_count', $count > 10000, $count, $network_id );
    13061450}
    13071451
Note: See TracChangeset for help on using the changeset viewer.