WordPress.org

Make WordPress Core

Ticket #29684: 29684.diff

File 29684.diff, 2.4 KB (added by jeremyfelt, 6 years ago)
  • src/wp-includes/ms-load.php

     
    456456        _deprecated_function( __FUNCTION__, '3.9' );
    457457        return $current_site;
    458458}
     459
     460/**
     461 * Retrieve the main site ID of a given network.
     462 *
     463 * @param int $network_id ID of the network.
     464 *
     465 * @return int|bool ID of the main site if found. False if the network ID is invalid.
     466 */
     467function get_main_site( $network_id = 0 ) {
     468        global $wpdb, $current_site, $current_blog;
     469
     470        if ( 0 === $network_id ) {
     471                if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
     472                        return BLOG_ID_CURRENT_SITE;
     473                } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
     474                        return BLOGID_CURRENT_SITE;
     475                } elseif ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
     476                        return $current_blog->blog_id;
     477                } else {
     478                        $network = $current_site;
     479                }
     480        } else {
     481                $network = wp_get_network( $network_id );
     482        }
     483
     484        if ( ! $network ) {
     485                return false;
     486        }
     487
     488        $site_id = wp_cache_get( 'network:' . $network->id . ':main_site', 'site-options' );
     489
     490        if ( ! $site_id ) {
     491                $site_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $network->domain, $network->path ) );
     492                wp_cache_add( 'network:' . $network->id . ':main_site', $site_id, 'site-options' );
     493        }
     494
     495        return $site_id;
     496}
     497 No newline at end of file
  • src/wp-includes/ms-settings.php

     
    171171
    172172        // Figure out the current network's main site.
    173173        if ( ! isset( $current_site->blog_id ) ) {
    174                 if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
    175                         $current_site->blog_id = $current_blog->blog_id;
    176                 } elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) {
    177                         $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
    178                                 $current_site->domain, $current_site->path ) );
    179                         wp_cache_add( 'network:' . $current_site->id . ':main_site', $current_site->blog_id, 'site-options' );
    180                 }
     174                $current_site->blog_id = get_main_site();
    181175        }
    182176
    183177        $blog_id = $current_blog->blog_id;