Tagged: buddypress, Private Message Rate Limiter, user roles
Regarding this tutorial:
https://buddydev.com/docs/buddypress-private-message-rate-limiter/modifying-the-limit-of-buddypress-private-message-rate-limiter/For the code
// Set message rate
add_filter( ‘bp_rate_limit_pm_throttle_duration’, ‘devb_custom_private_message_throttle_duration’, 10, 2 );
function devb_custom_private_message_throttle_duration( $duration, $user_id ) {
if( ! $user_id )
$user_id = get_current_user_id ();if( user_can( $user_id, ‘idm_limit_messages’ ) ) //admin
$duration = 2;// how many mins
elseif( user_can( $user_id, ‘paying_me’ ) )
$duration = 4; //for editorsreturn $duration;
}
It looks like I set the minute duration beside $duration, but where do I set the number of messages for that duration for each user capability?
Also, does this script override the settings in Settings->Buddypress->Options, or do those need to be set to something specific?
Thanks in advance!
Hi Greg,
Thank you for using the plugin.1. Please see the first snippet on that page. “devb_custom_private_message_limit_count”. It allows you to update message count. For this snippet, the values are no. of messages.
2. Yes, It overrides the settings and defaults to settings if you do not specify it for the current user.
3. It is priority and number of arguments the attached function receives.
Here 10 is priority on which our function gets called and 2 is no. of arguments it will be passed. You should not change it.Hope that helps.
Regards
Brajesh- This reply was modified 5 years, 8 months ago by
Brajesh Singh. Reason: updated to clarify 3
- This reply was modified 5 years, 8 months ago by
Sorry, I’m still confused. Am I correct that in the example below user with role idm_limit_message can send 2 messages every 60 minutes and user with role paying_me can send 2000 every 4 minutes? This is the code I’m running on my site and it is not limiting any messages.
// Set message rate
add_filter( ‘bp_rate_limit_pm_throttle_duration’, ‘devb_custom_private_message_throttle_duration’, 2, 2000 );
function devb_custom_private_message_throttle_duration( $duration, $user_id ) {
if( ! $user_id )
$user_id = get_current_user_id ();if( user_can( $user_id, ‘idm_limit_messages’ ) ) //admin
$duration = 60;// how many mins
elseif( user_can( $user_id, ‘paying_me’ ) )
$duration = 4; //for editorsreturn $duration;
}
Hi Greg,
For now, Please remove both the snippet. It seems confusing.I am putting a role based snippet where you can map role to duration/count directly.
In the above code, we are not using role, we are using capabilities to decide what to allow for user. That’s why you are seeing user_can.
Regards
BrajeshHi Greg,
Please remove the above codes. Once you have removed them, Please add the code from this pagehttps://gist.github.com/sbrajesh/cc37e8794527f86046e1d880490d974b
You can update the first function to add addition roles and their limits.
Regards
BrajeshThank you so much, but unfortunately it still isn’t working for me. I am using another plugin called BP Better Messages (Premium), might this be causing a problem? Please see the code I’m using below.
/**
* Get the applicable limit for the given role.
*
* @param string $role role name.
*
* @return array
*/
function buddydev_custom_get_role_based_message_limit( $role ) {// duration- in minutes
// limit – how many in that duration
$limits = array(
‘administrator’ => array(
‘duration’ => 1440,
‘limit’ => 0, // 0 means no limit.
),
‘editor’ => array(
‘duration’ => 60,
‘limit’ => 10,
),
‘author’ => array(
‘duration’ => 60,
‘limit’ => 5,
),
‘contributor’ => array(
‘duration’ => 1440,
‘limit’ => 20,
),
‘subscriber’ => array(
‘duration’ => 1440,
‘limit’ => 1,
),
‘paying_me’ => array(
‘duration’ => 60,
‘limit’ => 60,
),
);$default = array(
‘duration’ => bp_get_option( ‘bp_rate_limit_pm_throttle_duration’, 5 ),
‘limit’ => get_option( ‘bp_rate_limit_pm_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_message_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_message_limit( $role );
if ( $limit[‘limit’] > $allowed[‘limit’] ) {
$allowed = $limit;
}
}return $allowed;
}/**
* Filter Message count.
*
* @param int $limit limit applied for the specified duration.
* @param int $user_id user id.
*
* @return mixed
*/
function buddydev_custom_filter_pmrl_message_count( $limit, $user_id ) {$config = buddydev_custom_get_user_message_limits( $user_id );
return isset( $config[‘limit’] ) ? $config[‘limit’] : $limit;
}add_filter( ‘bp_rate_limit_pm_count’, ‘buddydev_custom_filter_pmrl_message_count’, 10, 2 );
/**
* Filter duration
*
* @param int $duration duration in minute
* @param int $user_id user id.
*
* @return int
*/
function buddydev_custom_filter_pmrl_message_throttle_duration( $duration, $user_id ) {$config = buddydev_custom_get_user_message_limits( $user_id );
return isset( $config[‘duration’] ) ? $config[‘duration’] : $duration;
}add_filter( ‘bp_rate_limit_pm_throttle_duration’, ‘buddydev_custom_filter_pmrl_message_throttle_duration’, 10, 2 );
To be clear, my understanding is that the code above should limit the messages subscriber can send to 1 every 1440 minutes. I just tried it and there doesn’t seem to be a limit on messages for that user role.
Hi Greg,
You are correct about the limits.The BuddyPress Private Message Rate limiter is not compatible with BP Better Message plugin. BP Better Message does not uses the BuddyPress Message API and simply overwrites the message component with their own implementation.
Any plugin written for BuddyPress Private Message won’t work with it. I am sorry but I am unable to support BP Better Message.
Regards
Brajesh
You must be logged in to reply to this topic.