| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Util; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Database\FormEntryModel; |
| 6 |
|
| 7 |
/** |
| 8 |
* Entry Limit Helper Class |
| 9 |
* This class handles all advanced entry limit functionality |
| 10 |
*/ |
| 11 |
class EntryLimitHelper |
| 12 |
{ |
| 13 |
private $formId; |
| 14 |
private $settings; |
| 15 |
private $enabled; |
| 16 |
|
| 17 |
public function __construct($formId, $settings, $enabled) |
| 18 |
{ |
| 19 |
$this->formId = $formId; |
| 20 |
$this->settings = $settings; |
| 21 |
$this->enabled = $enabled; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Check all entry limits and return restriction messages |
| 26 |
*/ |
| 27 |
public function checkAllLimits($ipAddress, $currentUserId) |
| 28 |
{ |
| 29 |
$restrictionMessages = []; |
| 30 |
|
| 31 |
// Check submission count limits |
| 32 |
if ($this->isEnabled('entry_limit')) { |
| 33 |
$message = $this->checkSubmissionCountLimit(); |
| 34 |
if ($message) { |
| 35 |
$restrictionMessages[] = $message; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
// Check per user limits |
| 40 |
if ($this->isEnabled('entry_limit_by_user')) { |
| 41 |
$message = $this->checkPerUserLimit($ipAddress, $currentUserId); |
| 42 |
if ($message) { |
| 43 |
$restrictionMessages[] = $message; |
| 44 |
} |
| 45 |
} |
| 46 |
return $restrictionMessages; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Check submission count limit |
| 51 |
*/ |
| 52 |
private function checkSubmissionCountLimit() |
| 53 |
{ |
| 54 |
$limit = intval($this->getSetting('entry_limit')); |
| 55 |
$type = $this->getSetting('entry_limit_count_type', 'total'); |
| 56 |
$message = $this->getSettingMessages('entry_limit_message', __('Sorry! Submission limit exceeded.', 'bit-form')); |
| 57 |
|
| 58 |
if (!$limit) { |
| 59 |
return null; |
| 60 |
} |
| 61 |
|
| 62 |
$count = $this->getSubmissionCountByType($type); |
| 63 |
|
| 64 |
if ($count >= $limit) { |
| 65 |
return apply_filters( |
| 66 |
'bitform_filter_restriction_entry_limit_message', |
| 67 |
$message, |
| 68 |
$this->formId |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
return null; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Check per user limit |
| 77 |
*/ |
| 78 |
private function checkPerUserLimit($ipAddress, $currentUserId) |
| 79 |
{ |
| 80 |
$limit = intval($this->getSetting('entry_limit_by_user')); |
| 81 |
$userType = $this->getSetting('entry_limit_by_user_type', 'ip'); |
| 82 |
$timeframe = $this->getSetting('entry_limit_by_user_count_type', 'total'); |
| 83 |
$message = $this->getSettingMessages('entry_limit_per_user_message', __('Sorry! You have exceeded the submission limit.', 'bit-form')); |
| 84 |
|
| 85 |
if (!$limit) { |
| 86 |
return null; |
| 87 |
} |
| 88 |
|
| 89 |
$count = 0; |
| 90 |
if ('per_user_ip' === $userType) { |
| 91 |
$count = $this->getSubmissionCountByUserAndType('per_user_ip', $ipAddress, $timeframe); |
| 92 |
} elseif ('per_user_id' === $userType && $currentUserId > 0) { |
| 93 |
$count = $this->getSubmissionCountByUserAndType('per_user_id', $currentUserId, $timeframe); |
| 94 |
} elseif ('per_user_id' === $userType && 0 === $currentUserId) { |
| 95 |
// Skip check for non-logged users when logged_user is selected |
| 96 |
return null; |
| 97 |
} |
| 98 |
|
| 99 |
if ($count >= $limit) { |
| 100 |
return apply_filters( |
| 101 |
'bitform_filter_restriction_entry_limit_message', |
| 102 |
$message, |
| 103 |
$this->formId |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
return null; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get submission count by type |
| 112 |
*/ |
| 113 |
private function getSubmissionCountByType($type) |
| 114 |
{ |
| 115 |
$formEntry = new FormEntryModel(); |
| 116 |
|
| 117 |
// Build base condition |
| 118 |
$condition = ['form_id' => $this->formId]; |
| 119 |
|
| 120 |
// Add time-based conditions using raw SQL for date functions |
| 121 |
$this->addTimeCondition($condition, $type); |
| 122 |
|
| 123 |
$countResult = $formEntry->count($condition); |
| 124 |
|
| 125 |
// Handle the result according to your existing pattern |
| 126 |
$count = !empty($countResult[0]) && !empty($countResult[0]->count) ? $countResult[0]->count : 0; |
| 127 |
|
| 128 |
return intval($count); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Get submission count by user and timeframe |
| 133 |
*/ |
| 134 |
private function getSubmissionCountByUserAndType($userType, $userValue, $timeframe) |
| 135 |
{ |
| 136 |
$formEntry = new FormEntryModel(); |
| 137 |
|
| 138 |
// Build base condition |
| 139 |
$condition = ['form_id' => $this->formId]; |
| 140 |
|
| 141 |
// Add user condition |
| 142 |
if ('per_user_ip' === $userType) { |
| 143 |
$condition['user_ip'] = ip2long($userValue); |
| 144 |
} elseif ('per_user_id' === $userType) { |
| 145 |
$condition['user_id'] = $userValue; |
| 146 |
} else { |
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
// Add time condition using helper |
| 151 |
$this->addTimeCondition($condition, $timeframe); |
| 152 |
|
| 153 |
$countResult = $formEntry->count($condition); |
| 154 |
|
| 155 |
// Handle the result according to your existing pattern |
| 156 |
$count = !empty($countResult[0]) && !empty($countResult[0]->count) ? $countResult[0]->count : 0; |
| 157 |
|
| 158 |
return intval($count); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Get burst submission count |
| 163 |
*/ |
| 164 |
private function getBurstSubmissionCount($ipAddress, $timeframe) |
| 165 |
{ |
| 166 |
$formEntry = new FormEntryModel(); |
| 167 |
|
| 168 |
$condition = [ |
| 169 |
'form_id' => $this->formId, |
| 170 |
'user_ip' => ip2long($ipAddress) |
| 171 |
]; |
| 172 |
|
| 173 |
// Burst timeframes use different intervals |
| 174 |
$burstIntervals = [ |
| 175 |
'minute' => '1 MINUTE', |
| 176 |
'5minutes' => '5 MINUTE', |
| 177 |
'15minutes' => '15 MINUTE' |
| 178 |
]; |
| 179 |
|
| 180 |
$interval = isset($burstIntervals[$timeframe]) ? $burstIntervals[$timeframe] : '1 MINUTE'; |
| 181 |
|
| 182 |
$condition['created_at'] = [ |
| 183 |
'operator' => '>=', |
| 184 |
'raw' => "DATE_SUB(NOW(), INTERVAL {$interval})" |
| 185 |
]; |
| 186 |
|
| 187 |
$countResult = $formEntry->count($condition); |
| 188 |
$count = !empty($countResult[0]) && !empty($countResult[0]->count) ? $countResult[0]->count : 0; |
| 189 |
|
| 190 |
return intval($count); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Check if a restriction is enabled |
| 195 |
*/ |
| 196 |
private function isEnabled($key) |
| 197 |
{ |
| 198 |
return isset($this->enabled->{$key}) && $this->enabled->{$key}; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Get setting value |
| 203 |
*/ |
| 204 |
private function getSetting($key, $default = null) |
| 205 |
{ |
| 206 |
return isset($this->settings->{$key}) |
| 207 |
? $this->settings->{$key} |
| 208 |
: $default; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Get setting value |
| 213 |
*/ |
| 214 |
private function getSettingMessages($key, $default = null) |
| 215 |
{ |
| 216 |
return isset($this->settings->messages->{$key}) |
| 217 |
? $this->settings->messages->{$key} |
| 218 |
: $default; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Add this helper method to your EntryLimitHelper class |
| 223 |
*/ |
| 224 |
private function addTimeCondition(&$condition, $timeframe) |
| 225 |
{ |
| 226 |
if ('total' === $timeframe) { |
| 227 |
return; // No time condition needed |
| 228 |
} |
| 229 |
|
| 230 |
$intervals = [ |
| 231 |
'per_minute' => '1 MINUTE', |
| 232 |
'per_hour' => '1 HOUR', |
| 233 |
'per_day' => '1 DAY', |
| 234 |
'per_week' => '1 WEEK', |
| 235 |
'per_month' => '1 MONTH', |
| 236 |
'per_year' => '1 YEAR' |
| 237 |
]; |
| 238 |
|
| 239 |
if (isset($intervals[$timeframe])) { |
| 240 |
$condition['created_at'] = [ |
| 241 |
'operator' => '>=', |
| 242 |
'raw' => "DATE_SUB(NOW(), INTERVAL {$intervals[$timeframe]})" |
| 243 |
]; |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
|