From d6297c051e82cf17d6e0eccd78a567fb97f6f9d4 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 20 Apr 2017 00:13:41 +0000 Subject: [PATCH] Multisite: Add `$network_id` parameter to `wp_update_network_user_counts()`. Using the new parameter, it is now possible to update the user count on a network different from the current one. While the count itself is technically a global user count and not network-wide, it is stored on each individual network, and the new parameter provides more control about where to update the count. Fixes #40349. See #38699. git-svn-id: https://develop.svn.wordpress.org/trunk@40485 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/ms-functions.php | 7 +++-- tests/phpunit/tests/multisite/network.php | 32 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/ms-functions.php b/src/wp-includes/ms-functions.php index b2cbd63931..e024789052 100644 --- a/src/wp-includes/ms-functions.php +++ b/src/wp-includes/ms-functions.php @@ -2354,14 +2354,17 @@ function wp_update_network_site_counts( $network_id = null ) { * Update the network-wide user count. * * @since 3.7.0 + * @since 4.8.0 The $network_id parameter has been added. * * @global wpdb $wpdb WordPress database abstraction object. + * + * @param int|null $network_id ID of the network. Default is the current network. */ -function wp_update_network_user_counts() { +function wp_update_network_user_counts( $network_id = null ) { global $wpdb; $count = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" ); - update_site_option( 'user_count', $count ); + update_network_option( $network_id, 'user_count', $count ); } /** diff --git a/tests/phpunit/tests/multisite/network.php b/tests/phpunit/tests/multisite/network.php index 1ace54e4f3..50c15d0393 100644 --- a/tests/phpunit/tests/multisite/network.php +++ b/tests/phpunit/tests/multisite/network.php @@ -408,6 +408,38 @@ class Tests_Multisite_Network extends WP_UnitTestCase { $result = get_blog_count( self::$different_network_id ); $this->assertEquals( 3, $result ); } + + /** + * @ticket 40349 + */ + public function test_wp_update_network_user_counts() { + global $wpdb; + + update_network_option( null, 'user_count', 40 ); + + $expected = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" ); + + wp_update_network_user_counts(); + + $result = get_user_count(); + $this->assertEquals( $expected, $result ); + } + + /** + * @ticket 40349 + */ + public function test_wp_update_network_user_counts_on_different_network() { + global $wpdb; + + update_network_option( self::$different_network_id, 'user_count', 40 ); + + $expected = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" ); + + wp_update_network_user_counts( self::$different_network_id ); + + $result = get_user_count( self::$different_network_id ); + $this->assertEquals( $expected, $result ); + } } endif;