BP_Groups_Invitation_Manager

Group invitations class.

Description

An extension of the core Invitations class that adapts the core logic to accommodate group invitation behavior.

Source

File: bp-groups/classes/class-bp-groups-invitation-manager.php

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
class BP_Groups_Invitation_Manager extends BP_Invitation_Manager {
    /**
     * Construct parameters.
     *
     * @since BuddyBoss 1.3.5
     *
     * @param array|string $args.
     */
    public function __construct( $args = '' ) {
        parent::__construct();
    }
 
    /**
     * This is where custom actions are added to run when notifications of an
     * invitation or request need to be generated & sent.
     *
     * @since BuddyBoss 1.3.5
     *
     * @param int $id The ID of the invitation to mark as sent.
     * @return bool True on success, false on failure.
     */
    public function run_send_action( BP_Invitation $invitation ) {
        // Notify group admins of the pending request
        if ( 'request' === $invitation->type ) {
            $admins = groups_get_group_admins( $invitation->item_id );
 
            foreach ( $admins as $admin ) {
                groups_notification_new_membership_request( $invitation->user_id, $admin->user_id, $invitation->item_id, $invitation->id );
            }
            return true;
 
        // Notify the invitee of the invitation.
        } else {
            $group = groups_get_group( $invitation->item_id );
            groups_notification_group_invites( $group, $invitation->user_id, $invitation->inviter_id );
            return true;
        }
    }
 
    /**
     * This is where custom actions are added to run when an invitation
     * or request is accepted.
     *
     * @since BuddyBoss 1.3.5
     *
     * @param string $type Are we accepting an invitation or request?
     * @param array  $r    Parameters that describe the invitation being accepted.
     * @return bool True on success, false on failure.
     */
    public function run_acceptance_action( $type = 'invite', $r  ) {
        // If the user is already a member (because BP at one point allowed two invitations to
        // slip through), return early.
        if ( groups_is_user_member( $r['user_id'], $r['item_id'] ) ) {
            return true;
        }
 
        // Create the new membership
        $member = new BP_Groups_Member( $r['user_id'], $r['item_id'] );
 
        if ( 'request' === $type ) {
            $member->accept_request();
        } else {
            $member->accept_invite();
        }
 
        if ( ! $member->save() ) {
            return false;
        }
 
        if ( 'request' === $type ) {
            /**
             * Fires after a group membership request has been accepted.
             *
             * @since BuddyPress 1.0.0
             *
             * @param int  $user_id  ID of the user who accepted membership.
             * @param int  $group_id ID of the group that was accepted membership to.
             * @param bool $value    If membership was accepted.
             */
            do_action( 'groups_membership_accepted', $r['user_id'], $r['item_id'], true );
        } else {
            // Get an inviter_id from the invitation.
            $invites = groups_get_invites( $r );
            $inviter_id = 0;
            if ( $invites ) {
                $inviter_id = current( $invites )->inviter_id;
            }
 
            /**
             * Fires after a user has accepted a group invite.
             *
             * @since BuddyPress 1.0.0
             * @since BuddyPress 2.8.0 The $inviter_id arg was added.
             *
             * @param int $user_id    ID of the user who accepted the group invite.
             * @param int $group_id   ID of the group being accepted to.
             * @param int $inviter_id ID of the user who invited this user to the group.
             */
            do_action( 'groups_accept_invite', $r['user_id'], $r['item_id'], $inviter_id );
        }
 
        // Modify group meta.
        groups_update_groupmeta( $r['item_id'], 'last_activity', bp_core_current_time() );
 
        return true;
    }
 
    /**
     * With group invitations, we don't need to keep the old record, so we delete rather than
     * mark invitations as "accepted."
     *
     * @since BuddyBoss 1.3.5
     *
     * @see BP_Invitation::mark_accepted_by_data()
     *      for a description of arguments.
     *
     * @param array $args.
     */
    public function mark_accepted( $args ) {
        // Delete all existing invitations/requests to this group for this user.
        $this->delete( array(
            'user_id' => $args['user_id'],
            'item_id' => $args['item_id'],
            'type'    => 'all'
        ) );
    }
 
    /**
     * Should this invitation be created?
     *
     * @since BuddyBoss 1.3.5
     *
     * @param array $args.
     * @return bool
     */
    public function allow_invitation( $args ) {
        // Does the inviter have this capability?
        if ( ! bp_user_can( $args['inviter_id'], 'groups_send_invitation', array( 'group_id' => $args['item_id'] ) ) ) {
            return false;
        }
 
        // Is the invited user eligible to receive an invitation?
        if ( ! bp_user_can( $args['user_id'], 'groups_receive_invitation', array( 'group_id' => $args['item_id'] ) ) ) {
            return false;
        }
 
        // Prevent duplicated invitations.
        if ( groups_check_has_invite_from_user( $args['user_id'], $args['item_id'], $args['inviter_id'], 'all' ) ) {
            return false;
        }
 
        return true;
    }
 
    /**
     * Should this request be created?
     *
     * @since BuddyBoss 1.3.5
     *
     * @param array $args.
     * @return bool.
     */
    public function allow_request( $args ) {
        // Does the requester have this capability? (Also checks for duplicates.)
        if ( ! bp_user_can( $args['user_id'], 'groups_request_membership', array( 'group_id' => $args['item_id'] ) ) ) {
            return false;
        }
 
        return true;
    }
}

Changelog

Changelog
Version Description
BuddyBoss 1.3.5 Introduced.

Methods

  • __construct — Construct parameters.
  • allow_invitation — Should this invitation be created?
  • allow_request — Should this request be created?
  • mark_accepted — With group invitations, we don't need to keep the old record, so we delete rather than mark invitations as "accepted."
  • run_acceptance_action — This is where custom actions are added to run when an invitation or request is accepted.
  • run_send_action — This is where custom actions are added to run when notifications of an invitation or request need to be generated & sent.

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.