| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentCommunity\App\Models\Comment; |
| 6 |
use FluentCommunity\App\Models\Feed; |
| 7 |
use FluentCommunity\App\Models\Media; |
| 8 |
use FluentCommunity\App\Models\User; |
| 9 |
use FluentCommunity\App\Services\Helper; |
| 10 |
|
| 11 |
class RateLimitHandler |
| 12 |
{ |
| 13 |
public function register() |
| 14 |
{ |
| 15 |
add_action('fluent_community/check_rate_limit/create_post', [$this, 'maybeLimitPost'], 10, 1); |
| 16 |
add_action('fluent_community/check_rate_limit/create_comment', [$this, 'maybeLimitComment'], 10, 1); |
| 17 |
add_action('fluent_community/check_rate_limit/media_upload', [$this, 'maybeLimitMediaUpload'], 10, 1); |
| 18 |
} |
| 19 |
|
| 20 |
public function maybeLimitPost(User $user) |
| 21 |
{ |
| 22 |
if (Helper::isSiteAdmin($user->ID, $user)) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
// Check how many posts user has created in last 5 minutes |
| 27 |
$postsCount = Feed::query()->withoutGlobalScopes()->where('user_id', $user->ID) |
| 28 |
->where('created_at', '>', gmdate('Y-m-d H:i:s', current_time('timestamp') - 300)) |
| 29 |
->count(); |
| 30 |
|
| 31 |
$limitPer5Minutes = apply_filters('fluent_community/rate_limit/posts_per_5_minutes', 5); |
| 32 |
|
| 33 |
if ($postsCount > $limitPer5Minutes) { |
| 34 |
throw new \Exception(esc_html__('You have reached the limit of posting. Please try after some time', 'fluent-community')); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
public function maybeLimitComment(User $user) |
| 39 |
{ |
| 40 |
if (Helper::isSiteAdmin($user->ID, $user)) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
// Check how many comments user has created in last 1 minute |
| 45 |
$commentsCount = Comment::query()->withoutGlobalScopes()->where('user_id', $user->ID) |
| 46 |
->where('created_at', '>', gmdate('Y-m-d H:i:s', current_time('timestamp') - 60)) |
| 47 |
->count(); |
| 48 |
|
| 49 |
$limitPerMinute = apply_filters('fluent_community/rate_limit/comments_per_minute', 5); |
| 50 |
|
| 51 |
if ($commentsCount > $limitPerMinute) { |
| 52 |
throw new \Exception(esc_html__('You have reached the limit of commenting. Please try after some time', 'fluent-community')); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
public function maybeLimitMediaUpload(User $user) |
| 57 |
{ |
| 58 |
if (Helper::isSiteAdmin($user->ID, $user)) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
// Check how many media items user has uploaded in last 1 minute |
| 63 |
$mediaCount = Media::query()->withoutGlobalScopes()->where('user_id', $user->ID) |
| 64 |
->where('created_at', '>', gmdate('Y-m-d H:i:s', current_time('timestamp') - 60)) |
| 65 |
->count(); |
| 66 |
|
| 67 |
$limitPerMinute = apply_filters('fluent_community/rate_limit/media_upload_per_minute', 10); |
| 68 |
|
| 69 |
if ($mediaCount > $limitPerMinute) { |
| 70 |
throw new \Exception(esc_html__('You have reached the limit of media uploads. Please try after some time', 'fluent-community')); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|