Ticket #34941: 34941.3.diff
| File 34941.3.diff, 38.0 KB (added by , 10 years ago) |
|---|
-
src/wp-includes/ms-load.php
261 261 } 262 262 263 263 /** 264 * Identify the network and site of a requested domain and path and populate 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 * @access private 280 * @since 4.6.0 281 * 282 * @global wpdb $wpdb 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 * 290 * @return bool|string True if bootstrap successfully populated `$current_blog` and `$current_site`. 291 * False if bootstrap could not be properly completed. 292 * Redirect URL if parts exist, but the request as a whole can not be fulfilled. 293 */ 294 function ms_load_current_site_and_network( $domain, $path, $subdomain = false ) { 295 global $wpdb, $current_site, $current_blog; 296 297 // If the network is defined in wp-config.php, we can simply use that. 298 if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) { 299 $current_site = new stdClass; 300 $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1; 301 $current_site->domain = DOMAIN_CURRENT_SITE; 302 $current_site->path = PATH_CURRENT_SITE; 303 if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) { 304 $current_site->blog_id = BLOG_ID_CURRENT_SITE; 305 } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated. 306 $current_site->blog_id = BLOGID_CURRENT_SITE; 307 } 308 309 if ( 0 === strcasecmp( $current_site->domain, $domain ) && 0 === strcasecmp( $current_site->path, $path ) ) { 310 $current_blog = get_site_by_path( $domain, $path ); 311 } elseif ( '/' !== $current_site->path && 0 === strcasecmp( $current_site->domain, $domain ) && 0 === stripos( $path, $current_site->path ) ) { 312 // If the current network has a path and also matches the domain and path of the request, 313 // we need to look for a site using the first path segment following the network's path. 314 $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) ); 315 } else { 316 // Otherwise, use the first path segment (as usual). 317 $current_blog = get_site_by_path( $domain, $path, 1 ); 318 } 319 320 } elseif ( ! $subdomain ) { 321 /* 322 * A "subdomain" install can be re-interpreted to mean "can support any domain". 323 * If we're not dealing with one of these installs, then the important part is determining 324 * the network first, because we need the network's path to identify any sites. 325 */ 326 if ( ! $current_site = wp_cache_get( 'current_network', 'site-options' ) ) { 327 // Are there even two networks installed? 328 $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic] 329 if ( 1 === $wpdb->num_rows ) { 330 $current_site = new WP_Network( $one_network ); 331 wp_cache_add( 'current_network', $current_site, 'site-options' ); 332 } elseif ( 0 === $wpdb->num_rows ) { 333 // A network not found hook should fire here. 334 return false; 335 } 336 } 337 338 if ( empty( $current_site ) ) { 339 $current_site = WP_Network::get_by_path( $domain, $path, 1 ); 340 } 341 342 if ( empty( $current_site ) ) { 343 /** 344 * Fires when a network cannot be found based on the requested domain and path. 345 * 346 * At the time of this action, the only recourse is to redirect somewhere 347 * and exit. If you want to declare a particular network, do so earlier. 348 * 349 * @since 4.4.0 350 * 351 * @param string $domain The domain used to search for a network. 352 * @param string $path The path used to search for a path. 353 */ 354 do_action( 'ms_network_not_found', $domain, $path ); 355 356 return false; 357 } elseif ( $path === $current_site->path ) { 358 $current_blog = get_site_by_path( $domain, $path ); 359 } else { 360 // Search the network path + one more path segment (on top of the network path). 361 $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) ); 362 } 363 } else { 364 // Find the site by the domain and at most the first path segment. 365 $current_blog = get_site_by_path( $domain, $path, 1 ); 366 if ( $current_blog ) { 367 $current_site = WP_Network::get_instance( $current_blog->site_id ? $current_blog->site_id : 1 ); 368 } else { 369 // If you don't have a site with the same domain/path as a network, you're pretty screwed, but: 370 $current_site = WP_Network::get_by_path( $domain, $path, 1 ); 371 } 372 } 373 374 // The network declared by the site trumps any constants. 375 if ( $current_blog && $current_blog->site_id != $current_site->id ) { 376 $current_site = WP_Network::get_instance( $current_blog->site_id ); 377 } 378 379 // No network has been found, bail. 380 if ( empty( $current_site ) ) { 381 /** This action is documented in wp-includes/ms-settings.php */ 382 do_action( 'ms_network_not_found', $domain, $path ); 383 384 return false; 385 } 386 387 // During activation of a new subdomain, the requested site does not yet exist. 388 if ( empty( $current_blog ) && wp_installing() ) { 389 $current_blog = new stdClass; 390 $current_blog->blog_id = $blog_id = 1; 391 $current_blog->public = 1; 392 } 393 394 // No site has been found, bail. 395 if ( empty( $current_blog ) ) { 396 // We're going to redirect to the network URL, with some possible modifications. 397 $scheme = is_ssl() ? 'https' : 'http'; 398 $destination = "$scheme://{$current_site->domain}{$current_site->path}"; 399 400 /** 401 * Fires when a network can be determined but a site cannot. 402 * 403 * At the time of this action, the only recourse is to redirect somewhere 404 * and exit. If you want to declare a particular site, do so earlier. 405 * 406 * @since 3.9.0 407 * 408 * @param object $current_site The network that had been determined. 409 * @param string $domain The domain used to search for a site. 410 * @param string $path The path used to search for a site. 411 */ 412 do_action( 'ms_site_not_found', $current_site, $domain, $path ); 413 414 if ( $subdomain && ! defined( 'NOBLOGREDIRECT' ) ) { 415 // For a "subdomain" install, redirect to the signup form specifically. 416 $destination .= 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain ); 417 } elseif ( $subdomain ) { 418 // For a "subdomain" install, the NOBLOGREDIRECT constant 419 // can be used to avoid a redirect to the signup form. 420 // Using the ms_site_not_found action is preferred to the constant. 421 if ( '%siteurl%' !== NOBLOGREDIRECT ) { 422 $destination = NOBLOGREDIRECT; 423 } 424 } elseif ( 0 === strcasecmp( $current_site->domain, $domain ) ) { 425 /* 426 * If the domain we were searching for matches the network's domain, 427 * it's no use redirecting back to ourselves -- it'll cause a loop. 428 * As we couldn't find a site, we're simply not installed. 429 */ 430 return false; 431 } 432 433 return $destination; 434 } 435 436 // Figure out the current network's main site. 437 if ( empty( $current_site->blog_id ) ) { 438 if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) { 439 $current_site->blog_id = $current_blog->blog_id; 440 } elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) { 441 $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", 442 $current_site->domain, $current_site->path ) ); 443 wp_cache_add( 'network:' . $current_site->id . ':main_site', $current_site->blog_id, 'site-options' ); 444 } 445 } 446 447 return true; 448 } 449 450 /** 264 451 * Displays a failure message. 265 452 * 266 453 * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well. -
src/wp-includes/ms-settings.php
41 41 /** Check for and define SUBDOMAIN_INSTALL and the deprecated VHOST constant. */ 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 47 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 50 48 $domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ) ); 51 49 if ( substr( $domain, -3 ) == ':80' ) { 52 50 $domain = substr( $domain, 0, -3 ); … … 62 60 } 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 } 73 unset( $bootstrap_result ); 202 74 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 }213 214 75 $blog_id = $current_blog->blog_id; 215 76 $public = $current_blog->public; 216 77 -
tests/phpunit/tests/multisite/bootstrap.php
23 23 parent::tearDown(); 24 24 } 25 25 26 27 26 /** 28 27 * @ticket 27003 29 28 */ … … 160 159 /** 161 160 * @ticket 27884 162 161 */ 163 function test_m ultisite_bootstrap() {164 global $current_blog ;162 function test_ms_load_current_site_and_network_as_subdirectory_with_default_path_segments() { 163 global $current_blog, $current_site; 165 164 166 165 $network_ids = array( 167 166 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ), … … 186 185 } 187 186 unset( $id ); 188 187 189 $this->_setup_host_request( 'wordpress.org', '/' ); 190 $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id ); 191 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 188 $requests = array( 189 'wordpress.org/' => array( 190 'network_id' => $network_ids['wordpress.org/'], 191 'site_id' => $ids['wordpress.org/'], 192 'domain' => 'wordpress.org', 193 'path' => '/', 194 'response' => true, 195 'message' => false, 196 ), 197 'wordpress.org/2014/04/23/hello-world/' => array( 198 'network_id' => $network_ids['wordpress.org/'], 199 'site_id' => $ids['wordpress.org/'], 200 'domain' => 'wordpress.org', 201 'path' => '/2014/04/23/hello-world/', 202 'response' => true, 203 'message' => 'A post or 404 on a site should result in the correct site and network.', 204 ), 205 'wordpress.org/sample-page/' => array( 206 'network_id' => $network_ids['wordpress.org/'], 207 'site_id' => $ids['wordpress.org/'], 208 'domain' => 'wordpress.org', 209 'path' => '/sample-page/', 210 'response' => true, 211 'message' => 'A page or 404 on a site should result in the correct site and network.', 212 ), 213 'wordpress.org/?p=1' => array( 214 'network_id' => $network_ids['wordpress.org/'], 215 'site_id' => $ids['wordpress.org/'], 216 'domain' => 'wordpress.org', 217 'path' => '/?p=1', 218 'response' => true, 219 'message' => 'A non pretty-permalink URL should result in the correct site and network.', 220 ), 221 'wordpress.org/wp-admin/' => array( 222 'network_id' => $network_ids['wordpress.org/'], 223 'site_id' => $ids['wordpress.org/'], 224 'domain' => 'wordpress.org', 225 'path' => '/wp-admin/', 226 'response' => true, 227 'message' => 'The standard admin area of the main site should load the correct site and network.', 228 ), 229 'wordpress.org/wp-admin/network/' => array( 230 'network_id' => $network_ids['wordpress.org/'], 231 'site_id' => $ids['wordpress.org/'], 232 'domain' => 'wordpress.org', 233 'path' => '/', 234 'response' => true, 235 'message' => 'The network admin area of the main site should load the correct site and network.', 236 ), 237 // See #17376, this is not correct behavior. 238 'wordpress.org/invalid/wp-admin/users.php' => array( 239 'network_id' => $network_ids['wordpress.org/'], 240 'site_id' => $ids['wordpress.org/'], 241 'domain' => 'wordpress.org', 242 'path' => '/', 243 'response' => true, 244 'message' => 'If wp-admin is requested after an invalid path, the parent site and network should still load.', 245 ), 246 // See #17376, this is not correct behavior. 247 'wordpress.org/invalid/wp-admin/network/users.php' => array( 248 'network_id' => $network_ids['wordpress.org/'], 249 'site_id' => $ids['wordpress.org/'], 250 'domain' => 'wordpress.org', 251 'path' => '/', 252 'response' => true, 253 'message' => 'Valid', 254 ), 255 'wordpress.org/foo/' => array( 256 'network_id' => $network_ids['wordpress.org/'], 257 'site_id' => $ids['wordpress.org/foo/'], 258 'domain' => 'wordpress.org', 259 'path' => '/foo/', 260 'response' => true, 261 'message' => 'A valid sub-site should load the correct site and network.', 262 ), 263 'wordpress.org/FOO/' => array( 264 'network_id' => $network_ids['wordpress.org/'], 265 'site_id' => $ids['wordpress.org/foo/'], 266 'domain' => 'wordpress.org', 267 'path' => '/FOO/', 268 'response' => true, 269 'message' => 'A valid sub-site should not be case sensitive.', 270 ), 271 'wordpress.org/foo/2014/04/23/hello-world/' => array( 272 'network_id' => $network_ids['wordpress.org/'], 273 'site_id' => $ids['wordpress.org/foo/'], 274 'domain' => 'wordpress.org', 275 'path' => '/foo/2014/04/23/hello-world/', 276 'response' => true, 277 'message' => 'A post or 404 on a valid sub site should load the correct site and network.', 278 ), 279 'wordpress.org/foo/sample-page/' => array( 280 'network_id' => $network_ids['wordpress.org/'], 281 'site_id' => $ids['wordpress.org/foo/'], 282 'domain' => 'wordpress.org', 283 'path' => '/foo/sample-page/', 284 'response' => true, 285 'message' => 'A page or 404 on a valid sub site should load the correct site and network.', 286 ), 287 'wordpress.org/foo/?p=1' => array( 288 'network_id' => $network_ids['wordpress.org/'], 289 'site_id' => $ids['wordpress.org/foo/'], 290 'domain' => 'wordpress.org', 291 'path' => '/foo/?p=1', 292 'response' => true, 293 'message' => 'A non pretty-permalink URL on a valid sub site should load the correct site and network.', 294 ), 295 'wordpress.org/foo/wp-admin/' => array( 296 'network_id' => $network_ids['wordpress.org/'], 297 'site_id' => $ids['wordpress.org/foo/'], 298 'domain' => 'wordpress.org', 299 'path' => '/foo/wp-admin/', 300 'response' => true, 301 'message' => 'If wp-admin is requested on a valid sub-site, the correct network and site should load.', 302 ), 303 // This will eventually redirect to the main site, but not during bootstrap. 304 'wordpress.org/foo/wp-admin/network/' => array( 305 'network_id' => $network_ids['wordpress.org/'], 306 'site_id' => $ids['wordpress.org/foo/'], 307 'domain' => 'wordpress.org', 308 'path' => '/foo/wp-admin/network/', 309 'response' => true, 310 'message' => 'If the network admin is requested on a valid sub-site, the correct network and site should still populate.', 311 ), 312 'make.wordpress.org/' => array( 313 'network_id' => $network_ids['make.wordpress.org/'], 314 'site_id' => $ids['make.wordpress.org/'], 315 'domain' => 'make.wordpress.org', 316 'path' => '/', 317 'response' => true, 318 'message' => false, 319 ), 320 'make.wordpress.org/foo/' => array( 321 'network_id' => $network_ids['make.wordpress.org/'], 322 'site_id' => $ids['make.wordpress.org/foo/'], 323 'domain' => 'make.wordpress.org', 324 'path' => '/foo/', 325 'response' => true, 326 'message' => false, 327 ), 328 'core.wordpress.org/' => array( 329 'domain' => 'core.wordpress.org', 330 'path' => '/', 331 'response' => 'http://wordpress.org/', 332 'message' => 'An invalid subdomain should redirect to the top level network address.', 333 ), 334 'invalidsite.org/' => array( 335 'domain' => 'invalidsite.org', 336 'path' => '/', 337 'response' => false, 338 'message' => 'An invalid site/network should result in false.', 339 ), 340 'sub.invalidsite.org/' => array( 341 'domain' => 'invalidsite.org', 342 'path' => '/', 343 'response' => false, 344 'message' => 'An invalid subdomain for an invalid top level domain should result in false.', 345 ), 346 ); 192 347 193 $this->_setup_host_request( 'wordpress.org', '/2014/04/23/hello-world/' ); 194 $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id ); 195 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 348 /** 349 * Loop through the possible requests and assert that the proper string/true/false response is received 350 * from `ms_bootstrap`. 351 * 352 * In cases where a successful response is expected, the blog_id and site_id properties of `$current_blog` 353 * should also be checked. 354 */ 355 foreach( $requests as $request_url => $request_data ) { 356 $requests[ $request_url ]['actual']['response'] = ms_load_current_site_and_network( $request_data['domain'], $request_data['path'], false ); 196 357 197 $this->_setup_host_request( 'wordpress.org', '/sample-page/' ); 198 $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id ); 199 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 358 if ( true === $request_data['response'] ) { 359 $requests[ $request_url ]['actual']['blog_id'] = $current_blog->blog_id; 360 $requests[ $request_url ]['actual']['site_id'] = $current_site->id; 361 } 362 } 200 363 201 $this->_setup_host_request( 'wordpress.org', '/?p=1' ); 202 $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id ); 203 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 364 // Request the original tests domain and path to unpollute the stack. 365 ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' ); 204 366 205 $this->_setup_host_request( 'wordpress.org', '/wp-admin/' ); 206 $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id ); 207 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 367 // Process assertions against expected and actual. 368 foreach( $requests as $request_data ) { 369 $message = $request_data['domain'] . $request_data['path'] . ' ' . $request_data['message']; 370 $this->assertEquals( $request_data['response'], $request_data['actual']['response'], $message ); 208 371 209 $this->_setup_host_request( 'wordpress.org', '/foo/' ); 210 $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id ); 211 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 372 if ( true === $request_data['response'] ) { 373 $this->assertEquals( $request_data['site_id'], $request_data['actual']['blog_id'], $message ); 374 $this->assertEquals( $request_data['network_id'], $request_data['actual']['site_id'], $message ); 375 } 376 } 377 } 212 378 213 $this->_setup_host_request( 'wordpress.org', '/FOO/' ); 214 $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id ); 215 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 379 /** 380 * @ticket 27884 381 */ 382 function test_ms_load_current_site_and_network_as_subdirectory_with_multiple_path_segments() { 383 global $current_blog, $current_site; 216 384 217 $ this->_setup_host_request( 'wordpress.org', '/foo/2014/04/23/hello-world/' );218 $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );219 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id);385 $network_ids = array( 386 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ), 387 ); 220 388 221 $this->_setup_host_request( 'wordpress.org', '/foo/sample-page/' ); 222 $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id ); 223 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 389 foreach ( $network_ids as &$id ) { 390 $id = self::factory()->network->create( $id ); 391 } 392 unset( $id ); 224 393 225 $this->_setup_host_request( 'wordpress.org', '/foo/?p=1' ); 226 $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id ); 227 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 394 $ids = array( 395 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/', 'site_id' => $network_ids['wordpress.org/'] ), 396 'wordpress.org/foo/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'site_id' => $network_ids['wordpress.org/'] ), 397 'wordpress.org/foo/bar/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/bar/', 'site_id' => $network_ids['wordpress.org/'] ), 398 ); 228 399 229 $this->_setup_host_request( 'wordpress.org', '/foo/wp-admin/' ); 230 $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id ); 231 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 400 foreach ( $ids as &$id ) { 401 $id = self::factory()->blog->create( $id ); 402 } 403 unset( $id ); 232 404 233 // @todo not currently passing. 234 //$this->_setup_host_request( 'wordpress.org', '/foo/bar/' ); 235 //$this->assertEquals( $ids['wordpress.org/foo/bar/'], $current_blog->blog_id ); 236 //$this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id ); 405 $requests = array( 406 'wordpress.org/' => array( 407 'network_id' => $network_ids['wordpress.org/'], 408 'site_id' => $ids['wordpress.org/'], 409 'domain' => 'wordpress.org', 410 'path' => '/', 411 'response' => true, 412 'message' => 'A site with 0 path segments should work when site_by_path_segments_count is 2.', 413 ), 414 'wordpress.org/foo/' => array( 415 'network_id' => $network_ids['wordpress.org/'], 416 'site_id' => $ids['wordpress.org/foo/'], 417 'domain' => 'wordpress.org', 418 'path' => '/foo/', 419 'response' => true, 420 'message' => 'A site with 1 path segment should work when site_by_path_segments_count is 2.', 421 ), 422 'wordpress.org/foo/bar/' => array( 423 'network_id' => $network_ids['wordpress.org/'], 424 'site_id' => $ids['wordpress.org/foo/bar/'], 425 'domain' => 'wordpress.org', 426 'path' => '/foo/bar/', 427 'response' => true, 428 'message' => 'A site with 2 path segments should work when site_by_path_segments_count is 3.', 429 ), 430 'wordpress.org/foo/bar/taco/' => array( 431 'network_id' => $network_ids['wordpress.org/'], 432 'site_id' => $ids['wordpress.org/foo/bar/'], 433 'domain' => 'wordpress.org', 434 'path' => '/foo/bar/taco/', 435 'response' => true, 436 'message' => 'A request with 3 path segments should find its matching site with 2 path segments.', 437 ), 438 'wordpress.org/foo/taco/' => array( 439 'network_id' => $network_ids['wordpress.org/'], 440 'site_id' => $ids['wordpress.org/foo/'], 441 'domain' => 'wordpress.org', 442 'path' => '/foo/taco/', 443 'response' => true, 444 'message' => 'A request with 2 path segments should find its matching site with 1 path segment.', 445 ), 446 ); 237 447 238 $this->_setup_host_request( 'make.wordpress.org', '/' ); 239 $this->assertEquals( $ids['make.wordpress.org/'], $current_blog->blog_id ); 240 $this->assertEquals( $network_ids['make.wordpress.org/'], $current_blog->site_id ); 448 add_filter( 'site_by_path_segments_count', array( $this, 'site_by_path_segments_count' ) ); 449 /** 450 * Loop through the possible requests and assert that the proper string/true/false response is received 451 * from `ms_bootstrap`. 452 * 453 * In cases where a successful response is expected, the blog_id and site_id properties of `$current_blog` 454 * should also be checked. 455 */ 456 foreach( $requests as $request_url => $request_data ) { 457 $requests[ $request_url ]['actual']['response'] = ms_load_current_site_and_network( $request_data['domain'], $request_data['path'], false ); 241 458 242 $this->_setup_host_request( 'make.wordpress.org', '/foo/' ); 243 $this->assertEquals( $ids['make.wordpress.org/foo/'], $current_blog->blog_id ); 244 $this->assertEquals( $network_ids['make.wordpress.org/'], $current_blog->site_id ); 459 if ( true === $request_data['response'] ) { 460 $requests[ $request_url ]['actual']['blog_id'] = $current_blog->blog_id; 461 $requests[ $request_url ]['actual']['site_id'] = $current_site->id; 462 } 463 } 464 remove_filter( 'site_by_path_segments_count', array( $this, 'site_by_path_segments_count' ) ); 245 465 246 466 // Request the original tests domain and path to unpollute the stack. 247 $this->_setup_host_request( WP_TESTS_DOMAIN, '/' ); 467 ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' ); 468 469 // Process assertions against expected and actual. 470 foreach( $requests as $request_data ) { 471 $message = $request_data['domain'] . $request_data['path'] . ' ' . $request_data['message']; 472 $this->assertEquals( $request_data['response'], $request_data['actual']['response'], $message ); 473 474 if ( true === $request_data['response'] ) { 475 $this->assertEquals( $request_data['site_id'], $request_data['actual']['blog_id'], $message ); 476 $this->assertEquals( $request_data['network_id'], $request_data['actual']['site_id'], $message ); 477 } 478 } 248 479 } 249 480 250 481 /** 251 * Reset various globals required for a 'clean' multisite boot.482 * Used as a callback to test multiple site paths. 252 483 * 253 * The $wpdb and $table_prefix globals are required for ms-settings.php to 254 * load properly. 255 * 256 * @param string $domain HTTP_HOST of the bootstrap request. 257 * @param string $path REQUEST_URI of the boot strap request. 484 * @return int 258 485 */ 259 function _setup_host_request( $domain, $path ) { 260 global $current_site, $current_blog, $table_prefix, $wpdb; 486 public function site_by_path_segments_count() { 487 return 2; 488 } 261 489 262 $table_prefix = WP_TESTS_TABLE_PREFIX; 263 $current_site = $current_blog = null; 264 $_SERVER['HTTP_HOST'] = $domain; 265 $_SERVER['REQUEST_URI'] = $path; 490 /** 491 * @ticket 27884 492 */ 493 function test_ms_load_current_site_and_network_as_subdomain_with_default_path_segments() { 494 global $current_blog, $current_site; 266 495 267 include ABSPATH . '/wp-includes/ms-settings.php'; 496 $network_ids = array( 497 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ), 498 'make.wordpress.org/' => array( 'domain' => 'make.wordpress.org', 'path' => '/' ), 499 ); 500 501 foreach ( $network_ids as &$id ) { 502 $id = self::factory()->network->create( $id ); 503 } 504 unset( $id ); 505 506 $ids = array( 507 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/', 'site_id' => $network_ids['wordpress.org/'] ), 508 'foo.org/' => array( 'domain' => 'foo.org', 'path' => '/', 'site_id' => $network_ids['wordpress.org/'] ), 509 'foo.org/taco/' => array( 'domain' => 'foo.org', 'path' => '/taco/', 'site_id' => $network_ids['make.wordpress.org/'] ), 510 'bar.foo.org/' => array( 'domain' => 'bar.foo.org', 'path' => '/', 'site_id' => $network_ids['wordpress.org/'] ), 511 'bar.foo.org/taco/' => array( 'domain' => 'bar.foo.org', 'path' => '/taco/', 'site_id' => $network_ids['make.wordpress.org/'] ), 512 ); 513 514 foreach ( $ids as &$id ) { 515 $id = self::factory()->blog->create( $id ); 516 } 517 unset( $id ); 518 519 $requests = array( 520 'wordpress.org/' => array( 521 'network_id' => $network_ids['wordpress.org/'], 522 'site_id' => $ids['wordpress.org/'], 523 'domain' => 'wordpress.org', 524 'path' => '/', 525 'response' => true, 526 'message' => 'A top level domain should work when the domain is the same as the network.', 527 ), 528 'foo.org/' => array( 529 'network_id' => $network_ids['wordpress.org/'], 530 'site_id' => $ids['foo.org/'], 531 'domain' => 'foo.org', 532 'path' => '/', 533 'response' => true, 534 'message' => 'A top level domain should work when the domain is different from the network.', 535 ), 536 'bar.foo.org/' => array( 537 'network_id' => $network_ids['wordpress.org/'], 538 'site_id' => $ids['bar.foo.org/'], 539 'domain' => 'bar.foo.org', 540 'path' => '/', 541 'response' => true, 542 'message' => 'A subdomain should work even when its top level domain is different from the network.', 543 ), 544 'foo.org/taco/' => array( 545 'network_id' => $network_ids['make.wordpress.org/'], 546 'site_id' => $ids['foo.org/taco/'], 547 'domain' => 'foo.org', 548 'path' => '/taco/', 549 'response' => true, 550 'message' => 'Different paths attached to the same site domain can exist on different networks.', 551 ), 552 'bar.foo.org/taco/' => array( 553 'network_id' => $network_ids['make.wordpress.org/'], 554 'site_id' => $ids['bar.foo.org/taco/'], 555 'domain' => 'bar.foo.org', 556 'path' => '/taco/', 557 'response' => true, 558 'message' => 'Different paths attached to the same site subdomain can exist on different networks.', 559 ), 560 'invalid.org' => array( 561 'domain' => 'invalid.org', 562 'path' => '/', 563 'response' => false, 564 'message' => 'An invalid domain should not work.', 565 ), 566 'taco.foo.org/' => array( 567 'domain' => 'taco.foo.org', 568 'path' => '/', 569 'response' => false, 570 'message' => 'An invalid subdomain should not redirect if the top level domain is not a network.', 571 ), 572 'taco.wordpress.org/' => array( 573 'domain' => 'taco.wordpress.org', 574 'path' => '/', 575 'response' => 'http://wordpress.org/wp-signup.php?new=taco', 576 'message' => 'An invalid subdomain should redirect to signup if the top level domain is a network.', 577 ), 578 ); 579 580 /** 581 * Loop through the possible requests and assert that the proper string/true/false response is received 582 * from `ms_bootstrap`. 583 * 584 * In cases where a successful response is expected, the blog_id and site_id properties of `$current_blog` 585 * should also be checked. 586 */ 587 foreach( $requests as $request_url => $request_data ) { 588 $requests[ $request_url ]['actual']['response'] = ms_load_current_site_and_network( $request_data['domain'], $request_data['path'], true ); 589 590 if ( true === $request_data['response'] ) { 591 $requests[ $request_url ]['actual']['blog_id'] = $current_blog->blog_id; 592 $requests[ $request_url ]['actual']['site_id'] = $current_site->id; 593 } 594 } 595 596 // Request the original tests domain and path to unpollute the stack. 597 ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' ); 598 599 // Process assertions against expected and actual. 600 foreach( $requests as $request_data ) { 601 $message = $request_data['domain'] . $request_data['path'] . ' ' . $request_data['message']; 602 $this->assertEquals( $request_data['response'], $request_data['actual']['response'], $message ); 603 604 if ( true === $request_data['response'] ) { 605 $this->assertEquals( $request_data['site_id'], $request_data['actual']['blog_id'], $message ); 606 $this->assertEquals( $request_data['network_id'], $request_data['actual']['site_id'], $message ); 607 } 608 } 268 609 } 269 610 } 270 611
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)