WordPress.org

Make WordPress Core

Changeset 40371


Ignore:
Timestamp:
04/03/2017 11:31:33 PM (15 months ago)
Author:
flixos90
Message:

Multisite: Add $network_id parameter to get_user_count().

The get_user_count() function returns the number of active users on a network, which is stored in a user_count network option. Since get_network_option() supports retrieving options from other networks than the current one, get_user_count() can now make use of that feature.

Fixes #37866.

Location:
trunk
Files:
2 edited

Legend:

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

    r40370 r40371  
    9797 *
    9898 * @since MU 2.7
    99  *
    100  * @return int
    101  */
    102 function get_user_count() {
    103     return get_site_option( 'user_count' );
     99 * @since 4.8.0 The $network_id parameter has been added.
     100 *
     101 * @param int|null $network_id ID of the network. Default is the current network.
     102 * @return int Number of active users on the network.
     103 */
     104function get_user_count( $network_id = null ) {
     105    return get_network_option( $network_id, 'user_count' );
    104106}
    105107
  • trunk/tests/phpunit/tests/multisite/network.php

    r40370 r40371  
    207207
    208208    /**
     209     * @ticket 37866
     210     */
     211    public function test_get_user_count_on_different_network() {
     212        global $current_site, $wpdb;
     213
     214        wp_update_network_user_counts();
     215        $current_network_user_count = get_user_count();
     216
     217        // switch_to_network()...
     218        $orig_network_id = $current_site->id;
     219        $orig_wpdb_network_id = $wpdb->siteid;
     220        $current_site->id = self::$different_network_id;
     221        $wpdb->siteid = self::$different_network_id;
     222
     223        // Add another user to fake the network user count to be different.
     224        wpmu_create_user( 'user', 'pass', 'email' );
     225
     226        wp_update_network_user_counts();
     227
     228        // restore_current_network()...
     229        $current_site->id = $orig_network_id;
     230        $wpdb->siteid = $orig_wpdb_network_id;
     231
     232        $user_count = get_user_count( self::$different_network_id );
     233
     234        $this->assertEquals( $current_network_user_count + 1, $user_count );
     235    }
     236
     237    /**
    209238     * @ticket 22917
    210239     */
Note: See TracChangeset for help on using the changeset viewer.