From 117f2da3ccff2be70178afc5bb63d533166ae268 Mon Sep 17 00:00:00 2001 From: flixos90 Date: Mon, 3 Apr 2017 23:13:40 +0000 Subject: [PATCH] Multisite: Support the `$network_id` parameter of `get_blog_count()`. The `get_blog_count()` function used to support an `$id` parameter for the network ID prior to WordPress 3.1. This parameter has not been used since the introduction of `get_site_option()` and was later deprecated in [25113]. With `get_network_option()` however it is possible to support the parameter again, now properly renamed as `$network_id`. A unit test has for the parameter has been added as well. Another unit test in the same class was adjusted to work properly with multiple networks existing. Fixes #37865. git-svn-id: https://develop.svn.wordpress.org/trunk@40370 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/ms-functions.php | 13 +++--- tests/phpunit/tests/multisite/network.php | 57 +++++++++++++++++++++-- 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/src/wp-includes/ms-functions.php b/src/wp-includes/ms-functions.php index d1ebe8d47f..d1536eb030 100644 --- a/src/wp-includes/ms-functions.php +++ b/src/wp-includes/ms-functions.php @@ -109,15 +109,14 @@ function get_user_count() { * The count is cached and updated twice daily. This is not a live count. * * @since MU 1.0 + * @since 3.7.0 The $network_id parameter has been deprecated. + * @since 4.8.0 The $network_id parameter is now being used. * - * @param int $network_id Deprecated, not supported. - * @return int + * @param int|null $network_id ID of the network. Default is the current network. + * @return int Number of active sites on the network. */ -function get_blog_count( $network_id = 0 ) { - if ( func_num_args() ) - _deprecated_argument( __FUNCTION__, '3.1.0' ); - - return get_site_option( 'blog_count' ); +function get_blog_count( $network_id = null ) { + return get_network_option( $network_id, 'blog_count' ); } /** diff --git a/tests/phpunit/tests/multisite/network.php b/tests/phpunit/tests/multisite/network.php index 981ed90f1f..6a94dafe44 100644 --- a/tests/phpunit/tests/multisite/network.php +++ b/tests/phpunit/tests/multisite/network.php @@ -12,6 +12,9 @@ class Tests_Multisite_Network extends WP_UnitTestCase { protected $plugin_hook_count = 0; protected $suppress = false; + protected static $different_network_id; + protected static $different_site_ids = array(); + function setUp() { global $wpdb; parent::setUp(); @@ -25,6 +28,33 @@ class Tests_Multisite_Network extends WP_UnitTestCase { parent::tearDown(); } + public static function wpSetUpBeforeClass( $factory ) { + self::$different_network_id = $factory->network->create( array( 'domain' => 'wordpress.org', 'path' => '/' ) ); + + $sites = array( + array( 'domain' => 'wordpress.org', 'path' => '/', 'site_id' => self::$different_network_id ), + array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'site_id' => self::$different_network_id ), + array( 'domain' => 'wordpress.org', 'path' => '/bar/', 'site_id' => self::$different_network_id ), + ); + + foreach ( $sites as $site ) { + self::$different_site_ids[] = $factory->blog->create( $site ); + } + } + + public static function wpTearDownAfterClass() { + global $wpdb; + + foreach( self::$different_site_ids as $id ) { + wpmu_delete_blog( $id, true ); + } + + $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", self::$different_network_id ) ); + $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", self::$different_network_id ) ); + + wp_update_network_site_counts(); + } + /** * By default, only one network exists and has a network ID of 1. */ @@ -66,15 +96,14 @@ class Tests_Multisite_Network extends WP_UnitTestCase { function test_get_main_network_id_after_network_delete() { global $wpdb, $current_site; - $id = self::factory()->network->create(); - $temp_id = $id + 1; + $temp_id = self::$different_network_id + 1; - $current_site->id = (int) $id; + $current_site->id = (int) self::$different_network_id; $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->site} SET id=%d WHERE id=1", $temp_id ) ); $main_network_id = get_main_network_id(); $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->site} SET id=1 WHERE id=%d", $temp_id ) ); - $this->assertEquals( $id, $main_network_id ); + $this->assertEquals( self::$different_network_id, $main_network_id ); } function test_get_main_network_id_filtered() { @@ -156,6 +185,26 @@ class Tests_Multisite_Network extends WP_UnitTestCase { $this->assertEquals( $site_count_start + 1, $actual ); } + /** + * @ticket 37865 + */ + public function test_get_blog_count_on_different_network() { + global $current_site, $wpdb; + + // 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; + wp_update_network_site_counts(); + $current_site->id = $orig_network_id; + $wpdb->siteid = $orig_wpdb_network_id; + + $site_count = get_blog_count( self::$different_network_id ); + + $this->assertSame( count( self::$different_site_ids ), $site_count ); + } + /** * @ticket 22917 */