From f158620eabb48e94d55447951e7d61e2c9575a1f Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 20 Sep 2022 14:24:08 +0000 Subject: [PATCH] Networks and Sites: Store main site id of a network in network options. Instead of caching main site id an object cache, store main site id on a network options. This results in less database queries on sites without persistent object caching. Props spacedmonkey, johnjamesjacoby, peterwilsoncc, desrosj. Fixes #55802. git-svn-id: https://develop.svn.wordpress.org/trunk@54256 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/schema.php | 10 ++++++++++ src/wp-includes/class-wp-network.php | 5 ++--- tests/phpunit/tests/multisite/getMainSiteId.php | 11 +++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/wp-admin/includes/schema.php b/src/wp-admin/includes/schema.php index c8bc92e44c..57ffe69054 100644 --- a/src/wp-admin/includes/schema.php +++ b/src/wp-admin/includes/schema.php @@ -1068,6 +1068,16 @@ function populate_network( $network_id = 1, $domain = '', $email = '', $site_nam update_user_meta( $site_user->ID, 'source_domain', $domain ); update_user_meta( $site_user->ID, 'primary_blog', $current_site->blog_id ); + // Unable to use update_network_option() while populating the network. + $wpdb->insert( + $wpdb->sitemeta, + array( + 'site_id' => $network_id, + 'meta_key' => 'main_site', + 'meta_value' => $current_site->blog_id, + ) + ); + if ( $subdomain_install ) { $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); } else { diff --git a/src/wp-includes/class-wp-network.php b/src/wp-includes/class-wp-network.php index 75b407e541..fd92c5ba70 100644 --- a/src/wp-includes/class-wp-network.php +++ b/src/wp-includes/class-wp-network.php @@ -255,9 +255,8 @@ class WP_Network { if ( $site->domain === $this->domain && $site->path === $this->path ) { $main_site_id = (int) $site->id; } else { - $cache_key = 'network:' . $this->id . ':main_site'; - $main_site_id = wp_cache_get( $cache_key, 'site-options' ); + $main_site_id = get_network_option( $this->id, 'main_site' ); if ( false === $main_site_id ) { $_sites = get_sites( array( @@ -270,7 +269,7 @@ class WP_Network { ); $main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0; - wp_cache_add( $cache_key, $main_site_id, 'site-options' ); + update_network_option( $this->id, 'main_site', $main_site_id ); } } diff --git a/tests/phpunit/tests/multisite/getMainSiteId.php b/tests/phpunit/tests/multisite/getMainSiteId.php index 3a6e9c4d8c..f68c5409e7 100644 --- a/tests/phpunit/tests/multisite/getMainSiteId.php +++ b/tests/phpunit/tests/multisite/getMainSiteId.php @@ -88,6 +88,17 @@ if ( is_multisite() ) : $this->assertSame( $main_site_id, $result ); } + /** + * @ticket 55802 + */ + public function test_get_main_site_id_with_different_network_cache_id() { + $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ), 'Main blog id needs to match blog id of wordpress.org/' ); + $this->assertSame( self::$site_ids['wordpress.org/'], (int) get_network_option( self::$network_ids['wordpress.org/'], 'main_site' ), 'Network option needs to match blog id of wordpress.org/' ); + + $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ), 'Main blog id should not be found' ); + $this->assertSame( 0, (int) get_network_option( self::$network_ids['wp.org/'], 'main_site' ), 'Network option should not be found' ); + } + /** * @ticket 29684 */