Make WordPress Core

Ticket #40201: 40201.4.diff

File 40201.4.diff, 4.2 KB (added by flixos90, 7 years ago)
  • src/wp-includes/ms-blogs.php

     
    265265                $blog_id = get_current_blog_id();
    266266        }
    267267
    268         $details = get_site( $blog_id );
    269         if ( ! $details ) {
    270                 // Make sure clean_blog_cache() gets the blog ID
    271                 // when the blog has been previously cached as
    272                 // non-existent.
    273                 $details = (object) array(
    274                         'blog_id' => $blog_id,
    275                         'domain' => null,
    276                         'path' => null
    277                 );
    278         }
    279 
    280         clean_blog_cache( $details );
    281 
    282         /**
    283          * Fires after the blog details cache is cleared.
    284          *
    285          * @since 3.4.0
    286          *
    287          * @param int $blog_id Blog ID.
    288          */
    289         do_action( 'refresh_blog_details', $blog_id );
     268        clean_blog_cache( $blog_id );
    290269}
    291270
    292271/**
     
    443422 *
    444423 * @global bool $_wp_suspend_cache_invalidation
    445424 *
    446  * @param WP_Site $blog The site object to be cleared from cache.
     425 * @param WP_Site|int $blog The site object or ID to be cleared from cache.
    447426 */
    448427function clean_blog_cache( $blog ) {
    449428        global $_wp_suspend_cache_invalidation;
     
    452431                return;
    453432        }
    454433
     434        if ( empty( $blog ) ) {
     435                return;
     436        }
     437
     438        $blog_id = $blog;
     439        $blog = get_site( $blog_id );
     440        if ( ! $blog ) {
     441                if ( ! is_numeric( $blog_id ) ) {
     442                        return;
     443                }
     444
     445                // Make sure a WP_Site object exists even when the site has been deleted.
     446                $blog = new WP_Site( (object) array(
     447                        'blog_id' => $blog_id,
     448                        'domain'  => null,
     449                        'path'    => null,
     450                ) );
     451        }
     452
    455453        $blog_id = $blog->blog_id;
    456454        $domain_path_key = md5( $blog->domain . $blog->path );
    457455
     
    476474        do_action( 'clean_site_cache', $blog_id, $blog, $domain_path_key );
    477475
    478476        wp_cache_set( 'last_changed', microtime(), 'sites' );
     477
     478        /**
     479         * Fires after the blog details cache is cleared.
     480         *
     481         * @since 3.4.0
     482         * @deprecated 4.9.0 Use clean_site_cache
     483         *
     484         * @param int $blog_id Blog ID.
     485         */
     486        do_action_deprecated( 'refresh_blog_details', array( $blog_id ), '4.9.0', 'clean_site_cache' );
    479487}
    480488
    481489/**
  • tests/phpunit/tests/multisite/site.php

     
    10861086
    10871087        /**
    10881088         * @ticket 40201
     1089         * @dataProvider data_get_site_caches
     1090         */
     1091        public function test_clean_blog_cache_with_id( $key, $group ) {
     1092                $site = get_site( self::$site_ids['make.wordpress.org/'] );
     1093
     1094                $replacements = array(
     1095                        '%blog_id%'         => $site->blog_id,
     1096                        '%domain%'          => $site->domain,
     1097                        '%path%'            => $site->path,
     1098                        '%domain_path_key%' => md5( $site->domain . $site->path ),
     1099                );
     1100
     1101                $key = str_replace( array_keys( $replacements ), array_values( $replacements ), $key );
     1102
     1103                if ( 'sites' === $group ) { // This needs to be actual data for get_site() lookups.
     1104                        wp_cache_set( $key, (object) $site->to_array(), $group );
     1105                } else {
     1106                        wp_cache_set( $key, 'something', $group );
     1107                }
     1108
     1109                clean_blog_cache( $site->blog_id );
     1110                $this->assertFalse( wp_cache_get( $key, $group ) );
     1111        }
     1112
     1113        /**
     1114         * @ticket 40201
    10891115         */
    10901116        public function test_clean_blog_cache_resets_last_changed() {
    10911117                $site = get_site( self::$site_ids['make.wordpress.org/'] );
     
    11241150
    11251151        /**
    11261152         * @ticket 40201
     1153         */
     1154        public function test_clean_blog_cache_bails_on_empty_input() {
     1155                $old_count = did_action( 'clean_site_cache' );
     1156
     1157                clean_blog_cache( null );
     1158                $this->assertEquals( $old_count, did_action( 'clean_site_cache' ) );
     1159        }
     1160
     1161        /**
     1162         * @ticket 40201
     1163         */
     1164        public function test_clean_blog_cache_bails_on_non_numeric_input() {
     1165                $old_count = did_action( 'clean_site_cache' );
     1166
     1167                clean_blog_cache( 'something' );
     1168                $this->assertEquals( $old_count, did_action( 'clean_site_cache' ) );
     1169        }
     1170
     1171        /**
     1172         * @ticket 40201
     1173         */
     1174        public function test_clean_blog_cache_works_with_deleted_site() {
     1175                $site_id = 12345;
     1176
     1177                wp_cache_set( $site_id, 'something', 'site-details' );
     1178
     1179                clean_blog_cache( $site_id );
     1180                $this->assertFalse( wp_cache_get( $site_id, 'site-details' ) );
     1181        }
     1182
     1183        /**
     1184         * @ticket 40201
    11271185         * @dataProvider data_get_site_caches
    11281186         */
    11291187        public function test_refresh_blog_details( $key, $group ) {