PluginProbe
The Innovative Form Builder – IvyForms / 1.5.1
The Innovative Form Builder – IvyForms v1.5.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
← All changes | backend/src/Controllers/Form/AddFormController.php +48 -64 0.4 → 1.5.1 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,22 +12,13 @@
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;
29 17 use IvyForms\Services\Template\TemplateService;
30 18 use WP_REST_Request;
31 19 use WP_REST_Response;
32 20 use IvyForms\Services\Translations\BackendStrings;
33 -use IvyForms\Services\FieldOptions\FieldOptionsService;
34 -use IvyForms\Factory\FieldOptions\FieldOptionsFactory;
35 21
36 22 /**
37 23 * Class AddFormController
38 24 *
@@ -39,29 +25,17 @@
39 25 * @package IvyForms\Controllers\Form
40 26 */
41 27 class AddFormController extends Controller
42 28 {
43 - private FormService $formService;
44 - private FieldService $fieldService;
45 - private NotificationService $notificationService;
46 - private ConfirmationService $confirmationService;
29 + private FormCreateService $formCreateService;
47 30 private TemplateService $templateService;
48 - private FieldOptionsService $fieldOptionsService;
49 31
50 32 public function __construct(
51 - FormService $formService,
52 - FieldService $fieldService,
53 - NotificationService $notificationService,
54 - ConfirmationService $confirmationService,
55 - TemplateService $templateService,
56 - FieldOptionsService $fieldOptionsService
33 + FormCreateService $formCreateService,
34 + TemplateService $templateService
57 35 ) {
58 - $this->formService = $formService;
59 - $this->fieldService = $fieldService;
60 - $this->notificationService = $notificationService;
61 - $this->confirmationService = $confirmationService;
62 - $this->templateService = $templateService;
63 - $this->fieldOptionsService = $fieldOptionsService;
36 + $this->formCreateService = $formCreateService;
37 + $this->templateService = $templateService;
64 38 }
65 39
66 40 /**
67 41 * @param WP_REST_Request<array<string, mixed>> $data
@@ -76,9 +50,8 @@
76 50 public function handle(WP_REST_Request $data): WP_REST_Response
77 51 {
78 52 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
79 53
80 - // Check if the form data is provided
81 54 if (empty($data->get_params())) {
82 55 throw new InvalidArgumentException(
83 56 BackendStrings::getExceptionStrings()['invalid_request_data']
84 57 );
@@ -83,49 +56,60 @@
83 56 BackendStrings::getExceptionStrings()['invalid_request_data']
84 57 );
85 58 }
86 59
87 - // TODO Add validation for template_id with Sanitizer
88 - //$templateId = Sanitizer::sanitizeText($data->get_param('id'));
89 60 $templateId = sanitize_text_field($data->get_param('template_id'));
90 61
91 - if (empty($templateId)) {
92 - throw new InvalidArgumentException(
93 - BackendStrings::getTemplateStrings()['template_not_found']
94 - );
62 + $mergedParams = $data->get_params();
63 + $formData = [];
64 + if (!empty($templateId)) {
65 + $formData = $this->templateService->getFormDataFromTemplate($templateId);
95 66 }
96 - $formData = $this->templateService->getFormDataFromTemplate($templateId);
97 - $params = Sanitizer::sanitizeFormData($formData);
98 - $form = FormFactory::create($params);
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 + ]);
99 87
100 - $formId = $this->formService->createForm($form);
101 - if (!$formId) {
102 - throw new QueryExecutionException(
103 - BackendStrings::getAllFormsStrings()['failed_to_create_form']
104 - );
105 - }
88 + $templateLockedKeys = [
89 + 'fields',
90 + 'integrationSettings',
91 + 'styleSettings',
92 + 'formActionButtons',
93 + ];
106 94
107 - $adminEmail = get_option('admin_email');
95 + foreach ($mergeableKeys as $key) {
96 + if (!array_key_exists($key, $mergedParams)) {
97 + continue;
98 + }
108 99
109 - // Create default notification
110 - $this->notificationService->createDefaultNotificationForForm(
111 - $formId,
112 - $params['name'],
113 - $adminEmail
114 - );
100 + if (!empty($templateId) && in_array($key, $templateLockedKeys, true)) {
101 + continue;
102 + }
115 103
116 - // Create default confirmation using the service
117 - $confirmationId = $this->confirmationService->createDefaultConfirmationForForm($formId);
118 -
119 - // Save form fields and their options using the service
120 - if (!empty($params['fields'])) {
121 - $this->fieldService->saveFieldsWithOptions($params['fields'], $formId, $this->fieldOptionsService);
104 + $formData[$key] = $mergedParams[$key];
122 105 }
123 106
124 - $form->setId($formId);
107 + $params = Sanitizer::sanitizeFormData($formData);
108 + $created = $this->formCreateService->createFromSanitizedParams($params);
125 109
126 110 return new WP_REST_Response([
127 111 'message' => BackendStrings::getCommonStrings()['ok'],
128 - 'data' => array_merge($form->toArray(), ['confirmationId' => $confirmationId])
112 + 'data' => $created,
129 113 ], 200);
130 114 }
131 115 }