- $domain = addslashes( $_SERVER['HTTP_HOST'] );
- if ( false !== strpos( $domain, ':' ) ) {
- if ( substr( $domain, -3 ) == ':80' ) {
- $domain = substr( $domain, 0, -3 );
- $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
- } elseif ( substr( $domain, -4 ) == ':443' ) {
- $domain = substr( $domain, 0, -4 );
- $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
+ // Given the domain and path, let's try to identify the network and site.
+ // Usually, it's easier to query the site first, which declares its network.
+ // In limited situations, though, we either can or must find the network first.
+
+ $domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ) );
+ if ( substr( $domain, -3 ) == ':80' ) {
+ $domain = substr( $domain, 0, -3 );
+ $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
+ } elseif ( substr( $domain, -4 ) == ':443' ) {
+ $domain = substr( $domain, 0, -4 );
+ $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
+ }
+
+ $path = stripslashes( $_SERVER['REQUEST_URI'] );
+ if ( is_admin() ) {
+ $path = preg_replace( '#(.*)/wp-admin/.*#', '$1/', $path );
+ }
+ list( $path ) = explode( '?', $path );
+
+ // If the network is defined in wp-config.php, we can simply use that.
+ if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
+ $current_site = new stdClass;
+ $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
+ $current_site->domain = DOMAIN_CURRENT_SITE;
+ $current_site->path = PATH_CURRENT_SITE;
+ if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
+ $current_site->blog_id = BLOG_ID_CURRENT_SITE;
+ } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
+ $current_site->blog_id = BLOGID_CURRENT_SITE;
+ }
+
+ if ( 0 === strcasecmp( $current_site->domain, $domain ) && 0 === strcasecmp( $current_site->path, $path ) ) {
+ $current_blog = get_site_by_path( $domain, $path );
+ } elseif ( '/' !== $current_site->path && 0 === strcasecmp( $current_site->domain, $domain ) && 0 === stripos( $path, $current_site->path ) ) {
+ // If the current network has a path and also matches the domain and path of the request,
+ // we need to look for a site using the first path segment following the network's path.
+ $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) );