Skip to:
Content

BuddyPress.org

Changeset 5676


Ignore:
Timestamp:
02/07/2012 12:19:43 AM (15 years ago)
Author:
johnjamesjacoby
Message:

Exclude existing group members and previously banned friends in group invites list. See #3969 (trunk)

Location:
trunk/bp-friends
Files:
2 edited

Legend:

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

    r5302 r5676  
    295295
    296296        function get_invitable_friend_count( $user_id, $group_id ) {
    297                 global $wpdb, $bp;
    298 
    299                 $friend_ids = BP_Friends_Friendship::get_friend_user_ids( $user_id );
    300 
     297
     298                // Setup some data we'll use below
     299                $is_group_admin  = BP_Groups_Member::check_is_admin( $user_id, $group_id );
     300                $friend_ids      = BP_Friends_Friendship::get_friend_user_ids( $user_id );
    301301                $invitable_count = 0;
     302
    302303                for ( $i = 0, $count = count( $friend_ids ); $i < $count; ++$i ) {
    303304
    304                         if ( BP_Groups_Member::check_is_member( (int)$friend_ids[$i], $group_id ) )
     305                        // If already a member, they cannot be invited again
     306                        if ( BP_Groups_Member::check_is_member( (int) $friend_ids[$i], $group_id ) )
    305307                                continue;
    306308
    307                         if ( BP_Groups_Member::check_has_invite( (int)$friend_ids[$i], $group_id )  )
     309                        // If user already has invite, they cannot be added
     310                        if ( BP_Groups_Member::check_has_invite( (int) $friend_ids[$i], $group_id )  )
     311                                continue;
     312
     313                        // If user is not group admin and friend is banned, they cannot be invited
     314                        if ( ( false === $is_group_admin ) && BP_Groups_Member::check_is_banned( (int) $friend_ids[$i], $group_id ) )
    308315                                continue;
    309316
  • trunk/bp-friends/bp-friends-functions.php

    r5389 r5676  
    210210}
    211211
    212 function friends_get_friends_invite_list( $user_id = 0 ) {
    213         global $bp;
    214 
    215         if ( !$user_id )
     212/**
     213 * Get a list of friends that a user can invite into this group.
     214 *
     215 * Excludes friends that are already in the group, and banned friends if the
     216 * user is not a group admin.
     217 *
     218 * @since 1.0
     219 * @param int $user_id User ID whose friends to see can be invited
     220 * @param int $group_id Group to check possible invitations against
     221 * @return mixed False if no friends, array of users if friends
     222 */
     223function friends_get_friends_invite_list( $user_id = 0, $group_id = 0 ) {
     224
     225        // Default to logged in user id
     226        if ( empty( $user_id ) )
    216227                $user_id = bp_loggedin_user_id();
    217228
    218         if ( bp_has_members( 'user_id=' . $user_id . '&type=alphabetical&per_page=0' ) ) {
    219                 while ( bp_members() ) : bp_the_member();
     229        // Only group admins can invited previously banned users
     230        $user_is_admin = (bool) groups_is_user_admin( $user_id, $group_id );
     231
     232        // Assume no friends
     233        $friends = array();
     234
     235        // Default args
     236        $args = apply_filters( 'bp_friends_pre_get_invite_list', array(
     237                'user_id'  => $user_id,
     238                'type'     => 'alphabetical',
     239                'per_page' => 0
     240        ) );
     241
     242        // User has friends
     243        if ( bp_has_members( $args ) ) {
     244
     245                /**
     246                 * Loop through all friends and try to add them to the invitation list.
     247                 *
     248                 * Exclude friends that:
     249                 *     1. are already members of the group
     250                 *     2. are banned from this group if the current user is also not a
     251                 *        group admin.
     252                 */
     253                while ( bp_members() ) :
     254
     255                        // Load the member
     256                        bp_the_member();
     257
     258                        // Get the user ID of the friend
     259                        $friend_user_id = bp_get_member_user_id();
     260
     261                        // Skip friend if already in the group
     262                        if ( groups_is_user_member( $friend_user_id, $group_id ) )
     263                                continue;
     264
     265                        // Skip friend if not group admin and user banned from group
     266                        if ( ( false === $user_is_admin ) && groups_is_user_banned( $friend_user_id, $group_id ) )
     267                                continue;
     268
     269                        // Friend is safe, so add it to the array of possible friends
    220270                        $friends[] = array(
    221                                 'id' => bp_get_member_user_id(),
     271                                'id'        => $friend_user_id,
    222272                                'full_name' => bp_get_member_name()
    223273                        );
     274
    224275                endwhile;
    225276        }
    226277
    227         if ( empty($friends) )
    228                 return false;
    229 
    230         return $friends;
     278        // If no friends, explicitly set to false
     279        if ( empty( $friends ) )
     280                $friends = false;
     281
     282        // Allow friends to be filtered
     283        return apply_filters( 'bp_friends_get_invite_list', $friends, $user_id, $group_id );
    231284}
    232285
Note: See TracChangeset for help on using the changeset viewer.