WordPress.org

Make WordPress Core

Ticket #27003: 27003.diff

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

     
    134134}
    135135
    136136/**
     137 * Retrieve an object containing information about the requested network.
     138 *
     139 * @since 3.9.0
     140 *
     141 * @param string $domain        The network's domain.
     142 * @param string $path          The primary path of the network.
     143 * @param string $cookie_domain The domain to be used for cookies on the network.
     144 *
     145 * @return mixed Object containing network information if found, false if not.
     146 */
     147function wp_get_network( $domain, $path, $cookie_domain = '' ) {
     148        global $wpdb;
     149
     150        if ( empty( $cookie_domain ) ) {
     151                if ( 'www.' === substr( $domain, 0, 4 ) ) {
     152                        $cookie_domain = substr( $domain, 4 );
     153                } else {
     154                        $cookie_domain = $domain;
     155                }
     156        }
     157
     158        if ( $domain === $cookie_domain ) {
     159                $network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path ) );
     160        } else {
     161                $network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE domain IN ( %s, %s ) AND path = %s ORDER BY CHAR_LENGTH( domain ) DESC LIMIT 1", $domain, $cookie_domain, $path ) );
     162        }
     163
     164        if ( $network ) {
     165                wp_load_core_site_options( $network->id );
     166                $network = get_current_site_name( $network );
     167                $network->cookie_domain = $cookie_domain;
     168                return $network;
     169        }
     170
     171        return false;
     172}
     173
     174/**
    137175 * Sets current_site object.
    138176 *
    139177 * @access private
     
    141179 * @return object $current_site object
    142180 */
    143181function wpmu_current_site() {
    144         global $wpdb, $current_site, $domain, $path, $sites, $cookie_domain;
     182        global $current_site, $domain, $path, $cookie_domain;
    145183
    146184        if ( empty( $current_site ) )
    147185                $current_site = new stdClass;
     
    150188                $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
    151189                $current_site->domain = DOMAIN_CURRENT_SITE;
    152190                $current_site->path   = $path = PATH_CURRENT_SITE;
     191
    153192                if ( defined( 'BLOG_ID_CURRENT_SITE' ) )
    154193                        $current_site->blog_id = BLOG_ID_CURRENT_SITE;
    155194                elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) // deprecated.
    156195                        $current_site->blog_id = BLOGID_CURRENT_SITE;
     196
    157197                if ( DOMAIN_CURRENT_SITE == $domain )
    158198                        $current_site->cookie_domain = $cookie_domain;
    159199                elseif ( substr( $current_site->domain, 0, 4 ) == 'www.' )
     
    170210        if ( $current_site )
    171211                return $current_site;
    172212
    173         $sites = $wpdb->get_results( "SELECT * FROM $wpdb->site" ); // usually only one site
    174         if ( 1 == count( $sites ) ) {
    175                 $current_site = $sites[0];
    176                 wp_load_core_site_options( $current_site->id );
    177                 $path = $current_site->path;
    178                 $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path ) );
    179                 $current_site = get_current_site_name( $current_site );
    180                 if ( substr( $current_site->domain, 0, 4 ) == 'www.' )
    181                         $current_site->cookie_domain = substr( $current_site->domain, 4 );
    182                 wp_cache_set( 'current_site', $current_site, 'site-options' );
    183                 return $current_site;
    184         }
     213        // Find the first complete path after the domain.
    185214        $path = substr( $_SERVER[ 'REQUEST_URI' ], 0, 1 + strpos( $_SERVER[ 'REQUEST_URI' ], '/', 1 ) );
    186215
    187         if ( $domain == $cookie_domain )
    188                 $current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path ) );
    189         else
    190                 $current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE domain IN ( %s, %s ) AND path = %s ORDER BY CHAR_LENGTH( domain ) DESC LIMIT 1", $domain, $cookie_domain, $path ) );
     216        // If we expect this to be a subdomain install, strip the first part of the domain for the network lookup.
     217        if ( is_subdomain_install() ) {
     218                $domain = substr( $domain, 1 + strpos( $domain, '.' ) );
     219        }
    191220
     221        $current_site = wp_get_network( $domain, $path, $cookie_domain );
     222
     223        // Repeat the network search with an empty path.
    192224        if ( ! $current_site ) {
    193                 if ( $domain == $cookie_domain )
    194                         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $domain ) );
    195                 else
    196                         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain IN ( %s, %s ) AND path = '/' ORDER BY CHAR_LENGTH( domain ) DESC LIMIT 1", $domain, $cookie_domain ) );
     225                $path = '/';
     226                $current_site = wp_get_network( $domain, $path, $cookie_domain );
    197227        }
    198228
    199229        if ( $current_site ) {
    200230                $path = $current_site->path;
    201                 $current_site->cookie_domain = $cookie_domain;
    202                 return $current_site;
    203         }
     231                wp_cache_set( 'current_site', $current_site, 'site-options' );
    204232
    205         if ( is_subdomain_install() ) {
    206                 $sitedomain = substr( $domain, 1 + strpos( $domain, '.' ) );
    207                 $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $sitedomain, $path) );
    208                 if ( $current_site ) {
    209                         $current_site->cookie_domain = $current_site->domain;
    210                         return $current_site;
    211                 }
    212 
    213                 $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $sitedomain) );
    214         }
    215 
    216         if ( $current_site || defined( 'WP_INSTALLING' ) ) {
    217                 $path = '/';
    218233                return $current_site;
    219234        }
    220235
    221         // Still no dice.
    222236        wp_load_translations_early();
    223 
    224         if ( 1 == count( $sites ) )
    225                 wp_die( sprintf( __( 'That site does not exist. Please try <a href="%s">%s</a>.' ), 'http://' . $sites[0]->domain . $sites[0]->path ) );
    226         else
    227                 wp_die( __( 'No site defined on this host. If you are the owner of this site, please check <a href="http://codex.wordpress.org/Debugging_a_WordPress_Network">Debugging a WordPress Network</a> for help.' ) );
     237        wp_die( __( 'No site defined on this host. If you are the owner of this site, please check <a href="http://codex.wordpress.org/Debugging_a_WordPress_Network">Debugging a WordPress Network</a> for help.' ) );
    228238}
    229239
    230240/**