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 / Confirmation / AddConfirmationController.php

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

78 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Controllers\Confirmation;
4
5 use IvyForms\Common\Exceptions\ForbiddenException;
6 use IvyForms\Common\Exceptions\InvalidArgumentException;
7 use IvyForms\Common\Exceptions\QueryExecutionException;
8 use IvyForms\Common\Sanitizer\Sanitizer;
9 use IvyForms\Controllers\Controller;
10 use IvyForms\Factory\Confirmation\ConfirmationFactory;
11 use IvyForms\Services\Confirmation\ConfirmationService;
12 use IvyForms\Services\Translations\BackendStrings;
13 use WP_REST_Request;
14 use WP_REST_Response;
15
16 /**
17 * Class AddConfirmationController
18 *
19 * @package IvyForms\Controllers\Confirmation
20 */
21 class AddConfirmationController extends Controller
22 {
23 private ConfirmationService $confirmationService;
24
25 public function __construct(ConfirmationService $confirmationService)
26 {
27 $this->confirmationService = $confirmationService;
28 }
29
30 /**
31 * @param WP_REST_Request $data
32 *
33 * @return WP_REST_Response
34 *
35 * @throws QueryExecutionException
36 * @throws InvalidArgumentException
37 * @throws ForbiddenException
38 */
39 public function handle(WP_REST_Request $data): WP_REST_Response
40 {
41 // Verify the nonce
42 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
43
44 // Validate request data
45 if (empty($data->get_params())) {
46 throw new InvalidArgumentException(
47 BackendStrings::getExceptionStrings()['invalid_request_data']
48 );
49 }
50
51 // Sanitize the input data
52 $params = Sanitizer::sanitizeConfirmationData($data->get_params());
53
54 if (empty(trim($params['name'] ?? ''))) {
55 throw new InvalidArgumentException(
56 BackendStrings::getSettingsFormBuilderStrings()['confirmation_name_required']
57 );
58 }
59
60 $confirmation = ConfirmationFactory::create($params);
61
62 $confirmationId = $this->confirmationService->createConfirmation($confirmation);
63
64 if (!$confirmationId) {
65 throw new QueryExecutionException(
66 BackendStrings::getSettingsFormBuilderStrings()['failed_to_create_confirmation']
67 );
68 }
69
70 $confirmation->setId($confirmationId);
71
72 return new WP_REST_Response([
73 'message' => BackendStrings::getCommonStrings()['ok'],
74 'data' => $confirmation->toArray()
75 ], 200);
76 }
77 }
78