WordPress.org

Make WordPress Core

Changeset 41661


Ignore:
Timestamp:
10/02/2017 01:43:48 AM (2 years ago)
Author:
jeremyfelt
Message:

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.

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/upgrade.php

    r41289 r41661  
    434434
    435435    if ( is_multisite() ) {
    436         if ( $wpdb->get_row( "SELECT blog_id FROM {$wpdb->blog_versions} WHERE blog_id = '{$wpdb->blogid}'" ) )
    437             $wpdb->query( "UPDATE {$wpdb->blog_versions} SET db_version = '{$wp_db_version}' WHERE blog_id = '{$wpdb->blogid}'" );
    438         else
    439             $wpdb->query( "INSERT INTO {$wpdb->blog_versions} ( `blog_id` , `db_version` , `last_updated` ) VALUES ( '{$wpdb->blogid}', '{$wp_db_version}', NOW());" );
     436        $site_id = get_current_blog_id();
     437
     438        if ( $wpdb->get_row( $wpdb->prepare( 'SELECT blog_id FROM %s WHERE blog_id = %d', $wpdb->blog_versions, $site_id ) ) ) {
     439            $wpdb->query( $wpdb->prepare( 'UPDATE %s SET db_version = %d WHERE blog_id = %d', $wpdb->blog_versions, $wp_db_version, $site_id ) );
     440        } else {
     441            $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() ) );
     442        }
    440443    }
    441444
     
    12581261            $start += 20;
    12591262        }
    1260         refresh_blog_details( $wpdb->blogid );
     1263        refresh_blog_details();
    12611264    }
    12621265}
  • trunk/src/wp-admin/ms-delete-site.php

    r38961 r41661  
    1818if ( isset( $_GET['h'] ) && $_GET['h'] != '' && get_option( 'delete_blog_hash' ) != false ) {
    1919    if ( hash_equals( get_option( 'delete_blog_hash' ), $_GET['h'] ) ) {
    20         wpmu_delete_blog( $wpdb->blogid );
     20        wpmu_delete_blog( get_current_blog_id() );
    2121        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 ) );
    2222    } else {
  • trunk/src/wp-admin/user-new.php

    r41225 r41661  
    157157                add_filter( 'wpmu_welcome_user_notification', '__return_false' ); // Disable welcome email
    158158            }
    159             wpmu_signup_user( $new_user_login, $new_user_email, array( 'add_to_blog' => $wpdb->blogid, 'new_role' => $_REQUEST['role'] ) );
     159            wpmu_signup_user( $new_user_login, $new_user_email, array( 'add_to_blog' => get_current_blog_id(), 'new_role' => $_REQUEST['role'] ) );
    160160            if ( isset( $_POST[ 'noconfirmation' ] ) && current_user_can( 'manage_network_users' ) ) {
    161161                $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 ) );
  • trunk/src/wp-includes/ms-blogs.php

    r41625 r41661  
    1313 *
    1414 * @since MU (3.0.0)
    15  *
    16  * @global wpdb $wpdb WordPress database abstraction object.
    1715 */
    1816function wpmu_update_blogs_date() {
    19     global $wpdb;
    20 
    21     update_blog_details( $wpdb->blogid, array('last_updated' => current_time('mysql', true)) );
     17    $site_id = get_current_blog_id();
     18
     19    update_blog_details( $site_id, array( 'last_updated' => current_time( 'mysql', true ) ) );
    2220    /**
    2321     * Fires after the blog details are updated.
     
    2725     * @param int $blog_id Site ID.
    2826     */
    29     do_action( 'wpmu_blog_updated', $wpdb->blogid );
     27    do_action( 'wpmu_blog_updated', $site_id );
    3028}
    3129
  • trunk/src/wp-includes/ms-default-constants.php

    r38814 r41661  
    1515 *
    1616 * @since 3.0.0
    17  *
    18  * @global wpdb $wpdb WordPress database abstraction object.
    1917 */
    2018function ms_upload_constants() {
    21     global $wpdb;
    22 
    2319    // This filter is attached in ms-default-filters.php but that file is not included during SHORTINIT.
    2420    add_filter( 'default_site_option_ms_files_rewriting', '__return_true' );
     
    3430    // This is handled in wp_upload_dir() by ignoring UPLOADS for this case.
    3531    if ( ! defined( 'UPLOADS' ) ) {
    36         define( 'UPLOADS', UPLOADBLOGSDIR . "/{$wpdb->blogid}/files/" );
     32        $site_id = get_current_blog_id();
     33
     34        define( 'UPLOADS', UPLOADBLOGSDIR . '/' . $site_id . '/files/' );
    3735
    3836        // Uploads dir relative to ABSPATH
    3937        if ( 'wp-content/blogs.dir' == UPLOADBLOGSDIR && ! defined( 'BLOGUPLOADDIR' ) )
    40             define( 'BLOGUPLOADDIR', WP_CONTENT_DIR . "/blogs.dir/{$wpdb->blogid}/files/" );
     38            define( 'BLOGUPLOADDIR', WP_CONTENT_DIR . '/blogs.dir/' . $site_id . '/files/' );
    4139    }
    4240}
  • trunk/src/wp-includes/ms-functions.php

    r41617 r41661  
    3535 * @since MU (3.0.0) 1.0
    3636 *
    37  * @global wpdb $wpdb WordPress database abstraction object.
    38  *
    3937 * @param int $user_id The unique ID of the user
    4038 * @return WP_Site|void The blog object
    4139 */
    4240function get_active_blog_for_user( $user_id ) {
    43     global $wpdb;
    4441    $blogs = get_blogs_of_user( $user_id );
    4542    if ( empty( $blogs ) )
    4643        return;
    4744
    48     if ( !is_multisite() )
    49         return $blogs[$wpdb->blogid];
     45    if ( ! is_multisite() ) {
     46        return $blogs[ get_current_blog_id() ];
     47    }
    5048
    5149    $primary_blog = get_user_meta( $user_id, 'primary_blog', true );
     
    22202218    $current_user = wp_get_current_user();
    22212219    if ( $blog_id == 0 ) {
    2222         $blog_id = $wpdb->blogid;
     2220        $blog_id = get_current_blog_id();
    22232221    }
    22242222    $local_key = $wpdb->get_blog_prefix( $blog_id ) . $key;
  • trunk/tests/phpunit/tests/multisite/site.php

    r41380 r41661  
    364364
    365365        // compare the update time with the current time, allow delta < 2
    366         $blog = get_site( $wpdb->blogid );
     366        $blog = get_site( get_current_blog_id() );
    367367        $current_time = time();
    368368        $time_difference = $current_time - strtotime( $blog->last_updated );
  • trunk/tests/phpunit/tests/user/multisite.php

    r41255 r41661  
    121121
    122122        $this->assertTrue( is_blog_user() );
    123         $this->assertTrue( is_blog_user( $wpdb->blogid ) );
     123        $this->assertTrue( is_blog_user( get_current_blog_id() ) );
    124124
    125125        $blog_ids = array();
     
    150150
    151151        wp_set_current_user( $user1_id );
     152        $site_id = get_current_blog_id();
    152153
    153154        $this->assertTrue( is_user_member_of_blog() );
    154155        $this->assertTrue( is_user_member_of_blog( 0, 0 ) );
    155         $this->assertTrue( is_user_member_of_blog( 0, $wpdb->blogid ) );
     156        $this->assertTrue( is_user_member_of_blog( 0, $site_id ) );
    156157        $this->assertTrue( is_user_member_of_blog( $user1_id ) );
    157         $this->assertTrue( is_user_member_of_blog( $user1_id, $wpdb->blogid ) );
     158        $this->assertTrue( is_user_member_of_blog( $user1_id, $site_id ) );
    158159
    159160        $blog_ids = self::factory()->blog->create_many( 1 );
Note: See TracChangeset for help on using the changeset viewer.