Make WordPress Core

Ticket #27003: 27003.2.diff

File 27003.2.diff, 5.6 KB (added by jeremyfelt, 11 years ago)
  • src/wp-includes/ms-load.php

     
    134134}
    135135
    136136/**
     137 * Retrieve a network object by its domain and path.
     138 *
     139 * @since 3.9.0
     140 *
     141 * @param string $domain
     142 * @param string $path
     143 *
     144 * @return mixed
     145 */
     146function get_network_by_path( $domain, $path ) {
     147        global $wpdb;
     148
     149        $network_id = false;
     150
     151        while ( 1 <= substr_count( $domain, '.' ) ) {
     152                // Check for a match on the full domain and first path
     153                $network_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path ) );
     154
     155                if ( $network_id ) {
     156                        break;
     157                }
     158
     159                // Check for a match on the full domain and / path
     160                if ( $path !== '/' ) {
     161                        $network_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE domain = %s AND path = '/'", $domain ) );
     162                }
     163
     164                if ( $network_id ) {
     165                        break;
     166                }
     167
     168                $domain = substr( $domain, 1 + strpos( $domain, '.' ) );
     169
     170                // Check for a match on the root domain and first path
     171                $network_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path ) );
     172
     173                if ( $network_id ) {
     174                        break;
     175                }
     176
     177                if ( $path !== '/' ) {
     178                        $network_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE domain = %s AND path = '/'", $domain ) );
     179                }
     180
     181                if ( $network_id ) {
     182                        break;
     183                }
     184
     185        }
     186
     187        if ( $network_id ) {
     188                $network = wp_get_network( $network_id );
     189
     190                return $network;
     191        }
     192
     193        return false;
     194}
     195
     196/**
     197 * Retrieve an object containing information about the requested network.
     198 *
     199 * @since 3.9.0
     200 *
     201 * @param int $network_id        The network's ID
     202 *
     203 * @return mixed Object containing network information if found, false if not.
     204 */
     205function wp_get_network( $network_id ) {
     206        global $wpdb;
     207
     208        $network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $network_id ) );
     209
     210        if ( $network ) {
     211                wp_load_core_site_options( $network->id );
     212                $network = get_current_site_name( $network );
     213                return $network;
     214        }
     215
     216        return false;
     217}
     218
     219/**
    137220 * Sets current_site object.
    138221 *
    139222 * @access private
     
    141224 * @return object $current_site object
    142225 */
    143226function wpmu_current_site() {
    144         global $wpdb, $current_site, $domain, $path, $sites, $cookie_domain;
     227        global $current_site, $domain, $path, $cookie_domain;
    145228
    146229        if ( empty( $current_site ) )
    147230                $current_site = new stdClass;
     
    170253        if ( $current_site )
    171254                return $current_site;
    172255
    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         }
     256        // Find the first complete path after the domain.
    185257        $path = substr( $_SERVER[ 'REQUEST_URI' ], 0, 1 + strpos( $_SERVER[ 'REQUEST_URI' ], '/', 1 ) );
    186258
    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 ) );
    191 
    192         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 ) );
    197         }
    198 
     259        $current_site = get_network_by_path( $domain, $path );
    199260        if ( $current_site ) {
    200261                $path = $current_site->path;
    201                 $current_site->cookie_domain = $cookie_domain;
    202                 return $current_site;
    203         }
     262                wp_cache_set( 'current_site', $current_site, 'site-options' );
    204263
    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 = '/';
    218264                return $current_site;
    219265        }
    220266
    221         // Still no dice.
    222267        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.' ) );
     268        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.' ) );
    228269}
    229270
    230271/**