Make WordPress Core

Ticket #27884: 27884.diff

File 27884.diff, 11.9 KB (added by jeremyfelt, 11 years ago)
  • src/wp-includes/ms-load.php

     
    436436        _deprecated_function( __FUNCTION__, '3.9' );
    437437        return $current_site;
    438438}
     439
     440/**
     441 * Determine if a network has been defined already.
     442 *
     443 * @since 4.0.0
     444 *
     445 * @return bool True if a network is defined with constants. False if it is not.
     446 */
     447function wp_network_is_defined() {
     448        if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
     449                $result = true;
     450        } else {
     451                $result = false;
     452        }
     453
     454        return apply_filters( 'network_is_defined', $result );
     455}
     456
     457/**
     458 * Build a network object with predefined data.
     459 *
     460 * @since 4.0.0
     461 *
     462 * @return object Basic network information.
     463 */
     464function wp_get_defined_network() {
     465        if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
     466                $current_site = new stdClass;
     467                $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
     468                $current_site->domain = DOMAIN_CURRENT_SITE;
     469                $current_site->path = PATH_CURRENT_SITE;
     470                if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
     471                        $current_site->blog_id = BLOG_ID_CURRENT_SITE;
     472                } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
     473                        $current_site->blog_id = BLOGID_CURRENT_SITE;
     474                }
     475        } else {
     476                $current_site = false;
     477        }
     478
     479        return apply_filters( 'get_defined_network', $current_site );
     480}
     481 No newline at end of file
  • src/wp-includes/ms-settings.php

     
    1010 * @since 3.0.0
    1111 */
    1212
    13 /** Include Multisite initialization functions */
    14 require( ABSPATH . WPINC . '/ms-load.php' );
    15 require( ABSPATH . WPINC . '/ms-default-constants.php' );
    16 
    17 if ( defined( 'SUNRISE' ) )
    18         include_once( WP_CONTENT_DIR . '/sunrise.php' );
    19 
    20 /** Check for and define SUBDOMAIN_INSTALL and the deprecated VHOST constant. */
    21 ms_subdomain_constants();
    22 
    2313if ( !isset( $current_site ) || !isset( $current_blog ) ) {
    2414
    2515        // Given the domain and path, let's try to identify the network and site.
     
    4232        list( $path ) = explode( '?', $path );
    4333
    4434        // If the network is defined in wp-config.php, we can simply use that.
    45         if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
    46                 $current_site = new stdClass;
    47                 $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
    48                 $current_site->domain = DOMAIN_CURRENT_SITE;
    49                 $current_site->path = PATH_CURRENT_SITE;
    50                 if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
    51                         $current_site->blog_id = BLOG_ID_CURRENT_SITE;
    52                 } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
    53                         $current_site->blog_id = BLOGID_CURRENT_SITE;
    54                 }
     35        if ( wp_network_is_defined() ) {
     36                $current_site = wp_get_defined_network();
    5537
    5638                if ( $current_site->domain === $domain && $current_site->path === $path ) {
    5739                        $current_blog = get_site_by_path( $domain, $path );
  • src/wp-settings.php

     
    9191// Initialize multisite if enabled.
    9292if ( is_multisite() ) {
    9393        require( ABSPATH . WPINC . '/ms-blogs.php' );
     94        /** Include Multisite initialization functions */
     95        require( ABSPATH . WPINC . '/ms-load.php' );
     96        require( ABSPATH . WPINC . '/ms-default-constants.php' );
     97
     98        if ( defined( 'SUNRISE' ) ) {
     99                include_once( WP_CONTENT_DIR . '/sunrise.php' );
     100        }
     101
     102        /** Check for and define SUBDOMAIN_INSTALL and the deprecated VHOST constant. */
     103        ms_subdomain_constants();
     104
    94105        require( ABSPATH . WPINC . '/ms-settings.php' );
    95106} elseif ( ! defined( 'MULTISITE' ) ) {
    96107        define( 'MULTISITE', false );
  • tests/phpunit/includes/bootstrap.php

     
    5555        echo "Running as multisite..." . PHP_EOL;
    5656        define( 'MULTISITE', true );
    5757        define( 'SUBDOMAIN_INSTALL', false );
    58         define( 'DOMAIN_CURRENT_SITE', WP_TESTS_DOMAIN );
    59         define( 'PATH_CURRENT_SITE', '/' );
    60         define( 'SITE_ID_CURRENT_SITE', 1 );
    61         define( 'BLOG_ID_CURRENT_SITE', 1 );
    6258        $GLOBALS['base'] = '/';
    6359} else {
    6460        echo "Running as single site... To run multisite, use -c tests/phpunit/multisite.xml" . PHP_EOL;
  • tests/phpunit/tests/ms.php

     
    455455                $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/test_blogname', 'title' => 'Test Title' ) );
    456456                $this->assertInternalType( 'int', $blog_id );
    457457
    458                 $this->assertEquals( 'http://' . DOMAIN_CURRENT_SITE . PATH_CURRENT_SITE . 'test_blogname/', get_blogaddress_by_name('test_blogname') );
     458                $this->assertEquals( 'http://' . $current_site->domain . $current_site->path . 'test_blogname/', get_blogaddress_by_name('test_blogname') );
    459459
    460460                $this->assertEquals( $blog_id, get_id_from_blogname('test_blogname') );
    461461        }
     
    13381338                        $GLOBALS['super_admins'] = $old_global;
    13391339                }
    13401340        }
     1341
     1342        /**
     1343         * @ticket 27884
     1344         */
     1345        function test_multisite_bootstrap() {
     1346                global $current_site, $current_blog;
     1347
     1348                $network_ids = array(
     1349                        'wordpress.org/'         => array( 'domain' => 'wordpress.org', 'path' => '/' ),
     1350                        'make.wordpress.org/'    => array( 'domain' => 'make.wordpress.org', 'path' => '/' ),
     1351                );
     1352
     1353                foreach ( $network_ids as &$id ) {
     1354                        $id = $this->factory->network->create( $id );
     1355                }
     1356                unset( $id );
     1357
     1358                $ids = array(
     1359                        'wordpress.org/'              => array( 'domain' => 'wordpress.org',      'path' => '/',         'site_id' => $network_ids['wordpress.org/'] ),
     1360                        'wordpress.org/foo/'          => array( 'domain' => 'wordpress.org',      'path' => '/foo/',     'site_id' => $network_ids['wordpress.org/'] ),
     1361                        'wordpress.org/foo/bar/'      => array( 'domain' => 'wordpress.org',      'path' => '/foo/bar/', 'site_id' => $network_ids['wordpress.org/'] ),
     1362                        'make.wordpress.org/'         => array( 'domain' => 'make.wordpress.org', 'path' => '/',         'site_id' => $network_ids['make.wordpress.org/'] ),
     1363                        'make.wordpress.org/foo/'     => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/',     'site_id' => $network_ids['make.wordpress.org/'] ),
     1364                );
     1365
     1366                foreach ( $ids as &$id ) {
     1367                        $id = $this->factory->blog->create( $id );
     1368                }
     1369                unset( $id );
     1370
     1371                add_filter( 'network_is_defined', '__return_true' );
     1372                $this->_setup_network_define_filter( 'wordpress.org', '/', 1, 1 );
     1373                $this->_setup_host_request( 'wordpress.org', '/' );
     1374                $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
     1375                $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     1376
     1377                $this->_setup_host_request( 'wordpress.org', '/2014/04/23/hello-world/' );
     1378                $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
     1379                $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     1380
     1381                $this->_setup_host_request( 'wordpress.org', '/sample-page/' );
     1382                $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
     1383                $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     1384
     1385                $this->_setup_host_request( 'wordpress.org', '/?p=1' );
     1386                $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
     1387                $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     1388
     1389                $this->_setup_host_request( 'wordpress.org', '/wp-admin/' );
     1390                $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
     1391                $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     1392
     1393                $this->_setup_host_request( 'wordpress.org', '/foo/' );
     1394                $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
     1395                $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     1396
     1397                $this->_setup_host_request( 'wordpress.org', '/FOO/' );
     1398                $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
     1399                $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     1400
     1401                $this->_setup_host_request( 'wordpress.org', '/foo/2014/04/23/hello-world/' );
     1402                $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
     1403                $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     1404
     1405                $this->_setup_host_request( 'wordpress.org', '/foo/sample-page/' );
     1406                $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
     1407                $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     1408
     1409                $this->_setup_host_request( 'wordpress.org', '/foo/?p=1' );
     1410                $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
     1411                $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     1412
     1413                $this->_setup_host_request( 'wordpress.org', '/foo/wp-admin/' );
     1414                $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
     1415                $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     1416
     1417                // @todo not currently passing.
     1418                //$this->_setup_host_request( 'wordpress.org', '/foo/bar/' );
     1419                //$this->assertEquals( $ids['wordpress.org/foo/bar/'], $current_blog->blog_id );
     1420                //$this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     1421
     1422                remove_filter( 'network_is_defined', '__return_true' );
     1423                add_filter( 'network_is_defined', '__return_false' );
     1424                $this->_setup_host_request( 'make.wordpress.org', '/' );
     1425                $this->assertEquals( $ids['make.wordpress.org/'], $current_blog->blog_id );
     1426                $this->assertEquals( $network_ids['make.wordpress.org/'], $current_blog->site_id );
     1427
     1428                $this->_setup_host_request( 'make.wordpress.org', '/foo/' );
     1429                $this->assertEquals( $ids['make.wordpress.org/foo/'], $current_blog->blog_id );
     1430                $this->assertEquals( $network_ids['make.wordpress.org/'], $current_blog->site_id );
     1431        }
     1432
     1433        /**
     1434         * Reset various globals required for a 'clean' multisite boot.
     1435         *
     1436         * The $wpdb and $table_prefix globals are required for ms-settings.php to
     1437         * load properly.
     1438         *
     1439         * @param string $domain HTTP_HOST of the bootstrap request.
     1440         * @param string $path   REQUEST_URI of the boot strap request.
     1441         */
     1442        function _setup_host_request( $domain, $path ) {
     1443                global $current_site, $current_blog, $wpdb, $table_prefix;
     1444
     1445                $current_site = $current_blog = null;
     1446                $_SERVER['HTTP_HOST'] = $domain;
     1447                $_SERVER['REQUEST_URI'] = $path;
     1448                include ABSPATH . '/wp-includes/ms-settings.php';
     1449        }
     1450
     1451        /**
     1452         * Use the get_defined_network filter to work around multisite constants.
     1453         *
     1454         * @param string $domain     Network domain that would be in DOMAIN_CURRENT_SITE.
     1455         * @param string $path       Network path that would be in PATH_CURRENT_SITE.
     1456         * @param int    $network_id Network ID that would be in SITE_ID_CURRENT_SITE.
     1457         * @param int    $site_id    Network's site ID that would be in BLOG_ID_CURRENT_SITE.
     1458         */
     1459        function _setup_network_define_filter( $domain, $path, $network_id = 1, $site_id = 1 ) {
     1460                global $test_network_domain, $test_network_path, $test_network_id, $test_site_id;
     1461
     1462                $test_network_domain = $domain;
     1463                $test_network_path = $path;
     1464                $test_network_id = $network_id;
     1465                $test_site_id = $site_id;
     1466
     1467                add_filter( 'get_defined_network', array( $this, '_filter_defined_network' ) );
     1468        }
     1469
     1470        /**
     1471         * Filter the current_site object as if it had been predefined per the globals
     1472         * configured in _setup_network_define_filter().
     1473         *
     1474         * @return stdClass The current_site object.
     1475         */
     1476        function _filter_defined_network() {
     1477                global $test_network_domain, $test_network_path, $test_network_id, $test_site_id;
     1478
     1479                $current_site = new stdClass();
     1480                $current_site->domain = $test_network_domain;
     1481                $current_site->path = $test_network_path;
     1482                $current_site->id = $test_network_id;
     1483                $current_site->blog_id = $test_site_id;
     1484
     1485                return $current_site;
     1486        }
    13411487}
    13421488
    13431489endif;