Make WordPress Core

Changeset 38659


Ignore:
Timestamp:
09/27/2016 06:17:23 PM (8 years ago)
Author:
jeremyfelt
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/ms-blogs.php

    r38658 r38659  
    7373
    7474/**
    75  * Given a blog's (subdomain or directory) slug, retrieve its id.
    76  *
    77  * @since MU
    78  *
    79  * @global wpdb $wpdb WordPress database abstraction object.
    80  *
    81  * @param string $slug
    82  * @return int A blog id
     75 * Retrieves a sites ID given its (subdomain or directory) slug.
     76 *
     77 * @since MU
     78 * @since 4.7.0 Converted to use get_sites().
     79 *
     80 * @param string $slug A site's slug.
     81 * @return int|null The site ID, or null if no site is found for the given slug.
    8382 */
    8483function get_id_from_blogname( $slug ) {
    85     global $wpdb;
    86 
    8784    $current_site = get_current_site();
    8885    $slug = trim( $slug, '/' );
    89 
    90     $blog_id = wp_cache_get( 'get_id_from_blogname_' . $slug, 'blog-details' );
    91     if ( $blog_id )
    92         return $blog_id;
    9386
    9487    if ( is_subdomain_install() ) {
     
    10093    }
    10194
    102     $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
    103     wp_cache_set( 'get_id_from_blogname_' . $slug, $blog_id, 'blog-details' );
    104     return $blog_id;
     95    $site_ids = get_sites( array(
     96        'number' => 1,
     97        'fields' => 'ids',
     98        'domain' => $domain,
     99        'path' => $path,
     100    ) );
     101
     102    if ( empty( $site_ids ) ) {
     103        return null;
     104    }
     105
     106    return array_shift( $site_ids );
    105107}
    106108
     
    454456    wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' );
    455457    wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' );
    456     wp_cache_delete( 'get_id_from_blogname_' . trim( $blog->path, '/' ), 'blog-details' );
    457458    wp_cache_delete( $domain_path_key, 'blog-id-cache' );
    458459
  • trunk/tests/phpunit/tests/multisite/site.php

    r38655 r38659  
    8888        $this->assertEquals( $details, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
    8989
    90         // get_id_from_blogname(), see #20950
    91         $this->assertEquals( $blog_id, get_id_from_blogname( $details->path ) );
    92         $this->assertEquals( $blog_id, wp_cache_get( 'get_id_from_blogname_' . trim( $details->path, '/' ), 'blog-details' ) );
    93 
    9490        // get_blogaddress_by_name()
    9591        $this->assertEquals( 'http://' . $details->domain . $details->path, get_blogaddress_by_name( trim( $details->path, '/' ) ) );
     
    139135        wpmu_delete_blog( $blog_id, false );
    140136
    141         $this->assertEquals( false, wp_cache_get( 'get_id_from_blogname_' . trim( $details->path, '/' ), 'blog-details' ) );
    142137        $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
    143138        $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
     
    178173        wpmu_delete_blog( $blog_id, true );
    179174
    180         $this->assertEquals( false, wp_cache_get( 'get_id_from_blogname_' . trim( $details->path, '/' ), 'blog-details' ) );
    181175        $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
    182176        $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
     
    217211        wpmu_delete_blog( $blog_id, true );
    218212
    219         $this->assertEquals( false, wp_cache_get( 'get_id_from_blogname_' . trim( $details->path, '/' ), 'blog-details' ) );
    220213        $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
    221214        $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
Note: See TracChangeset for help on using the changeset viewer.