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