From e02c481487b71addcd9c4719c7d13566793f24ba Mon Sep 17 00:00:00 2001 From: Jeremy Felt Date: Mon, 2 Oct 2017 01:43:48 +0000 Subject: [PATCH] Multisite: Use `get_current_blog_id()` instead of `$wpdb->blogid`. `get_current_blog_id()` is more appropriate for determining the ID of the current site in most cases. This eliminates the need for the global `$wpdb` in several functions and is better than the implicit global used in admin pages. Props bnap00, spacedmonkey. Fixes #41684. git-svn-id: https://develop.svn.wordpress.org/trunk@41661 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/upgrade.php | 13 ++++++++----- src/wp-admin/ms-delete-site.php | 2 +- src/wp-admin/user-new.php | 2 +- src/wp-includes/ms-blogs.php | 8 +++----- src/wp-includes/ms-default-constants.php | 10 ++++------ src/wp-includes/ms-functions.php | 10 ++++------ tests/phpunit/tests/multisite/site.php | 2 +- tests/phpunit/tests/user/multisite.php | 7 ++++--- 8 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/wp-admin/includes/upgrade.php b/src/wp-admin/includes/upgrade.php index 4f60cbd56f..96c8abb74e 100644 --- a/src/wp-admin/includes/upgrade.php +++ b/src/wp-admin/includes/upgrade.php @@ -433,10 +433,13 @@ function wp_upgrade() { wp_cache_flush(); if ( is_multisite() ) { - if ( $wpdb->get_row( "SELECT blog_id FROM {$wpdb->blog_versions} WHERE blog_id = '{$wpdb->blogid}'" ) ) - $wpdb->query( "UPDATE {$wpdb->blog_versions} SET db_version = '{$wp_db_version}' WHERE blog_id = '{$wpdb->blogid}'" ); - else - $wpdb->query( "INSERT INTO {$wpdb->blog_versions} ( `blog_id` , `db_version` , `last_updated` ) VALUES ( '{$wpdb->blogid}', '{$wp_db_version}', NOW());" ); + $site_id = get_current_blog_id(); + + if ( $wpdb->get_row( $wpdb->prepare( 'SELECT blog_id FROM %s WHERE blog_id = %d', $wpdb->blog_versions, $site_id ) ) ) { + $wpdb->query( $wpdb->prepare( 'UPDATE %s SET db_version = %d WHERE blog_id = %d', $wpdb->blog_versions, $wp_db_version, $site_id ) ); + } else { + $wpdb->query( $wpdb->prepare( 'INSERT INTO %s ( `blog_id` , `db_version` , `last_updated` ) VALUES ( %d, %d, %s);', $wpdb->blog_versions, $site_id, $wp_db_version, NOW() ) ); + } } /** @@ -1257,7 +1260,7 @@ function upgrade_280() { } $start += 20; } - refresh_blog_details( $wpdb->blogid ); + refresh_blog_details(); } } diff --git a/src/wp-admin/ms-delete-site.php b/src/wp-admin/ms-delete-site.php index 6bc3cfcd89..489426ccfa 100644 --- a/src/wp-admin/ms-delete-site.php +++ b/src/wp-admin/ms-delete-site.php @@ -17,7 +17,7 @@ if ( ! current_user_can( 'delete_site' ) ) if ( isset( $_GET['h'] ) && $_GET['h'] != '' && get_option( 'delete_blog_hash' ) != false ) { if ( hash_equals( get_option( 'delete_blog_hash' ), $_GET['h'] ) ) { - wpmu_delete_blog( $wpdb->blogid ); + wpmu_delete_blog( get_current_blog_id() ); wp_die( sprintf( __( 'Thank you for using %s, your site has been deleted. Happy trails to you until we meet again.' ), get_network()->site_name ) ); } else { wp_die( __( "I'm sorry, the link you clicked is stale. Please select another option." ) ); diff --git a/src/wp-admin/user-new.php b/src/wp-admin/user-new.php index 254e5f0f77..ebcd2a7dca 100644 --- a/src/wp-admin/user-new.php +++ b/src/wp-admin/user-new.php @@ -156,7 +156,7 @@ Please click the following link to confirm the invite: add_filter( 'wpmu_signup_user_notification', '__return_false' ); // Disable confirmation email add_filter( 'wpmu_welcome_user_notification', '__return_false' ); // Disable welcome email } - wpmu_signup_user( $new_user_login, $new_user_email, array( 'add_to_blog' => $wpdb->blogid, 'new_role' => $_REQUEST['role'] ) ); + wpmu_signup_user( $new_user_login, $new_user_email, array( 'add_to_blog' => get_current_blog_id(), 'new_role' => $_REQUEST['role'] ) ); if ( isset( $_POST[ 'noconfirmation' ] ) && current_user_can( 'manage_network_users' ) ) { $key = $wpdb->get_var( $wpdb->prepare( "SELECT activation_key FROM {$wpdb->signups} WHERE user_login = %s AND user_email = %s", $new_user_login, $new_user_email ) ); $new_user = wpmu_activate_signup( $key ); diff --git a/src/wp-includes/ms-blogs.php b/src/wp-includes/ms-blogs.php index edfb0e4443..dc52292a3b 100644 --- a/src/wp-includes/ms-blogs.php +++ b/src/wp-includes/ms-blogs.php @@ -12,13 +12,11 @@ * Update the last_updated field for the current site. * * @since MU (3.0.0) - * - * @global wpdb $wpdb WordPress database abstraction object. */ function wpmu_update_blogs_date() { - global $wpdb; + $site_id = get_current_blog_id(); - update_blog_details( $wpdb->blogid, array('last_updated' => current_time('mysql', true)) ); + update_blog_details( $site_id, array( 'last_updated' => current_time( 'mysql', true ) ) ); /** * Fires after the blog details are updated. * @@ -26,7 +24,7 @@ function wpmu_update_blogs_date() { * * @param int $blog_id Site ID. */ - do_action( 'wpmu_blog_updated', $wpdb->blogid ); + do_action( 'wpmu_blog_updated', $site_id ); } /** diff --git a/src/wp-includes/ms-default-constants.php b/src/wp-includes/ms-default-constants.php index 5a54b88dc3..f126d8eedf 100644 --- a/src/wp-includes/ms-default-constants.php +++ b/src/wp-includes/ms-default-constants.php @@ -14,12 +14,8 @@ * wp-includes/ms-files.php (wp-content/blogs.php in MU). * * @since 3.0.0 - * - * @global wpdb $wpdb WordPress database abstraction object. */ function ms_upload_constants() { - global $wpdb; - // This filter is attached in ms-default-filters.php but that file is not included during SHORTINIT. add_filter( 'default_site_option_ms_files_rewriting', '__return_true' ); @@ -33,11 +29,13 @@ function ms_upload_constants() { // Note, the main site in a post-MU network uses wp-content/uploads. // This is handled in wp_upload_dir() by ignoring UPLOADS for this case. if ( ! defined( 'UPLOADS' ) ) { - define( 'UPLOADS', UPLOADBLOGSDIR . "/{$wpdb->blogid}/files/" ); + $site_id = get_current_blog_id(); + + define( 'UPLOADS', UPLOADBLOGSDIR . '/' . $site_id . '/files/' ); // Uploads dir relative to ABSPATH if ( 'wp-content/blogs.dir' == UPLOADBLOGSDIR && ! defined( 'BLOGUPLOADDIR' ) ) - define( 'BLOGUPLOADDIR', WP_CONTENT_DIR . "/blogs.dir/{$wpdb->blogid}/files/" ); + define( 'BLOGUPLOADDIR', WP_CONTENT_DIR . '/blogs.dir/' . $site_id . '/files/' ); } } diff --git a/src/wp-includes/ms-functions.php b/src/wp-includes/ms-functions.php index 53da005cf9..f70deb4e57 100644 --- a/src/wp-includes/ms-functions.php +++ b/src/wp-includes/ms-functions.php @@ -34,19 +34,17 @@ function get_sitestats() { * * @since MU (3.0.0) 1.0 * - * @global wpdb $wpdb WordPress database abstraction object. - * * @param int $user_id The unique ID of the user * @return WP_Site|void The blog object */ function get_active_blog_for_user( $user_id ) { - global $wpdb; $blogs = get_blogs_of_user( $user_id ); if ( empty( $blogs ) ) return; - if ( !is_multisite() ) - return $blogs[$wpdb->blogid]; + if ( ! is_multisite() ) { + return $blogs[ get_current_blog_id() ]; + } $primary_blog = get_user_meta( $user_id, 'primary_blog', true ); $first_blog = current($blogs); @@ -2219,7 +2217,7 @@ function is_user_option_local( $key, $user_id = 0, $blog_id = 0 ) { $current_user = wp_get_current_user(); if ( $blog_id == 0 ) { - $blog_id = $wpdb->blogid; + $blog_id = get_current_blog_id(); } $local_key = $wpdb->get_blog_prefix( $blog_id ) . $key; diff --git a/tests/phpunit/tests/multisite/site.php b/tests/phpunit/tests/multisite/site.php index 47981d2f41..7e3bb54571 100644 --- a/tests/phpunit/tests/multisite/site.php +++ b/tests/phpunit/tests/multisite/site.php @@ -363,7 +363,7 @@ class Tests_Multisite_Site extends WP_UnitTestCase { wpmu_update_blogs_date(); // compare the update time with the current time, allow delta < 2 - $blog = get_site( $wpdb->blogid ); + $blog = get_site( get_current_blog_id() ); $current_time = time(); $time_difference = $current_time - strtotime( $blog->last_updated ); $this->assertLessThan( 2, $time_difference ); diff --git a/tests/phpunit/tests/user/multisite.php b/tests/phpunit/tests/user/multisite.php index f7e5e90289..09c6df086a 100644 --- a/tests/phpunit/tests/user/multisite.php +++ b/tests/phpunit/tests/user/multisite.php @@ -120,7 +120,7 @@ class Tests_Multisite_User extends WP_UnitTestCase { wp_set_current_user( $user1_id ); $this->assertTrue( is_blog_user() ); - $this->assertTrue( is_blog_user( $wpdb->blogid ) ); + $this->assertTrue( is_blog_user( get_current_blog_id() ) ); $blog_ids = array(); @@ -149,12 +149,13 @@ class Tests_Multisite_User extends WP_UnitTestCase { $this->assertFalse( is_user_member_of_blog() ); wp_set_current_user( $user1_id ); + $site_id = get_current_blog_id(); $this->assertTrue( is_user_member_of_blog() ); $this->assertTrue( is_user_member_of_blog( 0, 0 ) ); - $this->assertTrue( is_user_member_of_blog( 0, $wpdb->blogid ) ); + $this->assertTrue( is_user_member_of_blog( 0, $site_id ) ); $this->assertTrue( is_user_member_of_blog( $user1_id ) ); - $this->assertTrue( is_user_member_of_blog( $user1_id, $wpdb->blogid ) ); + $this->assertTrue( is_user_member_of_blog( $user1_id, $site_id ) ); $blog_ids = self::factory()->blog->create_many( 1 ); foreach ( $blog_ids as $blog_id ) {