← All changes
|
backend/src/Controllers/Form/FormSubmissionController.php
+36
-104
0.3
→
1.5.1
View file →
| @@ -1,18 +1,13 @@ | ||
| 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; |
| 18 | 13 | use IvyForms\Common\Exceptions\ValidationException; |
| @@ -17,17 +12,11 @@ | ||
| 17 | 12 | use IvyForms\Common\Exceptions\NotFoundException; |
| 18 | 13 | use IvyForms\Common\Exceptions\ValidationException; |
| 19 | 14 | use IvyForms\Common\Sanitizer\Sanitizer; |
| 20 | 15 | use IvyForms\Controllers\Controller; |
| 21 | -use IvyForms\Factory\Entry\EntryFactory; | |
| 22 | -use IvyForms\Factory\EntryField\EntryFieldFactory; | |
| 23 | -use IvyForms\Services\Entry\EntryService; | |
| 24 | -use IvyForms\Services\Field\FieldService; | |
| 25 | -use IvyForms\Services\Form\FormService; | |
| 26 | -use IvyForms\Services\Mailer\MailerService; | |
| 27 | -use IvyForms\Services\Notification\NotificationService; | |
| 28 | -use IvyForms\Repository\EntryField\EntryFieldRepositoryInterface; | |
| 29 | -use IvyForms\Services\Security\SecurityService; | |
| 16 | +use IvyForms\Infrastructure\WP\REST\SubmissionMultipartReader; | |
| 17 | +use IvyForms\Services\Submission\FormSubmissionService; | |
| 18 | +use IvyForms\Services\Submission\SubmissionPayloadParser; | |
| 30 | 19 | use IvyForms\Services\Translations\BackendStrings; |
| 31 | 20 | use WP_REST_Request; |
| 32 | 21 | use WP_REST_Response; |
| 33 | 22 | |
| @@ -37,29 +26,13 @@ | ||
| 37 | 26 | * @package IvyForms\Controllers\Form |
| 38 | 27 | */ |
| 39 | 28 | class FormSubmissionController extends Controller |
| 40 | 29 | { |
| 41 | - private FormService $formService; | |
| 42 | - private FieldService $fieldService; | |
| 43 | - private NotificationService $notificationService; | |
| 44 | - private MailerService $mailerService; | |
| 45 | - private EntryService $entryService; | |
| 46 | - private SecurityService $securityService; | |
| 30 | + private FormSubmissionService $formSubmissionService; | |
| 47 | 31 | |
| 48 | - public function __construct( | |
| 49 | - FormService $formService, | |
| 50 | - FieldService $fieldService, | |
| 51 | - NotificationService $notificationService, | |
| 52 | - MailerService $mailerService, | |
| 53 | - EntryService $entryService, | |
| 54 | - SecurityService $securityService | |
| 55 | - ) { | |
| 56 | - $this->formService = $formService; | |
| 57 | - $this->fieldService = $fieldService; | |
| 58 | - $this->notificationService = $notificationService; | |
| 59 | - $this->mailerService = $mailerService; | |
| 60 | - $this->entryService = $entryService; | |
| 61 | - $this->securityService = $securityService; | |
| 32 | + public function __construct(FormSubmissionService $formSubmissionService) | |
| 33 | + { | |
| 34 | + $this->formSubmissionService = $formSubmissionService; | |
| 62 | 35 | } |
| 63 | 36 | |
| 64 | 37 | /** |
| 65 | 38 | * @param WP_REST_Request $data |
| @@ -68,86 +41,45 @@ | ||
| 68 | 41 | * |
| 69 | 42 | * @throws InvalidArgumentException|NotFoundException|ForbiddenException |
| 70 | 43 | * @throws ValidationException |
| 71 | 44 | */ |
| 72 | - public function handle(WP_REST_Request $data): WP_REST_Response | |
| 45 | + protected function handle(WP_REST_Request $data): WP_REST_Response | |
| 73 | 46 | { |
| 74 | 47 | Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 75 | 48 | |
| 76 | - if (empty($data->get_params())) { | |
| 77 | - throw new InvalidArgumentException( | |
| 78 | - BackendStrings::getExceptionStrings()['invalid_request_data'] | |
| 79 | - ); | |
| 80 | - } | |
| 49 | + $params = SubmissionPayloadParser::parseRequestPayload($data); | |
| 50 | + $formId = Sanitizer::sanitizeId((int) ($params['formId'] ?? 0)); | |
| 51 | + Sanitizer::verifySubmissionNonce(isset($params['nonce']) ? (string) $params['nonce'] : null, $formId); | |
| 81 | 52 | |
| 82 | - $formId = Sanitizer::sanitizeId($data->get_param('formId')); | |
| 83 | - Sanitizer::verifySubmissionNonce($data->get_param('nonce'), $formId); | |
| 53 | + $uploadedFiles = SubmissionMultipartReader::resolveUploadedFiles($data); | |
| 54 | + $result = $this->formSubmissionService->processSubmission($params, $uploadedFiles); | |
| 84 | 55 | |
| 85 | - if ($formId <= 0) { | |
| 86 | - throw new InvalidArgumentException( | |
| 87 | - BackendStrings::getExceptionStrings()['invalid_form_id'] | |
| 88 | - ); | |
| 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); | |
| 89 | 67 | } |
| 90 | 68 | |
| 91 | - $form = $this->formService->getFormById($formId); | |
| 92 | - $formFields = $this->fieldService->getAllFields($form->getId()); | |
| 93 | - | |
| 94 | - // Validate CAPTCHA if present using SecurityService (automatically determines provider and validates) | |
| 95 | - $this->securityService->validateFormSubmission($data->get_params(), $formFields); | |
| 96 | - | |
| 97 | - // Sanitize form submission data based on field types | |
| 98 | - $submissionData = Sanitizer::sanitizeFormSubmissionData($data->get_params(), $formFields); | |
| 99 | - | |
| 100 | - $entry = [ | |
| 101 | - 'stored' => false | |
| 102 | - ]; | |
| 103 | - if ($form->isStoreEntries()) { | |
| 104 | - $entryData = $this->entryService->buildEntryData($formId); | |
| 105 | - $entryObj = EntryFactory::create($entryData); | |
| 106 | - $entryId = $this->entryService->createEntry($entryObj); | |
| 107 | - $this->entryService->addEntryFields($formFields, $entryId, $submissionData); | |
| 108 | - $entry = [ | |
| 109 | - 'stored' => true, | |
| 110 | - 'id' => $entryId, | |
| 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 | - $notificationsResult = $this->processNotifications($formId, $submissionData); | |
| 115 | - | |
| 116 | 81 | return new WP_REST_Response([ |
| 117 | - 'message' => BackendStrings::getCommonStrings()['ok'], | |
| 118 | - 'data' => [ | |
| 119 | - 'success' => $notificationsResult, | |
| 120 | - 'entry' => $entry, | |
| 121 | - ], | |
| 82 | + 'data' => $result, | |
| 122 | 83 | ], 200); |
| 123 | - } | |
| 124 | - | |
| 125 | - /** | |
| 126 | - * Process notifications for the form. | |
| 127 | - * | |
| 128 | - * @param int $formId | |
| 129 | - * @param array<mixed> $submissionData | |
| 130 | - * @return bool | |
| 131 | - * @throws NotFoundException | |
| 132 | - */ | |
| 133 | - private function processNotifications(int $formId, array $submissionData): bool | |
| 134 | - { | |
| 135 | - $activeNotifications = $this->notificationService->getActiveNotificationsByFormId($formId); | |
| 136 | - | |
| 137 | - // Process each active notification | |
| 138 | - foreach ($activeNotifications as $notification) { | |
| 139 | - // Skip notifications with empty receivers | |
| 140 | - if (empty($notification->getReceiver())) { | |
| 141 | - continue; | |
| 142 | - } | |
| 143 | - | |
| 144 | - // Send email using our mailer service | |
| 145 | - $sent = $this->mailerService->sendEmail($notification, $submissionData); | |
| 146 | - | |
| 147 | - if (!$sent) { | |
| 148 | - error_log('Failed to send email notification for form ID: ' . $formId); | |
| 149 | - } | |
| 150 | - } | |
| 151 | - return true; | |
| 152 | 84 | } |
| 153 | 85 | } |