| @@ -2,9 +2,8 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace FluentSupport\App\Http\Controllers; |
| 4 | 4 | |
| 5 | 5 | use Exception; |
| 6 | -use FluentSupport\App\Hooks\Handlers\ReCaptchaHandler; | |
| 7 | 6 | use FluentSupport\App\Http\Requests\TicketResponseRequest; |
| 8 | 7 | use FluentSupport\App\Models\Product; |
| 9 | 8 | use FluentSupport\App\Models\Ticket; |
| 10 | 9 | use FluentSupport\App\Services\CustomerPortalService; |
| @@ -28,9 +27,14 @@ | ||
| 28 | 27 | */ |
| 29 | 28 | public function getTickets(Request $request) |
| 30 | 29 | { |
| 31 | 30 | |
| 32 | - $onBehalf = $this->sanitizeOnBehalf($request); | |
| 31 | + $onBehalf = $request->get('on_behalf', []); | |
| 32 | + if ($onBehalf) { | |
| 33 | + $onBehalf = array_map(function ($item) { | |
| 34 | + return sanitize_text_field($item); | |
| 35 | + }, $onBehalf); | |
| 36 | + } | |
| 33 | 37 | |
| 34 | 38 | $userIP = $request->getIp(); |
| 35 | 39 | $requestedStatus = $request->getSafe('filter_type', 'sanitize_text_field'); |
| 36 | 40 | $ticketOptions = $request->getSafe([ |
| @@ -55,9 +59,9 @@ | ||
| 55 | 59 | ] |
| 56 | 60 | ]; |
| 57 | 61 | } |
| 58 | 62 | |
| 59 | - if (!$customer->canAccessPortal()) { | |
| 63 | + if ($customer->status !== 'active') { | |
| 60 | 64 | return $this->sendError([ |
| 61 | 65 | 'message' => __('Your account is not active. Please contact support.', 'fluent-support'), |
| 62 | 66 | 'error_type' => '403' |
| 63 | 67 | ], 403); |
| @@ -109,22 +113,8 @@ | ||
| 109 | 113 | * @return array | \WP_REST_Response |
| 110 | 114 | */ |
| 111 | 115 | public function createTicket(Request $request) |
| 112 | 116 | { |
| 113 | - if (ReCaptchaHandler::isRecaptchaApplicable('ticket_form')) { | |
| 114 | - $captchaResponse = $request->getSafe('g-recaptcha-response', 'sanitize_text_field'); | |
| 115 | - $isValidCaptcha = $captchaResponse && ReCaptchaHandler::validateRecaptcha( | |
| 116 | - $captchaResponse, null, null, 'create_ticket' | |
| 117 | - ); | |
| 118 | - | |
| 119 | - if (!$isValidCaptcha) { | |
| 120 | - return $this->sendError([ | |
| 121 | - 'message' => __('Your recaptcha is not verified', 'fluent-support'), | |
| 122 | - 'error_type' => '422' | |
| 123 | - ], 422); | |
| 124 | - } | |
| 125 | - } | |
| 126 | - | |
| 127 | 117 | $dataRules = $this->app->applyCustomFilters('custom_field_required_before_ticket_create', [ |
| 128 | 118 | 'required_fields' => [ |
| 129 | 119 | 'title' => 'required', |
| 130 | 120 | 'content' => 'required' |
| @@ -178,15 +168,20 @@ | ||
| 178 | 168 | $data = $this->validate($request->get(), $dataRules['required_fields'], $dataRules['error_messages']); |
| 179 | 169 | |
| 180 | 170 | $data['title'] = sanitize_text_field($data['title']); |
| 181 | 171 | $data['content'] = wp_kses_post($data['content']); |
| 182 | - $data['custom_data'] = $customData; | |
| 183 | 172 | |
| 184 | - $onBehalf = $this->sanitizeOnBehalf($request); | |
| 173 | + $onBehalf = $request->get('on_behalf', []); | |
| 185 | 174 | $userIP = $request->getIp(); |
| 186 | 175 | |
| 187 | - if (!empty($onBehalf['last_ip_address'])) { | |
| 188 | - $userIP = $onBehalf['last_ip_address']; | |
| 176 | + if ($onBehalf) { | |
| 177 | + $onBehalf = array_map(function ($item) { | |
| 178 | + return sanitize_text_field($item); | |
| 179 | + }, $onBehalf); | |
| 180 | + | |
| 181 | + if (!empty($onBehalf['last_ip_address'])) { | |
| 182 | + $userIP = $onBehalf['last_ip_address']; | |
| 183 | + } | |
| 189 | 184 | } |
| 190 | 185 | |
| 191 | 186 | try { |
| 192 | 187 | $customer = (new CustomerPortalService())->resolveCustomer($onBehalf, $userIP, true); |
| @@ -263,9 +258,9 @@ | ||
| 263 | 258 | { |
| 264 | 259 | |
| 265 | 260 | $customerAdditionalData = $this->getCustomerAdditionalData($request); |
| 266 | 261 | |
| 267 | - $ticket = Ticket::wherePublicIdentifier($ticket_id)->firstOrFail(); | |
| 262 | + $ticket = Ticket::findOrFail($ticket_id); | |
| 268 | 263 | |
| 269 | 264 | $data = $request->sanitize(); |
| 270 | 265 | |
| 271 | 266 | $canCreateResponse = apply_filters('fluent_support/can_customer_create_response', true, $ticket->customer, $ticket, $data); |
| @@ -332,9 +327,9 @@ | ||
| 332 | 327 | |
| 333 | 328 | $customerPortalService = new CustomerPortalService(); |
| 334 | 329 | |
| 335 | 330 | // just for validation |
| 336 | - $ticket = Ticket::with(['customer'])->wherePublicIdentifier($ticketId)->firstOrFail(); | |
| 331 | + $ticket = Ticket::with(['customer'])->findOrFail($ticketId); | |
| 337 | 332 | $customerAdditionalData = $this->getCustomerAdditionalData($request); |
| 338 | 333 | $customer = $customerPortalService->getCustomer($customerAdditionalData, $ticket); |
| 339 | 334 | $customerPortalService->checkCustomerTicketAccess($customer, $ticket, 'feedback'); |
| 340 | 335 | |
| @@ -341,9 +336,9 @@ | ||
| 341 | 336 | $conversationID = $request->getSafe('conversation_id', 'intval'); |
| 342 | 337 | $approvalStatus = $request->getSafe('approval_status', 'sanitize_text_field'); |
| 343 | 338 | |
| 344 | 339 | try { |
| 345 | - return $customerPortalService->addUserFeedback($approvalStatus, $conversationID, $ticket->id); | |
| 340 | + return $customerPortalService->addUserFeedback($approvalStatus, $conversationID); | |
| 346 | 341 | } catch (Exception $e) { |
| 347 | 342 | return $this->sendError([ |
| 348 | 343 | 'message' => Helper::getSafeErrorMessage($e), |
| 349 | 344 | 'error_type' => $e->getCode() |
| @@ -356,9 +351,9 @@ | ||
| 356 | 351 | * @return array |
| 357 | 352 | */ |
| 358 | 353 | public function getPublicOptions() |
| 359 | 354 | { |
| 360 | - $products = Product::select(['id', 'title'])->orderedByTitle()->get(); | |
| 355 | + $products = Product::select(['id', 'title'])->get(); | |
| 361 | 356 | |
| 362 | 357 | return [ |
| 363 | 358 | 'support_products' => $products, |
| 364 | 359 | 'customer_ticket_priorities' => Helper::customerTicketPriorities() |
| @@ -398,42 +393,20 @@ | ||
| 398 | 393 | |
| 399 | 394 | private function getCustomerAdditionalData($request) |
| 400 | 395 | { |
| 401 | 396 | |
| 397 | + $onBehalf = $request->get('on_behalf', []); | |
| 398 | + if ($onBehalf) { | |
| 399 | + $onBehalf = array_map(function ($item) { | |
| 400 | + return sanitize_text_field($item); | |
| 401 | + }, $onBehalf); | |
| 402 | + } | |
| 403 | + | |
| 402 | 404 | $customerAdditionalData = [ |
| 403 | 405 | 'intended_ticket_hash' => $request->getSafe('intended_ticket_hash', 'sanitize_text_field'), |
| 404 | - 'on_behalf' => $this->sanitizeOnBehalf($request), | |
| 406 | + 'on_behalf' => $onBehalf, | |
| 405 | 407 | 'user_ip' => $request->getIp() |
| 406 | 408 | ]; |
| 407 | 409 | |
| 408 | 410 | return $customerAdditionalData; |
| 409 | - } | |
| 410 | - | |
| 411 | - /** | |
| 412 | - * Read the `on_behalf` identity payload from the request in a predictable shape. | |
| 413 | - * Only an array of scalars is accepted; anything else (a scalar, or nested | |
| 414 | - * arrays) is discarded so callers can never hand a non-string to | |
| 415 | - * sanitize_text_field(). An empty result makes resolveCustomer() fall back to | |
| 416 | - * the logged-in user instead of a request-supplied identity. | |
| 417 | - * | |
| 418 | - * @param Request $request | |
| 419 | - * @return array | |
| 420 | - */ | |
| 421 | - private function sanitizeOnBehalf($request) | |
| 422 | - { | |
| 423 | - $onBehalf = $request->get('on_behalf', []); | |
| 424 | - | |
| 425 | - if (!is_array($onBehalf)) { | |
| 426 | - return []; | |
| 427 | - } | |
| 428 | - | |
| 429 | - $sanitized = []; | |
| 430 | - | |
| 431 | - foreach ($onBehalf as $key => $value) { | |
| 432 | - if (is_scalar($value)) { | |
| 433 | - $sanitized[sanitize_text_field($key)] = sanitize_text_field($value); | |
| 434 | - } | |
| 435 | - } | |
| 436 | - | |
| 437 | - return $sanitized; | |
| 438 | 411 | } |
| 439 | 412 | } |