Multisite: Add get_network_by_path() and wp_get_network() to begin cleanup of multisite load.
Tries to get network detection under control by simplifying wpmu_current_site(). It now also pops off each subdomain to find a more general match. Adds unit tests for get_network_by_path() and a new network factory for unit tests. Much of this is likely to change in 3.9 as more of ms-load.php and ms-settings.php gets hacked to bits. props jeremyfelt. see #27003. git-svn-id: https://develop.svn.wordpress.org/trunk@27178 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
f6af16cd76
commit
31d3af406c
@ -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 <a href="http://codex.wordpress.org/Debugging_a_WordPress_Network">Debugging a WordPress Network</a> 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 <a href="%s">%s</a>.' ), '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 <a href="http://codex.wordpress.org/Debugging_a_WordPress_Network">Debugging a WordPress Network</a> 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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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 ) );
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user