Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/29/2013 07:37:58 PM (11 years ago)
Author:
boonebgorges
Message:

Refactor get_group_extras logic in group loops

The previous logic made it so that is_pending, is_invited, and is_member
values were only set in the case where users had a membership for the group
in the database. This could result in PHP warnings in certain cases, and in
all cases results in inconsistent data structures between member and non-member
groups within group loops.

Fixes #5269

File:
1 edited

Legend:

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

    r7583 r7616  
    855855
    856856        // Fetch the logged-in user's status within each group
    857         $user_status = $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() ) );
     857        $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() ) );
     858
     859        // Reindex
     860        $user_status = array();
     861        foreach ( $user_status_results as $user_status_result ) {
     862            $user_status[ $user_status_result->group_id ] = $user_status_result;
     863        }
    858864
    859865        for ( $i = 0, $count = count( $paged_groups ); $i < $count; ++$i ) {
    860             foreach ( (array) $user_status as $group ) {
    861                 if ( $group->group_id == $paged_groups[ $i ]->id ) {
    862                     $is_member = $is_invited = $is_pending = '0';
    863 
    864                     // is_confirmed means the user is a member
    865                     if ( $group->is_confirmed ) {
    866                         $is_member = '1';
    867 
    868                     // invite_sent means the user has been invited
    869                     } else if ( $group->invite_sent ) {
    870                         $is_invited = '1';
    871 
    872                     // User has sent request, but has not been confirmed
    873                     } else {
    874                         $is_pending = '1';
    875                     }
    876 
    877                     $paged_groups[ $i ]->is_member = $is_member;
    878                     $paged_groups[ $i ]->is_invited = $is_invited;
    879                     $paged_groups[ $i ]->is_pending = $is_pending;
     866            $is_member = $is_invited = $is_pending = '0';
     867            $gid = $paged_groups[ $i ]->id;
     868
     869            if ( isset( $user_status[ $gid ] ) ) {
     870
     871                // is_confirmed means the user is a member
     872                if ( $user_status[ $gid ]->is_confirmed ) {
     873                    $is_member = '1';
     874
     875                // invite_sent means the user has been invited
     876                } else if ( $user_status[ $gid ]->invite_sent ) {
     877                    $is_invited = '1';
     878
     879                // User has sent request, but has not been confirmed
     880                } else {
     881                    $is_pending = '1';
    880882                }
    881883            }
     884
     885            $paged_groups[ $i ]->is_member = $is_member;
     886            $paged_groups[ $i ]->is_invited = $is_invited;
     887            $paged_groups[ $i ]->is_pending = $is_pending;
    882888        }
    883889
Note: See TracChangeset for help on using the changeset viewer.