BP_Friends_Friendship::get_invitable_friend_count( int $user_id, int $group_id )

Get a count of a user’s friends who can be invited to a given group.

Description

Users can invite any of their friends except:

  • users who are already in the group
  • users who have a pending invite to the group
  • users who have been banned from the group

Parameters

$user_id

(int) (Required) ID of the user whose friends are being counted.

$group_id

(int) (Required) ID of the group friends are being invited to.

Return

(int) $invitable_count Eligible friend count.

Source

File: bp-friends/classes/class-bp-friends-friendship.php

	public static function get_invitable_friend_count( $user_id, $group_id ) {

		// Setup some data we'll use below.
		$is_group_admin  = groups_is_user_admin( $user_id, $group_id );
		$friend_ids      = BP_Friends_Friendship::get_friend_user_ids( $user_id );
		$invitable_count = 0;

		for ( $i = 0, $count = count( $friend_ids ); $i < $count; ++$i ) {

			// If already a member, they cannot be invited again.
			if ( groups_is_user_member( (int) $friend_ids[$i], $group_id ) ) {
				continue;
			}

			// If user already has invite, they cannot be added.
			if ( groups_check_user_has_invite( (int) $friend_ids[$i], $group_id ) ) {
				continue;
			}

			// If user is not group admin and friend is banned, they cannot be invited.
			if ( ( false === $is_group_admin ) && groups_is_user_banned( (int) $friend_ids[$i], $group_id ) ) {
				continue;
			}

			$invitable_count++;
		}

		return $invitable_count;
	}

Changelog

Changelog
Version Description
BuddyPress 1.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.