From 69bee8ed43cafef3b5857c4ff55fe5beeed1d154 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Thu, 27 Aug 2015 20:02:23 +0000 Subject: [PATCH] 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 Built from https://develop.svn.wordpress.org/trunk@33771 git-svn-id: http://core.svn.wordpress.org/trunk@33739 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/user-functions.php | 49 +++++++++++++++++++++++++++++++--- wp-includes/version.php | 2 +- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/wp-includes/user-functions.php b/wp-includes/user-functions.php index 894e2b210c..fb13643757 100644 --- a/wp-includes/user-functions.php +++ b/wp-includes/user-functions.php @@ -583,17 +583,58 @@ function get_blogs_of_user( $user_id, $all = false ) { * @return bool */ function is_user_member_of_blog( $user_id = 0, $blog_id = 0 ) { + global $wpdb; + $user_id = (int) $user_id; $blog_id = (int) $blog_id; - if ( empty( $user_id ) ) + if ( empty( $user_id ) ) { $user_id = get_current_user_id(); + } - if ( empty( $blog_id ) ) + // Technically not needed, but does save calls to get_blog_details and get_user_meta + // in the event that the function is called when a user isn't logged in + if ( empty( $user_id ) ) { + return false; + } else { + $user = get_userdata( $user_id ); + if ( ! $user instanceof WP_User ) { + return false; + } + } + + if ( ! is_multisite() ) { + return true; + } + + if ( empty( $blog_id ) ) { $blog_id = get_current_blog_id(); + } - $blogs = get_blogs_of_user( $user_id ); - return array_key_exists( $blog_id, $blogs ); + $blog = get_blog_details( $blog_id ); + + if ( ! $blog || ! isset( $blog->domain ) || $blog->archived || $blog->spam || $blog->deleted ) { + return false; + } + + $keys = get_user_meta( $user_id ); + if ( empty( $keys ) ) { + return false; + } + + // no underscore before capabilities in $base_capabilities_key + $base_capabilities_key = $wpdb->base_prefix . 'capabilities'; + $site_capabilities_key = $wpdb->base_prefix . $blog_id . '_capabilities'; + + if ( isset( $keys[ $base_capabilities_key ] ) && $blog_id == 1 ) { + return true; + } + + if ( isset( $keys[ $site_capabilities_key ] ) ) { + return true; + } + + return false; } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index 191193ad90..b0b9912e14 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.4-alpha-33770'; +$wp_version = '4.4-alpha-33771'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.