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.

Built from https://develop.svn.wordpress.org/trunk@27178


git-svn-id: http://core.svn.wordpress.org/trunk@27040 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Nacin 2014-02-13 23:07:14 +00:00
parent f2f6d60624
commit eae1d5641d
2 changed files with 118 additions and 65 deletions

View File

@ -133,6 +133,93 @@ function get_current_site_name( $current_site ) {
return $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. * Sets current_site object.
* *
@ -141,11 +228,12 @@ function get_current_site_name( $current_site ) {
* @return object $current_site object * @return object $current_site object
*/ */
function wpmu_current_site() { function wpmu_current_site() {
global $wpdb, $current_site, $domain, $path, $sites, $cookie_domain; global $wpdb, $current_site, $domain, $path;
if ( empty( $current_site ) ) if ( empty( $current_site ) )
$current_site = new stdClass; $current_site = new stdClass;
// 1. If constants are defined, that's our network.
if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) { if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
$current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1; $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
$current_site->domain = DOMAIN_CURRENT_SITE; $current_site->domain = DOMAIN_CURRENT_SITE;
@ -154,78 +242,41 @@ function wpmu_current_site() {
$current_site->blog_id = BLOG_ID_CURRENT_SITE; $current_site->blog_id = BLOG_ID_CURRENT_SITE;
elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) // deprecated. elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) // deprecated.
$current_site->blog_id = BLOGID_CURRENT_SITE; $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 ( count( $networks ) <= 1 ) {
if ( $current_site ) $current_site = wp_get_network( $networks[0]->id );
return $current_site;
$sites = $wpdb->get_results( "SELECT * FROM $wpdb->site" ); // usually only one site $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id
if ( 1 == count( $sites ) ) { FROM $wpdb->blogs WHERE domain = %s AND path = %s",
$current_site = $sites[0]; $current_site->domain, $current_site->path ) );
wp_load_core_site_options( $current_site->id );
$path = $current_site->path; wp_cache_set( 'current_site', 'site-options' );
$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 ); // 4. Multiple networks are in play. Determine which via domain and path.
if ( substr( $current_site->domain, 0, 4 ) == 'www.' ) } else {
$current_site->cookie_domain = substr( $current_site->domain, 4 ); // Find the first path segment.
wp_cache_set( 'current_site', $current_site, 'site-options' );
return $current_site;
}
$path = substr( $_SERVER['REQUEST_URI'], 0, 1 + strpos( $_SERVER['REQUEST_URI'], '/', 1 ) ); $path = substr( $_SERVER['REQUEST_URI'], 0, 1 + strpos( $_SERVER['REQUEST_URI'], '/', 1 ) );
$current_site = get_network_by_path( $domain, $path );
if ( $domain == $cookie_domain ) // Option 1. We did not find anything.
$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 ) );
if ( ! $current_site ) { 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 ) );
}
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;
}
$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(); 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.' ) ); 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;
}
/** /**
* Displays a failure message. * Displays a failure message.

View File

@ -37,15 +37,17 @@ if ( !isset( $current_site ) || !isset( $current_blog ) ) {
} }
$domain = rtrim( $domain, '.' ); $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 = preg_replace( '|([a-z0-9-]+.php.*)|', '', $_SERVER['REQUEST_URI'] );
$path = str_replace ( '/wp-admin/', '/', $path ); $path = str_replace ( '/wp-admin/', '/', $path );
$path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path ); $path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path );
$current_site = wpmu_current_site(); $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 ) ) 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 ) ); $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 ) );