diff --git a/src/wp-includes/ms-functions.php b/src/wp-includes/ms-functions.php index d1536eb030..a562cdf241 100644 --- a/src/wp-includes/ms-functions.php +++ b/src/wp-includes/ms-functions.php @@ -96,11 +96,13 @@ function get_active_blog_for_user( $user_id ) { * The count is cached and updated twice daily. This is not a live count. * * @since MU 2.7 + * @since 4.8.0 The $network_id parameter has been added. * - * @return int + * @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() { - return get_site_option( 'user_count' ); +function get_user_count( $network_id = null ) { + return get_network_option( $network_id, 'user_count' ); } /** diff --git a/tests/phpunit/tests/multisite/network.php b/tests/phpunit/tests/multisite/network.php index 6a94dafe44..ffd976e10c 100644 --- a/tests/phpunit/tests/multisite/network.php +++ b/tests/phpunit/tests/multisite/network.php @@ -205,6 +205,35 @@ class Tests_Multisite_Network extends WP_UnitTestCase { $this->assertSame( count( self::$different_site_ids ), $site_count ); } + /** + * @ticket 37866 + */ + public function test_get_user_count_on_different_network() { + global $current_site, $wpdb; + + wp_update_network_user_counts(); + $current_network_user_count = get_user_count(); + + // switch_to_network()... + $orig_network_id = $current_site->id; + $orig_wpdb_network_id = $wpdb->siteid; + $current_site->id = self::$different_network_id; + $wpdb->siteid = self::$different_network_id; + + // Add another user to fake the network user count to be different. + wpmu_create_user( 'user', 'pass', 'email' ); + + wp_update_network_user_counts(); + + // restore_current_network()... + $current_site->id = $orig_network_id; + $wpdb->siteid = $orig_wpdb_network_id; + + $user_count = get_user_count( self::$different_network_id ); + + $this->assertEquals( $current_network_user_count + 1, $user_count ); + } + /** * @ticket 22917 */