| 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\Entry\EntryService; |
| 16 |
use IvyForms\Services\Permissions\PermissionResourceResolver; |
| 17 |
use IvyForms\Services\Permissions\RoutePermissionService; |
| 18 |
use IvyForms\Services\Translations\BackendStrings; |
| 19 |
use WP_REST_Request; |
| 20 |
use WP_REST_Response; |
| 21 |
|
| 22 |
class DeleteEntriesController extends Controller |
| 23 |
{ |
| 24 |
private EntryService $entryService; |
| 25 |
|
| 26 |
private RoutePermissionService $routePermissionService; |
| 27 |
|
| 28 |
private PermissionResourceResolver $permissionResourceResolver; |
| 29 |
|
| 30 |
public function __construct( |
| 31 |
EntryService $entryService, |
| 32 |
RoutePermissionService $routePermissionService, |
| 33 |
PermissionResourceResolver $permissionResourceResolver |
| 34 |
) { |
| 35 |
$this->entryService = $entryService; |
| 36 |
$this->routePermissionService = $routePermissionService; |
| 37 |
$this->permissionResourceResolver = $permissionResourceResolver; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @param WP_REST_Request $data |
| 42 |
* |
| 43 |
* @return WP_REST_Response |
| 44 |
* @throws NotFoundException |
| 45 |
* @throws InvalidArgumentException |
| 46 |
* @throws ForbiddenException |
| 47 |
*/ |
| 48 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 49 |
{ |
| 50 |
// Verify the nonce |
| 51 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 52 |
|
| 53 |
// Check if the form IDs are provided |
| 54 |
if (empty($data->get_params())) { |
| 55 |
throw new InvalidArgumentException( |
| 56 |
BackendStrings::getExceptionStrings()['invalid_request_data'] |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
$ids = Sanitizer::sanitizeIds($data->get_param('ids')); |
| 61 |
if ($ids === []) { |
| 62 |
return new WP_REST_Response([ |
| 63 |
'success' => false, |
| 64 |
'message' => BackendStrings::getExceptionStrings()['invalid_or_missing_ids'], |
| 65 |
], 400); |
| 66 |
} |
| 67 |
|
| 68 |
$formIdsByEntryId = $this->permissionResourceResolver->formIdsFromEntryIds($ids); |
| 69 |
|
| 70 |
foreach ($ids as $entryId) { |
| 71 |
$id = (int) $entryId; |
| 72 |
if (!isset($formIdsByEntryId[$id])) { |
| 73 |
throw new ForbiddenException('forbidden'); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
foreach (array_unique(array_values($formIdsByEntryId)) as $formId) { |
| 78 |
if (!$this->routePermissionService->canDeleteEntries($formId)) { |
| 79 |
throw new ForbiddenException('forbidden'); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
// Bulk delete all entry fields for these entries |
| 84 |
$this->entryService->getDeletionManager()->deleteEntryFieldsByEntryIds($ids); |
| 85 |
// Bulk delete all entries |
| 86 |
$this->entryService->getDeletionManager()->deleteEntriesByIds($ids); |
| 87 |
|
| 88 |
// Return a success response |
| 89 |
return new WP_REST_Response([ |
| 90 |
'message' => BackendStrings::getCommonStrings()['ok'], |
| 91 |
'data' => null, |
| 92 |
], 200); |
| 93 |
} |
| 94 |
} |
| 95 |
|