Skip to content

Commit

Permalink
Networks and Sites: Store main site id of a network in network options.
Browse files Browse the repository at this point in the history
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.
Built from https://develop.svn.wordpress.org/trunk@54256


git-svn-id: http://core.svn.wordpress.org/trunk@53815 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
spacedmonkey committed Sep 20, 2022
1 parent 5dcad0a commit da44566
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
10 changes: 10 additions & 0 deletions wp-admin/includes/schema.php
Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions wp-includes/class-wp-network.php
Expand Up @@ -255,9 +255,8 @@ private function get_main_site_id() {
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(
Expand All @@ -270,7 +269,7 @@ private function get_main_site_id() {
);
$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 );
}
}

Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Expand Up @@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.1-alpha-54255';
$wp_version = '6.1-alpha-54256';

/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
Expand Down

0 comments on commit da44566

Please sign in to comment.