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

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