diff --git a/src/wp-includes/ms-load.php b/src/wp-includes/ms-load.php index 72d71000ac..868c14171c 100644 --- a/src/wp-includes/ms-load.php +++ b/src/wp-includes/ms-load.php @@ -133,6 +133,93 @@ function get_current_site_name( $current_site ) { return $current_site; } +/** + * Retrieve a network object by its domain and path. + * + * @since 3.9.0 + * + * @param string $domain Domain to check. + * @param string $path Path to check. + * @return object|bool Network object if successful. False when no network is found. + */ +function get_network_by_path( $domain, $path ) { + global $wpdb; + + $network_id = false; + + $domains = $exact_domains = array( $domain ); + $pieces = explode( '.', $domain ); + + // It's possible one domain to search is 'com', but it might as well + // be 'localhost' or some other locally mapped domain. + while ( array_shift( $pieces ) ) { + if ( $pieces ) { + $domains[] = implode( '.', $pieces ); + } + } + + if ( '/' !== $path ) { + $paths = array( '/', $path ); + } else { + $paths = array( '/' ); + } + + $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'"; + $paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'"; + + $networks = $wpdb->get_results( "SELECT id, domain, path FROM $wpdb->site + WHERE domain IN ($search_domains) AND path IN ($paths) + ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC" ); + + /* + * Domains are sorted by length of domain, then by length of path. + * The domain must match for the path to be considered. Otherwise, + * a network with the path of / will suffice. + */ + $found = false; + foreach ( $networks as $network ) { + if ( $network->domain === $domain || "www.$network->domain" === $domain ) { + if ( $network->path === $path ) { + $found = true; + break; + } + } + if ( $network->path === '/' ) { + $found = true; + break; + } + } + + if ( $found ) { + $network = wp_get_network( $network ); + + return $network; + } + + return false; +} + +/** + * Retrieve an object containing information about the requested network. + * + * @since 3.9.0 + * + * @param int $network_id The network's DB row or ID. + * @return mixed Object containing network information if found, false if not. + */ +function wp_get_network( $network ) { + global $wpdb; + + if ( ! is_object( $network ) ) { + $network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $network ) ); + if ( ! $network ) { + return false; + } + } + + return $network; +} + /** * Sets current_site object. * @@ -141,11 +228,12 @@ function get_current_site_name( $current_site ) { * @return object $current_site object */ function wpmu_current_site() { - global $wpdb, $current_site, $domain, $path, $sites, $cookie_domain; + global $wpdb, $current_site, $domain, $path; if ( empty( $current_site ) ) $current_site = new stdClass; + // 1. If constants are defined, that's our network. if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) { $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1; $current_site->domain = DOMAIN_CURRENT_SITE; @@ -154,77 +242,40 @@ function wpmu_current_site() { $current_site->blog_id = BLOG_ID_CURRENT_SITE; elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) // deprecated. $current_site->blog_id = BLOGID_CURRENT_SITE; - if ( DOMAIN_CURRENT_SITE == $domain ) - $current_site->cookie_domain = $cookie_domain; - elseif ( substr( $current_site->domain, 0, 4 ) == 'www.' ) - $current_site->cookie_domain = substr( $current_site->domain, 4 ); - else - $current_site->cookie_domain = $current_site->domain; - wp_load_core_site_options( $current_site->id ); + // 2. Pull the network from cache, if possible. + } elseif ( ! $current_site = wp_cache_get( 'current_site', 'site-options' ) ) { - return $current_site; - } + // 3. See if they have only one network. + $networks = $wpdb->get_col( "SELECT id FROM $wpdb->site LIMIT 2" ); - $current_site = wp_cache_get( 'current_site', 'site-options' ); - if ( $current_site ) - return $current_site; + if ( count( $networks ) <= 1 ) { + $current_site = wp_get_network( $networks[0]->id ); - $sites = $wpdb->get_results( "SELECT * FROM $wpdb->site" ); // usually only one site - if ( 1 == count( $sites ) ) { - $current_site = $sites[0]; - wp_load_core_site_options( $current_site->id ); - $path = $current_site->path; - $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path ) ); - $current_site = get_current_site_name( $current_site ); - if ( substr( $current_site->domain, 0, 4 ) == 'www.' ) - $current_site->cookie_domain = substr( $current_site->domain, 4 ); - wp_cache_set( 'current_site', $current_site, 'site-options' ); - return $current_site; - } - $path = substr( $_SERVER[ 'REQUEST_URI' ], 0, 1 + strpos( $_SERVER[ 'REQUEST_URI' ], '/', 1 ) ); + $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id + FROM $wpdb->blogs WHERE domain = %s AND path = %s", + $current_site->domain, $current_site->path ) ); - if ( $domain == $cookie_domain ) - $current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path ) ); - else - $current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE domain IN ( %s, %s ) AND path = %s ORDER BY CHAR_LENGTH( domain ) DESC LIMIT 1", $domain, $cookie_domain, $path ) ); + wp_cache_set( 'current_site', 'site-options' ); - if ( ! $current_site ) { - if ( $domain == $cookie_domain ) - $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $domain ) ); - else - $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain IN ( %s, %s ) AND path = '/' ORDER BY CHAR_LENGTH( domain ) DESC LIMIT 1", $domain, $cookie_domain ) ); - } + // 4. Multiple networks are in play. Determine which via domain and path. + } else { + // Find the first path segment. + $path = substr( $_SERVER['REQUEST_URI'], 0, 1 + strpos( $_SERVER['REQUEST_URI'], '/', 1 ) ); + $current_site = get_network_by_path( $domain, $path ); - if ( $current_site ) { - $path = $current_site->path; - $current_site->cookie_domain = $cookie_domain; - return $current_site; - } - - if ( is_subdomain_install() ) { - $sitedomain = substr( $domain, 1 + strpos( $domain, '.' ) ); - $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $sitedomain, $path) ); - if ( $current_site ) { - $current_site->cookie_domain = $current_site->domain; - return $current_site; + // Option 1. We did not find anything. + if ( ! $current_site ) { + wp_load_translations_early(); + wp_die( __( 'No site defined on this host. If you are the owner of this site, please check Debugging a WordPress Network for help.' ) ); + } } - - $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $sitedomain) ); } - if ( $current_site || defined( 'WP_INSTALLING' ) ) { - $path = '/'; - return $current_site; - } - - // Still no dice. - wp_load_translations_early(); - - if ( 1 == count( $sites ) ) - wp_die( sprintf( __( 'That site does not exist. Please try %s.' ), 'http://' . $sites[0]->domain . $sites[0]->path ) ); - else - wp_die( __( 'No site defined on this host. If you are the owner of this site, please check Debugging a WordPress Network for help.' ) ); + // Option 2. We found something. Load up site meta and return. + wp_load_core_site_options(); + $current_site = get_current_site_name( $current_site ); + return $current_site; } /** diff --git a/src/wp-includes/ms-settings.php b/src/wp-includes/ms-settings.php index af2ea0d97a..2d0e660b03 100644 --- a/src/wp-includes/ms-settings.php +++ b/src/wp-includes/ms-settings.php @@ -37,15 +37,17 @@ if ( !isset( $current_site ) || !isset( $current_blog ) ) { } $domain = rtrim( $domain, '.' ); - $cookie_domain = $domain; - if ( substr( $cookie_domain, 0, 4 ) == 'www.' ) - $cookie_domain = substr( $cookie_domain, 4 ); $path = preg_replace( '|([a-z0-9-]+.php.*)|', '', $_SERVER['REQUEST_URI'] ); $path = str_replace ( '/wp-admin/', '/', $path ); $path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path ); $current_site = wpmu_current_site(); + $current_site->cookie_domain = $current_site->domain; + if ( 'www.' === substr( $current_site->cookie_domain, 0, 4 ) ) { + $current_site->cookie_domain = substr( $current_site->cookie_domain, 4 ); + } + if ( ! isset( $current_site->blog_id ) ) $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path ) ); diff --git a/tests/phpunit/includes/factory.php b/tests/phpunit/includes/factory.php index fbac418257..4adec23e89 100644 --- a/tests/phpunit/includes/factory.php +++ b/tests/phpunit/includes/factory.php @@ -42,6 +42,11 @@ class WP_UnitTest_Factory { */ public $blog; + /** + * @var WP_UnitTest_Factory_For_Network + */ + public $network; + function __construct() { $this->post = new WP_UnitTest_Factory_For_Post( $this ); $this->attachment = new WP_UnitTest_Factory_For_Attachment( $this ); @@ -50,8 +55,10 @@ class WP_UnitTest_Factory { $this->term = new WP_UnitTest_Factory_For_Term( $this ); $this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' ); $this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' ); - if ( is_multisite() ) + if ( is_multisite() ) { $this->blog = new WP_UnitTest_Factory_For_Blog( $this ); + $this->network = new WP_UnitTest_Factory_For_Network( $this ); + } } } @@ -177,6 +184,39 @@ class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing { } +class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing { + + function __construct( $factory = null ) { + parent::__construct( $factory ); + $this->default_generation_definitions = array( + 'domain' => WP_TESTS_DOMAIN, + 'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ), + 'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ), + 'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ), + 'subdomain_install' => false, + ); + } + + function create_object( $args ) { + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; + + if ( ! isset( $args['user'] ) ) { + $email = WP_TESTS_EMAIL; + } else { + $email = get_userdata( $args['user'] )->user_email; + } + + populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] ); + return $args['network_id']; + } + + function update_object( $network_id, $fields ) {} + + function get_object_by_id( $network_id ) { + return wp_get_network( $network_id ); + } +} + class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing { private $taxonomy; diff --git a/tests/phpunit/tests/ms.php b/tests/phpunit/tests/ms.php index 038492a13e..bcd597965b 100644 --- a/tests/phpunit/tests/ms.php +++ b/tests/phpunit/tests/ms.php @@ -1172,6 +1172,53 @@ class Tests_MS extends WP_UnitTestCase { // Expect 0 sites when using an offset larger than the number of sites $this->assertCount( 0, wp_get_sites( array( 'offset' => 20 ) ) ); } + + /** + * @ticket 27003 + */ + function test_get_network_by_path() { + global $wpdb; + + $ids = array( + 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ), + 'wordpress.org/one/' => array( 'domain' => 'wordpress.org', 'path' => '/one/' ), + 'wordpress.net/' => array( 'domain' => 'wordpress.net', 'path' => '/' ), + 'www.wordpress.net/' => array( 'domain' => 'www.wordpress.net', 'path' => '/' ), + 'www.wordpress.net/two/' => array( 'domain' => 'www.wordpress.net', 'path' => '/two/' ), + 'wordpress.net/three/' => array( 'domain' => 'wordpress.net', 'path' => '/three/' ), + ); + + foreach ( $ids as &$id ) { + $id = $this->factory->network->create( $id ); + } + unset( $id ); + + $this->assertEquals( $ids['www.wordpress.net/'], + get_network_by_path( 'www.wordpress.net', '/notapath/' )->id ); + + $this->assertEquals( $ids['www.wordpress.net/two/'], + get_network_by_path( 'www.wordpress.net', '/two/' )->id ); + + // This should find /one/ despite the www. + $this->assertEquals( $ids['wordpress.org/one/'], + get_network_by_path( 'www.wordpress.org', '/one/' )->id ); + + // This should not find /one/ because the domains don't match. + $this->assertEquals( $ids['wordpress.org/'], + get_network_by_path( 'site1.wordpress.org', '/one/' )->id ); + + $this->assertEquals( $ids['wordpress.net/three/'], + get_network_by_path( 'wordpress.net', '/three/' )->id ); + + $this->assertEquals( $ids['wordpress.net/'], + get_network_by_path( 'wordpress.net', '/notapath/' )->id ); + + $this->assertEquals( $ids['wordpress.net/'], + get_network_by_path( 'site1.wordpress.net', '/notapath/' )->id ); + + $this->assertEquals( $ids['wordpress.net/'], + get_network_by_path( 'site1.wordpress.net', '/three/' )->id ); + } } endif;