From 65f22692bbf45438e8e7a989a5e436bb98be9849 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 9 Jun 2020 12:39:09 +0000 Subject: [PATCH] Networks and Sites: Ensure the site ID in `wp_insert_site()` is still correct after calling `clean_blog_cache()`. By saving `$wpdb->insert_id` to a variable, we make sure the ID does not change if another database insert is performed on the `clean_site_cache` action. Props trepmal. Fixes #50324. git-svn-id: https://develop.svn.wordpress.org/trunk@47932 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/ms-site.php | 6 ++++-- tests/phpunit/tests/multisite/site.php | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/ms-site.php b/src/wp-includes/ms-site.php index d4f9a5f9d0..61b81185cc 100644 --- a/src/wp-includes/ms-site.php +++ b/src/wp-includes/ms-site.php @@ -69,9 +69,11 @@ function wp_insert_site( array $data ) { return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error ); } - clean_blog_cache( $wpdb->insert_id ); + $site_id = (int) $wpdb->insert_id; - $new_site = get_site( $wpdb->insert_id ); + clean_blog_cache( $site_id ); + + $new_site = get_site( $site_id ); if ( ! $new_site ) { return new WP_Error( 'get_site_error', __( 'Could not retrieve site data.' ) ); diff --git a/tests/phpunit/tests/multisite/site.php b/tests/phpunit/tests/multisite/site.php index fc7d5bf13b..de4a79f52d 100644 --- a/tests/phpunit/tests/multisite/site.php +++ b/tests/phpunit/tests/multisite/site.php @@ -1386,6 +1386,32 @@ if ( is_multisite() ) : ); } + /** + * @ticket 50324 + */ + public function test_wp_insert_site_with_clean_site_cache() { + remove_action( 'wp_initialize_site', 'wp_initialize_site', 10 ); + + add_action( 'clean_site_cache', array( $this, 'action_database_insert_on_clean_site_cache' ) ); + + $site_id = wp_insert_site( + array( + 'domain' => 'valid-domain.com', + 'path' => '/valid-path/', + 'network_id' => 1, + ) + ); + + remove_action( 'clean_site_cache', array( $this, 'action_database_insert_on_clean_site_cache' ) ); + + $this->assertInternalType( 'integer', $site_id ); + + } + + public function action_database_insert_on_clean_site_cache() { + update_site_option( 'database_write_test.' . time(), true ); + } + /** * @ticket 40364 */