PluginProbe
The Innovative Form Builder – IvyForms / 1.4.1
The Innovative Form Builder – IvyForms v1.4.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
ivyforms / backend / src / Controllers / Form / GetFormController.php

GetFormController.php in The Innovative Form Builder – IvyForms 1.4.1, at backend/src/Controllers/Form/GetFormController.php

82 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\NotFoundException;
13 use IvyForms\Common\Exceptions\QueryExecutionException;
14 use IvyForms\Common\Exceptions\ValidationException;
15 use IvyForms\Common\Sanitizer\Sanitizer;
16 use IvyForms\Controllers\Controller;
17 use IvyForms\Services\Confirmation\ConfirmationService;
18 use IvyForms\Services\Field\FieldService;
19 use IvyForms\Services\Form\FormService;
20 use IvyForms\Services\Translations\BackendStrings;
21 use WP_REST_Request;
22 use WP_REST_Response;
23
24 /**
25 * Class GetFormController
26 *
27 * @package IvyForms\Controllers\Form
28 */
29 class GetFormController extends Controller
30 {
31 private FormService $formService;
32 private FieldService $fieldService;
33 private ConfirmationService $confirmationService;
34
35 public function __construct(
36 FormService $formService,
37 FieldService $fieldService,
38 ConfirmationService $confirmationService
39 ) {
40 $this->formService = $formService;
41 $this->fieldService = $fieldService;
42 $this->confirmationService = $confirmationService;
43 }
44
45 /**
46 * @param WP_REST_Request $data
47 * @return WP_REST_Response
48 * @throws NotFoundException
49 * @throws InvalidArgumentException|ForbiddenException|ValidationException
50 * @throws QueryExecutionException
51 */
52 public function handle(WP_REST_Request $data): WP_REST_Response
53 {
54 // Verify the nonce
55 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
56
57 // Check if the form ID is provided
58 if (empty($data->get_params())) {
59 throw new InvalidArgumentException(
60 BackendStrings::getExceptionStrings()['invalid_request_data']
61 );
62 }
63
64 // Sanitize the input data
65 $formId = Sanitizer::sanitizeId($data->get_param('id'));
66
67 $form = $this->formService->getFormById($formId);
68 $fieldsMap = $this->fieldService->getAllFieldsWithOptions($form->getId());
69 $form->setFields($fieldsMap);
70
71 $defaultConfirmationId = $this->confirmationService->ensureDefaultConfirmationExists($formId);
72
73 return new WP_REST_Response([
74 'message' => BackendStrings::getCommonStrings()['ok'],
75 'data' => array_merge($form->toArray(), [
76 'confirmationId' => $defaultConfirmationId,
77 'submissionNonce' => wp_create_nonce('ivyformsFrontSubmissionNonce_' . $formId),
78 ]),
79 ], 200);
80 }
81 }
82