Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
05/21/2016 02:16:38 AM (10 years ago)
Author:
boonebgorges
Message:

Introduce caching for group memberships.

The new system works like this: The bp_groups_memberships_for_user cache
group stores arrays of membership IDs for individual users. The
bp_groups_memberships cache group stores data about individual memberships.
The new function bp_get_user_groups() populates a user's group memberships
from these caches, and filters them as requested in the function parameters.
Then, the various groups_is_user_*() functions use bp_get_user_groups()
instead of direct, uncached database queries to fetch their data.

In addition, the get_group_extras() method of BP_Groups_Group can now be
greatly simplified, since all necessary pre-fetching of current-user group
memberships happens via the bp_get_user_groups() cache.

Props boonebgorges, dcavins.
See #6327.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/classes/class-bp-groups-group.php

    r10767 r10794  
    14231423     */
    14241424    public static function get_group_extras( &$paged_groups, &$group_ids, $type = false ) {
    1425         global $wpdb;
    1426 
    1427         if ( empty( $group_ids ) )
    1428             return $paged_groups;
    1429 
    1430         $bp = buddypress();
    1431 
    1432         // Sanitize group IDs.
    1433         $group_ids = implode( ',', wp_parse_id_list( $group_ids ) );
    1434 
    1435         // Fetch the logged-in user's status within each group.
    1436         if ( is_user_logged_in() ) {
    1437             $user_status_results = $wpdb->get_results( $wpdb->prepare( "SELECT group_id, is_confirmed, invite_sent FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id IN ( {$group_ids} ) AND is_banned = 0", bp_loggedin_user_id() ) );
    1438         } else {
    1439             $user_status_results = array();
    1440         }
    1441 
    1442         // Reindex.
    1443         $user_status = array();
    1444         foreach ( $user_status_results as $user_status_result ) {
    1445             $user_status[ $user_status_result->group_id ] = $user_status_result;
    1446         }
    1447 
    1448         for ( $i = 0, $count = count( $paged_groups ); $i < $count; ++$i ) {
    1449             $is_member = $is_invited = $is_pending = '0';
    1450             $gid = $paged_groups[ $i ]->id;
    1451 
    1452             if ( isset( $user_status[ $gid ] ) ) {
    1453 
    1454                 // The is_confirmed means the user is a member.
    1455                 if ( $user_status[ $gid ]->is_confirmed ) {
    1456                     $is_member = '1';
    1457 
    1458                 // The invite_sent means the user has been invited.
    1459                 } elseif ( $user_status[ $gid ]->invite_sent ) {
    1460                     $is_invited = '1';
    1461 
    1462                 // User has sent request, but has not been confirmed.
    1463                 } else {
    1464                     $is_pending = '1';
    1465                 }
    1466             }
    1467 
    1468             $paged_groups[ $i ]->is_member = $is_member;
    1469             $paged_groups[ $i ]->is_invited = $is_invited;
    1470             $paged_groups[ $i ]->is_pending = $is_pending;
    1471         }
    1472 
    1473         if ( is_user_logged_in() ) {
    1474             $user_banned = $wpdb->get_col( $wpdb->prepare( "SELECT group_id FROM {$bp->groups->table_name_members} WHERE is_banned = 1 AND user_id = %d AND group_id IN ( {$group_ids} )", bp_loggedin_user_id() ) );
    1475         } else {
    1476             $user_banned = array();
    1477         }
    1478 
    1479         for ( $i = 0, $count = count( $paged_groups ); $i < $count; ++$i ) {
    1480             $paged_groups[$i]->is_banned = false;
    1481 
    1482             foreach ( (array) $user_banned as $group_id ) {
    1483                 if ( $group_id == $paged_groups[$i]->id ) {
    1484                     $paged_groups[$i]->is_banned = true;
    1485                 }
    1486             }
     1425        $user_id = bp_loggedin_user_id();
     1426
     1427        foreach ( $paged_groups as &$group ) {
     1428            $group->is_member  = groups_is_user_member( $user_id, $group->id ) ? '1' : '0';
     1429            $group->is_invited = groups_is_user_invited( $user_id, $group->id ) ? '1' : '0';
     1430            $group->is_pending = groups_is_user_pending( $user_id, $group->id ) ? '1' : '0';
     1431            $group->is_banned  = (bool) groups_is_user_banned( $user_id, $group->id );
    14871432        }
    14881433
Note: See TracChangeset for help on using the changeset viewer.