bp_groups_user_can_filter( bool $retval, int $user_id, string $capability, int $site_id, array $args )

Filter the bp_user_can value to determine what the user can do with regards to a specific group.

Description

Parameters

$retval

(Required) Whether or not the current user has the capability.

$user_id

(Required)

$capability

(Required) The capability being checked for.

$site_id

(Required) Site ID. Defaults to the BP root blog.

$args

(Required) Array of extra arguments passed.

Return

(bool)

Source

File: bp-groups/bp-groups-filters.php

253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
function bp_groups_user_can_filter( $retval, $user_id, $capability, $site_id, $args ) {
    if ( empty( $args['group_id'] ) ) {
        $group_id = bp_get_current_group_id();
    } else {
        $group_id = (int) $args['group_id'];
    }
 
    switch ( $capability ) {
        case 'groups_join_group':
            // Return early if the user isn't logged in or the group ID is unknown.
            if ( ! $user_id || ! $group_id ) {
                break;
            }
 
            // Set to false to begin with.
            $retval = false;
 
            // The group must allow joining, and the user should not currently be a member.
            $group = groups_get_group( $group_id );
            if ( ( 'public' === bp_get_group_status( $group )
                && ! groups_is_user_member( $user_id, $group->id )
                && ! groups_is_user_banned( $user_id, $group->id ) )
                // Site admins can join any group they are not a member of.
                || ( bp_user_can( $user_id, 'bp_moderate' )
                && ! groups_is_user_member( $user_id, $group->id ) )
            ) {
                $retval = true;
            }
            break;
 
        case 'groups_request_membership':
            // Return early if the user isn't logged in or the group ID is unknown.
            if ( ! $user_id || ! $group_id ) {
                break;
            }
 
            // Set to false to begin with.
            $retval = false;
 
            /*
            * The group must accept membership requests, and the user should not
            * currently be a member or be banned.
            */
            $group = groups_get_group( $group_id );
            if ( 'private' === bp_get_group_status( $group )
                && ! groups_is_user_member( $user_id, $group->id )
                && ! groups_check_for_membership_request( $user_id, $group->id )
                && ! groups_is_user_banned( $user_id, $group->id )
            ) {
                $retval = true;
            }
            break;
 
        case 'groups_send_invitation':
            // Return early if the user isn't logged in or the group ID is unknown.
            if ( ! $user_id || ! $group_id ) {
                break;
            }
 
            /*
            * The group must allow invitations, and the user should not
            * currently be a member or be banned from the group.
            */
            // Users with the 'bp_moderate' cap can always send invitations.
            if ( bp_user_can( $user_id, 'bp_moderate' ) ) {
                $retval = true;
            } else {
                $invite_status = bp_group_get_invite_status( $group_id );
 
                switch ( $invite_status ) {
                    case 'admins' :
                        if ( groups_is_user_admin( $user_id, $group_id ) ) {
                            $retval = true;
                        }
                        break;
 
                    case 'mods' :
                        if ( groups_is_user_mod( $user_id, $group_id ) || groups_is_user_admin( $user_id, $group_id ) ) {
                            $retval = true;
                        }
                        break;
 
                    case 'members' :
                        if ( groups_is_user_member( $user_id, $group_id ) ) {
                            $retval = true;
                        }
                        break;
                }
            }
            break;
 
        case 'groups_receive_invitation':
            // Return early if the user isn't logged in or the group ID is unknown.
            if ( ! $user_id || ! $group_id ) {
                break;
            }
 
            // Set to false to begin with.
            $retval = false;
 
            /*
            * The group must allow invitations, and the user should not
            * currently be a member or be banned from the group.
            */
            $group = groups_get_group( $group_id );
            if ( in_array( bp_get_group_status( $group ), array( 'private', 'hidden' ), true )
                && ! groups_is_user_member( $user_id, $group->id )
                && ! groups_is_user_banned( $user_id, $group->id )
            ) {
                $retval = true;
            }
            break;
 
        case 'groups_access_group':
            // Return early if the group ID is unknown.
            if ( ! $group_id ) {
                break;
            }
 
            $group = groups_get_group( $group_id );
 
            // If the check is for the logged-in user, use the BP_Groups_Group property.
            if ( $user_id === bp_loggedin_user_id() ) {
                $retval = $group->user_has_access;
 
            /*
             * If the check is for a specified user who is not the logged-in user
             * run the check manually.
             */
            } elseif ( 'public' === bp_get_group_status( $group ) || groups_is_user_member( $user_id, $group->id ) ) {
                $retval = true;
            }
            break;
 
        case 'groups_see_group':
            // Return early if the group ID is unknown.
            if ( ! $group_id ) {
                break;
            }
 
            $group = groups_get_group( $group_id );
 
            // If the check is for the logged-in user, use the BP_Groups_Group property.
            if ( $user_id === bp_loggedin_user_id() ) {
                $retval = $group->is_visible;
 
            /*
             * If the check is for a specified user who is not the logged-in user
             * run the check manually.
             */
            } elseif ( 'hidden' !== bp_get_group_status( $group ) || groups_is_user_member( $user_id, $group->id ) ) {
                $retval = true;
            }
            break;
    }
 
    return $retval;
 
}

Changelog

Changelog
Version Description
BuddyPress 3.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.