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
This commit is contained in:
Jonny Harris
2022-09-20 14:24:08 +00:00
parent 0cfcb3e272
commit f158620eab
3 changed files with 23 additions and 3 deletions

View File

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

View File

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

View File

@@ -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
*/