Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
08/01/2016 08:26:53 PM (8 years ago)
Author:
boonebgorges
Message:

Reduce the number of loops in bp_get_user_groups().

By applying $filters before adding to the found $groups array, we
eliminate one loop through the list of groups, and also eliminate a
costly call to wp_list_filter().

See #7208.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-functions.php

    r10977 r10978  
    860860    }
    861861
    862     // Populate group membership array from cache.
    863     $groups = array();
    864     foreach ( $membership_ids as $membership_id ) {
    865         $membership = wp_cache_get( $membership_id, 'bp_groups_memberships' );
    866 
    867         // Sanity check.
    868         if ( ! isset( $membership->group_id ) ) {
    869             continue;
    870         }
    871 
    872         $group_id = (int) $membership->group_id;
    873 
    874         $groups[ $group_id ] = $membership;
    875     }
    876 
    877     // Normalize group data.
    878     $int_keys  = array( 'id', 'group_id', 'user_id', 'inviter_id' );
    879     $bool_keys = array( 'is_admin', 'is_mod', 'is_confirmed', 'is_banned', 'invite_sent' );
    880     foreach ( $groups as &$group ) {
    881         // Integer values.
    882         foreach ( $int_keys as $index ) {
    883             $group->{$index} = intval( $group->{$index} );
    884         }
    885 
    886         // Boolean values.
    887         foreach ( $bool_keys as $index ) {
    888             $group->{$index} = (bool) $group->{$index};
    889         }
    890     }
    891 
    892862    // Assemble filter array for use in `wp_list_filter()`.
    893863    $filters = wp_array_slice_assoc( $r, array( 'is_confirmed', 'is_banned', 'is_admin', 'is_mod', 'invite_sent' ) );
     
    898868    }
    899869
    900     if ( ! empty( $filters ) ) {
    901         $groups = wp_list_filter( $groups, $filters );
     870    // Populate group membership array from cache, and normalize.
     871    $groups    = array();
     872    $int_keys  = array( 'id', 'group_id', 'user_id', 'inviter_id' );
     873    $bool_keys = array( 'is_admin', 'is_mod', 'is_confirmed', 'is_banned', 'invite_sent' );
     874    foreach ( $membership_ids as $membership_id ) {
     875        $membership = wp_cache_get( $membership_id, 'bp_groups_memberships' );
     876
     877        // Sanity check.
     878        if ( ! isset( $membership->group_id ) ) {
     879            continue;
     880        }
     881
     882        // Integer values.
     883        foreach ( $int_keys as $index ) {
     884            $membership->{$index} = intval( $membership->{$index} );
     885        }
     886
     887        // Boolean values.
     888        foreach ( $bool_keys as $index ) {
     889            $membership->{$index} = (bool) $membership->{$index};
     890        }
     891
     892        foreach ( $filters as $filter_name => $filter_value ) {
     893            if ( ! isset( $membership->{$filter_name} ) || $filter_value != $membership->{$filter_name} ) {
     894                continue 2;
     895            }
     896        }
     897
     898        $group_id = (int) $membership->group_id;
     899
     900        $groups[ $group_id ] = $membership;
    902901    }
    903902
Note: See TracChangeset for help on using the changeset viewer.