/**
 * Check whether a user has an outstanding membership request for a given group.
 *
 * @param int $user_id  ID of the user.
 * @param int $group_id ID of the group.
 *
 * @return int|null ID of the membership if found, otherwise false.
 */
function groups_check_for_membership_request($user_id, $group_id)
{
    return BP_Groups_Member::check_for_membership_request($user_id, $group_id);
}
/**
 * Invites a friend to join a group via a POST request.
 *
 * @since BuddyPress (1.2)
 * @todo Audit return types
 */
function bp_legacy_theme_ajax_invite_user()
{
    // Bail if not a POST action
    if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) {
        return;
    }
    check_ajax_referer('groups_invite_uninvite_user');
    if (!$_POST['friend_id'] || !$_POST['friend_action'] || !$_POST['group_id']) {
        return;
    }
    if (!bp_groups_user_can_send_invites($_POST['group_id'])) {
        return;
    }
    if (!friends_check_friendship(bp_loggedin_user_id(), $_POST['friend_id'])) {
        return;
    }
    $group_id = (int) $_POST['group_id'];
    $friend_id = (int) $_POST['friend_id'];
    if ('invite' == $_POST['friend_action']) {
        $group = groups_get_group($group_id);
        // Users who have previously requested membership do not need
        // another invitation created for them
        if (BP_Groups_Member::check_for_membership_request($friend_id, $group_id)) {
            $user_status = 'is_pending';
            // Create the user invitation
        } elseif (groups_invite_user(array('user_id' => $friend_id, 'group_id' => $group_id))) {
            $user_status = 'is_invited';
            // Miscellaneous failure
        } else {
            return;
        }
        $user = new BP_Core_User($friend_id);
        $uninvite_url = bp_is_current_action('create') ? bp_get_groups_directory_permalink() . 'create/step/group-invites/?user_id=' . $friend_id : bp_get_group_permalink($group) . 'send-invites/remove/' . $friend_id;
        echo '<li id="uid-' . esc_attr($user->id) . '">';
        echo $user->avatar_thumb;
        echo '<h4>' . $user->user_link . '</h4>';
        echo '<span class="activity">' . esc_attr($user->last_active) . '</span>';
        echo '<div class="action">
				<a class="button remove" href="' . wp_nonce_url($uninvite_url, 'groups_invite_uninvite_user') . '" id="uid-' . esc_attr($user->id) . '">' . __('Remove Invite', 'buddypress') . '</a>
			  </div>';
        if ('is_pending' == $user_status) {
            echo '<p class="description">' . sprintf(__('%s has previously requested to join this group. Sending an invitation will automatically add the member to the group.', 'buddypress'), $user->user_link) . '</p>';
        }
        echo '</li>';
        exit;
    } elseif ('uninvite' == $_POST['friend_action']) {
        // Users who have previously requested membership should not
        // have their requests deleted on the "uninvite" action
        if (BP_Groups_Member::check_for_membership_request($friend_id, $group_id)) {
            return;
        }
        // Remove the unsent invitation
        if (!groups_uninvite_user($friend_id, $group_id)) {
            return;
        }
        exit;
    } else {
        return;
    }
}
 /**
  * Set up data about the current group.
  */
 public function populate()
 {
     global $wpdb;
     // Get BuddyPress.
     $bp = buddypress();
     // Check cache for group data.
     $group = wp_cache_get($this->id, 'bp_groups');
     // Cache missed, so query the DB.
     if (false === $group) {
         $group = $wpdb->get_row($wpdb->prepare("SELECT g.* FROM {$bp->groups->table_name} g WHERE g.id = %d", $this->id));
         wp_cache_set($this->id, $group, 'bp_groups');
     }
     // No group found so set the ID and bail.
     if (empty($group) || is_wp_error($group)) {
         $this->id = 0;
         return;
     }
     // Group found so setup the object variables.
     $this->id = $group->id;
     $this->creator_id = $group->creator_id;
     $this->name = stripslashes($group->name);
     $this->slug = $group->slug;
     $this->description = stripslashes($group->description);
     $this->status = $group->status;
     $this->enable_forum = $group->enable_forum;
     $this->date_created = $group->date_created;
     // Are we getting extra group data?
     if (!empty($this->args['populate_extras'])) {
         /**
          * Filters the SQL prepared statement used to fetch group admins and mods.
          *
          * @since 1.5.0
          *
          * @param string $value SQL select statement used to fetch admins and mods.
          */
         $admin_mods = $wpdb->get_results(apply_filters('bp_group_admin_mods_user_join_filter', $wpdb->prepare("SELECT u.ID as user_id, u.user_login, u.user_email, u.user_nicename, m.is_admin, m.is_mod FROM {$wpdb->users} u, {$bp->groups->table_name_members} m WHERE u.ID = m.user_id AND m.group_id = %d AND ( m.is_admin = 1 OR m.is_mod = 1 )", $this->id)));
         // Add admins and moderators to their respective arrays.
         foreach ((array) $admin_mods as $user) {
             if (!empty($user->is_admin)) {
                 $this->admins[] = $user;
             } else {
                 $this->mods[] = $user;
             }
         }
         // Set up some specific group vars from meta. Excluded
         // from the bp_groups cache because it's cached independently.
         $this->last_activity = groups_get_groupmeta($this->id, 'last_activity');
         $this->total_member_count = groups_get_groupmeta($this->id, 'total_member_count');
         // Set user-specific data.
         $user_id = bp_loggedin_user_id();
         $this->is_member = BP_Groups_Member::check_is_member($user_id, $this->id);
         $this->is_invited = BP_Groups_Member::check_has_invite($user_id, $this->id);
         $this->is_pending = BP_Groups_Member::check_for_membership_request($user_id, $this->id);
         // If this is a private or hidden group, does the current user have access?
         if ('private' === $this->status || 'hidden' === $this->status) {
             // Assume user does not have access to hidden/private groups.
             $this->user_has_access = false;
             // Group members or community moderators have access.
             if ($this->is_member && is_user_logged_in() || bp_current_user_can('bp_moderate')) {
                 $this->user_has_access = true;
             }
         } else {
             $this->user_has_access = true;
         }
     }
 }
