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
This commit is contained in:
Felix Arntz 2017-04-20 00:13:41 +00:00
parent 9bf39fa0fc
commit d6297c051e
2 changed files with 37 additions and 2 deletions

View File

@ -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 );
}
/**

View File

@ -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;