PluginProbe
The Innovative Form Builder – IvyForms / 0.2.1
The Innovative Form Builder – IvyForms v0.2.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.2.1, at backend/src/Controllers/Form/FormSubmissionController.php

122 lines 3.8 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\Sanitizer\Sanitizer;
19 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;
24 use IvyForms\Services\Translations\BackendStrings;
25 use WP_REST_Request;
26 use WP_REST_Response;
27
28 /**
29 * Class FormSubmissionController
30 *
31 * @package IvyForms\Controllers\Form
32 */
33 class FormSubmissionController extends Controller
34 {
35 private FormService $formService;
36 private FieldService $fieldService;
37 private NotificationService $notificationService;
38 private MailerService $mailerService;
39
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;
50 }
51
52 /**
53 * @param WP_REST_Request $data
54 *
55 * @return WP_REST_Response
56 *
57 * @throws InvalidArgumentException|NotFoundException|ForbiddenException
58 */
59 public function handle(WP_REST_Request $data): WP_REST_Response
60 {
61 // Verify the nonce
62 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
63
64 // Validate request data
65 if (empty($data->get_params())) {
66 throw new InvalidArgumentException('Invalid request data.');
67 }
68
69 // Get form ID from submission
70 $formId = Sanitizer::sanitizeId($data->get_param('formId'));
71
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.');
77 }
78
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 }
112 }
113
114 return new WP_REST_Response([
115 'message' => BackendStrings::getCommonStrings()['ok'],
116 'data' => [
117 'success' => true
118 ],
119 ], 200);
120 }
121 }
122