← All changes
|
backend/src/Controllers/Form/DeleteFormsController.php
+44
-17
0.1.2
→
1.5
View file →
| @@ -1,20 +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; |
| 10 | +use IvyForms\Services\Confirmation\ConfirmationService; | |
| 11 | +use IvyForms\Services\Entry\EntryService; | |
| 15 | 12 | use IvyForms\Services\Field\FieldService; |
| 16 | 13 | use IvyForms\Services\Form\FormService; |
| 14 | +use IvyForms\Services\Notification\NotificationService; | |
| 15 | +use IvyForms\Services\Permissions\RoutePermissionService; | |
| 16 | +use IvyForms\Services\Translations\BackendStrings; | |
| 17 | 17 | use WP_REST_Request; |
| 18 | 18 | use WP_REST_Response; |
| 19 | 19 | |
| 20 | 20 | class DeleteFormsController extends Controller |
| @@ -20,15 +20,28 @@ | ||
| 20 | 20 | class DeleteFormsController extends Controller |
| 21 | 21 | { |
| 22 | 22 | private FormService $formService; |
| 23 | 23 | private FieldService $fieldService; |
| 24 | + private NotificationService $notificationService; | |
| 25 | + private ConfirmationService $confirmationService; | |
| 26 | + private EntryService $entryService; | |
| 24 | 27 | |
| 28 | + private RoutePermissionService $routePermissionService; | |
| 29 | + | |
| 25 | 30 | public function __construct( |
| 26 | 31 | FormService $formService, |
| 27 | - FieldService $fieldService | |
| 32 | + FieldService $fieldService, | |
| 33 | + NotificationService $notificationService, | |
| 34 | + ConfirmationService $confirmationService, | |
| 35 | + EntryService $entryService, | |
| 36 | + RoutePermissionService $routePermissionService | |
| 28 | 37 | ) { |
| 29 | - $this->formService = $formService; | |
| 30 | - $this->fieldService = $fieldService; | |
| 38 | + $this->formService = $formService; | |
| 39 | + $this->fieldService = $fieldService; | |
| 40 | + $this->notificationService = $notificationService; | |
| 41 | + $this->confirmationService = $confirmationService; | |
| 42 | + $this->entryService = $entryService; | |
| 43 | + $this->routePermissionService = $routePermissionService; | |
| 31 | 44 | } |
| 32 | 45 | |
| 33 | 46 | /** |
| 34 | 47 | * Delete forms by IDs |
| @@ -37,11 +50,10 @@ | ||
| 37 | 50 | * |
| 38 | 51 | * @return WP_REST_Response |
| 39 | 52 | * |
| 40 | 53 | * @throws InvalidArgumentException |
| 41 | - * @throws NotFoundException | |
| 42 | 54 | * @throws ForbiddenException |
| 43 | - * | |
| 55 | + * @throws QueryExecutionException | |
| 44 | 56 | */ |
| 45 | 57 | public function handle(WP_REST_Request $data): WP_REST_Response |
| 46 | 58 | { |
| 47 | 59 | // Verify the nonce |
| @@ -48,21 +60,36 @@ | ||
| 48 | 60 | Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 49 | 61 | |
| 50 | 62 | // Check if the form IDs are provided |
| 51 | 63 | if (empty($data->get_params())) { |
| 52 | - throw new InvalidArgumentException('Invalid request data.'); | |
| 64 | + throw new InvalidArgumentException( | |
| 65 | + BackendStrings::getExceptionStrings()['invalid_request_data'] | |
| 66 | + ); | |
| 53 | 67 | } |
| 54 | 68 | |
| 55 | - $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 | + } | |
| 56 | 76 | |
| 57 | - foreach ($ids as $id) { | |
| 58 | - $this->formService->deleteForm($id); | |
| 59 | - $this->fieldService->deleteFieldsByFormId($id); | |
| 77 | + foreach ($ids as $formId) { | |
| 78 | + if (!$this->routePermissionService->canDeleteForms((int) $formId)) { | |
| 79 | + throw new ForbiddenException('forbidden'); | |
| 80 | + } | |
| 60 | 81 | } |
| 61 | 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 | + | |
| 62 | 89 | return new WP_REST_Response( |
| 63 | 90 | [ |
| 64 | - 'message' => 'Successfully deleted forms.', | |
| 91 | + 'message' => BackendStrings::getCommonStrings()['ok'], | |
| 65 | 92 | 'data' => [], |
| 66 | 93 | ], |
| 67 | 94 | 200 |
| 68 | 95 | ); |