PluginProbe
The Innovative Form Builder – IvyForms / 1.5.1
The Innovative Form Builder – IvyForms v1.5.1
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
← All changes | backend/src/Controllers/Form/FormSubmissionController.php +33 -118 0.4 → 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,19 +12,12 @@
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\Repository\Entry\EntryHelper;
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\Services\Security\SecurityService;
16 +use IvyForms\Infrastructure\WP\REST\SubmissionMultipartReader;
17 +use IvyForms\Services\Submission\FormSubmissionService;
18 +use IvyForms\Services\Submission\SubmissionPayloadParser;
29 19 use IvyForms\Services\Translations\BackendStrings;
30 -use IvyForms\Services\Placeholder\PlaceholderService;
31 -use IvyForms\Services\Confirmation\ConfirmationService;
32 20 use WP_REST_Request;
33 21 use WP_REST_Response;
34 22
35 23 /**
@@ -38,32 +26,13 @@
38 26 * @package IvyForms\Controllers\Form
39 27 */
40 28 class FormSubmissionController extends Controller
41 29 {
42 - private FormService $formService;
43 - private FieldService $fieldService;
44 - private NotificationService $notificationService;
45 - private MailerService $mailerService;
46 - private EntryService $entryService;
47 - private SecurityService $securityService;
48 - private ConfirmationService $confirmationService;
30 + private FormSubmissionService $formSubmissionService;
49 31
50 - public function __construct(
51 - FormService $formService,
52 - FieldService $fieldService,
53 - NotificationService $notificationService,
54 - MailerService $mailerService,
55 - EntryService $entryService,
56 - SecurityService $securityService,
57 - ConfirmationService $confirmationService
58 - ) {
59 - $this->formService = $formService;
60 - $this->fieldService = $fieldService;
61 - $this->notificationService = $notificationService;
62 - $this->mailerService = $mailerService;
63 - $this->entryService = $entryService;
64 - $this->securityService = $securityService;
65 - $this->confirmationService = $confirmationService;
32 + public function __construct(FormSubmissionService $formSubmissionService)
33 + {
34 + $this->formSubmissionService = $formSubmissionService;
66 35 }
67 36
68 37 /**
69 38 * @param WP_REST_Request $data
@@ -72,99 +41,45 @@
72 41 *
73 42 * @throws InvalidArgumentException|NotFoundException|ForbiddenException
74 43 * @throws ValidationException
75 44 */
76 - public function handle(WP_REST_Request $data): WP_REST_Response
45 + protected function handle(WP_REST_Request $data): WP_REST_Response
77 46 {
78 47 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
79 48
80 - if (empty($data->get_params())) {
81 - throw new InvalidArgumentException(
82 - BackendStrings::getExceptionStrings()['invalid_request_data']
83 - );
84 - }
49 + $params = SubmissionPayloadParser::parseRequestPayload($data);
50 + $formId = Sanitizer::sanitizeId((int) ($params['formId'] ?? 0));
51 + Sanitizer::verifySubmissionNonce(isset($params['nonce']) ? (string) $params['nonce'] : null, $formId);
85 52
86 - $formId = Sanitizer::sanitizeId($data->get_param('formId'));
87 - Sanitizer::verifySubmissionNonce($data->get_param('nonce'), $formId);
53 + $uploadedFiles = SubmissionMultipartReader::resolveUploadedFiles($data);
54 + $result = $this->formSubmissionService->processSubmission($params, $uploadedFiles);
88 55
89 - if ($formId <= 0) {
90 - throw new InvalidArgumentException(
91 - BackendStrings::getExceptionStrings()['invalid_form_id']
92 - );
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);
93 67 }
94 68
95 - $form = $this->formService->getFormById($formId);
96 - $formFields = $this->fieldService->getAllFields($form->getId());
97 -
98 - // Build fieldLabels mapping for placeholders
99 - $fieldLabels = PlaceholderService::buildFieldLabels($formFields);
100 -
101 - // Validate CAPTCHA if present using SecurityService (automatically determines provider and validates)
102 - $this->securityService->validateFormSubmission($data->get_params(), $formFields);
103 -
104 - // Sanitize form submission data based on field types
105 - $submissionData = Sanitizer::sanitizeFormSubmissionData($data->get_params(), $formFields);
106 -
107 - [$isDuplicate, $duplicateErrors] = $this->fieldService->checkDuplicateFieldValues(
108 - $formFields,
109 - $submissionData,
110 - $formId,
111 - $this->entryService
112 - );
113 - if ($isDuplicate) {
69 + if (!empty($result['payment_error'])) {
114 70 return new WP_REST_Response([
115 - 'data' => [
116 - 'success' => null,
117 - 'entry' => ['stored' => false],
118 - 'confirmation' => null,
119 - 'is_duplicate' => true,
120 - 'duplicate_errors' => $duplicateErrors,
71 + 'message' => (string) (
72 + $result['message'] ?? BackendStrings::getPaymentStrings()['payment_failed']
73 + ),
74 + 'data' => [
75 + 'success' => false,
76 + 'payment_error' => true,
121 77 ],
122 78 ], 200);
123 79 }
124 80
125 - $entryId = null;
126 - $entry = [
127 - 'stored' => false
128 - ];
129 - if ($form->isStoreEntries()) {
130 - $entryData = EntryHelper::buildEntryData($formId);
131 - $entryObj = EntryFactory::create($entryData);
132 - $entryId = $this->entryService->createEntry($entryObj);
133 - $this->entryService->addEntryFields($formFields, $entryId, $submissionData);
134 - $entry = [
135 - 'stored' => true,
136 - 'id' => $entryId,
137 - ];
138 - }
139 -
140 - // Build field and general entry data for placeholders
141 - $fieldData = PlaceholderService::buildFieldData($formFields, $submissionData);
142 - $generalData = PlaceholderService::buildGeneralData($entryId ?? 0, $submissionData);
143 -
144 - // Use NotificationService for notifications
145 - $notificationsResult = $this->notificationService->processNotifications(
146 - $formId,
147 - $submissionData,
148 - $fieldData,
149 - $generalData,
150 - $this->mailerService,
151 - $fieldLabels
152 - );
153 - // Use ConfirmationService for confirmations
154 - $confirmationMessage = $this->confirmationService->processConfirmations(
155 - $formId,
156 - $fieldData,
157 - $generalData,
158 - $fieldLabels
159 - );
160 -
161 81 return new WP_REST_Response([
162 - 'message' => BackendStrings::getCommonStrings()['ok'],
163 - 'data' => [
164 - 'success' => $notificationsResult,
165 - 'entry' => $entry,
166 - 'confirmation' => $confirmationMessage,
167 - ],
82 + 'data' => $result,
168 83 ], 200);
169 84 }
170 85 }