bp_activity_user_can_read( BP_Activity_Activity $activity, integer $user_id )

Can a user see a particular activity item?

Description

Parameters

$activity

(Required) Activity object.

$user_id

(Required) User ID.

Return

(boolean) True on success, false on failure.

Source

File: bp-activity/bp-activity-functions.php

3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
function bp_activity_user_can_read( $activity, $user_id = 0 ) {
    $retval = true;
 
    // Fallback.
    if ( empty( $user_id ) ) {
        $user_id = bp_loggedin_user_id();
    }
 
    // If activity is from a group, do extra cap checks.
    if ( bp_is_active( 'groups' ) && buddypress()->groups->id === $activity->component ) {
        // Check to see if the user has access to the activity's parent group.
        $group = groups_get_group( $activity->item_id );
        if ( $group ) {
            // For logged-in user, we can check against the 'user_has_access' prop.
            if ( bp_loggedin_user_id() === $user_id ) {
                $retval = $group->user_has_access;
 
            // Manually check status.
            } elseif ( 'private' === $group->status || 'hidden' === $group->status ) {
                // Only group members that are not banned can view.
                if ( ! groups_is_user_member( $user_id, $activity->item_id ) || groups_is_user_banned( $user_id, $activity->item_id ) ) {
                    $retval = false;
                }
            }
        }
    }
 
    // Spammed items are not visible to the public.
    if ( $activity->is_spam ) {
        $retval = false;
    }
 
    // Site moderators can view anything.
    if ( bp_current_user_can( 'bp_moderate' ) ) {
        $retval = true;
    }
 
    /**
     * Filters whether the current user has access to an activity item.
     *
     * @since BuddyPress 3.0.0
     *
     * @param bool                 $retval   Return value.
     * @param int                  $user_id  Current user ID.
     * @param BP_Activity_Activity $activity Activity object.
     */
    return apply_filters( 'bp_activity_user_can_read', $retval, $user_id, $activity );
}

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.