WordPress.org

Make WordPress Core

Ticket #40646: 40646.diff

File 40646.diff, 8.7 KB (added by flixos90, 2 years ago)
  • src/wp-includes/ms-load.php

     
    135135
    136136/**
    137137 * Retrieves the closest matching site object by its domain and path.
    138  * 
     138 *
    139139 * This will not necessarily return an exact match for a domain and path. Instead, it
    140140 * breaks the domain and path into pieces that are then used to match the closest
    141141 * possibility from a query.
     
    437437}
    438438
    439439/**
     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 */
     458function 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 */
     487function 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 */
     507function 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 */
     528function 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 */
     562function 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/**
    440595 * Displays a failure message.
    441596 *
    442597 * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well.
  • src/wp-includes/ms-settings.php

     
    1010 * @since 3.0.0
    1111 */
    1212
    13 /**
    14  * Objects representing the current network and current site.
    15  *
    16  * These may be populated through a custom `sunrise.php`. If not, then this
    17  * file will attempt to populate them based on the current request.
    18  *
    19  * @global WP_Network $current_site The current network.
    20  * @global object     $current_blog The current site.
    21  * @since 3.0.0
    22  */
    23 global $current_site, $current_blog;
    24 
    2513/** WP_Network class */
    2614require_once( ABSPATH . WPINC . '/class-wp-network.php' );
    2715
     
    4129/** Check for and define SUBDOMAIN_INSTALL and the deprecated VHOST constant. */
    4230ms_subdomain_constants();
    4331
    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`.
    46 if ( !isset( $current_site ) || !isset( $current_blog ) ) {
    47 
    48         $domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ) );
    49         if ( substr( $domain, -3 ) == ':80' ) {
    50                 $domain = substr( $domain, 0, -3 );
    51                 $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
    52         } elseif ( substr( $domain, -4 ) == ':443' ) {
    53                 $domain = substr( $domain, 0, -4 );
    54                 $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
    55         }
    56 
    57         $path = stripslashes( $_SERVER['REQUEST_URI'] );
    58         if ( is_admin() ) {
    59                 $path = preg_replace( '#(.*)/wp-admin/.*#', '$1/', $path );
    60         }
    61         list( $path ) = explode( '?', $path );
    62 
    63         $bootstrap_result = ms_load_current_site_and_network( $domain, $path, is_subdomain_install() );
    64 
    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 );
    69         } else {
    70                 header( 'Location: ' . $bootstrap_result );
    71                 exit;
    72         }
    73         unset( $bootstrap_result );
    74 
    75         $blog_id = $current_blog->blog_id;
    76         $public  = $current_blog->public;
    77 
    78         if ( empty( $current_blog->site_id ) ) {
    79                 // This dates to [MU134] and shouldn't be relevant anymore,
    80                 // but it could be possible for arguments passed to insert_blog() etc.
    81                 $current_blog->site_id = 1;
    82         }
     32/** Detect and populate the current site and network. */
     33$bootstrap_result = ms_detect_current_site_and_network();
    8334
    84         $site_id = $current_blog->site_id;
    85         wp_load_core_site_options( $site_id );
    86 }
    87 
    88 $wpdb->set_prefix( $table_prefix, false ); // $table_prefix can be set in sunrise.php
    89 $wpdb->set_blog_id( $current_blog->blog_id, $current_blog->site_id );
    90 $table_prefix = $wpdb->get_blog_prefix();
    91 $_wp_switched_stack = array();
    92 $switched = false;
    93 
    94 // need to init cache again after blog_id is set
    95 wp_start_object_cache();
     35/** Handle possible failures from the bootstrapping process. */
     36ms_handle_bootstrap_result( $bootstrap_result );
     37unset( $bootstrap_result );
    9638
    97 if ( ! $current_site instanceof WP_Network ) {
    98         $current_site = new WP_Network( $current_site );
    99 }
    100 
    101 if ( ! $current_blog instanceof WP_Site ) {
    102         $current_blog = new WP_Site( $current_blog );
    103 }
     39/** Setup globals and initialize the database and cache. */
     40ms_setup_vars( $current_blog, $current_site );
    10441
    10542// Define upload directory constants
    10643ms_upload_constants();