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/UpdateFormController.php +34 -48 0.1.2 → 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,19 +12,16 @@
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 16 use IvyForms\Services\Field\FieldService;
24 17 use IvyForms\Services\Form\FormService;
18 +use IvyForms\Services\Translations\BackendStrings;
25 19 use WP_REST_Request;
26 20 use WP_REST_Response;
27 21
28 22 /**
29 23 * Class UpdateFormController
30 - *
31 - * @package IvyForms\Application\Controllers\Form
32 24 */
33 25 class UpdateFormController extends Controller
34 26 {
35 27 private FormService $formService;
@@ -43,70 +35,64 @@
43 35 $this->fieldService = $fieldService;
44 36 }
45 37
46 38 /**
47 - * @param WP_REST_Request $data
48 - *
39 + * @param WP_REST_Request<array<string,mixed>> $data
49 40 * @return WP_REST_Response
50 - *
51 41 * @throws QueryExecutionException
52 42 * @throws InvalidArgumentException|ValidationException|ForbiddenException
53 43 */
54 44 public function handle(WP_REST_Request $data): WP_REST_Response
55 45 {
56 - // Verify the nonce
57 46 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
58 47
59 - // Check if the form data is provided
60 48 if (empty($data->get_params())) {
61 - throw new InvalidArgumentException('Invalid request data.');
49 + throw new InvalidArgumentException(
50 + BackendStrings::getExceptionStrings()['invalid_request_data']
51 + );
62 52 }
63 53
64 - // Sanitize the update data
65 - $params = Sanitizer::sanitizeFormData($data->get_params());
54 + $formId = (int) $data->get_param('id');
66 55
67 - $form = FormFactory::create($params);
56 + $params = Sanitizer::sanitizeFormData(
57 + $this->formService->withPreservedFormType($data->get_params(), $formId)
58 + );
68 59
69 - $formId = $form->getId();
60 + $this->fieldService->validateFieldsType($params['fields']);
70 61
71 - $this->formService->updateForm($formId, $form);
72 -
73 - if (!$form->getId()) {
74 - throw new QueryExecutionException('Failed to update form.');
62 + [$formId, $form] = $this->formService->updateFormOrFail($params);
63 + if (!$formId) {
64 + throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_update_form'] . '.');
75 65 }
76 - // refactor by sending delete fields as array and delete it with one query
77 - $existingFields = $this->fieldService->getAllFields($formId);
78 - $existingFieldIds = array_map(fn($field) => $field->getId(), $existingFields);
79 66
67 + $existingFieldIds = $this->fieldService->collectExistingFieldIds($formId);
80 68 $submittedFieldIds = [];
81 69
82 70 if (!empty($params['fields'])) {
83 - foreach ($params['fields'] as $fieldData) {
84 - $fieldData['formId'] = $fieldData['formId'] ?? $formId;
85 - $field = FieldFactory::create($fieldData);
86 - $fieldId = $field->getId();
71 + $submittedFieldIds = $this->fieldService->updateFieldsWithOptions(
72 + $params['fields'],
73 + $formId
74 + );
75 + }
87 76
88 - if ($fieldId === 0) {
89 - // Add new field
90 - $newField = $this->fieldService->addField($field);
91 - $submittedFieldIds[] = $newField;
92 - continue;
93 - }
94 - // Update existing field
95 - $this->fieldService->updateField($fieldId, $field);
96 - $submittedFieldIds[] = $fieldId;
77 + if (!empty($existingFieldIds)) {
78 + $idsToDelete = array_diff($existingFieldIds, $submittedFieldIds);
79 + if (!empty($idsToDelete)) {
80 + $this->fieldService->deleteFields($idsToDelete);
97 81 }
98 82 }
99 83
100 - // Delete fields that are not in the submitted data
101 - foreach ($existingFieldIds as $existingId) {
102 - if (!in_array($existingId, $submittedFieldIds, true)) {
103 - $this->fieldService->deleteField($existingId);
104 - }
105 - }
84 + /**
85 + * After a form row (and fields) are persisted.
86 + *
87 + * @param \IvyForms\Entity\Form\Form $form
88 + * @param int $formId
89 + * @param string $context create|update
90 + */
91 + do_action('ivyforms/form/after_persist', $form, $formId, 'update');
106 92
107 93 return new WP_REST_Response([
108 - 'message' => 'OK',
109 - 'data' => $form->toArray()
94 + 'message' => BackendStrings::getCommonStrings()['ok'],
95 + 'data' => $form->toArray()
110 96 ], 200);
111 97 }
112 98 }