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/AddFormController.php +54 -90 0.2.1 → 1.5 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\QueryExecutionException;
18 13 use IvyForms\Common\Exceptions\ValidationException;
@@ -17,16 +12,10 @@
17 12 use IvyForms\Common\Exceptions\QueryExecutionException;
18 13 use IvyForms\Common\Exceptions\ValidationException;
19 14 use IvyForms\Common\Sanitizer\Sanitizer;
20 15 use IvyForms\Controllers\Controller;
21 -use IvyForms\Factory\Field\FieldFactory;
22 -use IvyForms\Factory\Form\FormFactory;
23 -use IvyForms\Factory\Notification\NotificationFactory;
24 -use IvyForms\Factory\Confirmation\ConfirmationFactory;
25 -use IvyForms\Services\Field\FieldService;
26 -use IvyForms\Services\Form\FormService;
27 -use IvyForms\Services\Notification\NotificationService;
28 -use IvyForms\Services\Confirmation\ConfirmationService;
16 +use IvyForms\Services\Form\FormCreateService;
17 +use IvyForms\Services\Template\TemplateService;
29 18 use WP_REST_Request;
30 19 use WP_REST_Response;
31 20 use IvyForms\Services\Translations\BackendStrings;
32 21
@@ -36,27 +25,21 @@
36 25 * @package IvyForms\Controllers\Form
37 26 */
38 27 class AddFormController extends Controller
39 28 {
40 - private FormService $formService;
41 - private FieldService $fieldService;
42 - private NotificationService $notificationService;
43 - private ConfirmationService $confirmationService;
29 + private FormCreateService $formCreateService;
30 + private TemplateService $templateService;
44 31
45 32 public function __construct(
46 - FormService $formService,
47 - FieldService $fieldService,
48 - NotificationService $notificationService,
49 - ConfirmationService $confirmationService
33 + FormCreateService $formCreateService,
34 + TemplateService $templateService
50 35 ) {
51 - $this->formService = $formService;
52 - $this->fieldService = $fieldService;
53 - $this->notificationService = $notificationService;
54 - $this->confirmationService = $confirmationService;
36 + $this->formCreateService = $formCreateService;
37 + $this->templateService = $templateService;
55 38 }
56 39
57 40 /**
58 - * @param WP_REST_Request $data
41 + * @param WP_REST_Request<array<string, mixed>> $data
59 42 *
60 43 * @return WP_REST_Response
61 44 *
62 45 * @throws QueryExecutionException
@@ -65,87 +48,68 @@
65 48 * @throws ForbiddenException
66 49 */
67 50 public function handle(WP_REST_Request $data): WP_REST_Response
68 51 {
69 - // Verify the nonce
70 52 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
71 53
72 - // Check if the form data is provided
73 54 if (empty($data->get_params())) {
74 - // TODO Put all exception strings to backend strings class
75 - throw new InvalidArgumentException('Invalid request data.');
55 + throw new InvalidArgumentException(
56 + BackendStrings::getExceptionStrings()['invalid_request_data']
57 + );
76 58 }
77 59
78 - // Sanitize the input data
79 - $params = Sanitizer::sanitizeFormData($data->get_params());
60 + $templateId = sanitize_text_field($data->get_param('template_id'));
80 61
81 - $form = FormFactory::create($params);
82 -
83 - $formId = $this->formService->createForm($form);
84 -
85 - if (!$formId) {
86 - throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_create_form']);
62 + $mergedParams = $data->get_params();
63 + $formData = [];
64 + if (!empty($templateId)) {
65 + $formData = $this->templateService->getFormDataFromTemplate($templateId);
87 66 }
67 + /**
68 + * Keys the client may override on create. When creating from a template, field
69 + * structure and layout always come from TemplateService — not the builder store.
70 + * Merging frontend fields caused duplicate name/address rows and single-row layout.
71 + *
72 + * @param list<string> $keys
73 + */
74 + $mergeableKeys = apply_filters('ivyforms/form/add/mergeable_keys', [
75 + 'name',
76 + 'description',
77 + 'published',
78 + 'showTitle',
79 + 'showDescription',
80 + 'storeEntries',
81 + 'fields',
82 + 'integrationSettings',
83 + 'styleSettings',
84 + 'formActionButtons',
85 + 'formType',
86 + ]);
88 87
89 - // Create default notification
90 - $adminEmail = get_option('admin_email');
91 -
92 - $defaultNotification = [
93 - 'formId' => $formId,
94 - 'name' => BackendStrings::getAllFormsStrings()['admin_notification_email'],
95 - 'sender' => $adminEmail,
96 - 'receiver' => $adminEmail,
97 - 'enabled' => 1,
98 - 'subject' => $params['name'],
99 - 'message' => '{all_data}',
100 - 'smartLogic' => 0,
88 + $templateLockedKeys = [
89 + 'fields',
90 + 'integrationSettings',
91 + 'styleSettings',
92 + 'formActionButtons',
101 93 ];
102 94
103 - $notification = NotificationFactory::create($defaultNotification);
104 - $notificationId = $this->notificationService->createNotification($notification);
95 + foreach ($mergeableKeys as $key) {
96 + if (!array_key_exists($key, $mergedParams)) {
97 + continue;
98 + }
105 99
106 - if (!$notificationId) {
107 - throw new QueryExecutionException(
108 - BackendStrings::getSettingsFormBuilderStrings()['failed_to_create_default_notification']
109 - );
110 - }
100 + if (!empty($templateId) && in_array($key, $templateLockedKeys, true)) {
101 + continue;
102 + }
111 103
112 - $notification->setId($notificationId);
113 -
114 - $defaultConfirmation = [
115 - 'formId' => $formId,
116 - 'type' => 'successMessage',
117 - 'enabled' => 1,
118 - 'showForm' => 0,
119 - 'message' => BackendStrings::getAllFormsStrings()['thanks_reaching'],
120 - 'url' => '',
121 - 'page' => '',
122 - ];
123 -
124 - $confirmation = ConfirmationFactory::create($defaultConfirmation);
125 - $confirmationId = $this->confirmationService->createConfirmation($confirmation);
126 -
127 - if (!$confirmationId) {
128 - throw new QueryExecutionException(
129 - BackendStrings::getSettingsFormBuilderStrings()['failed_to_create_confirmation']
130 - );
104 + $formData[$key] = $mergedParams[$key];
131 105 }
132 106
133 - $confirmation->setId($confirmationId);
107 + $params = Sanitizer::sanitizeFormData($formData);
108 + $created = $this->formCreateService->createFromSanitizedParams($params);
134 109
135 - // Save form fields
136 - if (!empty($params['fields'])) {
137 - foreach ($params['fields'] as $fieldData) {
138 - $fieldData['formId'] = $fieldData['formId'] ?? $formId;
139 - $field = FieldFactory::create($fieldData);
140 - $this->fieldService->addField($field);
141 - }
142 - }
143 -
144 - $form->setId($formId);
145 -
146 110 return new WP_REST_Response([
147 111 'message' => BackendStrings::getCommonStrings()['ok'],
148 - 'data' => array_merge($form->toArray(), ['confirmationId' => $confirmationId])
112 + 'data' => $created,
149 113 ], 200);
150 114 }
151 115 }