BuddyDev

Search

[Resolved] MediaPress user hogging site feed (moderation control?)


  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 9861
    Brajesh Singh on #20002

    Hi Richard,
    I just saw that you already purchased the plugin. Please let me know if you will like to continue with activity rate limiter or I can issue the refud for it.

    Regards
    Brajesh


  • Participant
    Level: Master
    Posts: 231
    Richard Foley on #20003

    hi Brajesh,

    I think I should wait for your extended restrictions plugin, before deciding (if that’s ok with you?), as it’s possible this will suffice. Then again, I might be tempted by the “fine-grained control” you mentioned to switch to the more elegant solution (the new plugin).

    Until you release at the end of the month (or whenever), I’ll dig around to see if I can find a filter to which I can map a role with the rate limiter. I may ask questions…

    R.


  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 9861
    Brajesh Singh on #20011

    Hi Richard,
    Thank you.
    Sure, Please feel free to ask any question.

    Regards
    Brajesh


  • Participant
    Level: Master
    Posts: 231
    Richard Foley on #20053

    hi Brajesh,

    here’s my question… I currently have this boilerplate, but am unsure how it will mesh with your class. Possibly I need to initialize, or call a global instance, here.

    
    // ignore the $user arg, just ask about the current user.
    function my_restrict_post( $user ) {
    
            // use my capability to block based on the user role
            if ( current_user_can( 'not_post_freely' ) ) {
                    return false; // block the post
            }
    
            return true; // let the user post
    
    }
    add_filter( 'can_post_update', array( 'BP_Rate_Limit_User_Activity_Helper', 'my_restrict_post' ) );
    

    How does this look – advice always welcome…

    R.


  • Participant
    Level: Master
    Posts: 231
    Richard Foley on #20054

    notes: this seemed to work, or at least the rate-limiter plugin certainly kicked in. However, it appeared to block all users, not just the ones I was after (with the restricted role). It’s possible I may be using the action/call incorrectly?

    R.


  • Participant
    Level: Master
    Posts: 231
    Richard Foley on #20057

    Looking at this a bit more, I’m thinking I might need to remove and replace your can_post_update() function entirely (via remove_action) and replace it (via add_action) with my own capability logic. Hmmm.


  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 9861
    Brajesh Singh on #20058

    Hi Richard,
    Please do not use above snippet. It won’t work.

    Will it be good enough If I can put an example using roles or member types(please let me know which one you will prefer)?

    Regards
    BRajesh


  • Participant
    Level: Master
    Posts: 231
    Richard Foley on #20062

    hi Brajesh,

    an example using a role, or capability, would be really useful.

    R.


  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 9861
    Brajesh Singh on #20063

    Sure,
    Here is an example using role.

    I have broken it into 2 functions for defining role based limit and then getting user specific limits.

    After that, there are two functions attached to filters. All you need to modify is the first function that supplies role based limit.

    
    
    /**
     * Get the applicable limit for the given role.
     *
     * @param string $role role name.
     *
     * @return array
     */
    function buddydev_custom_get_role_based_activity_limit( $role ) {
    
    	// duration- in minutes
    	// limit - how many in that duration
    	$limits = array(
    		'administrator' => array(
    			'duration' => 1440,
    			'limit'    => 0,
    		),
    		'editor'        => array(
    			'duration' => 60,
    			'limit'    => 10,
    		),
    		'author'        => array(
    			'duration' => 60,
    			'limit'    => 5,
    		),
    		'contributor'   => array(
    			'duration' => 1440,
    			'limit'    => 20,
    		),
    		'subscriber'    => array(
    			'duration' => 1440,
    			'limit'    => 10,
    		),
    	);
    
    	$default = array(
    		'duration' => get_option( 'bp_rate_limit_activity_throttle_duration', 5 ),
    		'limit'    => get_option( 'bp_rate_limit_activity_count', 5 ),
    	);
    
    	return isset( $limits[ $role ] ) ? $limits[ $role ] : $default;
    }
    
    /**
     * Get the limit/duration for the given user.
     *
     * @param int $user_id user id.
     *
     * @return array
     */
    function buddydev_custom_get_user_activity_limits( $user_id ) {
    	// since a user may have more than 1 role, which of the limit should we use? I will go with the highest allowed.
    	$allowed = array(
    		'duration' => 0,
    		'limit'    => - 1,
    	);
    
    	$user    = get_user_by( 'id', $user_id );
    
    	if ( ! $user ) {
    		return $allowed;
    	}
    
    	foreach ( $user->roles as $role ) {
    		$limit = buddydev_custom_get_role_based_activity_limit( $role );
    
    		if ( $limit['limit'] > $allowed['limit'] ) {
    			$allowed = $limit;
    		}
    	}
    
    	return $allowed;
    }
    
    /**
     * Filter activity count.
     *
     * @param int $limit limit applied for the specified duration.
     * @param int $user_id user id.
     *
     * @return mixed
     */
    function buddydev_custom_filter_ra_activity_count( $limit, $user_id ) {
    
    	$config = buddydev_custom_get_user_activity_limits( $user_id );
    
    	return isset( $config['limit'] ) ? $config['limit'] : $limit;
    }
    
    add_filter( 'bp_rate_limit_activity_count', 'buddydev_custom_filter_ra_activity_count', 10, 2 );
    
    /**
     * Filter duration
     *
     * @param int $duration duration in minute
     * @param int $user_id user id.
     *
     * @return int
     */
    function buddydev_custom_filter_ra_activity_throttle_duration( $duration, $user_id ) {
    
    	$config = buddydev_custom_get_user_activity_limits( $user_id );
    
    	return isset( $config['duration'] ) ? $config['duration'] : $duration;
    }
    
    add_filter( 'bp_rate_limit_activity_throttle_duration', 'buddydev_custom_filter_ra_activity_throttle_duration', 10, 2 );
    
    

    Hope that helps.

    Regards
    Brajesh


  • Participant
    Level: Master
    Posts: 231
    Richard Foley on #20064

    hi Brajesh,

    that looks very helpful, I think my solution was a tad “naive”, yours looks much more complete (and functional…). I’ll look at it tomorrow and be sure to let you know how I get on.

    R.

The topic ‘ [Resolved] MediaPress user hogging site feed (moderation control?)’ is closed to new replies.

This topic is: resolved
Subscribe