Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
01/10/2018 06:59:00 PM (4 years ago)
Author:
djpaul
Message:

Activity: add function to check if a user has access to a single activity.

This change extracts the existing logic from bp_activity_screen_single_activity_permalink() into a new function, allowing it to be used in multiple places, such as the REST API, or a WP-CLI extension.

Fixes #7048

Props espellcaste

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/bp-activity-functions.php

    r11793 r11806  
    30823082
    30833083/**
     3084 * Can a user see a particular activity item?
     3085 *
     3086 * @since 3.0.0
     3087 *
     3088 * @param  BP_Activity_Activity $activity Activity object.
     3089 * @param  integer              $user_id  User ID.
     3090 * @return boolean True on success, false on failure.
     3091 */
     3092function bp_activity_user_can_read( $activity, $user_id = 0 ) {
     3093    $retval = false;
     3094
     3095    // Fallback.
     3096    if ( empty( $user_id ) ) {
     3097        $user_id = bp_loggedin_user_id();
     3098    }
     3099
     3100    // Admins and moderators can see everything.
     3101    if ( bp_current_user_can( 'bp_moderate' ) ) {
     3102        $retval = true;
     3103    }
     3104
     3105    // If activity author match user, allow access as well.
     3106    if ( $user_id === $activity->user_id ) {
     3107        $retval = true;
     3108    }
     3109
     3110    // If activity is from a group, do an extra cap check.
     3111    if ( ! $retval && bp_is_active( 'groups' ) && $activity->component === buddypress()->groups->id ) {
     3112
     3113        // Check to see if the user has access to the activity's parent group.
     3114        $group = groups_get_group( $activity->item_id );
     3115        if ( $group ) {
     3116            $retval = $group->user_has_access;
     3117        }
     3118    }
     3119
     3120    /**
     3121     * Filters whether the current user has access to an activity item.
     3122     *
     3123     * @since 3.0.0
     3124     *
     3125     * @param bool                 $retval   Return value.
     3126     * @param int                  $user_id  Current user ID.
     3127     * @param BP_Activity_Activity $activity Activity object.
     3128     */
     3129    return apply_filters( 'bp_activity_user_can_read', $retval, $user_id, $activity );
     3130}
     3131
     3132/**
    30843133 * Hide a user's activity.
    30853134 *
Note: See TracChangeset for help on using the changeset viewer.