From b5d68be194087e4270c4ca5c389425dbb7f2db12 Mon Sep 17 00:00:00 2001 From: Jeremy Felt Date: Tue, 27 Sep 2016 18:18:28 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@38659 git-svn-id: http://core.svn.wordpress.org/trunk@38602 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/ms-blogs.php | 31 ++++++++++++++++--------------- wp-includes/version.php | 2 +- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/wp-includes/ms-blogs.php b/wp-includes/ms-blogs.php index bd4f5c1107..7627d11de1 100644 --- a/wp-includes/ms-blogs.php +++ b/wp-includes/ms-blogs.php @@ -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' ); /** diff --git a/wp-includes/version.php b/wp-includes/version.php index e1026a8e5d..4d0f6bd697 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.7-alpha-38658'; +$wp_version = '4.7-alpha-38659'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.