[wp-trac] [WordPress Trac] #31746: get_blogs_of_user() can be very slow when a user is a member of thousands of sites

WordPress Trac noreply at wordpress.org
Tue Mar 24 05:58:18 UTC 2015


#31746: get_blogs_of_user() can be very slow when a user is a member of thousands
of sites
--------------------------+------------------------------
 Reporter:  jtsternberg   |       Owner:
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Users         |     Version:  trunk
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:  multisite
--------------------------+------------------------------

Comment (by SergeyBiryukov):

 > When a user belongs to thousands of sites, the array of `$blogs` can
 become rather large.

 I encountered this issue as well. I help manage a network where WordPress
 is used as an LMS, and one of the super admins is also a trainer added to
 each student's blog.

 `get_blogs_of_user()` was making hundreds of queries for that account on
 each page load in the admin, often leading to slow response times and out
 of memory errors.

 A hacky workaround was to short-circuit the function for super admins, who
 have access to all the blogs anyway:
 {{{
 function wp31746_skip_get_blogs_of_user_for_super_admin( $null,
 $object_id, $meta_key, $single ) {
         global $wpdb;

         if ( $meta_key || ! is_super_admin() ) {
                 return $null;
         }

         remove_filter( 'get_user_metadata', __FUNCTION__, 10, 4 );

         $keys = get_user_meta( $object_id );

         add_filter( 'get_user_metadata', __FUNCTION__, 10, 4 );

         foreach ( $keys as $key => $value ) {
                 if ( 'capabilities' !== substr( $key, -12 ) ) {
                         continue;
                 }

                 if ( $wpdb->base_prefix && 0 !== strpos( $key,
 $wpdb->base_prefix ) ) {
                         continue;
                 }

                 $blog_id = str_replace( array( $wpdb->base_prefix,
 '_capabilities' ), '', $key );
                 if ( ! is_numeric( $blog_id ) ) {
                         continue;
                 }

                 unset( $keys[ $key ] );
         }

         return $keys;
 }
 add_filter( 'get_user_metadata',
 'wp31746_skip_get_blogs_of_user_for_super_admin', 10, 4 );
 }}}

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


More information about the wp-trac mailing list