PluginProbe
The Innovative Form Builder – IvyForms / 1.5
The Innovative Form Builder – IvyForms v1.5
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/DeleteFormsController.php +38 -21 0.2.1 → 1.5 View file →
@@ -1,22 +1,20 @@
1 1 <?php
2 2
3 3 namespace IvyForms\Controllers\Form;
4 4
5 -// phpcs:disable PSR1.Files.SideEffects
6 -if (!defined('ABSPATH')) {
7 - exit; // Exit if accessed directly
8 -}
9 -
10 5 use IvyForms\Common\Exceptions\ForbiddenException;
11 6 use IvyForms\Common\Exceptions\InvalidArgumentException;
12 -use IvyForms\Common\Exceptions\NotFoundException;
7 +use IvyForms\Common\Exceptions\QueryExecutionException;
13 8 use IvyForms\Common\Sanitizer\Sanitizer;
14 9 use IvyForms\Controllers\Controller;
15 10 use IvyForms\Services\Confirmation\ConfirmationService;
11 +use IvyForms\Services\Entry\EntryService;
16 12 use IvyForms\Services\Field\FieldService;
17 13 use IvyForms\Services\Form\FormService;
18 14 use IvyForms\Services\Notification\NotificationService;
15 +use IvyForms\Services\Permissions\RoutePermissionService;
16 +use IvyForms\Services\Translations\BackendStrings;
19 17 use WP_REST_Request;
20 18 use WP_REST_Response;
21 19
22 20 class DeleteFormsController extends Controller
@@ -24,19 +22,26 @@
24 22 private FormService $formService;
25 23 private FieldService $fieldService;
26 24 private NotificationService $notificationService;
27 25 private ConfirmationService $confirmationService;
26 + private EntryService $entryService;
28 27
28 + private RoutePermissionService $routePermissionService;
29 +
29 30 public function __construct(
30 31 FormService $formService,
31 32 FieldService $fieldService,
32 33 NotificationService $notificationService,
33 - ConfirmationService $confirmationService
34 + ConfirmationService $confirmationService,
35 + EntryService $entryService,
36 + RoutePermissionService $routePermissionService
34 37 ) {
35 - $this->formService = $formService;
36 - $this->fieldService = $fieldService;
37 - $this->notificationService = $notificationService;
38 - $this->confirmationService = $confirmationService;
38 + $this->formService = $formService;
39 + $this->fieldService = $fieldService;
40 + $this->notificationService = $notificationService;
41 + $this->confirmationService = $confirmationService;
42 + $this->entryService = $entryService;
43 + $this->routePermissionService = $routePermissionService;
39 44 }
40 45
41 46 /**
42 47 * Delete forms by IDs
@@ -45,11 +50,10 @@
45 50 *
46 51 * @return WP_REST_Response
47 52 *
48 53 * @throws InvalidArgumentException
49 - * @throws NotFoundException
50 54 * @throws ForbiddenException
51 - *
55 + * @throws QueryExecutionException
52 56 */
53 57 public function handle(WP_REST_Request $data): WP_REST_Response
54 58 {
55 59 // Verify the nonce
@@ -56,23 +60,36 @@
56 60 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
57 61
58 62 // Check if the form IDs are provided
59 63 if (empty($data->get_params())) {
60 - throw new InvalidArgumentException('Invalid request data.');
64 + throw new InvalidArgumentException(
65 + BackendStrings::getExceptionStrings()['invalid_request_data']
66 + );
61 67 }
62 68
63 - $ids = Sanitizer::sanitizeIds($data->get_param('ids')) ;
69 + $ids = Sanitizer::sanitizeIds($data->get_param('ids'));
70 + if ($ids === []) {
71 + return new WP_REST_Response([
72 + 'success' => false,
73 + 'message' => BackendStrings::getExceptionStrings()['invalid_or_missing_ids'],
74 + ], 400);
75 + }
64 76
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);
77 + foreach ($ids as $formId) {
78 + if (!$this->routePermissionService->canDeleteForms((int) $formId)) {
79 + throw new ForbiddenException('forbidden');
80 + }
70 81 }
71 82
83 + $this->entryService->getDeletionManager()->deleteEntriesByFormIds($ids);
84 + $this->confirmationService->deleteConfirmationsByFormIds($ids);
85 + $this->notificationService->deleteNotificationsByFormIds($ids);
86 + $this->fieldService->deleteFieldsByFormIds($ids);
87 + $this->formService->deleteForms($ids);
88 +
72 89 return new WP_REST_Response(
73 90 [
74 - 'message' => 'Successfully deleted forms.',
91 + 'message' => BackendStrings::getCommonStrings()['ok'],
75 92 'data' => [],
76 93 ],
77 94 200
78 95 );