Make WordPress Core

Changeset 41883


Ignore:
Timestamp:
10/16/2017 10:28:52 PM (9 years ago)
Author:
flixos90
Message:

Multisite: Revert [41719].

While get_site_by() makes sense as a more explicit and less complex replacement for get_blog_details(), it is not ready yet in terms of caching, where it currently falls short of the older function under specific circumstances.

See #40180, #40228.

Location:
trunk
Files:
4 edited

Legend:

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

    r41719 r41883  
    331331                }
    332332
    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                  */
     333                /** This filter is documented in wp-includes/ms-blogs.php */
    341334                $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' );
    342335
  • trunk/src/wp-includes/ms-blogs.php

    r41795 r41883  
    9494 *
    9595 * @since MU (3.0.0)
    96  * @since 4.9.0 Use get_site_by() internally.
     96 *
     97 * @global wpdb $wpdb WordPress database abstraction object.
    9798 *
    9899 * @param int|string|array $fields  Optional. A blog ID, a blog slug, or an array of fields to query against.
    … …  
    103104 */
    104105function get_blog_details( $fields = null, $get_all = true ) {
    105         if ( is_array( $fields ) ) {
    106                 if ( isset( $fields['blog_id'] ) ) {
    107                         $field = 'id';
    108                         $value = (int) $fields['blog_id'];
    109                 } elseif ( isset( $fields['domain'] ) && isset( $fields['path'] ) ) {
    110                         $field = 'url';
    111                         $value = $fields['domain'] . '/' . ltrim( $fields['path'], '/' );
    112                 } elseif ( isset( $fields['domain'] ) && is_subdomain_install() ) {
    113                         $field = 'domain';
    114                         $value = $fields['domain'];
     106        global $wpdb;
     107
     108        if ( is_array($fields ) ) {
     109                if ( isset($fields['blog_id']) ) {
     110                        $blog_id = $fields['blog_id'];
     111                } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
     112                        $key = md5( $fields['domain'] . $fields['path'] );
     113                        $blog = wp_cache_get($key, 'blog-lookup');
     114                        if ( false !== $blog )
     115                                return $blog;
     116                        if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
     117                                $nowww = substr( $fields['domain'], 4 );
     118                                $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'] ) );
     119                        } else {
     120                                $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
     121                        }
     122                        if ( $blog ) {
     123                                wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
     124                                $blog_id = $blog->blog_id;
     125                        } else {
     126                                return false;
     127                        }
     128                } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
     129                        $key = md5( $fields['domain'] );
     130                        $blog = wp_cache_get($key, 'blog-lookup');
     131                        if ( false !== $blog )
     132                                return $blog;
     133                        if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
     134                                $nowww = substr( $fields['domain'], 4 );
     135                                $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
     136                        } else {
     137                                $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
     138                        }
     139                        if ( $blog ) {
     140                                wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
     141                                $blog_id = $blog->blog_id;
     142                        } else {
     143                                return false;
     144                        }
    115145                } else {
    116146                        return false;
    117147                }
    118148        } else {
    119                 if ( ! $fields ) {
    120                         $field = 'id';
    121                         $value = get_current_blog_id();
    122                 } elseif ( ! is_numeric( $fields ) ) {
    123                         $field = 'slug';
    124                         $value = $fields;
     149                if ( ! $fields )
     150                        $blog_id = get_current_blog_id();
     151                elseif ( ! is_numeric( $fields ) )
     152                        $blog_id = get_id_from_blogname( $fields );
     153                else
     154                        $blog_id = $fields;
     155        }
     156
     157        $blog_id = (int) $blog_id;
     158
     159        $all = $get_all == true ? '' : 'short';
     160        $details = wp_cache_get( $blog_id . $all, 'blog-details' );
     161
     162        if ( $details ) {
     163                if ( ! is_object( $details ) ) {
     164                        if ( $details == -1 ) {
     165                                return false;
     166                        } else {
     167                                // Clear old pre-serialized objects. Cache clients do better with that.
     168                                wp_cache_delete( $blog_id . $all, 'blog-details' );
     169                                unset($details);
     170                        }
    125171                } else {
    126                         $field = 'id';
    127                         $value = (int) $fields;
    128                 }
    129         }
    130 
    131         $site = get_site_by( $field, $value );
    132 
    133         if ( ! $site ) {
    134                 return false;
    135         }
    136 
     172                        return $details;
     173                }
     174        }
     175
     176        // Try the other cache.
    137177        if ( $get_all ) {
    138                 // Prepopulate magic properties for backward compatibility.
    139                 foreach ( array( 'blogname', 'siteurl', 'post_count', 'home' ) as $detail ) {
    140                         $site->$detail = $site->$detail;
    141                 }
    142         }
    143 
    144         return $site;
     178                $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
     179        } else {
     180                $details = wp_cache_get( $blog_id, 'blog-details' );
     181                // If short was requested and full cache is set, we can return.
     182                if ( $details ) {
     183                        if ( ! is_object( $details ) ) {
     184                                if ( $details == -1 ) {
     185                                        return false;
     186                                } else {
     187                                        // Clear old pre-serialized objects. Cache clients do better with that.
     188                                        wp_cache_delete( $blog_id, 'blog-details' );
     189                                        unset($details);
     190                                }
     191                        } else {
     192                                return $details;
     193                        }
     194                }
     195        }
     196
     197        if ( empty($details) ) {
     198                $details = WP_Site::get_instance( $blog_id );
     199                if ( ! $details ) {
     200                        // Set the full cache.
     201                        wp_cache_set( $blog_id, -1, 'blog-details' );
     202                        return false;
     203                }
     204        }
     205
     206        if ( ! $details instanceof WP_Site ) {
     207                $details = new WP_Site( $details );
     208        }
     209
     210        if ( ! $get_all ) {
     211                wp_cache_set( $blog_id . $all, $details, 'blog-details' );
     212                return $details;
     213        }
     214
     215        switch_to_blog( $blog_id );
     216        $details->blogname   = get_option( 'blogname' );
     217        $details->siteurl    = get_option( 'siteurl' );
     218        $details->post_count = get_option( 'post_count' );
     219        $details->home       = get_option( 'home' );
     220        restore_current_blog();
     221
     222        /**
     223         * Filters a blog's details.
     224         *
     225         * @since MU (3.0.0)
     226         * @deprecated 4.7.0 Use site_details
     227         *
     228         * @param object $details The blog details.
     229         */
     230        $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' );
     231
     232        wp_cache_set( $blog_id . $all, $details, 'blog-details' );
     233
     234        $key = md5( $details->domain . $details->path );
     235        wp_cache_set( $key, $details, 'blog-lookup' );
     236
     237        return $details;
    145238}
    146239
  • trunk/tests/phpunit/tests/multisite/site.php

    r41719 r41883  
    119119                $details = get_blog_details( $blog_id, false );
    120120
    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 ) );
     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' ) );
    124125
    125126                // get_blogaddress_by_name()
    126127                $this->assertEquals( 'http://' . $details->domain . $details->path, get_blogaddress_by_name( trim( $details->path, '/' ) ) );
    127128
    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' ) );
     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' ) );
    130132
    131133                // $get_all = true, populate the full blog-details cache and the blog slug lookup cache
    132134                $details = get_blog_details( $blog_id, true );
    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 ) );
     135                $this->assertEquals( $details, wp_cache_get( $blog_id, 'blog-details' ) );
     136                $this->assertEquals( $details, wp_cache_get( $key, 'blog-lookup' ) );
    136137
    137138                // Check existence of each database table for the created site.
    … …  
    196197                wpmu_delete_blog( $blog_id, false );
    197198
    198                 $this->assertEquals( false, wp_cache_get( $blog_id, 'sites' ) );
    199                 $this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) );
     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' ) );
    200202                $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
    201203        }
    … …  
    233235                wpmu_delete_blog( $blog_id, true );
    234236
    235                 $this->assertEquals( false, wp_cache_get( $blog_id, 'sites' ) );
    236                 $this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) );
     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' ) );
    237240                $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
    238241        }
    … …  
    270273                wpmu_delete_blog( $blog_id, true );
    271274
    272                 $this->assertEquals( false, wp_cache_get( $blog_id, 'sites' ) );
    273                 $this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) );
     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' ) );
    274278                $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
    275279        }
    … …  
    387391                get_blog_details( $blog_id );
    388392
    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' ) );
     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' ) );
    391395
    392396                // Create a site in the invalid site's place.
    … …  
    394398
    395399                // When a new site is created, its cache is cleared through refresh_blog_details.
    396                 $this->assertFalse( wp_cache_get( $blog_id, 'site-details' )  );
     400                $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' )  );
    397401
    398402                $blog = get_blog_details( $blog_id );
    399403
    400404                // When the cache is refreshed, it should now equal the site data.
    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 ) );
     405                $this->assertEquals( $blog, wp_cache_get( $blog_id, 'blog-details' ) );
    404406        }
    405407
  • trunk/tests/phpunit/tests/multisite/siteDetails.php

    r41719 r41883  
    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         */
    3554        public function test_update_whitelisted_option_does_not_delete_site_cache( $whitelisted_option, $temporary_value ) {
    3655                $site = get_site();
    … …  
    4059
    4160                $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' );
    4280
    4381                /* Reset to original value. */
Note: See TracChangeset for help on using the changeset viewer.