| 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\NotFoundException; |
| 13 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 14 |
use IvyForms\Controllers\Controller; |
| 15 |
use IvyForms\Services\Field\FieldService; |
| 16 |
use IvyForms\Services\Form\FormService; |
| 17 |
use WP_REST_Request; |
| 18 |
use WP_REST_Response; |
| 19 |
|
| 20 |
class DeleteFormsController extends Controller |
| 21 |
{ |
| 22 |
private FormService $formService; |
| 23 |
private FieldService $fieldService; |
| 24 |
|
| 25 |
public function __construct( |
| 26 |
FormService $formService, |
| 27 |
FieldService $fieldService |
| 28 |
) { |
| 29 |
$this->formService = $formService; |
| 30 |
$this->fieldService = $fieldService; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Delete forms by IDs |
| 35 |
* |
| 36 |
* @param WP_REST_Request $data |
| 37 |
* |
| 38 |
* @return WP_REST_Response |
| 39 |
* |
| 40 |
* @throws InvalidArgumentException |
| 41 |
* @throws NotFoundException |
| 42 |
* @throws ForbiddenException |
| 43 |
* |
| 44 |
*/ |
| 45 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 46 |
{ |
| 47 |
// Verify the nonce |
| 48 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 49 |
|
| 50 |
// Check if the form IDs are provided |
| 51 |
if (empty($data->get_params())) { |
| 52 |
throw new InvalidArgumentException('Invalid request data.'); |
| 53 |
} |
| 54 |
|
| 55 |
$ids = Sanitizer::sanitizeIds($data->get_param('ids')) ; |
| 56 |
|
| 57 |
foreach ($ids as $id) { |
| 58 |
$this->formService->deleteForm($id); |
| 59 |
$this->fieldService->deleteFieldsByFormId($id); |
| 60 |
} |
| 61 |
|
| 62 |
return new WP_REST_Response( |
| 63 |
[ |
| 64 |
'message' => 'Successfully deleted forms.', |
| 65 |
'data' => [], |
| 66 |
], |
| 67 |
200 |
| 68 |
); |
| 69 |
} |
| 70 |
} |
| 71 |
|