Make WordPress Core

Ticket #38175: 38175.diff

File 38175.diff, 4.0 KB (added by jeremyfelt, 9 years ago)
  • src/wp-includes/ms-blogs.php

     
    7272}
    7373
    7474/**
    75  * Given a blog's (subdomain or directory) slug, retrieve its id.
     75 * Retrieves a sites ID given its (subdomain or directory) slug.
    7676 *
    7777 * @since MU
     78 * @since 4.7.0 Converted to use get_sites().
    7879 *
    79  * @global wpdb $wpdb WordPress database abstraction object.
    80  *
    81  * @param string $slug
    82  * @return int A blog id
     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, '/' );
    8986
    90         $blog_id = wp_cache_get( 'get_id_from_blogname_' . $slug, 'blog-details' );
    91         if ( $blog_id )
    92                 return $blog_id;
    93 
    9487        if ( is_subdomain_install() ) {
    9588                $domain = $slug . '.' . preg_replace( '|^www\.|', '', $current_site->domain );
    9689                $path = $current_site->path;
     
    9992                $path = $current_site->path . $slug . '/';
    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
    107109/**
     
    453455        wp_cache_delete(  $domain_path_key, 'blog-lookup' );
    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
    459460        /**
  • tests/phpunit/tests/multisite/site.php

     
    8787
    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, '/' ) ) );
    9692
     
    138134                // Delete the site without forcing a table drop.
    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' ) );
    144139                $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
     
    177172                // Delete the site and force a table drop.
    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' ) );
    183177                $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
     
    216210                // Delete the site and force a table drop.
    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' ) );
    222215                $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );