Skip to:
Content

BuddyPress.org

Changeset 5677


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

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

Location:
branches/1.5/bp-friends
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/1.5/bp-friends/bp-friends-classes.php

    r4934 r5677  
    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
  • branches/1.5/bp-friends/bp-friends-functions.php

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