Make WordPress Core

Changeset 54256


Ignore:
Timestamp:
09/20/2022 02:24:08 PM (2 years ago)
Author:
spacedmonkey
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/schema.php

    r54240 r54256  
    10691069        update_user_meta( $site_user->ID, 'primary_blog', $current_site->blog_id );
    10701070
     1071        // Unable to use update_network_option() while populating the network.
     1072        $wpdb->insert(
     1073            $wpdb->sitemeta,
     1074            array(
     1075                'site_id'    => $network_id,
     1076                'meta_key'   => 'main_site',
     1077                'meta_value' => $current_site->blog_id,
     1078            )
     1079        );
     1080
    10711081        if ( $subdomain_install ) {
    10721082            $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
  • trunk/src/wp-includes/class-wp-network.php

    r54133 r54256  
    256256            $main_site_id = (int) $site->id;
    257257        } else {
    258             $cache_key = 'network:' . $this->id . ':main_site';
    259 
    260             $main_site_id = wp_cache_get( $cache_key, 'site-options' );
     258
     259            $main_site_id = get_network_option( $this->id, 'main_site' );
    261260            if ( false === $main_site_id ) {
    262261                $_sites       = get_sites(
     
    271270                $main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0;
    272271
    273                 wp_cache_add( $cache_key, $main_site_id, 'site-options' );
     272                update_network_option( $this->id, 'main_site', $main_site_id );
    274273            }
    275274        }
  • trunk/tests/phpunit/tests/multisite/getMainSiteId.php

    r51860 r54256  
    9090
    9191        /**
     92         * @ticket 55802
     93         */
     94        public function test_get_main_site_id_with_different_network_cache_id() {
     95            $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/' );
     96            $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/' );
     97
     98            $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ), 'Main blog id should not be found' );
     99            $this->assertSame( 0, (int) get_network_option( self::$network_ids['wp.org/'], 'main_site' ), 'Network option should not be found' );
     100        }
     101
     102        /**
    92103         * @ticket 29684
    93104         */
Note: See TracChangeset for help on using the changeset viewer.