/**
 * Process group invitation removal requests.
 *
 * Note that this function is only used when JS is disabled. Normally, clicking
 * Remove Invite removes the invitation via AJAX.
 *
 * @since 2.0.0
 */
function groups_remove_group_invite()
{
    if (!bp_is_group_invites()) {
        return;
    }
    if (!bp_is_action_variable('remove', 0) || !is_numeric(bp_action_variable(1))) {
        return;
    }
    if (!check_admin_referer('groups_invite_uninvite_user')) {
        return false;
    }
    $friend_id = intval(bp_action_variable(1));
    $group_id = bp_get_current_group_id();
    $message = __('Invite successfully removed', 'buddypress');
    $redirect = wp_get_referer();
    $error = false;
    if (!bp_groups_user_can_send_invites($group_id)) {
        $message = __('You are not allowed to send or remove invites', 'buddypress');
        $error = 'error';
    } elseif (BP_Groups_Member::check_for_membership_request($friend_id, $group_id)) {
        $message = __('The member requested to join the group', 'buddypress');
        $error = 'error';
    } elseif (!groups_uninvite_user($friend_id, $group_id)) {
        $message = __('There was an error removing the invite', 'buddypress');
        $error = 'error';
    }
    bp_core_add_message($message, $error);
    bp_core_redirect($redirect);
}
/**
 * Process the acceptance of a group invitation.
 *
 * Returns true if a user is already a member of the group.
 *
 * @since 1.0.0
 *
 * @param int $user_id  ID of the user.
 * @param int $group_id ID of the group.
 * @return bool True when the user is a member of the group, otherwise false.
 */
function groups_accept_invite($user_id, $group_id)
{
    // If the user is already a member (because BP at one point allowed two invitations to
    // slip through), delete all existing invitations/requests and return true.
    if (groups_is_user_member($user_id, $group_id)) {
        if (groups_check_user_has_invite($user_id, $group_id)) {
            groups_delete_invite($user_id, $group_id);
        }
        if (groups_check_for_membership_request($user_id, $group_id)) {
            groups_delete_membership_request(null, $user_id, $group_id);
        }
        return true;
    }
    $member = new BP_Groups_Member($user_id, $group_id);
    $member->accept_invite();
    if (!$member->save()) {
        return false;
    }
    // Remove request to join.
    if ($member->check_for_membership_request($user_id, $group_id)) {
        $member->delete_request($user_id, $group_id);
    }
    // Modify group meta.
    groups_update_groupmeta($group_id, 'last_activity', bp_core_current_time());
    /**
     * Fires after a user has accepted a group invite.
     *
     * @since 1.0.0
     *
     * @param int $user_id  ID of the user who accepted the group invite.
     * @param int $group_id ID of the group being accepted to.
     */
    do_action('groups_accept_invite', $user_id, $group_id);
    return true;
}