Make WordPress Core

Ticket #34941: 34941.4.diff

File 34941.4.diff, 18.5 KB (added by jeremyfelt, 8 years ago)
  • src/wp-includes/ms-load.php

     
    261261}
    262262
    263263/**
     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 */
     294function 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/**
    264451 * Displays a failure message.
    265452 *
    266453 * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well.
  • src/wp-includes/ms-settings.php

     
    4141/** Check for and define SUBDOMAIN_INSTALL and the deprecated VHOST constant. */
    4242ms_subdomain_constants();
    4343
     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`.
    4446if ( !isset( $current_site ) || !isset( $current_blog ) ) {
    4547
    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 
    5048        $domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ) );
    5149        if ( substr( $domain, -3 ) == ':80' ) {
    5250                $domain = substr( $domain, 0, -3 );
     
    6260        }
    6361        list( $path ) = explode( '?', $path );
    6462
    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() );
    7664
    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 );
    12969        } 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 );
    20071                exit;
    20172        }
     73        unset( $bootstrap_result );
    20274
    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 
    21475        $blog_id = $current_blog->blog_id;
    21576        $public  = $current_blog->public;
    21677
  • tests/phpunit/tests/multisite/bootstrap.php

     
    158158                        'site_id' => self::$site_ids[ $site_key ],
    159159                );
    160160
    161                 $this->_setup_host_request( $domain, $path );
     161                ms_load_current_site_and_network( $domain, $path );
    162162                $actual = array(
    163163                        'network_id' => $current_blog->site_id,
    164164                        'site_id' => $current_blog->blog_id,
    165165                );
    166                 $this->_setup_host_request( WP_TESTS_DOMAIN, '/' );
     166                ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' );
    167167
    168168                $this->assertEqualSetsWithIndex( $expected, $actual );
    169169        }
     
    197197                        'site_id'    => self::$site_ids['wordpress.org/foo/bar/'],
    198198                );
    199199                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/' );
    201201                $actual = array(
    202202                        'network_id' => $current_blog->site_id,
    203203                        'site_id' => $current_blog->blog_id,
    204204                );
    205205                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, '/' );
    207207
    208208                $this->assertEqualSetsWithIndex( $expected, $actual );
    209209        }
     
    211211        public function filter_path_segments_to_two() {
    212212                return 2;
    213213        }
    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 to
    219          * 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         }
    234214}
    235215
    236216endif;