Created
January 9, 2019 23:15
-
-
Save sbrajesh/cc37e8794527f86046e1d880490d974b to your computer and use it in GitHub Desktop.
BuddyPress Private Message Rate Limiter, Role based customization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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' => 10, | |
), | |
); | |
$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 ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment