PluginProbe
The Innovative Form Builder – IvyForms / 0.2.1
The Innovative Form Builder – IvyForms v0.2.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 / DeleteFormsController.php

DeleteFormsController.php in The Innovative Form Builder – IvyForms 0.2.1, at backend/src/Controllers/Form/DeleteFormsController.php

81 lines 2.4 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\Form;
4
5 // phpcs:disable PSR1.Files.SideEffects
6 if (!defined('ABSPATH')) {
7 exit; // Exit if accessed directly
8 }
9
10 use IvyForms\Common\Exceptions\ForbiddenException;
11 use IvyForms\Common\Exceptions\InvalidArgumentException;
12 use IvyForms\Common\Exceptions\NotFoundException;
13 use IvyForms\Common\Sanitizer\Sanitizer;
14 use IvyForms\Controllers\Controller;
15 use IvyForms\Services\Confirmation\ConfirmationService;
16 use IvyForms\Services\Field\FieldService;
17 use IvyForms\Services\Form\FormService;
18 use IvyForms\Services\Notification\NotificationService;
19 use WP_REST_Request;
20 use WP_REST_Response;
21
22 class DeleteFormsController extends Controller
23 {
24 private FormService $formService;
25 private FieldService $fieldService;
26 private NotificationService $notificationService;
27 private ConfirmationService $confirmationService;
28
29 public function __construct(
30 FormService $formService,
31 FieldService $fieldService,
32 NotificationService $notificationService,
33 ConfirmationService $confirmationService
34 ) {
35 $this->formService = $formService;
36 $this->fieldService = $fieldService;
37 $this->notificationService = $notificationService;
38 $this->confirmationService = $confirmationService;
39 }
40
41 /**
42 * Delete forms by IDs
43 *
44 * @param WP_REST_Request $data
45 *
46 * @return WP_REST_Response
47 *
48 * @throws InvalidArgumentException
49 * @throws NotFoundException
50 * @throws ForbiddenException
51 *
52 */
53 public function handle(WP_REST_Request $data): WP_REST_Response
54 {
55 // Verify the nonce
56 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
57
58 // Check if the form IDs are provided
59 if (empty($data->get_params())) {
60 throw new InvalidArgumentException('Invalid request data.');
61 }
62
63 $ids = Sanitizer::sanitizeIds($data->get_param('ids')) ;
64
65 foreach ($ids as $id) {
66 $this->formService->deleteForm($id);
67 $this->fieldService->deleteFieldsByFormId($id);
68 $this->notificationService->deleteNotificationsByFormId($id);
69 $this->confirmationService->deleteConfirmationsByFormId($id);
70 }
71
72 return new WP_REST_Response(
73 [
74 'message' => 'Successfully deleted forms.',
75 'data' => [],
76 ],
77 200
78 );
79 }
80 }
81