@@ -13,285 +13,285 @@ |
||
| 13 | 13 | |
| 14 | 14 | class ValidateReview |
| 15 | 15 | { |
| 16 | - const RECAPTCHA_ENDPOINT = 'https://www.google.com/recaptcha/api/siteverify'; |
|
| 16 | + const RECAPTCHA_ENDPOINT = 'https://www.google.com/recaptcha/api/siteverify'; |
|
| 17 | 17 | |
| 18 | - const RECAPTCHA_DISABLED = 0; |
|
| 19 | - const RECAPTCHA_EMPTY = 1; |
|
| 20 | - const RECAPTCHA_FAILED = 2; |
|
| 21 | - const RECAPTCHA_INVALID = 3; |
|
| 22 | - const RECAPTCHA_VALID = 4; |
|
| 18 | + const RECAPTCHA_DISABLED = 0; |
|
| 19 | + const RECAPTCHA_EMPTY = 1; |
|
| 20 | + const RECAPTCHA_FAILED = 2; |
|
| 21 | + const RECAPTCHA_INVALID = 3; |
|
| 22 | + const RECAPTCHA_VALID = 4; |
|
| 23 | 23 | |
| 24 | - const VALIDATION_RULES = [ |
|
| 25 | - 'content' => 'required', |
|
| 26 | - 'email' => 'required|email', |
|
| 27 | - 'name' => 'required', |
|
| 28 | - 'rating' => 'required|number|between:1,5', |
|
| 29 | - 'terms' => 'accepted', |
|
| 30 | - 'title' => 'required', |
|
| 31 | - ]; |
|
| 24 | + const VALIDATION_RULES = [ |
|
| 25 | + 'content' => 'required', |
|
| 26 | + 'email' => 'required|email', |
|
| 27 | + 'name' => 'required', |
|
| 28 | + 'rating' => 'required|number|between:1,5', |
|
| 29 | + 'terms' => 'accepted', |
|
| 30 | + 'title' => 'required', |
|
| 31 | + ]; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @var string|void |
|
| 35 | - */ |
|
| 36 | - public $error; |
|
| 33 | + /** |
|
| 34 | + * @var string|void |
|
| 35 | + */ |
|
| 36 | + public $error; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @var string |
|
| 40 | - */ |
|
| 41 | - public $form_id; |
|
| 38 | + /** |
|
| 39 | + * @var string |
|
| 40 | + */ |
|
| 41 | + public $form_id; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @var bool |
|
| 45 | - */ |
|
| 46 | - public $recaptchaIsUnset = false; |
|
| 43 | + /** |
|
| 44 | + * @var bool |
|
| 45 | + */ |
|
| 46 | + public $recaptchaIsUnset = false; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * @var array |
|
| 50 | - */ |
|
| 51 | - public $request; |
|
| 48 | + /** |
|
| 49 | + * @var array |
|
| 50 | + */ |
|
| 51 | + public $request; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @var array |
|
| 55 | - */ |
|
| 56 | - protected $options; |
|
| 53 | + /** |
|
| 54 | + * @var array |
|
| 55 | + */ |
|
| 56 | + protected $options; |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * @return static |
|
| 60 | - */ |
|
| 61 | - public function validate(array $request) |
|
| 62 | - { |
|
| 63 | - $this->form_id = $request['form_id']; |
|
| 64 | - $this->options = glsr(OptionManager::class)->all(); |
|
| 65 | - $this->request = $this->validateRequest($request); |
|
| 66 | - $this->validateCustom(); |
|
| 67 | - $this->validateHoneyPot(); |
|
| 68 | - $this->validateReviewLimits(); |
|
| 69 | - $this->validateBlacklist(); |
|
| 70 | - $this->validateAkismet(); |
|
| 71 | - $this->validateRecaptcha(); |
|
| 72 | - if (!empty($this->error)) { |
|
| 73 | - $this->setSessionValues('message', $this->error); |
|
| 74 | - } |
|
| 75 | - return $this; |
|
| 76 | - } |
|
| 58 | + /** |
|
| 59 | + * @return static |
|
| 60 | + */ |
|
| 61 | + public function validate(array $request) |
|
| 62 | + { |
|
| 63 | + $this->form_id = $request['form_id']; |
|
| 64 | + $this->options = glsr(OptionManager::class)->all(); |
|
| 65 | + $this->request = $this->validateRequest($request); |
|
| 66 | + $this->validateCustom(); |
|
| 67 | + $this->validateHoneyPot(); |
|
| 68 | + $this->validateReviewLimits(); |
|
| 69 | + $this->validateBlacklist(); |
|
| 70 | + $this->validateAkismet(); |
|
| 71 | + $this->validateRecaptcha(); |
|
| 72 | + if (!empty($this->error)) { |
|
| 73 | + $this->setSessionValues('message', $this->error); |
|
| 74 | + } |
|
| 75 | + return $this; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * @param string $path |
|
| 80 | - * @param mixed $fallback |
|
| 81 | - * @return mixed |
|
| 82 | - */ |
|
| 83 | - protected function getOption($path, $fallback = '') |
|
| 84 | - { |
|
| 85 | - return Arr::get($this->options, $path, $fallback); |
|
| 86 | - } |
|
| 78 | + /** |
|
| 79 | + * @param string $path |
|
| 80 | + * @param mixed $fallback |
|
| 81 | + * @return mixed |
|
| 82 | + */ |
|
| 83 | + protected function getOption($path, $fallback = '') |
|
| 84 | + { |
|
| 85 | + return Arr::get($this->options, $path, $fallback); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * @return int |
|
| 90 | - */ |
|
| 91 | - protected function getRecaptchaStatus() |
|
| 92 | - { |
|
| 93 | - if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
| 94 | - return static::RECAPTCHA_DISABLED; |
|
| 95 | - } |
|
| 96 | - if (empty($this->request['_recaptcha-token'])) { |
|
| 97 | - return $this->request['_counter'] < intval(apply_filters('site-reviews/recaptcha/timeout', 5)) |
|
| 98 | - ? static::RECAPTCHA_EMPTY |
|
| 99 | - : static::RECAPTCHA_FAILED; |
|
| 100 | - } |
|
| 101 | - return $this->getRecaptchaTokenStatus(); |
|
| 102 | - } |
|
| 88 | + /** |
|
| 89 | + * @return int |
|
| 90 | + */ |
|
| 91 | + protected function getRecaptchaStatus() |
|
| 92 | + { |
|
| 93 | + if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
| 94 | + return static::RECAPTCHA_DISABLED; |
|
| 95 | + } |
|
| 96 | + if (empty($this->request['_recaptcha-token'])) { |
|
| 97 | + return $this->request['_counter'] < intval(apply_filters('site-reviews/recaptcha/timeout', 5)) |
|
| 98 | + ? static::RECAPTCHA_EMPTY |
|
| 99 | + : static::RECAPTCHA_FAILED; |
|
| 100 | + } |
|
| 101 | + return $this->getRecaptchaTokenStatus(); |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * @return int |
|
| 106 | - */ |
|
| 107 | - protected function getRecaptchaTokenStatus() |
|
| 108 | - { |
|
| 109 | - $endpoint = add_query_arg([ |
|
| 110 | - 'remoteip' => Helper::getIpAddress(), |
|
| 111 | - 'response' => $this->request['_recaptcha-token'], |
|
| 112 | - 'secret' => $this->getOption('settings.submissions.recaptcha.secret'), |
|
| 113 | - ], static::RECAPTCHA_ENDPOINT); |
|
| 114 | - if (is_wp_error($response = wp_remote_get($endpoint))) { |
|
| 115 | - glsr_log()->error($response->get_error_message()); |
|
| 116 | - return static::RECAPTCHA_FAILED; |
|
| 117 | - } |
|
| 118 | - $response = json_decode(wp_remote_retrieve_body($response)); |
|
| 119 | - if (!empty($response->success)) { |
|
| 120 | - return boolval($response->success) |
|
| 121 | - ? static::RECAPTCHA_VALID |
|
| 122 | - : static::RECAPTCHA_INVALID; |
|
| 123 | - } |
|
| 124 | - foreach ($response->{'error-codes'} as $error) { |
|
| 125 | - glsr_log()->error('reCAPTCHA error: '.$error); |
|
| 126 | - } |
|
| 127 | - return static::RECAPTCHA_INVALID; |
|
| 128 | - } |
|
| 104 | + /** |
|
| 105 | + * @return int |
|
| 106 | + */ |
|
| 107 | + protected function getRecaptchaTokenStatus() |
|
| 108 | + { |
|
| 109 | + $endpoint = add_query_arg([ |
|
| 110 | + 'remoteip' => Helper::getIpAddress(), |
|
| 111 | + 'response' => $this->request['_recaptcha-token'], |
|
| 112 | + 'secret' => $this->getOption('settings.submissions.recaptcha.secret'), |
|
| 113 | + ], static::RECAPTCHA_ENDPOINT); |
|
| 114 | + if (is_wp_error($response = wp_remote_get($endpoint))) { |
|
| 115 | + glsr_log()->error($response->get_error_message()); |
|
| 116 | + return static::RECAPTCHA_FAILED; |
|
| 117 | + } |
|
| 118 | + $response = json_decode(wp_remote_retrieve_body($response)); |
|
| 119 | + if (!empty($response->success)) { |
|
| 120 | + return boolval($response->success) |
|
| 121 | + ? static::RECAPTCHA_VALID |
|
| 122 | + : static::RECAPTCHA_INVALID; |
|
| 123 | + } |
|
| 124 | + foreach ($response->{'error-codes'} as $error) { |
|
| 125 | + glsr_log()->error('reCAPTCHA error: '.$error); |
|
| 126 | + } |
|
| 127 | + return static::RECAPTCHA_INVALID; |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * @return array |
|
| 132 | - */ |
|
| 133 | - protected function getValidationRules(array $request) |
|
| 134 | - { |
|
| 135 | - $rules = array_intersect_key( |
|
| 136 | - apply_filters('site-reviews/validation/rules', static::VALIDATION_RULES, $request), |
|
| 137 | - array_flip($this->getOption('settings.submissions.required', [])) |
|
| 138 | - ); |
|
| 139 | - $excluded = explode(',', Arr::get($request, 'excluded')); |
|
| 140 | - return array_diff_key($rules, array_flip($excluded)); |
|
| 141 | - } |
|
| 130 | + /** |
|
| 131 | + * @return array |
|
| 132 | + */ |
|
| 133 | + protected function getValidationRules(array $request) |
|
| 134 | + { |
|
| 135 | + $rules = array_intersect_key( |
|
| 136 | + apply_filters('site-reviews/validation/rules', static::VALIDATION_RULES, $request), |
|
| 137 | + array_flip($this->getOption('settings.submissions.required', [])) |
|
| 138 | + ); |
|
| 139 | + $excluded = explode(',', Arr::get($request, 'excluded')); |
|
| 140 | + return array_diff_key($rules, array_flip($excluded)); |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - /** |
|
| 144 | - * @return bool |
|
| 145 | - */ |
|
| 146 | - protected function isRequestValid(array $request) |
|
| 147 | - { |
|
| 148 | - $rules = $this->getValidationRules($request); |
|
| 149 | - $errors = glsr(Validator::class)->validate($request, $rules); |
|
| 150 | - if (empty($errors)) { |
|
| 151 | - return true; |
|
| 152 | - } |
|
| 153 | - $this->error = __('Please fix the submission errors.', 'site-reviews'); |
|
| 154 | - $this->setSessionValues('errors', $errors); |
|
| 155 | - $this->setSessionValues('values', $request); |
|
| 156 | - return false; |
|
| 157 | - } |
|
| 143 | + /** |
|
| 144 | + * @return bool |
|
| 145 | + */ |
|
| 146 | + protected function isRequestValid(array $request) |
|
| 147 | + { |
|
| 148 | + $rules = $this->getValidationRules($request); |
|
| 149 | + $errors = glsr(Validator::class)->validate($request, $rules); |
|
| 150 | + if (empty($errors)) { |
|
| 151 | + return true; |
|
| 152 | + } |
|
| 153 | + $this->error = __('Please fix the submission errors.', 'site-reviews'); |
|
| 154 | + $this->setSessionValues('errors', $errors); |
|
| 155 | + $this->setSessionValues('values', $request); |
|
| 156 | + return false; |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - protected function setError($message, $loggedMessage = '') |
|
| 160 | - { |
|
| 161 | - $this->setSessionValues('errors', [], $loggedMessage); |
|
| 162 | - $this->error = $message; |
|
| 163 | - } |
|
| 159 | + protected function setError($message, $loggedMessage = '') |
|
| 160 | + { |
|
| 161 | + $this->setSessionValues('errors', [], $loggedMessage); |
|
| 162 | + $this->error = $message; |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | - /** |
|
| 166 | - * @param string $type |
|
| 167 | - * @param mixed $value |
|
| 168 | - * @param string $loggedMessage |
|
| 169 | - * @return void |
|
| 170 | - */ |
|
| 171 | - protected function setSessionValues($type, $value, $loggedMessage = '') |
|
| 172 | - { |
|
| 173 | - glsr()->sessionSet($this->form_id.$type, $value); |
|
| 174 | - if (!empty($loggedMessage)) { |
|
| 175 | - glsr_log()->warning($loggedMessage)->debug($this->request); |
|
| 176 | - } |
|
| 177 | - } |
|
| 165 | + /** |
|
| 166 | + * @param string $type |
|
| 167 | + * @param mixed $value |
|
| 168 | + * @param string $loggedMessage |
|
| 169 | + * @return void |
|
| 170 | + */ |
|
| 171 | + protected function setSessionValues($type, $value, $loggedMessage = '') |
|
| 172 | + { |
|
| 173 | + glsr()->sessionSet($this->form_id.$type, $value); |
|
| 174 | + if (!empty($loggedMessage)) { |
|
| 175 | + glsr_log()->warning($loggedMessage)->debug($this->request); |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - /** |
|
| 180 | - * @return void |
|
| 181 | - */ |
|
| 182 | - protected function validateAkismet() |
|
| 183 | - { |
|
| 184 | - if (!empty($this->error)) { |
|
| 185 | - return; |
|
| 186 | - } |
|
| 187 | - if (glsr(Akismet::class)->isSpam($this->request)) { |
|
| 188 | - $this->setError(__('This review has been flagged as possible spam and cannot be submitted.', 'site-reviews'), |
|
| 189 | - 'Akismet caught a spam submission (consider adding the IP address to the blacklist):' |
|
| 190 | - ); |
|
| 191 | - } |
|
| 192 | - } |
|
| 179 | + /** |
|
| 180 | + * @return void |
|
| 181 | + */ |
|
| 182 | + protected function validateAkismet() |
|
| 183 | + { |
|
| 184 | + if (!empty($this->error)) { |
|
| 185 | + return; |
|
| 186 | + } |
|
| 187 | + if (glsr(Akismet::class)->isSpam($this->request)) { |
|
| 188 | + $this->setError(__('This review has been flagged as possible spam and cannot be submitted.', 'site-reviews'), |
|
| 189 | + 'Akismet caught a spam submission (consider adding the IP address to the blacklist):' |
|
| 190 | + ); |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - /** |
|
| 195 | - * @return void |
|
| 196 | - */ |
|
| 197 | - protected function validateBlacklist() |
|
| 198 | - { |
|
| 199 | - if (!empty($this->error)) { |
|
| 200 | - return; |
|
| 201 | - } |
|
| 202 | - if (!glsr(Blacklist::class)->isBlacklisted($this->request)) { |
|
| 203 | - return; |
|
| 204 | - } |
|
| 205 | - $blacklistAction = $this->getOption('settings.submissions.blacklist.action'); |
|
| 206 | - if ('reject' != $blacklistAction) { |
|
| 207 | - $this->request['blacklisted'] = true; |
|
| 208 | - return; |
|
| 209 | - } |
|
| 210 | - $this->setError(__('Your review cannot be submitted at this time.', 'site-reviews'), |
|
| 211 | - 'Blacklisted submission detected:' |
|
| 212 | - ); |
|
| 213 | - } |
|
| 194 | + /** |
|
| 195 | + * @return void |
|
| 196 | + */ |
|
| 197 | + protected function validateBlacklist() |
|
| 198 | + { |
|
| 199 | + if (!empty($this->error)) { |
|
| 200 | + return; |
|
| 201 | + } |
|
| 202 | + if (!glsr(Blacklist::class)->isBlacklisted($this->request)) { |
|
| 203 | + return; |
|
| 204 | + } |
|
| 205 | + $blacklistAction = $this->getOption('settings.submissions.blacklist.action'); |
|
| 206 | + if ('reject' != $blacklistAction) { |
|
| 207 | + $this->request['blacklisted'] = true; |
|
| 208 | + return; |
|
| 209 | + } |
|
| 210 | + $this->setError(__('Your review cannot be submitted at this time.', 'site-reviews'), |
|
| 211 | + 'Blacklisted submission detected:' |
|
| 212 | + ); |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - /** |
|
| 216 | - * @return void |
|
| 217 | - */ |
|
| 218 | - protected function validateCustom() |
|
| 219 | - { |
|
| 220 | - if (!empty($this->error)) { |
|
| 221 | - return; |
|
| 222 | - } |
|
| 223 | - $validated = apply_filters('site-reviews/validate/custom', true, $this->request); |
|
| 224 | - if (true === $validated) { |
|
| 225 | - return; |
|
| 226 | - } |
|
| 227 | - $errorMessage = is_string($validated) |
|
| 228 | - ? $validated |
|
| 229 | - : __('The review submission failed. Please notify the site administrator.', 'site-reviews'); |
|
| 230 | - $this->setError($errorMessage); |
|
| 231 | - $this->setSessionValues('values', $this->request); |
|
| 232 | - } |
|
| 215 | + /** |
|
| 216 | + * @return void |
|
| 217 | + */ |
|
| 218 | + protected function validateCustom() |
|
| 219 | + { |
|
| 220 | + if (!empty($this->error)) { |
|
| 221 | + return; |
|
| 222 | + } |
|
| 223 | + $validated = apply_filters('site-reviews/validate/custom', true, $this->request); |
|
| 224 | + if (true === $validated) { |
|
| 225 | + return; |
|
| 226 | + } |
|
| 227 | + $errorMessage = is_string($validated) |
|
| 228 | + ? $validated |
|
| 229 | + : __('The review submission failed. Please notify the site administrator.', 'site-reviews'); |
|
| 230 | + $this->setError($errorMessage); |
|
| 231 | + $this->setSessionValues('values', $this->request); |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - /** |
|
| 235 | - * @return void |
|
| 236 | - */ |
|
| 237 | - protected function validateHoneyPot() |
|
| 238 | - { |
|
| 239 | - if (!empty($this->error)) { |
|
| 240 | - return; |
|
| 241 | - } |
|
| 242 | - if (!empty($this->request['gotcha'])) { |
|
| 243 | - $this->setError(__('The review submission failed. Please notify the site administrator.', 'site-reviews'), |
|
| 244 | - 'The Honeypot caught a bad submission:' |
|
| 245 | - ); |
|
| 246 | - } |
|
| 247 | - } |
|
| 234 | + /** |
|
| 235 | + * @return void |
|
| 236 | + */ |
|
| 237 | + protected function validateHoneyPot() |
|
| 238 | + { |
|
| 239 | + if (!empty($this->error)) { |
|
| 240 | + return; |
|
| 241 | + } |
|
| 242 | + if (!empty($this->request['gotcha'])) { |
|
| 243 | + $this->setError(__('The review submission failed. Please notify the site administrator.', 'site-reviews'), |
|
| 244 | + 'The Honeypot caught a bad submission:' |
|
| 245 | + ); |
|
| 246 | + } |
|
| 247 | + } |
|
| 248 | 248 | |
| 249 | - /** |
|
| 250 | - * @return void |
|
| 251 | - */ |
|
| 252 | - protected function validateReviewLimits() |
|
| 253 | - { |
|
| 254 | - if (!empty($this->error)) { |
|
| 255 | - return; |
|
| 256 | - } |
|
| 257 | - if (glsr(ReviewLimits::class)->hasReachedLimit($this->request)) { |
|
| 258 | - $this->setError(__('You have already submitted a review.', 'site-reviews')); |
|
| 259 | - } |
|
| 260 | - } |
|
| 249 | + /** |
|
| 250 | + * @return void |
|
| 251 | + */ |
|
| 252 | + protected function validateReviewLimits() |
|
| 253 | + { |
|
| 254 | + if (!empty($this->error)) { |
|
| 255 | + return; |
|
| 256 | + } |
|
| 257 | + if (glsr(ReviewLimits::class)->hasReachedLimit($this->request)) { |
|
| 258 | + $this->setError(__('You have already submitted a review.', 'site-reviews')); |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | 261 | |
| 262 | - /** |
|
| 263 | - * @return void |
|
| 264 | - */ |
|
| 265 | - protected function validateRecaptcha() |
|
| 266 | - { |
|
| 267 | - if (!empty($this->error)) { |
|
| 268 | - return; |
|
| 269 | - } |
|
| 270 | - $status = $this->getRecaptchaStatus(); |
|
| 271 | - if (in_array($status, [static::RECAPTCHA_DISABLED, static::RECAPTCHA_VALID])) { |
|
| 272 | - return; |
|
| 273 | - } |
|
| 274 | - if (static::RECAPTCHA_EMPTY === $status) { |
|
| 275 | - $this->setSessionValues('recaptcha', 'unset'); |
|
| 276 | - $this->recaptchaIsUnset = true; |
|
| 277 | - return; |
|
| 278 | - } |
|
| 279 | - $this->setSessionValues('recaptcha', 'reset'); |
|
| 280 | - $errors = [ |
|
| 281 | - static::RECAPTCHA_FAILED => __('The reCAPTCHA failed to load, please refresh the page and try again.', 'site-reviews'), |
|
| 282 | - static::RECAPTCHA_INVALID => __('The reCAPTCHA verification failed, please try again.', 'site-reviews'), |
|
| 283 | - ]; |
|
| 284 | - $this->setError($errors[$status]); |
|
| 285 | - } |
|
| 262 | + /** |
|
| 263 | + * @return void |
|
| 264 | + */ |
|
| 265 | + protected function validateRecaptcha() |
|
| 266 | + { |
|
| 267 | + if (!empty($this->error)) { |
|
| 268 | + return; |
|
| 269 | + } |
|
| 270 | + $status = $this->getRecaptchaStatus(); |
|
| 271 | + if (in_array($status, [static::RECAPTCHA_DISABLED, static::RECAPTCHA_VALID])) { |
|
| 272 | + return; |
|
| 273 | + } |
|
| 274 | + if (static::RECAPTCHA_EMPTY === $status) { |
|
| 275 | + $this->setSessionValues('recaptcha', 'unset'); |
|
| 276 | + $this->recaptchaIsUnset = true; |
|
| 277 | + return; |
|
| 278 | + } |
|
| 279 | + $this->setSessionValues('recaptcha', 'reset'); |
|
| 280 | + $errors = [ |
|
| 281 | + static::RECAPTCHA_FAILED => __('The reCAPTCHA failed to load, please refresh the page and try again.', 'site-reviews'), |
|
| 282 | + static::RECAPTCHA_INVALID => __('The reCAPTCHA verification failed, please try again.', 'site-reviews'), |
|
| 283 | + ]; |
|
| 284 | + $this->setError($errors[$status]); |
|
| 285 | + } |
|
| 286 | 286 | |
| 287 | - /** |
|
| 288 | - * @return array |
|
| 289 | - */ |
|
| 290 | - protected function validateRequest(array $request) |
|
| 291 | - { |
|
| 292 | - if ($this->isRequestValid($request)) { |
|
| 293 | - return array_merge(glsr(ValidateReviewDefaults::class)->defaults(), $request); |
|
| 294 | - } |
|
| 295 | - return $request; |
|
| 296 | - } |
|
| 287 | + /** |
|
| 288 | + * @return array |
|
| 289 | + */ |
|
| 290 | + protected function validateRequest(array $request) |
|
| 291 | + { |
|
| 292 | + if ($this->isRequestValid($request)) { |
|
| 293 | + return array_merge(glsr(ValidateReviewDefaults::class)->defaults(), $request); |
|
| 294 | + } |
|
| 295 | + return $request; |
|
| 296 | + } |
|
| 297 | 297 | } |
@@ -58,19 +58,19 @@ discard block |
||
| 58 | 58 | /** |
| 59 | 59 | * @return static |
| 60 | 60 | */ |
| 61 | - public function validate(array $request) |
|
| 61 | + public function validate( array $request ) |
|
| 62 | 62 | { |
| 63 | 63 | $this->form_id = $request['form_id']; |
| 64 | - $this->options = glsr(OptionManager::class)->all(); |
|
| 65 | - $this->request = $this->validateRequest($request); |
|
| 64 | + $this->options = glsr( OptionManager::class )->all(); |
|
| 65 | + $this->request = $this->validateRequest( $request ); |
|
| 66 | 66 | $this->validateCustom(); |
| 67 | 67 | $this->validateHoneyPot(); |
| 68 | 68 | $this->validateReviewLimits(); |
| 69 | 69 | $this->validateBlacklist(); |
| 70 | 70 | $this->validateAkismet(); |
| 71 | 71 | $this->validateRecaptcha(); |
| 72 | - if (!empty($this->error)) { |
|
| 73 | - $this->setSessionValues('message', $this->error); |
|
| 72 | + if( !empty($this->error) ) { |
|
| 73 | + $this->setSessionValues( 'message', $this->error ); |
|
| 74 | 74 | } |
| 75 | 75 | return $this; |
| 76 | 76 | } |
@@ -80,9 +80,9 @@ discard block |
||
| 80 | 80 | * @param mixed $fallback |
| 81 | 81 | * @return mixed |
| 82 | 82 | */ |
| 83 | - protected function getOption($path, $fallback = '') |
|
| 83 | + protected function getOption( $path, $fallback = '' ) |
|
| 84 | 84 | { |
| 85 | - return Arr::get($this->options, $path, $fallback); |
|
| 85 | + return Arr::get( $this->options, $path, $fallback ); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | /** |
@@ -90,11 +90,11 @@ discard block |
||
| 90 | 90 | */ |
| 91 | 91 | protected function getRecaptchaStatus() |
| 92 | 92 | { |
| 93 | - if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
| 93 | + if( !glsr( OptionManager::class )->isRecaptchaEnabled() ) { |
|
| 94 | 94 | return static::RECAPTCHA_DISABLED; |
| 95 | 95 | } |
| 96 | - if (empty($this->request['_recaptcha-token'])) { |
|
| 97 | - return $this->request['_counter'] < intval(apply_filters('site-reviews/recaptcha/timeout', 5)) |
|
| 96 | + if( empty($this->request['_recaptcha-token']) ) { |
|
| 97 | + return $this->request['_counter'] < intval( apply_filters( 'site-reviews/recaptcha/timeout', 5 ) ) |
|
| 98 | 98 | ? static::RECAPTCHA_EMPTY |
| 99 | 99 | : static::RECAPTCHA_FAILED; |
| 100 | 100 | } |
@@ -106,23 +106,23 @@ discard block |
||
| 106 | 106 | */ |
| 107 | 107 | protected function getRecaptchaTokenStatus() |
| 108 | 108 | { |
| 109 | - $endpoint = add_query_arg([ |
|
| 109 | + $endpoint = add_query_arg( [ |
|
| 110 | 110 | 'remoteip' => Helper::getIpAddress(), |
| 111 | 111 | 'response' => $this->request['_recaptcha-token'], |
| 112 | - 'secret' => $this->getOption('settings.submissions.recaptcha.secret'), |
|
| 113 | - ], static::RECAPTCHA_ENDPOINT); |
|
| 114 | - if (is_wp_error($response = wp_remote_get($endpoint))) { |
|
| 115 | - glsr_log()->error($response->get_error_message()); |
|
| 112 | + 'secret' => $this->getOption( 'settings.submissions.recaptcha.secret' ), |
|
| 113 | + ], static::RECAPTCHA_ENDPOINT ); |
|
| 114 | + if( is_wp_error( $response = wp_remote_get( $endpoint ) ) ) { |
|
| 115 | + glsr_log()->error( $response->get_error_message() ); |
|
| 116 | 116 | return static::RECAPTCHA_FAILED; |
| 117 | 117 | } |
| 118 | - $response = json_decode(wp_remote_retrieve_body($response)); |
|
| 119 | - if (!empty($response->success)) { |
|
| 120 | - return boolval($response->success) |
|
| 118 | + $response = json_decode( wp_remote_retrieve_body( $response ) ); |
|
| 119 | + if( !empty($response->success) ) { |
|
| 120 | + return boolval( $response->success ) |
|
| 121 | 121 | ? static::RECAPTCHA_VALID |
| 122 | 122 | : static::RECAPTCHA_INVALID; |
| 123 | 123 | } |
| 124 | - foreach ($response->{'error-codes'} as $error) { |
|
| 125 | - glsr_log()->error('reCAPTCHA error: '.$error); |
|
| 124 | + foreach( $response->{'error-codes'} as $error ) { |
|
| 125 | + glsr_log()->error( 'reCAPTCHA error: '.$error ); |
|
| 126 | 126 | } |
| 127 | 127 | return static::RECAPTCHA_INVALID; |
| 128 | 128 | } |
@@ -130,35 +130,35 @@ discard block |
||
| 130 | 130 | /** |
| 131 | 131 | * @return array |
| 132 | 132 | */ |
| 133 | - protected function getValidationRules(array $request) |
|
| 133 | + protected function getValidationRules( array $request ) |
|
| 134 | 134 | { |
| 135 | 135 | $rules = array_intersect_key( |
| 136 | - apply_filters('site-reviews/validation/rules', static::VALIDATION_RULES, $request), |
|
| 137 | - array_flip($this->getOption('settings.submissions.required', [])) |
|
| 136 | + apply_filters( 'site-reviews/validation/rules', static::VALIDATION_RULES, $request ), |
|
| 137 | + array_flip( $this->getOption( 'settings.submissions.required', [] ) ) |
|
| 138 | 138 | ); |
| 139 | - $excluded = explode(',', Arr::get($request, 'excluded')); |
|
| 140 | - return array_diff_key($rules, array_flip($excluded)); |
|
| 139 | + $excluded = explode( ',', Arr::get( $request, 'excluded' ) ); |
|
| 140 | + return array_diff_key( $rules, array_flip( $excluded ) ); |
|
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | /** |
| 144 | 144 | * @return bool |
| 145 | 145 | */ |
| 146 | - protected function isRequestValid(array $request) |
|
| 146 | + protected function isRequestValid( array $request ) |
|
| 147 | 147 | { |
| 148 | - $rules = $this->getValidationRules($request); |
|
| 149 | - $errors = glsr(Validator::class)->validate($request, $rules); |
|
| 150 | - if (empty($errors)) { |
|
| 148 | + $rules = $this->getValidationRules( $request ); |
|
| 149 | + $errors = glsr( Validator::class )->validate( $request, $rules ); |
|
| 150 | + if( empty($errors) ) { |
|
| 151 | 151 | return true; |
| 152 | 152 | } |
| 153 | - $this->error = __('Please fix the submission errors.', 'site-reviews'); |
|
| 154 | - $this->setSessionValues('errors', $errors); |
|
| 155 | - $this->setSessionValues('values', $request); |
|
| 153 | + $this->error = __( 'Please fix the submission errors.', 'site-reviews' ); |
|
| 154 | + $this->setSessionValues( 'errors', $errors ); |
|
| 155 | + $this->setSessionValues( 'values', $request ); |
|
| 156 | 156 | return false; |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | - protected function setError($message, $loggedMessage = '') |
|
| 159 | + protected function setError( $message, $loggedMessage = '' ) |
|
| 160 | 160 | { |
| 161 | - $this->setSessionValues('errors', [], $loggedMessage); |
|
| 161 | + $this->setSessionValues( 'errors', [], $loggedMessage ); |
|
| 162 | 162 | $this->error = $message; |
| 163 | 163 | } |
| 164 | 164 | |
@@ -168,11 +168,11 @@ discard block |
||
| 168 | 168 | * @param string $loggedMessage |
| 169 | 169 | * @return void |
| 170 | 170 | */ |
| 171 | - protected function setSessionValues($type, $value, $loggedMessage = '') |
|
| 171 | + protected function setSessionValues( $type, $value, $loggedMessage = '' ) |
|
| 172 | 172 | { |
| 173 | - glsr()->sessionSet($this->form_id.$type, $value); |
|
| 174 | - if (!empty($loggedMessage)) { |
|
| 175 | - glsr_log()->warning($loggedMessage)->debug($this->request); |
|
| 173 | + glsr()->sessionSet( $this->form_id.$type, $value ); |
|
| 174 | + if( !empty($loggedMessage) ) { |
|
| 175 | + glsr_log()->warning( $loggedMessage )->debug( $this->request ); |
|
| 176 | 176 | } |
| 177 | 177 | } |
| 178 | 178 | |
@@ -181,11 +181,11 @@ discard block |
||
| 181 | 181 | */ |
| 182 | 182 | protected function validateAkismet() |
| 183 | 183 | { |
| 184 | - if (!empty($this->error)) { |
|
| 184 | + if( !empty($this->error) ) { |
|
| 185 | 185 | return; |
| 186 | 186 | } |
| 187 | - if (glsr(Akismet::class)->isSpam($this->request)) { |
|
| 188 | - $this->setError(__('This review has been flagged as possible spam and cannot be submitted.', 'site-reviews'), |
|
| 187 | + if( glsr( Akismet::class )->isSpam( $this->request ) ) { |
|
| 188 | + $this->setError( __( 'This review has been flagged as possible spam and cannot be submitted.', 'site-reviews' ), |
|
| 189 | 189 | 'Akismet caught a spam submission (consider adding the IP address to the blacklist):' |
| 190 | 190 | ); |
| 191 | 191 | } |
@@ -196,18 +196,18 @@ discard block |
||
| 196 | 196 | */ |
| 197 | 197 | protected function validateBlacklist() |
| 198 | 198 | { |
| 199 | - if (!empty($this->error)) { |
|
| 199 | + if( !empty($this->error) ) { |
|
| 200 | 200 | return; |
| 201 | 201 | } |
| 202 | - if (!glsr(Blacklist::class)->isBlacklisted($this->request)) { |
|
| 202 | + if( !glsr( Blacklist::class )->isBlacklisted( $this->request ) ) { |
|
| 203 | 203 | return; |
| 204 | 204 | } |
| 205 | - $blacklistAction = $this->getOption('settings.submissions.blacklist.action'); |
|
| 206 | - if ('reject' != $blacklistAction) { |
|
| 205 | + $blacklistAction = $this->getOption( 'settings.submissions.blacklist.action' ); |
|
| 206 | + if( 'reject' != $blacklistAction ) { |
|
| 207 | 207 | $this->request['blacklisted'] = true; |
| 208 | 208 | return; |
| 209 | 209 | } |
| 210 | - $this->setError(__('Your review cannot be submitted at this time.', 'site-reviews'), |
|
| 210 | + $this->setError( __( 'Your review cannot be submitted at this time.', 'site-reviews' ), |
|
| 211 | 211 | 'Blacklisted submission detected:' |
| 212 | 212 | ); |
| 213 | 213 | } |
@@ -217,18 +217,18 @@ discard block |
||
| 217 | 217 | */ |
| 218 | 218 | protected function validateCustom() |
| 219 | 219 | { |
| 220 | - if (!empty($this->error)) { |
|
| 220 | + if( !empty($this->error) ) { |
|
| 221 | 221 | return; |
| 222 | 222 | } |
| 223 | - $validated = apply_filters('site-reviews/validate/custom', true, $this->request); |
|
| 224 | - if (true === $validated) { |
|
| 223 | + $validated = apply_filters( 'site-reviews/validate/custom', true, $this->request ); |
|
| 224 | + if( true === $validated ) { |
|
| 225 | 225 | return; |
| 226 | 226 | } |
| 227 | - $errorMessage = is_string($validated) |
|
| 227 | + $errorMessage = is_string( $validated ) |
|
| 228 | 228 | ? $validated |
| 229 | - : __('The review submission failed. Please notify the site administrator.', 'site-reviews'); |
|
| 230 | - $this->setError($errorMessage); |
|
| 231 | - $this->setSessionValues('values', $this->request); |
|
| 229 | + : __( 'The review submission failed. Please notify the site administrator.', 'site-reviews' ); |
|
| 230 | + $this->setError( $errorMessage ); |
|
| 231 | + $this->setSessionValues( 'values', $this->request ); |
|
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | /** |
@@ -236,11 +236,11 @@ discard block |
||
| 236 | 236 | */ |
| 237 | 237 | protected function validateHoneyPot() |
| 238 | 238 | { |
| 239 | - if (!empty($this->error)) { |
|
| 239 | + if( !empty($this->error) ) { |
|
| 240 | 240 | return; |
| 241 | 241 | } |
| 242 | - if (!empty($this->request['gotcha'])) { |
|
| 243 | - $this->setError(__('The review submission failed. Please notify the site administrator.', 'site-reviews'), |
|
| 242 | + if( !empty($this->request['gotcha']) ) { |
|
| 243 | + $this->setError( __( 'The review submission failed. Please notify the site administrator.', 'site-reviews' ), |
|
| 244 | 244 | 'The Honeypot caught a bad submission:' |
| 245 | 245 | ); |
| 246 | 246 | } |
@@ -251,11 +251,11 @@ discard block |
||
| 251 | 251 | */ |
| 252 | 252 | protected function validateReviewLimits() |
| 253 | 253 | { |
| 254 | - if (!empty($this->error)) { |
|
| 254 | + if( !empty($this->error) ) { |
|
| 255 | 255 | return; |
| 256 | 256 | } |
| 257 | - if (glsr(ReviewLimits::class)->hasReachedLimit($this->request)) { |
|
| 258 | - $this->setError(__('You have already submitted a review.', 'site-reviews')); |
|
| 257 | + if( glsr( ReviewLimits::class )->hasReachedLimit( $this->request ) ) { |
|
| 258 | + $this->setError( __( 'You have already submitted a review.', 'site-reviews' ) ); |
|
| 259 | 259 | } |
| 260 | 260 | } |
| 261 | 261 | |
@@ -264,33 +264,33 @@ discard block |
||
| 264 | 264 | */ |
| 265 | 265 | protected function validateRecaptcha() |
| 266 | 266 | { |
| 267 | - if (!empty($this->error)) { |
|
| 267 | + if( !empty($this->error) ) { |
|
| 268 | 268 | return; |
| 269 | 269 | } |
| 270 | 270 | $status = $this->getRecaptchaStatus(); |
| 271 | - if (in_array($status, [static::RECAPTCHA_DISABLED, static::RECAPTCHA_VALID])) { |
|
| 271 | + if( in_array( $status, [static::RECAPTCHA_DISABLED, static::RECAPTCHA_VALID] ) ) { |
|
| 272 | 272 | return; |
| 273 | 273 | } |
| 274 | - if (static::RECAPTCHA_EMPTY === $status) { |
|
| 275 | - $this->setSessionValues('recaptcha', 'unset'); |
|
| 274 | + if( static::RECAPTCHA_EMPTY === $status ) { |
|
| 275 | + $this->setSessionValues( 'recaptcha', 'unset' ); |
|
| 276 | 276 | $this->recaptchaIsUnset = true; |
| 277 | 277 | return; |
| 278 | 278 | } |
| 279 | - $this->setSessionValues('recaptcha', 'reset'); |
|
| 279 | + $this->setSessionValues( 'recaptcha', 'reset' ); |
|
| 280 | 280 | $errors = [ |
| 281 | - static::RECAPTCHA_FAILED => __('The reCAPTCHA failed to load, please refresh the page and try again.', 'site-reviews'), |
|
| 282 | - static::RECAPTCHA_INVALID => __('The reCAPTCHA verification failed, please try again.', 'site-reviews'), |
|
| 281 | + static::RECAPTCHA_FAILED => __( 'The reCAPTCHA failed to load, please refresh the page and try again.', 'site-reviews' ), |
|
| 282 | + static::RECAPTCHA_INVALID => __( 'The reCAPTCHA verification failed, please try again.', 'site-reviews' ), |
|
| 283 | 283 | ]; |
| 284 | - $this->setError($errors[$status]); |
|
| 284 | + $this->setError( $errors[$status] ); |
|
| 285 | 285 | } |
| 286 | 286 | |
| 287 | 287 | /** |
| 288 | 288 | * @return array |
| 289 | 289 | */ |
| 290 | - protected function validateRequest(array $request) |
|
| 290 | + protected function validateRequest( array $request ) |
|
| 291 | 291 | { |
| 292 | - if ($this->isRequestValid($request)) { |
|
| 293 | - return array_merge(glsr(ValidateReviewDefaults::class)->defaults(), $request); |
|
| 292 | + if( $this->isRequestValid( $request ) ) { |
|
| 293 | + return array_merge( glsr( ValidateReviewDefaults::class )->defaults(), $request ); |
|
| 294 | 294 | } |
| 295 | 295 | return $request; |
| 296 | 296 | } |