From 43396e2b42e638d179c025902462aef03a3bd5b9 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Mon, 30 Oct 2017 19:55:09 +0000 Subject: [PATCH] Multisite: Pass network object instead of ID to `pre_get_main_site_id`. In the original function introduced in [41380], and subsequently [41861], only the network ID was passed to the new `pre_get_main_site_id` filter. In order to make give quick access to other network properties, it was decided to pass the whole object instead. The changeset includes an additional test. Fixes #29684. git-svn-id: https://develop.svn.wordpress.org/trunk@42043 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-network.php | 6 +++--- .../phpunit/tests/multisite/getMainSiteId.php | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-network.php b/src/wp-includes/class-wp-network.php index 5c326dd740..cc96f56ea4 100644 --- a/src/wp-includes/class-wp-network.php +++ b/src/wp-includes/class-wp-network.php @@ -219,10 +219,10 @@ class WP_Network { * * @since 4.9.0 * - * @param int|null $main_site_id If a positive integer is returned, it is interpreted as the main site ID. - * @param int $network_id The ID of the network for which the main site was detected. + * @param int|null $main_site_id If a positive integer is returned, it is interpreted as the main site ID. + * @param WP_Network $network The network object for which the main site was detected. */ - $main_site_id = (int) apply_filters( 'pre_get_main_site_id', null, $this->id ); + $main_site_id = (int) apply_filters( 'pre_get_main_site_id', null, $this ); if ( 0 < $main_site_id ) { return $main_site_id; } diff --git a/tests/phpunit/tests/multisite/getMainSiteId.php b/tests/phpunit/tests/multisite/getMainSiteId.php index 3e1f4d16b1..11bf6ce8d4 100644 --- a/tests/phpunit/tests/multisite/getMainSiteId.php +++ b/tests/phpunit/tests/multisite/getMainSiteId.php @@ -106,6 +106,25 @@ class Tests_Multisite_Get_Main_Site_ID extends WP_UnitTestCase { return 333; } + /** + * @ticket 29684 + */ + public function test_get_main_site_id_filtered_depending_on_network() { + add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id_depending_on_network' ), 10, 2 ); + $result = get_main_site_id( self::$network_ids['wordpress.org/'] ); + + $this->assertSame( 333, $result ); + } + + public function filter_get_main_site_id_depending_on_network( $main_site_id, $network ) { + // Override main site ID for a specific network for the test. + if ( $network->id === (int) self::$network_ids['wordpress.org/'] ) { + return 333; + } + + return $main_site_id; + } + /** * @ticket 41936 */