Replies
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.
Thank 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 );
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;
}