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 */