| 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\Form; |
| 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\QueryExecutionException; |
| 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 DeleteFormController |
| 31 |
* |
| 32 |
* @package IvyForms\Controllers\Form |
| 33 |
*/ |
| 34 |
class DeleteFormController extends Controller |
| 35 |
{ |
| 36 |
private FormService $formService; |
| 37 |
private FieldService $fieldService; |
| 38 |
private NotificationService $notificationService; |
| 39 |
private ConfirmationService $confirmationService; |
| 40 |
private EntryService $entryService; |
| 41 |
|
| 42 |
public function __construct( |
| 43 |
FormService $formService, |
| 44 |
FieldService $fieldService, |
| 45 |
NotificationService $notificationService, |
| 46 |
ConfirmationService $confirmationService, |
| 47 |
EntryService $entryService |
| 48 |
) { |
| 49 |
$this->formService = $formService; |
| 50 |
$this->fieldService = $fieldService; |
| 51 |
$this->notificationService = $notificationService; |
| 52 |
$this->confirmationService = $confirmationService; |
| 53 |
$this->entryService = $entryService; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @param WP_REST_Request $data |
| 58 |
* |
| 59 |
* @return WP_REST_Response |
| 60 |
* |
| 61 |
* @throws InvalidArgumentException |
| 62 |
* @throws ForbiddenException |
| 63 |
* @throws QueryExecutionException |
| 64 |
*/ |
| 65 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 66 |
{ |
| 67 |
// Verify the nonce |
| 68 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 69 |
|
| 70 |
// Check if the form ID is provided |
| 71 |
if (empty($data->get_param('id'))) { |
| 72 |
throw new InvalidArgumentException( |
| 73 |
BackendStrings::getExceptionStrings()['form_id_required'] |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
// Sanitize the input data |
| 78 |
$formId = Sanitizer::sanitizeId($data->get_param('id')); |
| 79 |
|
| 80 |
$this->entryService->getDeletionManager()->deleteEntriesByFormIds([$formId]); |
| 81 |
$this->confirmationService->deleteConfirmationsByFormIds([$formId]); |
| 82 |
$this->notificationService->deleteNotificationsByFormIds([$formId]); |
| 83 |
$this->fieldService->deleteFieldsByFormIds([$formId]); |
| 84 |
$this->formService->deleteForm($formId); |
| 85 |
|
| 86 |
return new WP_REST_Response([ |
| 87 |
'message' => BackendStrings::getCommonStrings()['ok'], |
| 88 |
'data' => [] |
| 89 |
], 200); |
| 90 |
} |
| 91 |
} |
| 92 |
|