| 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 |
// phpcs:disable PSR1.Files.SideEffects |
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
use IvyForms\Common\Exceptions\ForbiddenException; |
| 16 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 17 |
use IvyForms\Common\Exceptions\NotFoundException; |
| 18 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 19 |
use IvyForms\Controllers\Controller; |
| 20 |
use IvyForms\Services\Confirmation\ConfirmationService; |
| 21 |
use IvyForms\Services\Entry\EntryService; |
| 22 |
use IvyForms\Services\Field\FieldService; |
| 23 |
use IvyForms\Services\Form\FormService; |
| 24 |
use IvyForms\Services\Notification\NotificationService; |
| 25 |
use IvyForms\Services\Translations\BackendStrings; |
| 26 |
use WP_REST_Request; |
| 27 |
use WP_REST_Response; |
| 28 |
|
| 29 |
/** |
| 30 |
* Class DeleteEntryController |
| 31 |
* |
| 32 |
* @package IvyForms\Application\Controllers\Entry |
| 33 |
*/ |
| 34 |
class DeleteEntryController extends Controller |
| 35 |
{ |
| 36 |
private EntryService $entryService; |
| 37 |
|
| 38 |
public function __construct( |
| 39 |
EntryService $entryService |
| 40 |
) { |
| 41 |
$this->entryService = $entryService; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @param WP_REST_Request $data |
| 46 |
* |
| 47 |
* @return WP_REST_Response |
| 48 |
* |
| 49 |
* @throws ForbiddenException |
| 50 |
* @throws InvalidArgumentException |
| 51 |
*/ |
| 52 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 53 |
{ |
| 54 |
// Verify the nonce |
| 55 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 56 |
|
| 57 |
// Check if the entry ID is provided |
| 58 |
if (empty($data->get_param('id'))) { |
| 59 |
throw new InvalidArgumentException( |
| 60 |
BackendStrings::getExceptionStrings()['entry_id_required'] |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
$id = Sanitizer::sanitizeId($data->get_param('id')) ; |
| 66 |
|
| 67 |
// Bulk delete all entry fields for the entry |
| 68 |
$this->entryService->getDeletionManager()->deleteEntryFieldsByEntryIds([$id]); |
| 69 |
|
| 70 |
// Delete the entry itself |
| 71 |
$this->entryService->getDeletionManager()->deleteEntry($id); |
| 72 |
|
| 73 |
// Return a success response |
| 74 |
return new WP_REST_Response([ |
| 75 |
'message' => BackendStrings::getCommonStrings()['ok'], |
| 76 |
'data' => null, |
| 77 |
], 200); |
| 78 |
} |
| 79 |
} |
| 80 |
|