Make WordPress Core

Changeset 27359


Ignore:
Timestamp:
03/02/2014 10:24:50 PM (13 years ago)
Author:
nacin
Message:

Introduce get_site_by_path() and further rewrite the site detection process for multisite.

This is the first big step to supporting arbitrary domains and paths. In this new approach, sites are detected first where possible, then the network is inferred. Allows filtering for arbitrary path segments, smooths out some weirdness, and removes various restrictions. A sunrise plugin could do much of its work by adding filters, if those are even needed.

see #27003.

Location:
trunk
Files:
4 edited

Legend:

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

    r26120 r27359  
    890890        }
    891891
     892        wp_cache_delete( 'networks_have_paths', 'site-options' );
     893
    892894        if ( !is_multisite() ) {
    893895                $site_admins = array( $site_user->user_login );
  • trunk/src/wp-includes/ms-load.php

    r27275 r27359  
    116116 * Sets current site name.
    117117 *
     118 * @todo deprecate
     119 *
    118120 * @access private
    119121 * @since 3.0.0
    … …  
    139141 * @since 3.9.0
    140142 *
    141  * @param string $domain Domain to check.
    142  * @param string $path   Path to check.
     143 * @param string $domain   Domain to check.
     144 * @param string $path     Path to check.
     145 * @param int    $segments Path segments to use. Defaults to null, or the full path.
    143146 * @return object|bool Network object if successful. False when no network is found.
    144147 */
    145 function get_network_by_path( $domain, $path ) {
     148function get_network_by_path( $domain, $path, $segments = null ) {
    146149        global $wpdb;
    147 
    148         $network_id = false;
    149150
    150151        $domains = $exact_domains = array( $domain );
    … …  
    159160        }
    160161
    161         if ( '/' !== $path ) {
    162                 $paths = array( '/', $path );
     162        /*
     163         * If we've gotten to this function during normal execution, there is
     164         * more than one network installed. At this point, who knows how many
     165         * we have. Attempt to optimize for the situation where networks are
     166         * only domains, thus meaning paths never need to be considered.
     167         *
     168         * This is a very basic optimization; anything further could have drawbacks
     169         * depending on the setup, so this is best done per-install.
     170         */
     171        $using_paths = true;
     172        if ( wp_using_ext_object_cache() ) {
     173                $using_paths = wp_cache_get( 'networks_have_paths', 'site-options' );
     174                if ( false === $using_paths ) {
     175                        $using_paths = (bool) $wpdb->get_var( "SELECT id FROM $wpdb->site WHERE path <> '/' LIMIT 1" );
     176                        wp_cache_add( 'networks_have_paths', (int) $using_paths, 'site-options'  );
     177                }
     178        }
     179
     180        $paths = array();
     181        if ( $using_paths ) {
     182                $path_segments = array_filter( explode( '/', trim( $path, "/" ) ) );
     183
     184                /**
     185                 * Filter the number of path segments to consider when searching for a site.
     186                 *
     187                 * @since 3.9.0
     188                 *
     189                 * @param mixed  $segments The number of path segments to consider. WordPress by default looks at
     190                 *                         one path segment. The function default of null only makes sense when you
     191                 *                         know the requested path should match a network.
     192                 * @param string $domain   The requested domain.
     193                 * @param string $path     The requested path, in full.
     194                 */
     195                $segments = apply_filters( 'network_by_path_segments_count', $segments, $domain, $path );
     196
     197                if ( null !== $segments && count($path_segments ) > $segments ) {
     198                        $path_segments = array_slice( $path_segments, 0, $segments );
     199                }
     200
     201                while ( count( $path_segments ) ) {
     202                        $paths[] = '/' . implode( '/', $path_segments ) . '/';
     203                        array_pop( $path_segments );
     204                }
     205
     206                $paths[] = '/';
     207        }
     208
     209        /**
     210         * Determine a network by its domain and path.
     211         *
     212         * This allows one to short-circuit the default logic, perhaps by
     213         * replacing it with a routine that is more optimal for your setup.
     214         *
     215         * Return null to avoid the short-circuit. Return false if no network
     216         * can be found at the requested domain and path. Otherwise, return
     217         * an object from wp_get_network().
     218         *
     219         * @since 3.9.0
     220         *
     221         * @param string $domain   The requested domain.
     222         * @param string $path     The requested path, in full.
     223         * @param mixed  $segments The suggested number of paths to consult.
     224         *                         Default null, meaning the entire path was to be consulted.
     225         * @param array  $paths    The paths to search for, based on $path and $segments.
     226         */
     227        $pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths );
     228        if ( null !== $pre ) {
     229                return $pre;
     230        }
     231
     232        // @todo Consider additional optimization routes, perhaps as an opt-in for plugins.
     233        // We already have paths covered. What about how far domains should be drilled down (including www)?
     234
     235        $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
     236
     237        if ( ! $using_paths ) {
     238                $network = $wpdb->get_row( "SELECT id, domain, path FROM $wpdb->site
     239                        WHERE domain IN ($search_domains) ORDER BY CHAR_LENGTH(domain) DESC LIMIT 1" );
     240                if ( $network ) {
     241                        return wp_get_network( $network );
     242                }
     243                return false;
     244
    163245        } else {
    164                 $paths = array( '/' );
    165         }
    166 
    167         $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
    168         $paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
    169 
    170         $networks = $wpdb->get_results( "SELECT id, domain, path FROM $wpdb->site
    171                 WHERE domain IN ($search_domains) AND path IN ($paths)
    172                 ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC" );
     246                $search_paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
     247                $networks = $wpdb->get_results( "SELECT id, domain, path FROM $wpdb->site
     248                        WHERE domain IN ($search_domains) AND path IN ($search_paths)
     249                        ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC" );
     250        }
    173251
    174252        /*
    … …  
    180258        foreach ( $networks as $network ) {
    181259                if ( $network->domain === $domain || "www.$network->domain" === $domain ) {
    182                         if ( $network->path === $path ) {
     260                        if ( in_array( $network->path, $paths, true ) ) {
    183261                                $found = true;
    184262                                break;
    … …  
    192270
    193271        if ( $found ) {
    194                 $network = wp_get_network( $network );
    195 
    196                 return $network;
     272                return wp_get_network( $network );
    197273        }
    198274
    … …  
    222298
    223299/**
    224  * Sets current_site object.
    225  *
    226  * @access private
    227  * @since 3.0.0
    228  * @return object $current_site object
     300 * @todo deprecate
    229301 */
    230302function wpmu_current_site() {
    231         global $wpdb, $current_site, $domain, $path;
    232 
    233         if ( empty( $current_site ) )
    234                 $current_site = new stdClass;
    235 
    236         // 1. If constants are defined, that's our network.
    237         if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
    238                 $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
    239                 $current_site->domain = DOMAIN_CURRENT_SITE;
    240                 $current_site->path   = $path = PATH_CURRENT_SITE;
    241                 if ( defined( 'BLOG_ID_CURRENT_SITE' ) )
    242                         $current_site->blog_id = BLOG_ID_CURRENT_SITE;
    243                 elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) // deprecated.
    244                         $current_site->blog_id = BLOGID_CURRENT_SITE;
    245 
    246         // 2. Pull the network from cache, if possible.
    247         } elseif ( ! $current_site = wp_cache_get( 'current_site', 'site-options' ) ) {
    248 
    249                 // 3. See if they have only one network.
    250                 $networks = $wpdb->get_col( "SELECT id FROM $wpdb->site LIMIT 2" );
    251 
    252                 if ( count( $networks ) <= 1 ) {
    253                         $current_site = wp_get_network( $networks[0] );
    254 
    255                         $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id
    256                                 FROM $wpdb->blogs WHERE domain = %s AND path = %s",
    257                                 $current_site->domain, $current_site->path ) );
    258 
    259                         wp_cache_set( 'current_site', 'site-options' );
    260 
    261                 // 4. Multiple networks are in play. Determine which via domain and path.
    262                 } else {
    263                         // Find the first path segment.
    264                         $path = substr( $_SERVER['REQUEST_URI'], 0, 1 + strpos( $_SERVER['REQUEST_URI'], '/', 1 ) );
    265                         $current_site = get_network_by_path( $domain, $path );
    266 
    267                         // Option 1. We did not find anything.
    268                         if ( ! $current_site ) {
    269                                 wp_load_translations_early();
    270                                 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.' ) );
    271                         }
    272                 }
    273         }
    274 
    275         // Option 2. We found something. Load up site meta and return.
    276         wp_load_core_site_options();
    277         $current_site = get_current_site_name( $current_site );
    278         return $current_site;
     303}
     304
     305/**
     306 * Retrieve a site object by its domain and path.
     307 *
     308 * @since 3.9.0
     309 *
     310 * @param string $domain   Domain to check.
     311 * @param string $path     Path to check.
     312 * @param int    $segments Path segments to use. Defaults to null, or the full path.
     313 * @return object|bool Site object if successful. False when no site is found.
     314 */
     315function get_site_by_path( $domain, $path, $segments = null ) {
     316        global $wpdb;
     317
     318        $path_segments = array_filter( explode( '/', trim( $path, "/" ) ) );
     319
     320        /**
     321         * Filter the number of path segments to consider when searching for a site.
     322         *
     323         * @since 3.9.0
     324         *
     325
     326         * @param mixed  $segments The number of path segments to consider. WordPress by default looks at
     327         *                         one path segment following the network path. The function default of
     328         *                         null only makes sense when you know the requested path should match a site.
     329         * @param string $domain   The requested domain.
     330         * @param string $path     The requested path, in full.
     331         */
     332        $segments = apply_filters( 'site_by_path_segments_count', $segments, $domain, $path );
     333
     334        if ( null !== $segments && count($path_segments ) > $segments ) {
     335                $path_segments = array_slice( $path_segments, 0, $segments );
     336        }
     337
     338        while ( count( $path_segments ) ) {
     339                $paths[] = '/' . implode( '/', $path_segments ) . '/';
     340                array_pop( $path_segments );
     341        }
     342
     343        $paths[] = '/';
     344
     345        /**
     346         * Determine a site by its domain and path.
     347         *
     348         * This allows one to short-circuit the default logic, perhaps by
     349         * replacing it with a routine that is more optimal for your setup.
     350         *
     351         * Return null to avoid the short-circuit. Return false if no site
     352         * can be found at the requested domain and path. Otherwise, return
     353         * a site object.
     354         *
     355         * @since 3.9.0
     356         *
     357         * @param string $domain   The requested domain.
     358         * @param string $path     The requested path, in full.
     359         * @param mixed  $segments The suggested number of paths to consult.
     360         *                         Default null, meaning the entire path was to be consulted.
     361         * @param array  $paths    The paths to search for, based on $path and $segments.
     362         */
     363        $pre = apply_filters( 'pre_get_site_by_path', null, $domain, $path, $segments, $paths );
     364        if ( null !== $pre ) {
     365                return $pre;
     366        }
     367
     368        // @todo
     369        // get_blog_details(), caching, etc. Consider alternative optimization routes,
     370        // perhaps as an opt-in for plugins, rather than using the pre_* filter.
     371        // For example: The segments filter can expand or ignore paths.
     372        // If persistent caching is enabled, we could query the DB for a path <> '/'
     373        // then cache whether we can just always ignore paths.
     374
     375        if ( count( $paths ) > 1 ) {
     376                $paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
     377                $site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs
     378                        WHERE domain = %s AND path IN ($paths) ORDER BY CHAR_LENGTH(path) DESC LIMIT 1", $domain ) );
     379        } else {
     380                $site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s and path = %s", $domain, $paths[0] ) );
     381        }
     382
     383        if ( $site ) {
     384                // @todo get_blog_details()
     385                return $site;
     386        }
     387
     388        return false;
    279389}
    280390
  • trunk/src/wp-includes/ms-settings.php

    r27178 r27359  
    2323if ( !isset( $current_site ) || !isset( $current_blog ) ) {
    2424
    25         $domain = addslashes( $_SERVER['HTTP_HOST'] );
    26         if ( false !== strpos( $domain, ':' ) ) {
    27                 if ( substr( $domain, -3 ) == ':80' ) {
    28                         $domain = substr( $domain, 0, -3 );
    29                         $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
    30                 } elseif ( substr( $domain, -4 ) == ':443' ) {
    31                         $domain = substr( $domain, 0, -4 );
    32                         $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
     25        // Given the domain and path, let's try to identify the network and site.
     26        // Usually, it's easier to query the site first, which declares its network.
     27        // In limited situations, though, we either can or must find the network first.
     28
     29        $domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ) );
     30        if ( substr( $domain, -3 ) == ':80' ) {
     31                $domain = substr( $domain, 0, -3 );
     32                $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
     33        } elseif ( substr( $domain, -4 ) == ':443' ) {
     34                $domain = substr( $domain, 0, -4 );
     35                $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
     36        }
     37
     38        $path = stripslashes( $_SERVER['REQUEST_URI'] );
     39        if ( is_admin() ) {
     40                $path = preg_replace( '#(.*)/wp-admin/.*#', '$1/', $path );
     41        }
     42        list( $path ) = explode( '?', $path );
     43
     44        // If the network is defined in wp-config.php, we can simply use that.
     45        if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
     46                $current_site = new stdClass;
     47                $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
     48                $current_site->domain = DOMAIN_CURRENT_SITE;
     49                $current_site->path = PATH_CURRENT_SITE;
     50                if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
     51                        $current_site->blog_id = BLOG_ID_CURRENT_SITE;
     52                } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
     53                        $current_site->blog_id = BLOGID_CURRENT_SITE;
     54                }
     55
     56                if ( $current_site->domain === $domain && $current_site->path === $path ) {
     57                        $current_blog = get_site_by_path( $domain, $path );
     58                } elseif ( '/' !== $current_site->path && $current_site->domain === $domain && 0 === strpos( $path, $current_site->path ) ) {
     59                        // If the current network has a path and also matches the domain and path of the request,
     60                        // we need to look for a site using the first path segment following the network's path.
     61                        $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) );
    3362                } else {
    34                         wp_load_translations_early();
    35                         wp_die( __( 'Multisite only works without the port number in the URL.' ) );
     63                        // Otherwise, use the first path segment (as usual).
     64                        $current_blog = get_site_by_path( $domain, $path, 1 );
     65                }
     66
     67        } elseif ( ! is_subdomain_install() ) {
     68                /*
     69                 * A "subdomain" install can be re-interpreted to mean "can support any domain".
     70                 * If we're not dealing with one of these installs, then the important part is determing
     71                 * the network first, because we need the network's path to identify any sites.
     72                 */
     73                if ( ! $current_site = wp_cache_get( 'current_network', 'site-options' ) ) {
     74                        // Are there even two networks installed?
     75                        $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic]
     76                        if ( 1 === $wpdb->num_rows ) {
     77                                $current_site = wp_get_network( $one_network );
     78                                wp_cache_set( 'current_network', 'site-options' );
     79                        } elseif ( 0 === $wpdb->num_rows ) {
     80                                ms_not_installed();
     81                        }
     82                }
     83                if ( empty( $current_site ) ) {
     84                        $current_site = get_network_by_path( $domain, $path, 1 );
     85                }
     86
     87                if ( empty( $current_site ) ) {
     88                        ms_not_installed();
     89                } elseif ( $path === $current_site->path ) {
     90                        $current_blog = get_site_by_path( $domain, $path );
     91                } else {
     92                        // Search the network path + one more path segment (on top of the network path).
     93                        $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) );
     94                }
     95        } else {
     96                // Find the site by the domain and at most the first path segment.
     97                $current_blog = get_site_by_path( $domain, $path, 1 );
     98                if ( $current_blog ) {
     99                        $current_site = wp_get_network( $current_blog->site_id ? $current_blog->site_id : 1 );
     100                } else {
     101                        // If you don't have a site with the same domain/path as a network, you're pretty screwed, but:
     102                        $current_site = get_network_by_path( $domain, $path, 1 );
    36103                }
    37104        }
    38105
    39         $domain = rtrim( $domain, '.' );
     106        // The network declared by the site trumps any constants.
     107        if ( $current_blog && $current_blog->site_id != $current_site->id ) {
     108                $current_site = wp_get_network( $current_blog->site_id );
     109        }
    40110
    41         $path = preg_replace( '|([a-z0-9-]+.php.*)|', '', $_SERVER['REQUEST_URI'] );
    42         $path = str_replace ( '/wp-admin/', '/', $path );
    43         $path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path );
     111        // If we don't have a network by now, we have a problem.
     112        if ( empty( $current_site ) ) {
     113                ms_not_installed();
     114        }
    44115
    45         $current_site = wpmu_current_site();
     116        // @todo What if the domain of the network doesn't match the current site?
    46117        $current_site->cookie_domain = $current_site->domain;
    47118        if ( 'www.' === substr( $current_site->cookie_domain, 0, 4 ) ) {
    … …  
    49120        }
    50121
    51         if ( ! isset( $current_site->blog_id ) )
    52                 $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 ) );
    53 
    54         if ( is_subdomain_install() ) {
    55                 $current_blog = wp_cache_get( 'current_blog_' . $domain, 'site-options' );
    56                 if ( !$current_blog ) {
    57                         $current_blog = get_blog_details( array( 'domain' => $domain ), false );
    58                         if ( $current_blog )
    59                                 wp_cache_set( 'current_blog_' . $domain, $current_blog, 'site-options' );
     122        // Figure out the current network's main site.
     123        if ( ! isset( $current_site->blog_id ) ) {
     124                if ( $current_blog && $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
     125                        $current_site->blog_id = $current_blog->blog_id;
     126                } else {
     127                        // @todo we should be able to cache the blog ID of a network's main site easily.
     128                        $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
     129                                $current_site->domain, $current_site->path ) );
    60130                }
    61                 if ( $current_blog && $current_blog->site_id != $current_site->id ) {
    62                         $current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $current_blog->site_id ) );
    63                         if ( ! isset( $current_site->blog_id ) )
    64                                 $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 ) );
    65                 } else
    66                         $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
    67         } else {
    68                 $blogname = htmlspecialchars( substr( $_SERVER[ 'REQUEST_URI' ], strlen( $path ) ) );
    69                 if ( false !== strpos( $blogname, '/' ) )
    70                         $blogname = substr( $blogname, 0, strpos( $blogname, '/' ) );
    71                 if ( false !== strpos( $blogname, '?' ) )
    72                         $blogname = substr( $blogname, 0, strpos( $blogname, '?' ) );
    73                 $reserved_blognames = array( 'page', 'comments', 'blog', 'wp-admin', 'wp-includes', 'wp-content', 'files', 'feed' );
    74                 if ( $blogname != '' && ! in_array( $blogname, $reserved_blognames ) && ! is_file( $blogname ) )
    75                         $path .= $blogname . '/';
    76                 $current_blog = wp_cache_get( 'current_blog_' . $domain . $path, 'site-options' );
    77                 if ( ! $current_blog ) {
    78                         $current_blog = get_blog_details( array( 'domain' => $domain, 'path' => $path ), false );
    79                         if ( $current_blog )
    80                                 wp_cache_set( 'current_blog_' . $domain . $path, $current_blog, 'site-options' );
    81                 }
    82                 unset($reserved_blognames);
    83131        }
    84132
    85         if ( ! defined( 'WP_INSTALLING' ) && is_subdomain_install() && ! is_object( $current_blog ) ) {
    86                 if ( defined( 'NOBLOGREDIRECT' ) ) {
    87                         $destination = NOBLOGREDIRECT;
    88                         if ( '%siteurl%' == $destination )
    89                                 $destination = "http://" . $current_site->domain . $current_site->path;
     133        // If we haven't figured out our site, give up.
     134        if ( empty( $current_blog ) ) {
     135                if ( defined( 'WP_INSTALLING' ) ) {
     136                        $current_blog->blog_id = $blog_id = 1;
     137
     138                } elseif ( is_subdomain_install() ) {
     139                        // @todo This is only for an open registration subdomain network.
     140                        if ( defined( 'NOBLOGREDIRECT' ) ) {
     141                                if ( '%siteurl%' === NOBLOGREDIRECT ) {
     142                                        $destination = "http://" . $current_site->domain . $current_site->path;
     143                                } else {
     144                                        $destination = NOBLOGREDIRECT;
     145                                }
     146                        } else {
     147                                $destination = 'http://' . $current_site->domain . $current_site->path . 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
     148                        }
     149                        header( 'Location: ' . $destination );
     150                        exit;
     151
    90152                } else {
    91                         $destination = 'http://' . $current_site->domain . $current_site->path . 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
    92                 }
    93                 header( 'Location: ' . $destination );
    94                 die();
    95         }
    96 
    97         if ( ! defined( 'WP_INSTALLING' ) ) {
    98                 if ( $current_site && ! $current_blog ) {
    99                         if ( $current_site->domain != $_SERVER[ 'HTTP_HOST' ] ) {
     153                        if ( 0 !== strcasecmp( $current_site->domain, $domain ) ) {
    100154                                header( 'Location: http://' . $current_site->domain . $current_site->path );
    101155                                exit;
    102156                        }
    103                         $current_blog = get_blog_details( array( 'domain' => $current_site->domain, 'path' => $current_site->path ), false );
     157                        ms_not_installed();
    104158                }
    105                 if ( ! $current_blog || ! $current_site )
    106                         ms_not_installed();
    107159        }
    108160
    … …  
    110162        $public  = $current_blog->public;
    111163
    112         if ( empty( $current_blog->site_id ) )
     164        if ( empty( $current_blog->site_id ) ) {
     165                // This dates to [MU134] and shouldn't be relevant anymore,
     166                // but it could be possible for arguments passed to insert_blog() etc.
    113167                $current_blog->site_id = 1;
     168        }
     169
    114170        $site_id = $current_blog->site_id;
     171        wp_load_core_site_options( $site_id );
     172}
    115173
    116         $current_site = get_current_site_name( $current_site );
    117 
    118         if ( ! $blog_id ) {
    119                 if ( defined( 'WP_INSTALLING' ) ) {
    120                         $current_blog->blog_id = $blog_id = 1;
    121                 } else {
    122                         wp_load_translations_early();
    123                         $msg = ! $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->site'" ) ? ' ' . __( 'Database tables are missing.' ) : '';
    124                         wp_die( __( 'No site by that name on this system.' ) . $msg );
    125                 }
    126         }
    127 }
    128174$wpdb->set_prefix( $table_prefix, false ); // $table_prefix can be set in sunrise.php
    129175$wpdb->set_blog_id( $current_blog->blog_id, $current_blog->site_id );
    … …  
    135181wp_start_object_cache();
    136182
     183if ( ! isset( $current_site->site_name ) ) {
     184        $current_site->site_name = get_site_option( 'site_name' );
     185        if ( ! $current_site->site_name ) {
     186                $current_site->site_name = ucfirst( $current_site->domain );
     187        }
     188}
     189
    137190// Define upload directory constants
    138191ms_upload_constants();
  • trunk/tests/phpunit/tests/ms.php

    r27290 r27359  
    12221222
    12231223        /**
     1224         * @ticket 27003
     1225         */
     1226        function test_get_site_by_path() {
     1227                $ids = array(
     1228                        'wordpress.org/'              => array( 'domain' => 'wordpress.org',      'path' => '/' ),
     1229                        'wordpress.org/foo/'          => array( 'domain' => 'wordpress.org',      'path' => '/foo/' ),
     1230                        'wordpress.org/foo/bar/'      => array( 'domain' => 'wordpress.org',      'path' => '/foo/bar/' ),
     1231                        'make.wordpress.org/'         => array( 'domain' => 'make.wordpress.org', 'path' => '/' ),
     1232                        'make.wordpress.org/foo/'     => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/' ),
     1233                );
     1234
     1235                foreach ( $ids as &$id ) {
     1236                        $id = $this->factory->blog->create( $id );
     1237                }
     1238                unset( $id );
     1239
     1240                $this->assertEquals( $ids['wordpress.org/'],
     1241                        get_site_by_path( 'wordpress.org', '/notapath/' )->blog_id );
     1242
     1243                $this->assertEquals( $ids['wordpress.org/foo/bar/'],
     1244                        get_site_by_path( 'wordpress.org', '/foo/bar/baz/' )->blog_id );
     1245
     1246                $this->assertEquals( $ids['wordpress.org/foo/bar/'],
     1247                        get_site_by_path( 'wordpress.org', '/foo/bar/baz/', 3 )->blog_id );
     1248
     1249                $this->assertEquals( $ids['wordpress.org/foo/bar/'],
     1250                        get_site_by_path( 'wordpress.org', '/foo/bar/baz/', 2 )->blog_id );
     1251
     1252                $this->assertEquals( $ids['wordpress.org/foo/'],
     1253                        get_site_by_path( 'wordpress.org', '/foo/bar/baz/', 1 )->blog_id );
     1254
     1255                $this->assertEquals( $ids['wordpress.org/'],
     1256                        get_site_by_path( 'wordpress.org', '/', 0 )->blog_id );
     1257
     1258                $this->assertEquals( $ids['make.wordpress.org/foo/'],
     1259                        get_site_by_path( 'make.wordpress.org', '/foo/bar/baz/qux/', 4 )->blog_id );
     1260        }
     1261
     1262        /**
    12241263         * @ticket 20601
    12251264         */
Note: See TracChangeset for help on using the changeset viewer.