From da4456672cf1ac9793ae9171290c1a7638edd0b7 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Tue, 20 Sep 2022 14:26:10 +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. Built from https://develop.svn.wordpress.org/trunk@54256 git-svn-id: http://core.svn.wordpress.org/trunk@53815 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/schema.php | 10 ++++++++++ wp-includes/class-wp-network.php | 5 ++--- wp-includes/version.php | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/wp-admin/includes/schema.php b/wp-admin/includes/schema.php index c8bc92e44c..57ffe69054 100644 --- a/wp-admin/includes/schema.php +++ b/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/wp-includes/class-wp-network.php b/wp-includes/class-wp-network.php index 75b407e541..fd92c5ba70 100644 --- a/wp-includes/class-wp-network.php +++ b/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/wp-includes/version.php b/wp-includes/version.php index cc39678976..c4e5912422 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.