PluginProbe
The Innovative Form Builder – IvyForms / 1.5
The Innovative Form Builder – IvyForms v1.5
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 +39 -74 0.1.2 → 1.5 View file →
@@ -1,27 +1,23 @@
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;
19 +use IvyForms\Services\Translations\BackendStrings;
24 20 use WP_REST_Request;
25 21 use WP_REST_Response;
26 22
27 23 /**
@@ -30,23 +26,13 @@
30 26 * @package IvyForms\Controllers\Form
31 27 */
32 28 class FormSubmissionController extends Controller
33 29 {
34 - private FormService $formService;
35 - private FieldService $fieldService;
36 - private NotificationService $notificationService;
37 - private MailerService $mailerService;
30 + private FormSubmissionService $formSubmissionService;
38 31
39 - public function __construct(
40 - FormService $formService,
41 - FieldService $fieldService,
42 - NotificationService $notificationService,
43 - MailerService $mailerService
44 - ) {
45 - $this->formService = $formService;
46 - $this->fieldService = $fieldService;
47 - $this->notificationService = $notificationService;
48 - $this->mailerService = $mailerService;
32 + public function __construct(FormSubmissionService $formSubmissionService)
33 + {
34 + $this->formSubmissionService = $formSubmissionService;
49 35 }
50 36
51 37 /**
52 38 * @param WP_REST_Request $data
@@ -53,68 +39,47 @@
53 39 *
54 40 * @return WP_REST_Response
55 41 *
56 42 * @throws InvalidArgumentException|NotFoundException|ForbiddenException
43 + * @throws ValidationException
57 44 */
58 - public function handle(WP_REST_Request $data): WP_REST_Response
45 + protected function handle(WP_REST_Request $data): WP_REST_Response
59 46 {
60 - // Verify the nonce
61 47 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
62 48
63 - // Validate request data
64 - if (empty($data->get_params())) {
65 - throw new InvalidArgumentException('Invalid request data.');
66 - }
49 + $params = SubmissionPayloadParser::parseRequestPayload($data);
50 + $formId = Sanitizer::sanitizeId((int) ($params['formId'] ?? 0));
51 + Sanitizer::verifySubmissionNonce(isset($params['nonce']) ? (string) $params['nonce'] : null, $formId);
67 52
68 - // Get form ID from submission
69 - $formId = Sanitizer::sanitizeId($data->get_param('formId'));
53 + $uploadedFiles = SubmissionMultipartReader::resolveUploadedFiles($data);
54 + $result = $this->formSubmissionService->processSubmission($params, $uploadedFiles);
70 55
71 - // Verify the submission nonce
72 - Sanitizer::verifySubmissionNonce($data->get_param('nonce'), $formId);
73 -
74 - if ($formId <= 0) {
75 - 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);
76 67 }
77 68
78 - // Get form details
79 - $form = $this->formService->getFormById($formId);
80 -
81 - // Get form fields
82 - $formFields = $this->fieldService->getAllFields($form->getId());
83 -
84 - // Sanitize form submission data based on field types
85 - $submissionData = Sanitizer::sanitizeFormSubmissionData($data->get_params(), $formFields);
86 -
87 - // Get active notifications for this form
88 - $activeNotifications = $this->notificationService->getActiveNotificationsByFormId($form->getId());
89 -
90 - // Process each active notification
91 - foreach ($activeNotifications as $notification) {
92 - // Skip notifications with empty receivers
93 - if (empty($notification->getReceiver())) {
94 - continue;
95 - }
96 -
97 - // Send email using our mailer service
98 - $sent = $this->mailerService->sendEmail($notification, $submissionData);
99 -
100 - if (!$sent) {
101 - if (defined('WP_DEBUG') && WP_DEBUG) {
102 - error_log('Failed to send email notification for form ID: ' . $formId);
103 - }
104 - return new WP_REST_Response([
105 - 'message' => 'OK',
106 - 'data' => [
107 - 'success' => false
108 - ],
109 - ], 200);
110 - }
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);
111 79 }
112 80
113 81 return new WP_REST_Response([
114 - 'message' => 'OK',
115 - 'data' => [
116 - 'success' => true
117 - ],
82 + 'data' => $result,
118 83 ], 200);
119 84 }
120 85 }