Changeset 37475
- Timestamp:
- 05/20/2016 08:56:54 PM (9 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/ms-load.php
r35668 r37475 259 259 260 260 return false; 261 } 262 263 /** 264 * Identifies the network and site of a requested domain and path and populates the 265 * corresponding network and site global objects as part of the multisite bootstrap process. 266 * 267 * Prior to 4.6.0, this was a procedural block in `ms-settings.php`. It was wrapped into 268 * a function to facilitate unit tests. It should not be used outside of core. 269 * 270 * Usually, it's easier to query the site first, which then declares its network. 271 * In limited situations, we either can or must find the network first. 272 * 273 * If a network and site are found, a `true` response will be returned so that the 274 * request can continue. 275 * 276 * If neither a network or site is found, `false` or a URL string will be returned 277 * so that either an error can be shown or a redirect can occur. 278 * 279 * @since 4.6.0 280 * @access private 281 * 282 * @global wpdb $wpdb WordPress database abstraction object. 283 * @global WP_Network $current_site The current network. 284 * @global WP_Site $current_blog The current site. 285 * 286 * @param string $domain The requested domain. 287 * @param string $path The requested path. 288 * @param bool $subdomain Whether a subdomain (true) or subdirectory (false) configuration. 289 * @return bool|string True if bootstrap successfully populated `$current_blog` and `$current_site`. 290 * False if bootstrap could not be properly completed. 291 * Redirect URL if parts exist, but the request as a whole can not be fulfilled. 292 */ 293 function ms_load_current_site_and_network( $domain, $path, $subdomain = false ) { 294 global $wpdb, $current_site, $current_blog; 295 296 // If the network is defined in wp-config.php, we can simply use that. 297 if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) { 298 $current_site = new stdClass; 299 $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1; 300 $current_site->domain = DOMAIN_CURRENT_SITE; 301 $current_site->path = PATH_CURRENT_SITE; 302 if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) { 303 $current_site->blog_id = BLOG_ID_CURRENT_SITE; 304 } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated. 305 $current_site->blog_id = BLOGID_CURRENT_SITE; 306 } 307 308 if ( 0 === strcasecmp( $current_site->domain, $domain ) && 0 === strcasecmp( $current_site->path, $path ) ) { 309 $current_blog = get_site_by_path( $domain, $path ); 310 } elseif ( '/' !== $current_site->path && 0 === strcasecmp( $current_site->domain, $domain ) && 0 === stripos( $path, $current_site->path ) ) { 311 // If the current network has a path and also matches the domain and path of the request, 312 // we need to look for a site using the first path segment following the network's path. 313 $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) ); 314 } else { 315 // Otherwise, use the first path segment (as usual). 316 $current_blog = get_site_by_path( $domain, $path, 1 ); 317 } 318 319 } elseif ( ! $subdomain ) { 320 /* 321 * A "subdomain" install can be re-interpreted to mean "can support any domain". 322 * If we're not dealing with one of these installs, then the important part is determining 323 * the network first, because we need the network's path to identify any sites. 324 */ 325 if ( ! $current_site = wp_cache_get( 'current_network', 'site-options' ) ) { 326 // Are there even two networks installed? 327 $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic] 328 if ( 1 === $wpdb->num_rows ) { 329 $current_site = new WP_Network( $one_network ); 330 wp_cache_add( 'current_network', $current_site, 'site-options' ); 331 } elseif ( 0 === $wpdb->num_rows ) { 332 // A network not found hook should fire here. 333 return false; 334 } 335 } 336 337 if ( empty( $current_site ) ) { 338 $current_site = WP_Network::get_by_path( $domain, $path, 1 ); 339 } 340 341 if ( empty( $current_site ) ) { 342 /** 343 * Fires when a network cannot be found based on the requested domain and path. 344 * 345 * At the time of this action, the only recourse is to redirect somewhere 346 * and exit. If you want to declare a particular network, do so earlier. 347 * 348 * @since 4.4.0 349 * 350 * @param string $domain The domain used to search for a network. 351 * @param string $path The path used to search for a path. 352 */ 353 do_action( 'ms_network_not_found', $domain, $path ); 354 355 return false; 356 } elseif ( $path === $current_site->path ) { 357 $current_blog = get_site_by_path( $domain, $path ); 358 } else { 359 // Search the network path + one more path segment (on top of the network path). 360 $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) ); 361 } 362 } else { 363 // Find the site by the domain and at most the first path segment. 364 $current_blog = get_site_by_path( $domain, $path, 1 ); 365 if ( $current_blog ) { 366 $current_site = WP_Network::get_instance( $current_blog->site_id ? $current_blog->site_id : 1 ); 367 } else { 368 // If you don't have a site with the same domain/path as a network, you're pretty screwed, but: 369 $current_site = WP_Network::get_by_path( $domain, $path, 1 ); 370 } 371 } 372 373 // The network declared by the site trumps any constants. 374 if ( $current_blog && $current_blog->site_id != $current_site->id ) { 375 $current_site = WP_Network::get_instance( $current_blog->site_id ); 376 } 377 378 // No network has been found, bail. 379 if ( empty( $current_site ) ) { 380 /** This action is documented in wp-includes/ms-settings.php */ 381 do_action( 'ms_network_not_found', $domain, $path ); 382 383 return false; 384 } 385 386 // During activation of a new subdomain, the requested site does not yet exist. 387 if ( empty( $current_blog ) && wp_installing() ) { 388 $current_blog = new stdClass; 389 $current_blog->blog_id = $blog_id = 1; 390 $current_blog->public = 1; 391 } 392 393 // No site has been found, bail. 394 if ( empty( $current_blog ) ) { 395 // We're going to redirect to the network URL, with some possible modifications. 396 $scheme = is_ssl() ? 'https' : 'http'; 397 $destination = "$scheme://{$current_site->domain}{$current_site->path}"; 398 399 /** 400 * Fires when a network can be determined but a site cannot. 401 * 402 * At the time of this action, the only recourse is to redirect somewhere 403 * and exit. If you want to declare a particular site, do so earlier. 404 * 405 * @since 3.9.0 406 * 407 * @param object $current_site The network that had been determined. 408 * @param string $domain The domain used to search for a site. 409 * @param string $path The path used to search for a site. 410 */ 411 do_action( 'ms_site_not_found', $current_site, $domain, $path ); 412 413 if ( $subdomain && ! defined( 'NOBLOGREDIRECT' ) ) { 414 // For a "subdomain" install, redirect to the signup form specifically. 415 $destination .= 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain ); 416 } elseif ( $subdomain ) { 417 // For a "subdomain" install, the NOBLOGREDIRECT constant 418 // can be used to avoid a redirect to the signup form. 419 // Using the ms_site_not_found action is preferred to the constant. 420 if ( '%siteurl%' !== NOBLOGREDIRECT ) { 421 $destination = NOBLOGREDIRECT; 422 } 423 } elseif ( 0 === strcasecmp( $current_site->domain, $domain ) ) { 424 /* 425 * If the domain we were searching for matches the network's domain, 426 * it's no use redirecting back to ourselves -- it'll cause a loop. 427 * As we couldn't find a site, we're simply not installed. 428 */ 429 return false; 430 } 431 432 return $destination; 433 } 434 435 // Figure out the current network's main site. 436 if ( empty( $current_site->blog_id ) ) { 437 if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) { 438 $current_site->blog_id = $current_blog->blog_id; 439 } elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) { 440 $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", 441 $current_site->domain, $current_site->path ) ); 442 wp_cache_add( 'network:' . $current_site->id . ':main_site', $current_site->blog_id, 'site-options' ); 443 } 444 } 445 446 return true; 261 447 } 262 448 -
trunk/src/wp-includes/ms-settings.php
r37226 r37475 42 42 ms_subdomain_constants(); 43 43 44 // This block will process a request if the current network or current site objects 45 // have not been populated in the global scope through something like `sunrise.php`. 44 46 if ( !isset( $current_site ) || !isset( $current_blog ) ) { 45 46 // Given the domain and path, let's try to identify the network and site.47 // Usually, it's easier to query the site first, which declares its network.48 // In limited situations, though, we either can or must find the network first.49 47 50 48 $domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ) ); … … 63 61 list( $path ) = explode( '?', $path ); 64 62 65 // If the network is defined in wp-config.php, we can simply use that. 66 if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) { 67 $current_site = new stdClass; 68 $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1; 69 $current_site->domain = DOMAIN_CURRENT_SITE; 70 $current_site->path = PATH_CURRENT_SITE; 71 if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) { 72 $current_site->blog_id = BLOG_ID_CURRENT_SITE; 73 } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated. 74 $current_site->blog_id = BLOGID_CURRENT_SITE; 75 } 63 $bootstrap_result = ms_load_current_site_and_network( $domain, $path, is_subdomain_install() ); 76 64 77 if ( 0 === strcasecmp( $current_site->domain, $domain ) && 0 === strcasecmp( $current_site->path, $path ) ) { 78 $current_blog = get_site_by_path( $domain, $path ); 79 } elseif ( '/' !== $current_site->path && 0 === strcasecmp( $current_site->domain, $domain ) && 0 === stripos( $path, $current_site->path ) ) { 80 // If the current network has a path and also matches the domain and path of the request, 81 // we need to look for a site using the first path segment following the network's path. 82 $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) ); 83 } else { 84 // Otherwise, use the first path segment (as usual). 85 $current_blog = get_site_by_path( $domain, $path, 1 ); 86 } 87 88 } elseif ( ! is_subdomain_install() ) { 89 /* 90 * A "subdomain" install can be re-interpreted to mean "can support any domain". 91 * If we're not dealing with one of these installs, then the important part is determining 92 * the network first, because we need the network's path to identify any sites. 93 */ 94 if ( ! $current_site = wp_cache_get( 'current_network', 'site-options' ) ) { 95 // Are there even two networks installed? 96 $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic] 97 if ( 1 === $wpdb->num_rows ) { 98 $current_site = new WP_Network( $one_network ); 99 wp_cache_add( 'current_network', $current_site, 'site-options' ); 100 } elseif ( 0 === $wpdb->num_rows ) { 101 ms_not_installed( $domain, $path ); 102 } 103 } 104 if ( empty( $current_site ) ) { 105 $current_site = WP_Network::get_by_path( $domain, $path, 1 ); 106 } 107 108 if ( empty( $current_site ) ) { 109 /** 110 * Fires when a network cannot be found based on the requested domain and path. 111 * 112 * At the time of this action, the only recourse is to redirect somewhere 113 * and exit. If you want to declare a particular network, do so earlier. 114 * 115 * @since 4.4.0 116 * 117 * @param string $domain The domain used to search for a network. 118 * @param string $path The path used to search for a path. 119 */ 120 do_action( 'ms_network_not_found', $domain, $path ); 121 122 ms_not_installed( $domain, $path ); 123 } elseif ( $path === $current_site->path ) { 124 $current_blog = get_site_by_path( $domain, $path ); 125 } else { 126 // Search the network path + one more path segment (on top of the network path). 127 $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) ); 128 } 65 if ( true === $bootstrap_result ) { 66 // `$current_blog` and `$current_site are now populated. 67 } elseif ( false === $bootstrap_result ) { 68 ms_not_installed( $domain, $path ); 129 69 } else { 130 // Find the site by the domain and at most the first path segment. 131 $current_blog = get_site_by_path( $domain, $path, 1 ); 132 if ( $current_blog ) { 133 $current_site = WP_Network::get_instance( $current_blog->site_id ? $current_blog->site_id : 1 ); 134 } else { 135 // If you don't have a site with the same domain/path as a network, you're pretty screwed, but: 136 $current_site = WP_Network::get_by_path( $domain, $path, 1 ); 137 } 138 } 139 140 // The network declared by the site trumps any constants. 141 if ( $current_blog && $current_blog->site_id != $current_site->id ) { 142 $current_site = WP_Network::get_instance( $current_blog->site_id ); 143 } 144 145 // No network has been found, bail. 146 if ( empty( $current_site ) ) { 147 /** This action is documented in wp-includes/ms-settings.php */ 148 do_action( 'ms_network_not_found', $domain, $path ); 149 150 ms_not_installed( $domain, $path ); 151 } 152 153 // During activation of a new subdomain, the requested site does not yet exist. 154 if ( empty( $current_blog ) && wp_installing() ) { 155 $current_blog = new stdClass; 156 $current_blog->blog_id = $blog_id = 1; 157 $current_blog->public = 1; 158 } 159 160 // No site has been found, bail. 161 if ( empty( $current_blog ) ) { 162 // We're going to redirect to the network URL, with some possible modifications. 163 $scheme = is_ssl() ? 'https' : 'http'; 164 $destination = "$scheme://{$current_site->domain}{$current_site->path}"; 165 166 /** 167 * Fires when a network can be determined but a site cannot. 168 * 169 * At the time of this action, the only recourse is to redirect somewhere 170 * and exit. If you want to declare a particular site, do so earlier. 171 * 172 * @since 3.9.0 173 * 174 * @param object $current_site The network that had been determined. 175 * @param string $domain The domain used to search for a site. 176 * @param string $path The path used to search for a site. 177 */ 178 do_action( 'ms_site_not_found', $current_site, $domain, $path ); 179 180 if ( is_subdomain_install() && ! defined( 'NOBLOGREDIRECT' ) ) { 181 // For a "subdomain" install, redirect to the signup form specifically. 182 $destination .= 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain ); 183 } elseif ( is_subdomain_install() ) { 184 // For a "subdomain" install, the NOBLOGREDIRECT constant 185 // can be used to avoid a redirect to the signup form. 186 // Using the ms_site_not_found action is preferred to the constant. 187 if ( '%siteurl%' !== NOBLOGREDIRECT ) { 188 $destination = NOBLOGREDIRECT; 189 } 190 } elseif ( 0 === strcasecmp( $current_site->domain, $domain ) ) { 191 /* 192 * If the domain we were searching for matches the network's domain, 193 * it's no use redirecting back to ourselves -- it'll cause a loop. 194 * As we couldn't find a site, we're simply not installed. 195 */ 196 ms_not_installed( $domain, $path ); 197 } 198 199 header( 'Location: ' . $destination ); 70 header( 'Location: ' . $bootstrap_result ); 200 71 exit; 201 72 } 202 203 // Figure out the current network's main site. 204 if ( empty( $current_site->blog_id ) ) { 205 if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) { 206 $current_site->blog_id = $current_blog->blog_id; 207 } elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) { 208 $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", 209 $current_site->domain, $current_site->path ) ); 210 wp_cache_add( 'network:' . $current_site->id . ':main_site', $current_site->blog_id, 'site-options' ); 211 } 212 } 73 unset( $bootstrap_result ); 213 74 214 75 $blog_id = $current_blog->blog_id; -
trunk/tests/phpunit/tests/multisite/bootstrap.php
r37241 r37475 159 159 ); 160 160 161 $this->_setup_host_request( $domain, $path );161 ms_load_current_site_and_network( $domain, $path ); 162 162 $actual = array( 163 163 'network_id' => $current_blog->site_id, 164 164 'site_id' => $current_blog->blog_id, 165 165 ); 166 $this->_setup_host_request( WP_TESTS_DOMAIN, '/' );166 ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' ); 167 167 168 168 $this->assertEqualSetsWithIndex( $expected, $actual ); … … 198 198 ); 199 199 add_filter( 'site_by_path_segments_count', array( $this, 'filter_path_segments_to_two' ) ); 200 $this->_setup_host_request( 'wordpress.org', '/foo/bar/' );200 ms_load_current_site_and_network( 'wordpress.org', '/foo/bar/' ); 201 201 $actual = array( 202 202 'network_id' => $current_blog->site_id, … … 204 204 ); 205 205 remove_filter( 'site_by_path_segments_count', array( $this, 'filter_path_segments_to_two' ) ); 206 $this->_setup_host_request( WP_TESTS_DOMAIN, '/' );206 ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' ); 207 207 208 208 $this->assertEqualSetsWithIndex( $expected, $actual ); … … 212 212 return 2; 213 213 } 214 215 /**216 * Reset various globals required for a 'clean' multisite boot.217 *218 * The $wpdb and $table_prefix globals are required for ms-settings.php to219 * load properly.220 *221 * @param string $domain HTTP_HOST of the bootstrap request.222 * @param string $path REQUEST_URI of the boot strap request.223 */224 function _setup_host_request( $domain, $path ) {225 global $current_site, $current_blog, $table_prefix, $wpdb;226 227 $table_prefix = WP_TESTS_TABLE_PREFIX;228 $current_site = $current_blog = null;229 $_SERVER['HTTP_HOST'] = $domain;230 $_SERVER['REQUEST_URI'] = $path;231 232 include ABSPATH . '/wp-includes/ms-settings.php';233 }234 214 } 235 215
Note: See TracChangeset
for help on using the changeset viewer.