3 * Used to set up and fix common variables and include
4 * the Multisite procedural and class library.
6 * Allows for some configuration in wp-config.php (see ms-default-constants.php)
9 * @subpackage Multisite
13 /** WP_Network class */
14 require_once( ABSPATH . WPINC . '/class-wp-network.php' );
16 /** Multisite loader */
17 require_once( ABSPATH . WPINC . '/ms-load.php' );
19 /** Default Multisite constants */
20 require_once( ABSPATH . WPINC . '/ms-default-constants.php' );
22 if ( defined( 'SUNRISE' ) ) {
23 include_once( WP_CONTENT_DIR . '/sunrise.php' );
26 /** Check for and define SUBDOMAIN_INSTALL and the deprecated VHOST constant. */
27 ms_subdomain_constants();
29 if ( !isset( $current_site ) || !isset( $current_blog ) ) {
31 // Given the domain and path, let's try to identify the network and site.
32 // Usually, it's easier to query the site first, which declares its network.
33 // In limited situations, though, we either can or must find the network first.
35 $domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ) );
36 if ( substr( $domain, -3 ) == ':80' ) {
37 $domain = substr( $domain, 0, -3 );
38 $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
39 } elseif ( substr( $domain, -4 ) == ':443' ) {
40 $domain = substr( $domain, 0, -4 );
41 $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
44 $path = stripslashes( $_SERVER['REQUEST_URI'] );
46 $path = preg_replace( '#(.*)/wp-admin/.*#', '$1/', $path );
48 list( $path ) = explode( '?', $path );
50 // If the network is defined in wp-config.php, we can simply use that.
51 if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
52 $current_site = new stdClass;
53 $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
54 $current_site->domain = DOMAIN_CURRENT_SITE;
55 $current_site->path = PATH_CURRENT_SITE;
56 if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
57 $current_site->blog_id = BLOG_ID_CURRENT_SITE;
58 } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
59 $current_site->blog_id = BLOGID_CURRENT_SITE;
62 if ( 0 === strcasecmp( $current_site->domain, $domain ) && 0 === strcasecmp( $current_site->path, $path ) ) {
63 $current_blog = get_site_by_path( $domain, $path );
64 } elseif ( '/' !== $current_site->path && 0 === strcasecmp( $current_site->domain, $domain ) && 0 === stripos( $path, $current_site->path ) ) {
65 // If the current network has a path and also matches the domain and path of the request,
66 // we need to look for a site using the first path segment following the network's path.
67 $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) );
69 // Otherwise, use the first path segment (as usual).
70 $current_blog = get_site_by_path( $domain, $path, 1 );
73 } elseif ( ! is_subdomain_install() ) {
75 * A "subdomain" install can be re-interpreted to mean "can support any domain".
76 * If we're not dealing with one of these installs, then the important part is determining
77 * the network first, because we need the network's path to identify any sites.
79 if ( ! $current_site = wp_cache_get( 'current_network', 'site-options' ) ) {
80 // Are there even two networks installed?
81 $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic]
82 if ( 1 === $wpdb->num_rows ) {
83 $current_site = new WP_Network( $one_network );
84 wp_cache_add( 'current_network', $current_site, 'site-options' );
85 } elseif ( 0 === $wpdb->num_rows ) {
86 ms_not_installed( $domain, $path );
89 if ( empty( $current_site ) ) {
90 $current_site = WP_Network::get_by_path( $domain, $path, 1 );
93 if ( empty( $current_site ) ) {
95 * Fires when a network cannot be found based on the requested domain and path.
97 * At the time of this action, the only recourse is to redirect somewhere
98 * and exit. If you want to declare a particular network, do so earlier.
102 * @param string $domain The domain used to search for a network.
103 * @param string $path The path used to search for a path.
105 do_action( 'ms_network_not_found', $domain, $path );
107 ms_not_installed( $domain, $path );
108 } elseif ( $path === $current_site->path ) {
109 $current_blog = get_site_by_path( $domain, $path );
111 // Search the network path + one more path segment (on top of the network path).
112 $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) );
115 // Find the site by the domain and at most the first path segment.
116 $current_blog = get_site_by_path( $domain, $path, 1 );
117 if ( $current_blog ) {
118 $current_site = WP_Network::get_instance( $current_blog->site_id ? $current_blog->site_id : 1 );
120 // If you don't have a site with the same domain/path as a network, you're pretty screwed, but:
121 $current_site = WP_Network::get_by_path( $domain, $path, 1 );
125 // The network declared by the site trumps any constants.
126 if ( $current_blog && $current_blog->site_id != $current_site->id ) {
127 $current_site = WP_Network::get_instance( $current_blog->site_id );
130 // No network has been found, bail.
131 if ( empty( $current_site ) ) {
132 /** This action is documented in wp-includes/ms-settings.php */
133 do_action( 'ms_network_not_found', $domain, $path );
135 ms_not_installed( $domain, $path );
138 // @todo Investigate when exactly this can occur.
139 if ( empty( $current_blog ) && wp_installing() ) {
140 $current_blog = new stdClass;
141 $current_blog->blog_id = $blog_id = 1;
144 // No site has been found, bail.
145 if ( empty( $current_blog ) ) {
146 // We're going to redirect to the network URL, with some possible modifications.
147 $scheme = is_ssl() ? 'https' : 'http';
148 $destination = "$scheme://{$current_site->domain}{$current_site->path}";
151 * Fires when a network can be determined but a site cannot.
153 * At the time of this action, the only recourse is to redirect somewhere
154 * and exit. If you want to declare a particular site, do so earlier.
158 * @param object $current_site The network that had been determined.
159 * @param string $domain The domain used to search for a site.
160 * @param string $path The path used to search for a site.
162 do_action( 'ms_site_not_found', $current_site, $domain, $path );
164 if ( is_subdomain_install() && ! defined( 'NOBLOGREDIRECT' ) ) {
165 // For a "subdomain" install, redirect to the signup form specifically.
166 $destination .= 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
167 } elseif ( is_subdomain_install() ) {
168 // For a "subdomain" install, the NOBLOGREDIRECT constant
169 // can be used to avoid a redirect to the signup form.
170 // Using the ms_site_not_found action is preferred to the constant.
171 if ( '%siteurl%' !== NOBLOGREDIRECT ) {
172 $destination = NOBLOGREDIRECT;
174 } elseif ( 0 === strcasecmp( $current_site->domain, $domain ) ) {
176 * If the domain we were searching for matches the network's domain,
177 * it's no use redirecting back to ourselves -- it'll cause a loop.
178 * As we couldn't find a site, we're simply not installed.
180 ms_not_installed( $domain, $path );
183 header( 'Location: ' . $destination );
187 // Figure out the current network's main site.
188 if ( empty( $current_site->blog_id ) ) {
189 if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
190 $current_site->blog_id = $current_blog->blog_id;
191 } elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) {
192 $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
193 $current_site->domain, $current_site->path ) );
194 wp_cache_add( 'network:' . $current_site->id . ':main_site', $current_site->blog_id, 'site-options' );
198 $blog_id = $current_blog->blog_id;
199 $public = $current_blog->public;
201 if ( empty( $current_blog->site_id ) ) {
202 // This dates to [MU134] and shouldn't be relevant anymore,
203 // but it could be possible for arguments passed to insert_blog() etc.
204 $current_blog->site_id = 1;
207 $site_id = $current_blog->site_id;
208 wp_load_core_site_options( $site_id );
211 $wpdb->set_prefix( $table_prefix, false ); // $table_prefix can be set in sunrise.php
212 $wpdb->set_blog_id( $current_blog->blog_id, $current_blog->site_id );
213 $table_prefix = $wpdb->get_blog_prefix();
214 $_wp_switched_stack = array();
217 // need to init cache again after blog_id is set
218 wp_start_object_cache();
220 if ( ! $current_site instanceof WP_Network ) {
221 $current_site = new WP_Network( $current_site );
224 // Define upload directory constants
225 ms_upload_constants();