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' );
17 require_once( ABSPATH . WPINC . '/class-wp-site.php' );
19 /** Multisite loader */
20 require_once( ABSPATH . WPINC . '/ms-load.php' );
22 /** Default Multisite constants */
23 require_once( ABSPATH . WPINC . '/ms-default-constants.php' );
25 if ( defined( 'SUNRISE' ) ) {
26 include_once( WP_CONTENT_DIR . '/sunrise.php' );
29 /** Check for and define SUBDOMAIN_INSTALL and the deprecated VHOST constant. */
30 ms_subdomain_constants();
32 if ( !isset( $current_site ) || !isset( $current_blog ) ) {
34 // Given the domain and path, let's try to identify the network and site.
35 // Usually, it's easier to query the site first, which declares its network.
36 // In limited situations, though, we either can or must find the network first.
38 $domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ) );
39 if ( substr( $domain, -3 ) == ':80' ) {
40 $domain = substr( $domain, 0, -3 );
41 $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
42 } elseif ( substr( $domain, -4 ) == ':443' ) {
43 $domain = substr( $domain, 0, -4 );
44 $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
47 $path = stripslashes( $_SERVER['REQUEST_URI'] );
49 $path = preg_replace( '#(.*)/wp-admin/.*#', '$1/', $path );
51 list( $path ) = explode( '?', $path );
53 // If the network is defined in wp-config.php, we can simply use that.
54 if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
55 $current_site = new stdClass;
56 $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
57 $current_site->domain = DOMAIN_CURRENT_SITE;
58 $current_site->path = PATH_CURRENT_SITE;
59 if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
60 $current_site->blog_id = BLOG_ID_CURRENT_SITE;
61 } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
62 $current_site->blog_id = BLOGID_CURRENT_SITE;
65 if ( 0 === strcasecmp( $current_site->domain, $domain ) && 0 === strcasecmp( $current_site->path, $path ) ) {
66 $current_blog = get_site_by_path( $domain, $path );
67 } elseif ( '/' !== $current_site->path && 0 === strcasecmp( $current_site->domain, $domain ) && 0 === stripos( $path, $current_site->path ) ) {
68 // If the current network has a path and also matches the domain and path of the request,
69 // we need to look for a site using the first path segment following the network's path.
70 $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) );
72 // Otherwise, use the first path segment (as usual).
73 $current_blog = get_site_by_path( $domain, $path, 1 );
76 } elseif ( ! is_subdomain_install() ) {
78 * A "subdomain" install can be re-interpreted to mean "can support any domain".
79 * If we're not dealing with one of these installs, then the important part is determining
80 * the network first, because we need the network's path to identify any sites.
82 if ( ! $current_site = wp_cache_get( 'current_network', 'site-options' ) ) {
83 // Are there even two networks installed?
84 $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic]
85 if ( 1 === $wpdb->num_rows ) {
86 $current_site = new WP_Network( $one_network );
87 wp_cache_add( 'current_network', $current_site, 'site-options' );
88 } elseif ( 0 === $wpdb->num_rows ) {
89 ms_not_installed( $domain, $path );
92 if ( empty( $current_site ) ) {
93 $current_site = WP_Network::get_by_path( $domain, $path, 1 );
96 if ( empty( $current_site ) ) {
98 * Fires when a network cannot be found based on the requested domain and path.
100 * At the time of this action, the only recourse is to redirect somewhere
101 * and exit. If you want to declare a particular network, do so earlier.
105 * @param string $domain The domain used to search for a network.
106 * @param string $path The path used to search for a path.
108 do_action( 'ms_network_not_found', $domain, $path );
110 ms_not_installed( $domain, $path );
111 } elseif ( $path === $current_site->path ) {
112 $current_blog = get_site_by_path( $domain, $path );
114 // Search the network path + one more path segment (on top of the network path).
115 $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) );
118 // Find the site by the domain and at most the first path segment.
119 $current_blog = get_site_by_path( $domain, $path, 1 );
120 if ( $current_blog ) {
121 $current_site = WP_Network::get_instance( $current_blog->site_id ? $current_blog->site_id : 1 );
123 // If you don't have a site with the same domain/path as a network, you're pretty screwed, but:
124 $current_site = WP_Network::get_by_path( $domain, $path, 1 );
128 // The network declared by the site trumps any constants.
129 if ( $current_blog && $current_blog->site_id != $current_site->id ) {
130 $current_site = WP_Network::get_instance( $current_blog->site_id );
133 // No network has been found, bail.
134 if ( empty( $current_site ) ) {
135 /** This action is documented in wp-includes/ms-settings.php */
136 do_action( 'ms_network_not_found', $domain, $path );
138 ms_not_installed( $domain, $path );
141 // During activation of a new subdomain, the requested site does not yet exist.
142 if ( empty( $current_blog ) && wp_installing() ) {
143 $current_blog = new stdClass;
144 $current_blog->blog_id = $blog_id = 1;
145 $current_blog->public = 1;
148 // No site has been found, bail.
149 if ( empty( $current_blog ) ) {
150 // We're going to redirect to the network URL, with some possible modifications.
151 $scheme = is_ssl() ? 'https' : 'http';
152 $destination = "$scheme://{$current_site->domain}{$current_site->path}";
155 * Fires when a network can be determined but a site cannot.
157 * At the time of this action, the only recourse is to redirect somewhere
158 * and exit. If you want to declare a particular site, do so earlier.
162 * @param object $current_site The network that had been determined.
163 * @param string $domain The domain used to search for a site.
164 * @param string $path The path used to search for a site.
166 do_action( 'ms_site_not_found', $current_site, $domain, $path );
168 if ( is_subdomain_install() && ! defined( 'NOBLOGREDIRECT' ) ) {
169 // For a "subdomain" install, redirect to the signup form specifically.
170 $destination .= 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
171 } elseif ( is_subdomain_install() ) {
172 // For a "subdomain" install, the NOBLOGREDIRECT constant
173 // can be used to avoid a redirect to the signup form.
174 // Using the ms_site_not_found action is preferred to the constant.
175 if ( '%siteurl%' !== NOBLOGREDIRECT ) {
176 $destination = NOBLOGREDIRECT;
178 } elseif ( 0 === strcasecmp( $current_site->domain, $domain ) ) {
180 * If the domain we were searching for matches the network's domain,
181 * it's no use redirecting back to ourselves -- it'll cause a loop.
182 * As we couldn't find a site, we're simply not installed.
184 ms_not_installed( $domain, $path );
187 header( 'Location: ' . $destination );
191 // Figure out the current network's main site.
192 if ( empty( $current_site->blog_id ) ) {
193 if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
194 $current_site->blog_id = $current_blog->blog_id;
195 } elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) {
196 $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
197 $current_site->domain, $current_site->path ) );
198 wp_cache_add( 'network:' . $current_site->id . ':main_site', $current_site->blog_id, 'site-options' );
202 $blog_id = $current_blog->blog_id;
203 $public = $current_blog->public;
205 if ( empty( $current_blog->site_id ) ) {
206 // This dates to [MU134] and shouldn't be relevant anymore,
207 // but it could be possible for arguments passed to insert_blog() etc.
208 $current_blog->site_id = 1;
211 $site_id = $current_blog->site_id;
212 wp_load_core_site_options( $site_id );
215 $wpdb->set_prefix( $table_prefix, false ); // $table_prefix can be set in sunrise.php
216 $wpdb->set_blog_id( $current_blog->blog_id, $current_blog->site_id );
217 $table_prefix = $wpdb->get_blog_prefix();
218 $_wp_switched_stack = array();
221 // need to init cache again after blog_id is set
222 wp_start_object_cache();
224 if ( ! $current_site instanceof WP_Network ) {
225 $current_site = new WP_Network( $current_site );
228 if ( ! $current_blog instanceof WP_Site ) {
229 $current_blog = new WP_Site( $current_blog );
232 // Define upload directory constants
233 ms_upload_constants();