Make WordPress Core

Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#19160 closed defect (bug) (fixed)

is_user_member_of_blog() gives unexpected results on single site

Reported by: duck_'s profile duck_ Owned by: duck_'s profile duck_
Milestone: 3.3 Priority: normal
Severity: normal Version: 3.1
Component: Users Keywords: has-patch
Focuses: Cc:

Description (last modified by duck_)

On single site with a logged in user:

var_dump( is_user_member_of_blog() ); // bool(false)

This is because when passed an empty $blog_id the function uses $wpdb->blogid as the current blog which is 0 in single site, but the blog ID set by get_blogs_of_user() is get_current_blog_id().

Two possible solutions:

  1. Use get_current_blog_id()
  2. Return result of is_user_logged_in() if ! is_multisite() Only works for the current user

Attached patch for 1.

is_blog_user() was included for single site in 3.1, see [15671], and didn't work as expected then either. It also caused a PHP notice as well as returning false.

Also see #16702, especially comment 21. Noticed whilst writing patches for #19122.

Attachments (3)

19160.diff (480 bytes) - added by duck_ 13 years ago.
19160.2.diff (1.0 KB) - added by duck_ 13 years ago.
19160.get_blogs_of_user.diff (935 bytes) - added by duck_ 13 years ago.

Download all attachments as: .zip

Change History (10)

13 years ago

#1 @duck_
13 years ago

  • Description modified (diff)

Scratch out 2. as I wasn't thinking about passing a $user_id.

#2 @nacin
13 years ago

Looks good. Any other instances of blogid we need to be worried about?

I'm thinking $wpdb->blogid shouldn't be 0 in single-site, either.

13 years ago

#3 @duck_
13 years ago

Unfortunately this would mean any user, including logged out visitors, would be a member of the single site. 19160.2.diff also moves the empty( $user_id ) checking in get_blogs_of_user() to be before the ! is_multisite() check.

What about passing random IDs of non-existent users?

#4 @duck_
13 years ago

In [19208]:

get_blogs_of_user() should return false for logged out and non-existent users on single site. See #19160.

#5 @duck_
13 years ago

  • Owner set to duck_
  • Resolution set to fixed
  • Status changed from new to closed

In [19209]:

is_user_member_of_blog() [no args] should return true for logged in users on single site. $wpdb->blogid is 0, but the current blog ID isn't. Fixes #19160.

#6 @duck_
13 years ago

[UT476] (currently has some funkiness due to lingering user_meta caches after user deletion)

#7 @duck_
13 years ago

In [19265]:

Default arguments for is_user_member_of_blog() can now be used. See #19122, #19160.

Note: See TracTickets for help on using tickets.