Make WordPress Core

Ticket #42251: 42251.2.diff

File 42251.2.diff, 5.1 KB (added by nielsdeblaauw, 5 years ago)
  • src/wp-admin/includes/schema.php

     
    11671167                }
    11681168        }
    11691169
     1170        if ( function_exists( 'clean_network_cache' ) ) {
     1171                clean_network_cache( $network_id );
     1172        } else {
     1173                wp_cache_delete( $network_id, 'networks' );
     1174        }
     1175
    11701176        wp_cache_delete( 'networks_have_paths', 'site-options' );
    11711177
    11721178        if ( ! is_multisite() ) {
  • src/wp-includes/class-wp-network.php

     
    101101
    102102                $_network = wp_cache_get( $network_id, 'networks' );
    103103
    104                 if ( ! $_network ) {
     104                if ( false === $_network ) {
    105105                        $_network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->site} WHERE id = %d LIMIT 1", $network_id ) );
    106106
    107107                        if ( empty( $_network ) || is_wp_error( $_network ) ) {
    108                                 return false;
     108                                $_network = -1;
    109109                        }
    110110
    111111                        wp_cache_add( $network_id, $_network, 'networks' );
    112112                }
    113113
     114                if ( is_numeric( $_network ) ) {
     115                        return false;
     116                }
     117
    114118                return new WP_Network( $_network );
    115119        }
    116120
  • src/wp-includes/class-wp-site.php

     
    162162
    163163                $_site = wp_cache_get( $site_id, 'sites' );
    164164
    165                 if ( ! $_site ) {
     165                if ( false === $_site ) {
    166166                        $_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d LIMIT 1", $site_id ) );
    167167
    168168                        if ( empty( $_site ) || is_wp_error( $_site ) ) {
    169                                 return false;
     169                                $_site = -1;
    170170                        }
    171171
    172172                        wp_cache_add( $site_id, $_site, 'sites' );
    173173                }
    174174
     175                if ( is_numeric( $_site ) ) {
     176                        return false;
     177                }
     178
    175179                return new WP_Site( $_site );
    176180        }
    177181
  • tests/phpunit/tests/multisite/network.php

     
    613613
    614614                        $this->assertSame( (string) self::$different_site_ids[0], $network->blog_id );
    615615                }
     616
     617                /**
     618                 * @ticket 42251
     619                 */
     620                public function test_wp_site_get_instance_doesnt_exist() {
     621                        $this->assertFalse( WP_Network::get_instance( $this->_get_next_network_id() ) );
     622                }
     623
     624                /**
     625                 * @ticket 42251
     626                 */
     627                public function test_wp_site_get_instance_doesnt_exist_but_later_exists() {
     628                        $new_site_id = $this->_get_next_network_id();
     629                        $this->assertFalse( WP_Network::get_instance( $new_site_id ) );
     630                        $new_network = $this->factory()->network->create_and_get();
     631                        //make sure we were right about what the new network's ID would be
     632                        $this->assertEquals( $new_site_id, $new_network->id );
     633                        //verify that if we fetch the network now, it's no longer false
     634                        $fetched_network = WP_Network::get_instance( $new_site_id );
     635                        $this->assertInstanceOf( 'WP_Network', $fetched_network );
     636                        $this->assertEquals( $new_site_id, $fetched_network->id );
     637                }
     638
     639                /**
     640                 * Gets the ID of the site with the highest ID
     641                 * @return int
     642                 */
     643                protected function _get_next_network_id() {
     644                        global $wpdb;
     645                        //create an extra network, just to make sure we know the ID of the following one
     646                        static::factory()->network->create();
     647                        return (int) $wpdb->get_var( 'SELECT id FROM ' . $wpdb->site . ' ORDER BY id DESC LIMIT 1' ) + 1;
     648                }
    616649        }
    617650
    618651endif;
  • tests/phpunit/tests/multisite/site.php

     
    23702370                }
    23712371
    23722372                /**
     2373                 * @ticket 42251
     2374                 */
     2375                public function test_wp_site_get_instance_doesnt_exist() {
     2376                        $this->assertFalse( WP_Site::get_instance( $this->_get_next_site_id() ) );
     2377                }
     2378
     2379                /**
     2380                 * @ticket 42251
     2381                 */
     2382                public function test_wp_site_get_instance_doesnt_exist_but_later_exists() {
     2383                        $new_site_id = $this->_get_next_site_id();
     2384                        $this->assertFalse( WP_Site::get_instance( $new_site_id ) );
     2385                        $new_site = $this->factory()->blog->create_and_get();
     2386                        //double check we got the ID of the new site correct
     2387                        $this->assertEquals( $new_site_id, $new_site->blog_id );
     2388                        //verify that if we fetch the site now, it's no longer false
     2389                        $fetched_site = WP_Site::get_instance( $new_site_id );
     2390                        $this->assertInstanceOf( 'WP_Site', $fetched_site );
     2391                        $this->assertEquals( $new_site_id, $fetched_site->blog_id );
     2392
     2393                }
     2394
     2395                /**
     2396                 * Gets the ID of the next site that will get inserted
     2397                 * @return int
     2398                 */
     2399                protected function _get_next_site_id() {
     2400                        global $wpdb;
     2401                        //create an entry
     2402                        static::factory()->blog->create();
     2403                        //get the ID after it
     2404                        return (int)$wpdb->get_var( 'SELECT blog_id FROM ' . $wpdb->blogs . ' ORDER BY blog_ID DESC LIMIT 1' ) + 1;
     2405                }
     2406
     2407                /**
    23732408                 * Capture the $meta value passed to the wpmu_new_blog action and compare it.
    23742409                 */
    23752410                public function wpmu_new_blog_callback( $blog_id, $user_id, $domain, $path, $network_id, $meta ) {