← All changes
|
backend/src/Controllers/Form/FormSubmissionController.php
+38
-74
0.2
→
1.5.1
View file →
| @@ -1,27 +1,22 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * @copyright © TMS-Plugins. All rights reserved. | |
| 4 | + * @copyright © Melograno Venture Studio. All rights reserved. | |
| 5 | 5 | * @licence See COPYING.md for license details. |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace IvyForms\Controllers\Form; |
| 9 | 9 | |
| 10 | -// phpcs:disable PSR1.Files.SideEffects | |
| 11 | -if (!defined('ABSPATH')) { | |
| 12 | - exit; // Exit if accessed directly | |
| 13 | -} | |
| 14 | - | |
| 15 | 10 | use IvyForms\Common\Exceptions\ForbiddenException; |
| 16 | 11 | use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 17 | 12 | use IvyForms\Common\Exceptions\NotFoundException; |
| 13 | +use IvyForms\Common\Exceptions\ValidationException; | |
| 18 | 14 | use IvyForms\Common\Sanitizer\Sanitizer; |
| 19 | 15 | use IvyForms\Controllers\Controller; |
| 20 | -use IvyForms\Services\Field\FieldService; | |
| 21 | -use IvyForms\Services\Form\FormService; | |
| 22 | -use IvyForms\Services\Mailer\MailerService; | |
| 23 | -use IvyForms\Services\Notification\NotificationService; | |
| 16 | +use IvyForms\Infrastructure\WP\REST\SubmissionMultipartReader; | |
| 17 | +use IvyForms\Services\Submission\FormSubmissionService; | |
| 18 | +use IvyForms\Services\Submission\SubmissionPayloadParser; | |
| 24 | 19 | use IvyForms\Services\Translations\BackendStrings; |
| 25 | 20 | use WP_REST_Request; |
| 26 | 21 | use WP_REST_Response; |
| 27 | 22 | |
| @@ -31,23 +26,13 @@ | ||
| 31 | 26 | * @package IvyForms\Controllers\Form |
| 32 | 27 | */ |
| 33 | 28 | class FormSubmissionController extends Controller |
| 34 | 29 | { |
| 35 | - private FormService $formService; | |
| 36 | - private FieldService $fieldService; | |
| 37 | - private NotificationService $notificationService; | |
| 38 | - private MailerService $mailerService; | |
| 30 | + private FormSubmissionService $formSubmissionService; | |
| 39 | 31 | |
| 40 | - public function __construct( | |
| 41 | - FormService $formService, | |
| 42 | - FieldService $fieldService, | |
| 43 | - NotificationService $notificationService, | |
| 44 | - MailerService $mailerService | |
| 45 | - ) { | |
| 46 | - $this->formService = $formService; | |
| 47 | - $this->fieldService = $fieldService; | |
| 48 | - $this->notificationService = $notificationService; | |
| 49 | - $this->mailerService = $mailerService; | |
| 32 | + public function __construct(FormSubmissionService $formSubmissionService) | |
| 33 | + { | |
| 34 | + $this->formSubmissionService = $formSubmissionService; | |
| 50 | 35 | } |
| 51 | 36 | |
| 52 | 37 | /** |
| 53 | 38 | * @param WP_REST_Request $data |
| @@ -54,68 +39,47 @@ | ||
| 54 | 39 | * |
| 55 | 40 | * @return WP_REST_Response |
| 56 | 41 | * |
| 57 | 42 | * @throws InvalidArgumentException|NotFoundException|ForbiddenException |
| 43 | + * @throws ValidationException | |
| 58 | 44 | */ |
| 59 | - public function handle(WP_REST_Request $data): WP_REST_Response | |
| 45 | + protected function handle(WP_REST_Request $data): WP_REST_Response | |
| 60 | 46 | { |
| 61 | - // Verify the nonce | |
| 62 | 47 | Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 63 | 48 | |
| 64 | - // Validate request data | |
| 65 | - if (empty($data->get_params())) { | |
| 66 | - throw new InvalidArgumentException('Invalid request data.'); | |
| 67 | - } | |
| 49 | + $params = SubmissionPayloadParser::parseRequestPayload($data); | |
| 50 | + $formId = Sanitizer::sanitizeId((int) ($params['formId'] ?? 0)); | |
| 51 | + Sanitizer::verifySubmissionNonce(isset($params['nonce']) ? (string) $params['nonce'] : null, $formId); | |
| 68 | 52 | |
| 69 | - // Get form ID from submission | |
| 70 | - $formId = Sanitizer::sanitizeId($data->get_param('formId')); | |
| 53 | + $uploadedFiles = SubmissionMultipartReader::resolveUploadedFiles($data); | |
| 54 | + $result = $this->formSubmissionService->processSubmission($params, $uploadedFiles); | |
| 71 | 55 | |
| 72 | - // Verify the submission nonce | |
| 73 | - Sanitizer::verifySubmissionNonce($data->get_param('nonce'), $formId); | |
| 74 | - | |
| 75 | - if ($formId <= 0) { | |
| 76 | - throw new InvalidArgumentException('Invalid form ID.'); | |
| 56 | + if (!empty($result['requires_action'])) { | |
| 57 | + return new WP_REST_Response([ | |
| 58 | + 'message' => BackendStrings::getCommonStrings()['ok'], | |
| 59 | + 'data' => [ | |
| 60 | + 'success' => false, | |
| 61 | + 'requires_action' => true, | |
| 62 | + 'client_secret' => $result['client_secret'] ?? null, | |
| 63 | + 'payment_intent_id' => $result['payment_intent_id'] ?? null, | |
| 64 | + 'payment_method' => $result['payment_method'] ?? 'stripe', | |
| 65 | + ], | |
| 66 | + ], 200); | |
| 77 | 67 | } |
| 78 | 68 | |
| 79 | - // Get form details | |
| 80 | - $form = $this->formService->getFormById($formId); | |
| 81 | - | |
| 82 | - // Get form fields | |
| 83 | - $formFields = $this->fieldService->getAllFields($form->getId()); | |
| 84 | - | |
| 85 | - // Sanitize form submission data based on field types | |
| 86 | - $submissionData = Sanitizer::sanitizeFormSubmissionData($data->get_params(), $formFields); | |
| 87 | - | |
| 88 | - // Get active notifications for this form | |
| 89 | - $activeNotifications = $this->notificationService->getActiveNotificationsByFormId($form->getId()); | |
| 90 | - | |
| 91 | - // Process each active notification | |
| 92 | - foreach ($activeNotifications as $notification) { | |
| 93 | - // Skip notifications with empty receivers | |
| 94 | - if (empty($notification->getReceiver())) { | |
| 95 | - continue; | |
| 96 | - } | |
| 97 | - | |
| 98 | - // Send email using our mailer service | |
| 99 | - $sent = $this->mailerService->sendEmail($notification, $submissionData); | |
| 100 | - | |
| 101 | - if (!$sent) { | |
| 102 | - if (defined('WP_DEBUG') && WP_DEBUG) { | |
| 103 | - error_log('Failed to send email notification for form ID: ' . $formId); | |
| 104 | - } | |
| 105 | - return new WP_REST_Response([ | |
| 106 | - 'message' => BackendStrings::getCommonStrings()['ok'], | |
| 107 | - 'data' => [ | |
| 108 | - 'success' => false | |
| 109 | - ], | |
| 110 | - ], 200); | |
| 111 | - } | |
| 69 | + if (!empty($result['payment_error'])) { | |
| 70 | + return new WP_REST_Response([ | |
| 71 | + 'message' => (string) ( | |
| 72 | + $result['message'] ?? BackendStrings::getPaymentStrings()['payment_failed'] | |
| 73 | + ), | |
| 74 | + 'data' => [ | |
| 75 | + 'success' => false, | |
| 76 | + 'payment_error' => true, | |
| 77 | + ], | |
| 78 | + ], 200); | |
| 112 | 79 | } |
| 113 | 80 | |
| 114 | 81 | return new WP_REST_Response([ |
| 115 | - 'message' => BackendStrings::getCommonStrings()['ok'], | |
| 116 | - 'data' => [ | |
| 117 | - 'success' => true | |
| 118 | - ], | |
| 82 | + 'data' => $result, | |
| 119 | 83 | ], 200); |
| 120 | 84 | } |
| 121 | 85 | } |