Skip to:
Content

BuddyPress.org


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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.