| | 440 | * Detects the current site and network if they haven't been set up yet. |
| | 441 | * |
| | 442 | * Prior to 4.8.0, this was a procedural block in `ms-settings.php`. It was wrapped into a |
| | 443 | * function to facilitate unit tests. It should not be used outside of core. |
| | 444 | * |
| | 445 | * If the 'SUNRISE' constant is defined, the code in the custom `sunrise.php` will be executed |
| | 446 | * prior to this function. |
| | 447 | * |
| | 448 | * @since 4.8.0 |
| | 449 | * |
| | 450 | * @global WP_Network $current_site The current network. |
| | 451 | * @global WP_Site $current_blog The current site. |
| | 452 | * |
| | 453 | * @return bool|string True if bootstrap successfully populated `$current_blog` and `$current_site`, |
| | 454 | * or if they had already been populated prior. |
| | 455 | * False if bootstrap could not be properly completed. |
| | 456 | * Redirect URL if parts exist, but the request as a whole can not be fulfilled. |
| | 457 | */ |
| | 458 | function ms_detect_current_site_and_network() { |
| | 459 | global $current_site, $current_blog; |
| | 460 | |
| | 461 | if ( isset( $current_site ) && isset( $current_blog ) ) { |
| | 462 | // Ensure objects populated in custom `sunrise.php` are proper class instances. |
| | 463 | if ( ! $current_site instanceof WP_Network ) { |
| | 464 | $current_site = new WP_Network( $current_site ); |
| | 465 | } |
| | 466 | |
| | 467 | if ( ! $current_blog instanceof WP_Site ) { |
| | 468 | $current_blog = new WP_Site( $current_blog ); |
| | 469 | } |
| | 470 | |
| | 471 | return true; |
| | 472 | } |
| | 473 | |
| | 474 | $domain = ms_get_request_domain(); |
| | 475 | $path = ms_get_request_path(); |
| | 476 | |
| | 477 | return ms_load_current_site_and_network( $domain, $path, is_subdomain_install() ); |
| | 478 | } |
| | 479 | |
| | 480 | /** |
| | 481 | * Returns the domain from the current request. |
| | 482 | * |
| | 483 | * @since 4.8.0 |
| | 484 | * |
| | 485 | * @return string Current domain. |
| | 486 | */ |
| | 487 | function ms_get_request_domain() { |
| | 488 | $domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ) ); |
| | 489 | if ( substr( $domain, -3 ) == ':80' ) { |
| | 490 | $domain = substr( $domain, 0, -3 ); |
| | 491 | $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 ); |
| | 492 | } elseif ( substr( $domain, -4 ) == ':443' ) { |
| | 493 | $domain = substr( $domain, 0, -4 ); |
| | 494 | $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 ); |
| | 495 | } |
| | 496 | |
| | 497 | return $domain; |
| | 498 | } |
| | 499 | |
| | 500 | /** |
| | 501 | * Returns the path from the current request. |
| | 502 | * |
| | 503 | * @since 4.8.0 |
| | 504 | * |
| | 505 | * @return string Current path. |
| | 506 | */ |
| | 507 | function ms_get_request_path() { |
| | 508 | $path = stripslashes( $_SERVER['REQUEST_URI'] ); |
| | 509 | if ( is_admin() ) { |
| | 510 | $path = preg_replace( '#(.*)/wp-admin/.*#', '$1/', $path ); |
| | 511 | } |
| | 512 | list( $path ) = explode( '?', $path ); |
| | 513 | |
| | 514 | return $path; |
| | 515 | } |
| | 516 | |
| | 517 | /** |
| | 518 | * Handles the multisite bootstrap result from loading the current site and network. |
| | 519 | * |
| | 520 | * It may exit or redirect depending on the result. |
| | 521 | * |
| | 522 | * @since 4.8.0 |
| | 523 | * |
| | 524 | * @param bool|string @bootstrap_result True if bootstrap successfully populated `$current_blog` and `$current_site`. |
| | 525 | * False if bootstrap could not be properly completed. |
| | 526 | * Redirect URL if parts exist, but the request as a whole can not be fulfilled. |
| | 527 | */ |
| | 528 | function ms_handle_bootstrap_result( $bootstrap_result ) { |
| | 529 | if ( true === $bootstrap_result ) { |
| | 530 | return; |
| | 531 | } |
| | 532 | |
| | 533 | if ( false === $bootstrap_result ) { |
| | 534 | $domain = ms_get_request_domain(); |
| | 535 | $path = ms_get_request_path(); |
| | 536 | |
| | 537 | ms_not_installed( $domain, $path ); |
| | 538 | } |
| | 539 | |
| | 540 | header( 'Location: ' . $bootstrap_result ); |
| | 541 | exit; |
| | 542 | } |
| | 543 | |
| | 544 | /** |
| | 545 | * Sets up related globals after the current site and network have been populated successfully. |
| | 546 | * |
| | 547 | * The database and cache are properly setup as well as the network options. |
| | 548 | * |
| | 549 | * @since 4.8.0 |
| | 550 | * |
| | 551 | * @param WP_Site $site Current site. |
| | 552 | * @param WP_Network $network Current network. |
| | 553 | * |
| | 554 | * @global wpdb $wpdb WordPress database abstraction object. |
| | 555 | * @global string $table_prefix The database table prefix. |
| | 556 | * @global int $site_id The current network ID. |
| | 557 | * @global int $blog_id The current site ID. |
| | 558 | * @global int $public Whether the current site is public. |
| | 559 | * @global array $_wp_switched_stack Stack for storing site IDs when switching sites. |
| | 560 | * @global bool $switched Whether sites are currently switched. |
| | 561 | */ |
| | 562 | function ms_setup_vars( $site, $network ) { |
| | 563 | global $wpdb, $table_prefix, $site_id, $blog_id, $public, $_wp_switched_stack, $switched; |
| | 564 | |
| | 565 | if ( ! isset( $blog_id ) ) { |
| | 566 | $blog_id = $site->id; |
| | 567 | } |
| | 568 | |
| | 569 | if ( ! isset( $public ) ) { |
| | 570 | $public = $site->public; |
| | 571 | } |
| | 572 | |
| | 573 | if ( empty( $site->network_id ) ) { |
| | 574 | $site->network_id = 1; |
| | 575 | } |
| | 576 | |
| | 577 | if ( ! isset( $site_id ) ) { |
| | 578 | $site_id = $site->network_id; |
| | 579 | } |
| | 580 | |
| | 581 | wp_load_core_site_options( $site_id ); |
| | 582 | |
| | 583 | $wpdb->set_prefix( $table_prefix, false ); // $table_prefix can be set in sunrise.php |
| | 584 | $wpdb->set_blog_id( $site->id, $site->network_id ); |
| | 585 | |
| | 586 | $table_prefix = $wpdb->get_blog_prefix(); |
| | 587 | $_wp_switched_stack = array(); |
| | 588 | $switched = false; |
| | 589 | |
| | 590 | // need to init cache again after blog_id is set |
| | 591 | wp_start_object_cache(); |
| | 592 | } |
| | 593 | |
| | 594 | /** |