| 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\Entry\EntryService; |
| 21 |
use IvyForms\Services\Translations\BackendStrings; |
| 22 |
use WP_REST_Request; |
| 23 |
use WP_REST_Response; |
| 24 |
|
| 25 |
class DeleteEntriesController extends Controller |
| 26 |
{ |
| 27 |
private EntryService $entryService; |
| 28 |
|
| 29 |
public function __construct( |
| 30 |
EntryService $entryService |
| 31 |
) { |
| 32 |
$this->entryService = $entryService; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @param WP_REST_Request $data |
| 37 |
* |
| 38 |
* @return WP_REST_Response |
| 39 |
* @throws NotFoundException |
| 40 |
* @throws InvalidArgumentException |
| 41 |
* @throws ForbiddenException |
| 42 |
*/ |
| 43 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 44 |
{ |
| 45 |
// Verify the nonce |
| 46 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 47 |
|
| 48 |
// Check if the form IDs are provided |
| 49 |
if (empty($data->get_params())) { |
| 50 |
throw new InvalidArgumentException( |
| 51 |
BackendStrings::getExceptionStrings()['invalid_request_data'] |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
$ids = Sanitizer::sanitizeIds($data->get_param('ids')) ; |
| 56 |
|
| 57 |
// Bulk delete all entry fields for these entries |
| 58 |
$this->entryService->getDeletionManager()->deleteEntryFieldsByEntryIds($ids); |
| 59 |
// Bulk delete all entries |
| 60 |
$this->entryService->getDeletionManager()->deleteEntriesByIds($ids); |
| 61 |
|
| 62 |
// Return a success response |
| 63 |
return new WP_REST_Response([ |
| 64 |
'message' => BackendStrings::getCommonStrings()['ok'], |
| 65 |
'data' => null, |
| 66 |
], 200); |
| 67 |
} |
| 68 |
} |
| 69 |
|