Make WordPress Core


Ignore:
Timestamp:
09/27/2016 06:17:23 PM (10 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.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.