WordPress.org

Make WordPress Core

Changeset 41719


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.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-site.php

    r41162 r41719  
    331331        }
    332332
    333         /** This filter is documented in wp-includes/ms-blogs.php */
     333        /**
     334         * Filters a blog's details.
     335         *
     336         * @since MU (3.0.0)
     337         * @deprecated 4.7.0 Use site_details
     338         *
     339         * @param object $details The blog details.
     340         */
    334341        $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' );
    335342
  • trunk/src/wp-includes/ms-blogs.php

    r41717 r41719  
    109109 *
    110110 * @since MU (3.0.0)
    111  *
    112  * @global wpdb $wpdb WordPress database abstraction object.
     111 * @since 4.9.0 Use get_site_by() internally.
    113112 *
    114113 * @param int|string|array $fields  Optional. A blog ID, a blog slug, or an array of fields to query against.
     
    119118 */
    120119function get_blog_details( $fields = null, $get_all = true ) {
    121     global $wpdb;
    122 
    123     if ( is_array($fields ) ) {
    124         if ( isset($fields['blog_id']) ) {
    125             $blog_id = $fields['blog_id'];
    126         } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
    127             $key = md5( $fields['domain'] . $fields['path'] );
    128             $blog = wp_cache_get($key, 'blog-lookup');
    129             if ( false !== $blog )
    130                 return $blog;
    131             if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
    132                 $nowww = substr( $fields['domain'], 4 );
    133                 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) AND path = %s ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'], $fields['path'] ) );
    134             } else {
    135                 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
    136             }
    137             if ( $blog ) {
    138                 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
    139                 $blog_id = $blog->blog_id;
    140             } else {
    141                 return false;
    142             }
    143         } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
    144             $key = md5( $fields['domain'] );
    145             $blog = wp_cache_get($key, 'blog-lookup');
    146             if ( false !== $blog )
    147                 return $blog;
    148             if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
    149                 $nowww = substr( $fields['domain'], 4 );
    150                 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
    151             } else {
    152                 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
    153             }
    154             if ( $blog ) {
    155                 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
    156                 $blog_id = $blog->blog_id;
    157             } else {
    158                 return false;
    159             }
     120    if ( is_array( $fields ) ) {
     121        if ( isset( $fields['blog_id'] ) ) {
     122            $field = 'id';
     123            $value = (int) $fields['blog_id'];
     124        } elseif ( isset( $fields['domain'] ) && isset( $fields['path'] ) ) {
     125            $field = 'url';
     126            $value = $fields['domain'] . '/' . ltrim( $fields['path'], '/' );
     127        } elseif ( isset( $fields['domain'] ) && is_subdomain_install() ) {
     128            $field = 'domain';
     129            $value = $fields['domain'];
    160130        } else {
    161131            return false;
    162132        }
    163133    } else {
    164         if ( ! $fields )
    165             $blog_id = get_current_blog_id();
    166         elseif ( ! is_numeric( $fields ) )
    167             $blog_id = get_id_from_blogname( $fields );
    168         else
    169             $blog_id = $fields;
    170     }
    171 
    172     $blog_id = (int) $blog_id;
    173 
    174     $all = $get_all == true ? '' : 'short';
    175     $details = wp_cache_get( $blog_id . $all, 'blog-details' );
    176 
    177     if ( $details ) {
    178         if ( ! is_object( $details ) ) {
    179             if ( $details == -1 ) {
    180                 return false;
    181             } else {
    182                 // Clear old pre-serialized objects. Cache clients do better with that.
    183                 wp_cache_delete( $blog_id . $all, 'blog-details' );
    184                 unset($details);
    185             }
     134        if ( ! $fields ) {
     135            $field = 'id';
     136            $value = get_current_blog_id();
     137        } elseif ( ! is_numeric( $fields ) ) {
     138            $field = 'slug';
     139            $value = $fields;
    186140        } else {
    187             return $details;
    188         }
    189     }
    190 
    191     // Try the other cache.
     141            $field = 'id';
     142            $value = (int) $fields;
     143        }
     144    }
     145
     146    $site = get_site_by( $field, $value );
     147
     148    if ( ! $site ) {
     149        return false;
     150    }
     151
    192152    if ( $get_all ) {
    193         $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
    194     } else {
    195         $details = wp_cache_get( $blog_id, 'blog-details' );
    196         // If short was requested and full cache is set, we can return.
    197         if ( $details ) {
    198             if ( ! is_object( $details ) ) {
    199                 if ( $details == -1 ) {
    200                     return false;
    201                 } else {
    202                     // Clear old pre-serialized objects. Cache clients do better with that.
    203                     wp_cache_delete( $blog_id, 'blog-details' );
    204                     unset($details);
    205                 }
    206             } else {
    207                 return $details;
    208             }
    209         }
    210     }
    211 
    212     if ( empty($details) ) {
    213         $details = WP_Site::get_instance( $blog_id );
    214         if ( ! $details ) {
    215             // Set the full cache.
    216             wp_cache_set( $blog_id, -1, 'blog-details' );
    217             return false;
    218         }
    219     }
    220 
    221     if ( ! $details instanceof WP_Site ) {
    222         $details = new WP_Site( $details );
    223     }
    224 
    225     if ( ! $get_all ) {
    226         wp_cache_set( $blog_id . $all, $details, 'blog-details' );
    227         return $details;
    228     }
    229 
    230     switch_to_blog( $blog_id );
    231     $details->blogname   = get_option( 'blogname' );
    232     $details->siteurl    = get_option( 'siteurl' );
    233     $details->post_count = get_option( 'post_count' );
    234     $details->home       = get_option( 'home' );
    235     restore_current_blog();
    236 
    237     /**
    238      * Filters a blog's details.
    239      *
    240      * @since MU (3.0.0)
    241      * @deprecated 4.7.0 Use site_details
    242      *
    243      * @param object $details The blog details.
    244      */
    245     $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' );
    246 
    247     wp_cache_set( $blog_id . $all, $details, 'blog-details' );
    248 
    249     $key = md5( $details->domain . $details->path );
    250     wp_cache_set( $key, $details, 'blog-lookup' );
    251 
    252     return $details;
     153        // Prepopulate magic properties for backward compatibility.
     154        foreach ( array( 'blogname', 'siteurl', 'post_count', 'home' ) as $detail ) {
     155            $site->$detail = $site->$detail;
     156        }
     157    }
     158
     159    return $site;
    253160}
    254161
  • 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
  • trunk/tests/phpunit/tests/multisite/siteDetails.php

    r40478 r41719  
    3333     * @ticket 40063
    3434     */
    35     public function test_update_whitelisted_option_deletes_blog_details_cache( $whitelisted_option, $temporary_value ) {
    36         $blog_details = get_blog_details();
    37 
    38         $original_value = $blog_details->$whitelisted_option;
    39         update_option( $whitelisted_option, $temporary_value );
    40 
    41         $cached_result = wp_cache_get( $blog_details->id, 'blog-details' );
    42 
    43         /* Reset to original value. */
    44         update_option( $whitelisted_option, $original_value );
    45 
    46         $this->assertFalse( $cached_result );
    47     }
    48 
    49     /**
    50      * @dataProvider data_whitelisted_options
    51      *
    52      * @ticket 40063
    53      */
    5435    public function test_update_whitelisted_option_does_not_delete_site_cache( $whitelisted_option, $temporary_value ) {
    5536        $site = get_site();
     
    5940
    6041        $cached_result = wp_cache_get( $site->id, 'sites' );
    61 
    62         /* Reset to original value. */
    63         update_option( $whitelisted_option, $original_value );
    64 
    65         $this->assertNotFalse( $cached_result );
    66     }
    67 
    68     /**
    69      * @dataProvider data_whitelisted_options
    70      *
    71      * @ticket 40063
    72      */
    73     public function test_update_whitelisted_option_does_not_delete_short_blog_details_cache( $whitelisted_option, $temporary_value ) {
    74         $blog_details = get_blog_details( null, false );
    75 
    76         $original_value = get_option( $whitelisted_option );
    77         update_option( $whitelisted_option, $temporary_value );
    78 
    79         $cached_result = wp_cache_get( $blog_details->id . 'short', 'blog-details' );
    8042
    8143        /* Reset to original value. */
Note: See TracChangeset for help on using the changeset viewer.