| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Controllers\Form; |
| 4 |
|
| 5 |
// phpcs:disable PSR1.Files.SideEffects |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; // Exit if accessed directly |
| 8 |
} |
| 9 |
|
| 10 |
use IvyForms\Common\Exceptions\ForbiddenException; |
| 11 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 12 |
use IvyForms\Common\Exceptions\QueryExecutionException; |
| 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 |
class DeleteFormsController extends Controller |
| 25 |
{ |
| 26 |
private FormService $formService; |
| 27 |
private FieldService $fieldService; |
| 28 |
private NotificationService $notificationService; |
| 29 |
private ConfirmationService $confirmationService; |
| 30 |
private EntryService $entryService; |
| 31 |
|
| 32 |
public function __construct( |
| 33 |
FormService $formService, |
| 34 |
FieldService $fieldService, |
| 35 |
NotificationService $notificationService, |
| 36 |
ConfirmationService $confirmationService, |
| 37 |
EntryService $entryService |
| 38 |
) { |
| 39 |
$this->formService = $formService; |
| 40 |
$this->fieldService = $fieldService; |
| 41 |
$this->notificationService = $notificationService; |
| 42 |
$this->confirmationService = $confirmationService; |
| 43 |
$this->entryService = $entryService; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Delete forms by IDs |
| 48 |
* |
| 49 |
* @param WP_REST_Request $data |
| 50 |
* |
| 51 |
* @return WP_REST_Response |
| 52 |
* |
| 53 |
* @throws InvalidArgumentException |
| 54 |
* @throws ForbiddenException |
| 55 |
* @throws QueryExecutionException |
| 56 |
*/ |
| 57 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 58 |
{ |
| 59 |
// Verify the nonce |
| 60 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 61 |
|
| 62 |
// Check if the form IDs are provided |
| 63 |
if (empty($data->get_params())) { |
| 64 |
throw new InvalidArgumentException( |
| 65 |
BackendStrings::getExceptionStrings()['invalid_request_data'] |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
$ids = Sanitizer::sanitizeIds($data->get_param('ids')); |
| 70 |
|
| 71 |
$this->entryService->getDeletionManager()->deleteEntriesByFormIds($ids); |
| 72 |
$this->confirmationService->deleteConfirmationsByFormIds($ids); |
| 73 |
$this->notificationService->deleteNotificationsByFormIds($ids); |
| 74 |
$this->fieldService->deleteFieldsByFormIds($ids); |
| 75 |
$this->formService->deleteForms($ids); |
| 76 |
|
| 77 |
return new WP_REST_Response( |
| 78 |
[ |
| 79 |
'message' => BackendStrings::getCommonStrings()['ok'], |
| 80 |
'data' => [], |
| 81 |
], |
| 82 |
200 |
| 83 |
); |
| 84 |
} |
| 85 |
} |
| 86 |
|