WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/03/2017 07:40:01 PM (2 years ago)
Author:
flixos90
Message:

Multisite: Improve get_blog_details() by using get_site_by().

get_site_by() is now the preferred way to retrieve a site object by lookup for identifying data. By using a coherent structure and get_sites() internally, it has several advantages over the direct database queries and complex code in get_blog_details(). Therefore get_blog_details() is now a wrapper for get_site_by(), providing backward compatibility fixes where necessary.

Unit tests have been adjusted to account for the blog-details and blog-lookup cache groups, which are no longer needed.

Props spacedmonkey, jeremyfelt, flixos90.
Fixes #40228.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/multisite/site.php

    r41716 r41719  
    119119        $details = get_blog_details( $blog_id, false );
    120120
    121         // Combine domain and path for a site specific cache key.
    122         $key = md5( $details->domain . $details->path );
    123 
    124         $this->assertEquals( $details, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
     121        $cached_details = wp_cache_get( $blog_id, 'sites' );
     122        $this->assertNotFalse( $cached_details );
     123        $this->assertEqualSets( get_object_vars( $details ), get_object_vars( $cached_details ) );
    125124
    126125        // get_blogaddress_by_name()
    127126        $this->assertEquals( 'http://' . $details->domain . $details->path, get_blogaddress_by_name( trim( $details->path, '/' ) ) );
    128127
    129         // These are empty until get_blog_details() is called with $get_all = true
    130         $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
    131         $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
     128        // This is empty until get_blog_details() is called with $get_all = true
     129        $this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) );
    132130
    133131        // $get_all = true, populate the full blog-details cache and the blog slug lookup cache
    134132        $details = get_blog_details( $blog_id, true );
    135         $this->assertEquals( $details, wp_cache_get( $blog_id, 'blog-details' ) );
    136         $this->assertEquals( $details, wp_cache_get( $key, 'blog-lookup' ) );
     133        $cached_details = wp_cache_get( $blog_id, 'site-details' );
     134        $this->assertNotFalse( $cached_details );
     135        $this->assertEqualSets( get_object_vars( $details ), get_object_vars( $cached_details ) );
    137136
    138137        // Check existence of each database table for the created site.
     
    197196        wpmu_delete_blog( $blog_id, false );
    198197
    199         $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
    200         $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
    201         $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
     198        $this->assertEquals( false, wp_cache_get( $blog_id, 'sites' ) );
     199        $this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) );
    202200        $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
    203201    }
     
    235233        wpmu_delete_blog( $blog_id, true );
    236234
    237         $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
    238         $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
    239         $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
     235        $this->assertEquals( false, wp_cache_get( $blog_id, 'sites' ) );
     236        $this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) );
    240237        $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
    241238    }
     
    273270        wpmu_delete_blog( $blog_id, true );
    274271
    275         $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
    276         $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
    277         $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
     272        $this->assertEquals( false, wp_cache_get( $blog_id, 'sites' ) );
     273        $this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) );
    278274        $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
    279275    }
     
    391387        get_blog_details( $blog_id );
    392388
    393         // When the cache is primed with an invalid site, the value is set to -1.
    394         $this->assertEquals( -1, wp_cache_get( $blog_id, 'blog-details' ) );
     389        // When the cache is primed with an invalid site, the value is not set.
     390        $this->assertFalse( wp_cache_get( $blog_id, 'site-details' ) );
    395391
    396392        // Create a site in the invalid site's place.
     
    398394
    399395        // When a new site is created, its cache is cleared through refresh_blog_details.
    400         $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' )  );
     396        $this->assertFalse( wp_cache_get( $blog_id, 'site-details' )  );
    401397
    402398        $blog = get_blog_details( $blog_id );
    403399
    404400        // When the cache is refreshed, it should now equal the site data.
    405         $this->assertEquals( $blog, wp_cache_get( $blog_id, 'blog-details' ) );
     401        $cached_blog = wp_cache_get( $blog_id, 'site-details' );
     402        $this->assertNotFalse( $cached_blog );
     403        $this->assertEqualSets( get_object_vars( $blog ), get_object_vars( $cached_blog ) );
    406404    }
    407405
Note: See TracChangeset for help on using the changeset viewer.