Make WordPress Core


Ignore:
Timestamp:
08/27/2015 08:01:37 PM (9 years ago)
Author:
johnbillion
Message:

Improve the efficiency of is_user_member_of_blog() by removing its use of get_blogs_of_user(). Adds additional tests.

Fixes #32472
Props BinaryKitten, sammybeats, johnbillion

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user-functions.php

    r33749 r33771  
    584584 */
    585585function is_user_member_of_blog( $user_id = 0, $blog_id = 0 ) {
     586    global $wpdb;
     587
    586588    $user_id = (int) $user_id;
    587589    $blog_id = (int) $blog_id;
    588590
    589     if ( empty( $user_id ) )
     591    if ( empty( $user_id ) ) {
    590592        $user_id = get_current_user_id();
    591 
    592     if ( empty( $blog_id ) )
     593    }
     594
     595    // Technically not needed, but does save calls to get_blog_details and get_user_meta
     596    // in the event that the function is called when a user isn't logged in
     597    if ( empty( $user_id ) ) {
     598        return false;
     599    } else {
     600        $user = get_userdata( $user_id );
     601        if ( ! $user instanceof WP_User ) {
     602            return false;
     603        }
     604    }
     605
     606    if ( ! is_multisite() ) {
     607        return true;
     608    }
     609
     610    if ( empty( $blog_id ) ) {
    593611        $blog_id = get_current_blog_id();
    594 
    595     $blogs = get_blogs_of_user( $user_id );
    596     return array_key_exists( $blog_id, $blogs );
     612    }
     613
     614    $blog = get_blog_details( $blog_id );
     615
     616    if ( ! $blog || ! isset( $blog->domain ) || $blog->archived || $blog->spam || $blog->deleted ) {
     617        return false;
     618    }
     619
     620    $keys = get_user_meta( $user_id );
     621    if ( empty( $keys ) ) {
     622        return false;
     623    }
     624
     625    // no underscore before capabilities in $base_capabilities_key
     626    $base_capabilities_key = $wpdb->base_prefix . 'capabilities';
     627    $site_capabilities_key = $wpdb->base_prefix . $blog_id . '_capabilities';
     628
     629    if ( isset( $keys[ $base_capabilities_key ] ) && $blog_id == 1 ) {
     630        return true;
     631    }
     632
     633    if ( isset( $keys[ $site_capabilities_key ] ) ) {
     634        return true;
     635    }
     636
     637    return false;
    597638}
    598639
Note: See TracChangeset for help on using the changeset viewer.