PluginProbe
The Innovative Form Builder – IvyForms / 0.3.1
The Innovative Form Builder – IvyForms v0.3.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
ivyforms / backend / src / Controllers / Form / FormSubmissionController.php

FormSubmissionController.php in The Innovative Form Builder – IvyForms 0.3.1, at backend/src/Controllers/Form/FormSubmissionController.php

154 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @copyright © TMS-Plugins. All rights reserved.
5 * @licence See COPYING.md for license details.
6 */
7
8 namespace IvyForms\Controllers\Form;
9
10 // phpcs:disable PSR1.Files.SideEffects
11 if (!defined('ABSPATH')) {
12 exit; // Exit if accessed directly
13 }
14
15 use IvyForms\Common\Exceptions\ForbiddenException;
16 use IvyForms\Common\Exceptions\InvalidArgumentException;
17 use IvyForms\Common\Exceptions\NotFoundException;
18 use IvyForms\Common\Exceptions\ValidationException;
19 use IvyForms\Common\Sanitizer\Sanitizer;
20 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;
30 use IvyForms\Services\Translations\BackendStrings;
31 use WP_REST_Request;
32 use WP_REST_Response;
33
34 /**
35 * Class FormSubmissionController
36 *
37 * @package IvyForms\Controllers\Form
38 */
39 class FormSubmissionController extends Controller
40 {
41 private FormService $formService;
42 private FieldService $fieldService;
43 private NotificationService $notificationService;
44 private MailerService $mailerService;
45 private EntryService $entryService;
46 private SecurityService $securityService;
47
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;
62 }
63
64 /**
65 * @param WP_REST_Request $data
66 *
67 * @return WP_REST_Response
68 *
69 * @throws InvalidArgumentException|NotFoundException|ForbiddenException
70 * @throws ValidationException
71 */
72 public function handle(WP_REST_Request $data): WP_REST_Response
73 {
74 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
75
76 if (empty($data->get_params())) {
77 throw new InvalidArgumentException(
78 BackendStrings::getExceptionStrings()['invalid_request_data']
79 );
80 }
81
82 $formId = Sanitizer::sanitizeId($data->get_param('formId'));
83 Sanitizer::verifySubmissionNonce($data->get_param('nonce'), $formId);
84
85 if ($formId <= 0) {
86 throw new InvalidArgumentException(
87 BackendStrings::getExceptionStrings()['invalid_form_id']
88 );
89 }
90
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 ];
112 }
113
114 $notificationsResult = $this->processNotifications($formId, $submissionData);
115
116 return new WP_REST_Response([
117 'message' => BackendStrings::getCommonStrings()['ok'],
118 'data' => [
119 'success' => $notificationsResult,
120 'entry' => $entry,
121 ],
122 ], 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 }
153 }
154