| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Controllers\Form; |
| 4 |
|
| 5 |
use IvyForms\Common\Exceptions\ForbiddenException; |
| 6 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 7 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 8 |
use IvyForms\Controllers\Controller; |
| 9 |
use IvyForms\Services\Form\FormImportExportService; |
| 10 |
use IvyForms\Services\Permissions\RoutePermissionService; |
| 11 |
use IvyForms\Services\Translations\BackendStrings; |
| 12 |
use IvyForms\ValueObjects\Form\FormExportOptions; |
| 13 |
use WP_REST_Request; |
| 14 |
use WP_REST_Response; |
| 15 |
|
| 16 |
class ExportFormController extends Controller |
| 17 |
{ |
| 18 |
private FormImportExportService $importExportService; |
| 19 |
private RoutePermissionService $routePermissionService; |
| 20 |
|
| 21 |
public function __construct( |
| 22 |
FormImportExportService $importExportService, |
| 23 |
RoutePermissionService $routePermissionService |
| 24 |
) { |
| 25 |
$this->importExportService = $importExportService; |
| 26 |
$this->routePermissionService = $routePermissionService; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Handle the export request |
| 31 |
* |
| 32 |
* @param WP_REST_Request<array<string, mixed>> $data |
| 33 |
* @return WP_REST_Response |
| 34 |
* @throws ForbiddenException |
| 35 |
* @throws InvalidArgumentException |
| 36 |
*/ |
| 37 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 38 |
{ |
| 39 |
// Verify the nonce |
| 40 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 41 |
|
| 42 |
// Get form IDs from request |
| 43 |
$formIds = $data->get_param('formIds'); |
| 44 |
|
| 45 |
if (empty($formIds)) { |
| 46 |
throw new InvalidArgumentException( |
| 47 |
BackendStrings::getExceptionStrings()['no_form_ids_for_export'] |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
// Sanitize and validate form IDs |
| 52 |
$formIds = array_values(array_unique(array_filter( |
| 53 |
array_map('intval', (array) $formIds), |
| 54 |
static fn (int $id): bool => $id > 0 |
| 55 |
))); |
| 56 |
|
| 57 |
if ($formIds === []) { |
| 58 |
throw new InvalidArgumentException( |
| 59 |
BackendStrings::getExceptionStrings()['no_form_ids_for_export'] |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
foreach ($formIds as $formId) { |
| 64 |
if (!$this->routePermissionService->canExportForms($formId)) { |
| 65 |
throw new ForbiddenException( |
| 66 |
BackendStrings::getExceptionStrings()['forbidden'] |
| 67 |
); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
$exportOptions = FormExportOptions::fromRequestParams( |
| 72 |
$data->get_param('includeStyles'), |
| 73 |
$data->get_param('includeEntries') |
| 74 |
); |
| 75 |
|
| 76 |
// Export forms using service |
| 77 |
$exportData = $this->importExportService->exportForms($formIds, $exportOptions); |
| 78 |
|
| 79 |
return new WP_REST_Response([ |
| 80 |
'success' => true, |
| 81 |
'data' => $exportData, |
| 82 |
'message' => BackendStrings::getCommonStrings()['forms_exported_successfully'] |
| 83 |
], 200); |
| 84 |
} |
| 85 |
} |
| 86 |
|