Multisite: Use `get_sites()` instead of a database lookup in `get_id_from_blogname()`.

Because queries generated via `get_sites()` are cached, we can remove the `get_id_from_blogname` cache key.

Props flixos90.
Fixes #38175.


git-svn-id: https://develop.svn.wordpress.org/trunk@38659 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jeremy Felt 2016-09-27 18:17:23 +00:00
parent 1f143d995b
commit 8fcec07595
2 changed files with 16 additions and 22 deletions

View File

@ -72,25 +72,18 @@ function get_blogaddress_by_name( $blogname ) {
}
/**
* Given a blog's (subdomain or directory) slug, retrieve its id.
* Retrieves a sites ID given its (subdomain or directory) slug.
*
* @since MU
* @since 4.7.0 Converted to use get_sites().
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $slug
* @return int A blog id
* @param string $slug A site's slug.
* @return int|null The site ID, or null if no site is found for the given slug.
*/
function get_id_from_blogname( $slug ) {
global $wpdb;
$current_site = get_current_site();
$slug = trim( $slug, '/' );
$blog_id = wp_cache_get( 'get_id_from_blogname_' . $slug, 'blog-details' );
if ( $blog_id )
return $blog_id;
if ( is_subdomain_install() ) {
$domain = $slug . '.' . preg_replace( '|^www\.|', '', $current_site->domain );
$path = $current_site->path;
@ -99,9 +92,18 @@ function get_id_from_blogname( $slug ) {
$path = $current_site->path . $slug . '/';
}
$blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
wp_cache_set( 'get_id_from_blogname_' . $slug, $blog_id, 'blog-details' );
return $blog_id;
$site_ids = get_sites( array(
'number' => 1,
'fields' => 'ids',
'domain' => $domain,
'path' => $path,
) );
if ( empty( $site_ids ) ) {
return null;
}
return array_shift( $site_ids );
}
/**
@ -453,7 +455,6 @@ function clean_blog_cache( $blog ) {
wp_cache_delete( $domain_path_key, 'blog-lookup' );
wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' );
wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' );
wp_cache_delete( 'get_id_from_blogname_' . trim( $blog->path, '/' ), 'blog-details' );
wp_cache_delete( $domain_path_key, 'blog-id-cache' );
/**

View File

@ -87,10 +87,6 @@ class Tests_Multisite_Site extends WP_UnitTestCase {
$this->assertEquals( $details, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
// get_id_from_blogname(), see #20950
$this->assertEquals( $blog_id, get_id_from_blogname( $details->path ) );
$this->assertEquals( $blog_id, wp_cache_get( 'get_id_from_blogname_' . trim( $details->path, '/' ), 'blog-details' ) );
// get_blogaddress_by_name()
$this->assertEquals( 'http://' . $details->domain . $details->path, get_blogaddress_by_name( trim( $details->path, '/' ) ) );
@ -138,7 +134,6 @@ class Tests_Multisite_Site extends WP_UnitTestCase {
// Delete the site without forcing a table drop.
wpmu_delete_blog( $blog_id, false );
$this->assertEquals( false, wp_cache_get( 'get_id_from_blogname_' . trim( $details->path, '/' ), 'blog-details' ) );
$this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
$this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
$this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
@ -177,7 +172,6 @@ class Tests_Multisite_Site extends WP_UnitTestCase {
// Delete the site and force a table drop.
wpmu_delete_blog( $blog_id, true );
$this->assertEquals( false, wp_cache_get( 'get_id_from_blogname_' . trim( $details->path, '/' ), 'blog-details' ) );
$this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
$this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
$this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
@ -216,7 +210,6 @@ class Tests_Multisite_Site extends WP_UnitTestCase {
// Delete the site and force a table drop.
wpmu_delete_blog( $blog_id, true );
$this->assertEquals( false, wp_cache_get( 'get_id_from_blogname_' . trim( $details->path, '/' ), 'blog-details' ) );
$this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
$this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
$this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );