[wp-trac] [WordPress Trac] #32472: is_user_member_of_blog() can be very slow when a user is a member of hundreds of sites

WordPress Trac noreply at wordpress.org
Sat May 23 16:40:53 UTC 2015


#32472: is_user_member_of_blog() can be very slow when a user is a member of
hundreds of sites
--------------------------+-------------------------------------
 Reporter:  sammybeats    |       Owner:
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Users         |     Version:  3.0
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:  multisite, performance
--------------------------+-------------------------------------

Comment (by BinaryKitten):

 Replying to [ticket:32472 sammybeats]:
 Hi Sammybeats,

 I like this conceptually, but possibly there is further scope  for
 improvement

 {{{
 function is_user_member_of_blog( $user_id = 0, $blog_id = 0 ) {
     global $wpdb;

     if( ! is_multisite() )
         return true;

     $user_id = (int) $user_id;
     $blog_id = (int) $blog_id;

     if ( empty( $user_id ) )
         $user_id = get_current_user_id();

     if ( empty( $blog_id ) )
         $blog_id = get_current_blog_id();

     $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;

     $base_capabilities_key = $wpdb->base_prefix . '_capabilities';
     $site_capabilities_key = $wpdb->base_prefix . $blog_id .
 '_capabilities';

     if ( isset( $keys[ $base_capabilities_key ] ) && defined( 'MULTISITE'
 ) && $blog_id == 1 )
         return true;

     if ( isset( $keys[ $site_capabilities_key ] ) )
         return true;

 }
 }}}

 Where there are many sites and many plugins that have added to the
 usermeta this prevents the need for the iteration.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/32472#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list