Make WordPress Core

Changeset 53018


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.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

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

    r53016 r53018  
    84198419    return abs( (float) $expected - (float) $actual ) <= $precision;
    84208420}
    8421 
    8422 /**
    8423  * Returns the number of active users in your installation.
    8424  *
    8425  * Note that on a large site the count may be cached and only updated twice daily.
    8426  *
    8427  * @since MU (3.0.0)
    8428  * @since 4.8.0 The `$network_id` parameter has been added.
    8429  * @since 6.0.0 Move to wp-includes/functions.php.
    8430  *
    8431  * @param int|null $network_id ID of the network. Default is the current network.
    8432  * @return int Number of active users on the network.
    8433  */
    8434 function get_user_count( $network_id = null ) {
    8435     if ( ! is_multisite() && null !== $network_id ) {
    8436         _doing_it_wrong(
    8437             __FUNCTION__,
    8438             sprintf(
    8439                 /* translators: %s: $network_id */
    8440                 __( 'Unable to pass %s if not using multisite.' ),
    8441                 '<code>$network_id</code>'
    8442             ),
    8443             '6.0.0'
    8444         );
    8445     }
    8446 
    8447     return (int) get_network_option( $network_id, 'user_count', -1 );
    8448 }
    8449 
    8450 /**
    8451  * Updates the total count of users on the site if live user counting is enabled.
    8452  *
    8453  * @since 6.0.0
    8454  *
    8455  * @param int|null $network_id ID of the network. Default is the current network.
    8456  * @return bool Whether the update was successful.
    8457  */
    8458 function wp_maybe_update_user_counts( $network_id = null ) {
    8459     if ( ! is_multisite() && null !== $network_id ) {
    8460         _doing_it_wrong(
    8461             __FUNCTION__,
    8462             sprintf(
    8463                 /* translators: %s: $network_id */
    8464                 __( 'Unable to pass %s if not using multisite.' ),
    8465                 '<code>$network_id</code>'
    8466             ),
    8467             '6.0.0'
    8468         );
    8469     }
    8470 
    8471     $is_small_network = ! wp_is_large_user_count( $network_id );
    8472     /** This filter is documented in wp-includes/ms-functions.php */
    8473     if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'users' ) ) {
    8474         return false;
    8475     }
    8476 
    8477     return wp_update_user_counts( $network_id );
    8478 }
    8479 
    8480 /**
    8481  * Updates the total count of users on the site.
    8482  *
    8483  * @global wpdb $wpdb WordPress database abstraction object.
    8484  * @since 6.0.0
    8485  *
    8486  * @param int|null $network_id ID of the network. Default is the current network.
    8487  * @return bool Whether the update was successful.
    8488  */
    8489 function wp_update_user_counts( $network_id = null ) {
    8490     global $wpdb;
    8491 
    8492     if ( ! is_multisite() && null !== $network_id ) {
    8493         _doing_it_wrong(
    8494             __FUNCTION__,
    8495             sprintf(
    8496                 /* translators: %s: $network_id */
    8497                 __( 'Unable to pass %s if not using multisite.' ),
    8498                 '<code>$network_id</code>'
    8499             ),
    8500             '6.0.0'
    8501         );
    8502     }
    8503 
    8504     $query = "SELECT COUNT(ID) as c FROM $wpdb->users";
    8505     if ( is_multisite() ) {
    8506         $query .= " WHERE spam = '0' AND deleted = '0'";
    8507     }
    8508 
    8509     $count = $wpdb->get_var( $query );
    8510 
    8511     return update_network_option( $network_id, 'user_count', $count );
    8512 }
    8513 
    8514 /**
    8515  * Schedules a recurring recalculation of the total count of users.
    8516  *
    8517  * @since 6.0.0
    8518  */
    8519 function wp_schedule_update_user_counts() {
    8520     if ( ! is_main_site() ) {
    8521         return;
    8522     }
    8523 
    8524     if ( ! wp_next_scheduled( 'wp_update_user_counts' ) && ! wp_installing() ) {
    8525         wp_schedule_event( time(), 'twicedaily', 'wp_update_user_counts' );
    8526     }
    8527 }
    8528 
    8529 /**
    8530  * Determines whether the site has a large number of users.
    8531  *
    8532  * The default criteria for a large site is more than 10,000 users.
    8533  *
    8534  * @since 6.0.0
    8535  *
    8536  * @param int|null $network_id ID of the network. Default is the current network.
    8537  * @return bool Whether the site has a large number of users.
    8538  */
    8539 function wp_is_large_user_count( $network_id = null ) {
    8540     if ( ! is_multisite() && null !== $network_id ) {
    8541         _doing_it_wrong(
    8542             __FUNCTION__,
    8543             sprintf(
    8544                 /* translators: %s: $network_id */
    8545                 __( 'Unable to pass %s if not using multisite.' ),
    8546                 '<code>$network_id</code>'
    8547             ),
    8548             '6.0.0'
    8549         );
    8550     }
    8551 
    8552     $count = get_user_count( $network_id );
    8553 
    8554     /**
    8555      * Filters whether the site is considered large, based on its number of users.
    8556      *
    8557      * @since 6.0.0
    8558      *
    8559      * @param bool     $is_large_user_count Whether the site has a large number of users.
    8560      * @param int      $count               The total number of users.
    8561      * @param int|null $network_id          ID of the network. `null` represents the current network.
    8562      */
    8563     return apply_filters( 'wp_is_large_user_count', $count > 10000, $count, $network_id );
    8564 }
  • 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.