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 +98 -0 0.6.1 → 1.5.1 View file →
@@ -1,0 +1,98 @@
1 +<?php
2 +
3 +/**
4 + * @copyright © Melograno Venture Studio. All rights reserved.
5 + * @licence See COPYING.md for license details.
6 + */
7 +
8 +namespace IvyForms\Controllers\Form;
9 +
10 +use IvyForms\Common\Exceptions\ForbiddenException;
11 +use IvyForms\Common\Exceptions\InvalidArgumentException;
12 +use IvyForms\Common\Exceptions\QueryExecutionException;
13 +use IvyForms\Common\Exceptions\ValidationException;
14 +use IvyForms\Common\Sanitizer\Sanitizer;
15 +use IvyForms\Controllers\Controller;
16 +use IvyForms\Services\Field\FieldService;
17 +use IvyForms\Services\Form\FormService;
18 +use IvyForms\Services\Translations\BackendStrings;
19 +use WP_REST_Request;
20 +use WP_REST_Response;
21 +
22 +/**
23 + * Class UpdateFormController
24 + */
25 +class UpdateFormController extends Controller
26 +{
27 + private FormService $formService;
28 + private FieldService $fieldService;
29 +
30 + public function __construct(
31 + FormService $formService,
32 + FieldService $fieldService
33 + ) {
34 + $this->formService = $formService;
35 + $this->fieldService = $fieldService;
36 + }
37 +
38 + /**
39 + * @param WP_REST_Request<array<string,mixed>> $data
40 + * @return WP_REST_Response
41 + * @throws QueryExecutionException
42 + * @throws InvalidArgumentException|ValidationException|ForbiddenException
43 + */
44 + public function handle(WP_REST_Request $data): WP_REST_Response
45 + {
46 + Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
47 +
48 + if (empty($data->get_params())) {
49 + throw new InvalidArgumentException(
50 + BackendStrings::getExceptionStrings()['invalid_request_data']
51 + );
52 + }
53 +
54 + $formId = (int) $data->get_param('id');
55 +
56 + $params = Sanitizer::sanitizeFormData(
57 + $this->formService->withPreservedFormType($data->get_params(), $formId)
58 + );
59 +
60 + $this->fieldService->validateFieldsType($params['fields']);
61 +
62 + [$formId, $form] = $this->formService->updateFormOrFail($params);
63 + if (!$formId) {
64 + throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_update_form'] . '.');
65 + }
66 +
67 + $existingFieldIds = $this->fieldService->collectExistingFieldIds($formId);
68 + $submittedFieldIds = [];
69 +
70 + if (!empty($params['fields'])) {
71 + $submittedFieldIds = $this->fieldService->updateFieldsWithOptions(
72 + $params['fields'],
73 + $formId
74 + );
75 + }
76 +
77 + if (!empty($existingFieldIds)) {
78 + $idsToDelete = array_diff($existingFieldIds, $submittedFieldIds);
79 + if (!empty($idsToDelete)) {
80 + $this->fieldService->deleteFields($idsToDelete);
81 + }
82 + }
83 +
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');
92 +
93 + return new WP_REST_Response([
94 + 'message' => BackendStrings::getCommonStrings()['ok'],
95 + 'data' => $form->toArray()
96 + ], 200);
97 + }
98 +}