← All changes
|
backend/src/Controllers/Entry/DeleteEntryController.php
+72
-0
0.6.1
→
1.5
View file →
| @@ -1,0 +1,72 @@ | ||
| 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\Entry; | |
| 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\Entry\EntryService; | |
| 17 | +use IvyForms\Services\Field\FieldService; | |
| 18 | +use IvyForms\Services\Form\FormService; | |
| 19 | +use IvyForms\Services\Notification\NotificationService; | |
| 20 | +use IvyForms\Services\Translations\BackendStrings; | |
| 21 | +use WP_REST_Request; | |
| 22 | +use WP_REST_Response; | |
| 23 | + | |
| 24 | +/** | |
| 25 | + * Class DeleteEntryController | |
| 26 | + * | |
| 27 | + * @package IvyForms\Application\Controllers\Entry | |
| 28 | + */ | |
| 29 | +class DeleteEntryController extends Controller | |
| 30 | +{ | |
| 31 | + private EntryService $entryService; | |
| 32 | + | |
| 33 | + public function __construct( | |
| 34 | + EntryService $entryService | |
| 35 | + ) { | |
| 36 | + $this->entryService = $entryService; | |
| 37 | + } | |
| 38 | + | |
| 39 | + /** | |
| 40 | + * @param WP_REST_Request $data | |
| 41 | + * | |
| 42 | + * @return WP_REST_Response | |
| 43 | + * | |
| 44 | + * @throws ForbiddenException | |
| 45 | + * @throws InvalidArgumentException | |
| 46 | + */ | |
| 47 | + public function handle(WP_REST_Request $data): WP_REST_Response | |
| 48 | + { | |
| 49 | + // Verify the nonce | |
| 50 | + Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); | |
| 51 | + | |
| 52 | + // Check if the entry ID is provided | |
| 53 | + if (empty($data->get_param('id'))) { | |
| 54 | + throw new InvalidArgumentException( | |
| 55 | + BackendStrings::getExceptionStrings()['entry_id_required'] | |
| 56 | + ); | |
| 57 | + } | |
| 58 | + $id = Sanitizer::sanitizeId($data->get_param('id')) ; | |
| 59 | + | |
| 60 | + // Bulk delete all entry fields for the entry | |
| 61 | + $this->entryService->getDeletionManager()->deleteEntryFieldsByEntryIds([$id]); | |
| 62 | + | |
| 63 | + // Delete the entry itself | |
| 64 | + $this->entryService->getDeletionManager()->deleteEntry($id); | |
| 65 | + | |
| 66 | + // Return a success response | |
| 67 | + return new WP_REST_Response([ | |
| 68 | + 'message' => BackendStrings::getCommonStrings()['ok'], | |
| 69 | + 'data' => null, | |
| 70 | + ], 200); | |
| 71 | + } | |
| 72 | +} | |