PluginProbe
The Innovative Form Builder – IvyForms / 1.1
The Innovative Form Builder – IvyForms v1.1
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Controllers / Form / ImportFormController.php

ImportFormController.php in The Innovative Form Builder – IvyForms 1.1, at backend/src/Controllers/Form/ImportFormController.php

52 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Helpers\FormImportValidator;
8 use IvyForms\Common\Sanitizer\Sanitizer;
9 use IvyForms\Controllers\Controller;
10 use IvyForms\Services\Form\FormImportExportService;
11 use IvyForms\Services\Translations\BackendStrings;
12 use WP_REST_Request;
13 use WP_REST_Response;
14
15 class ImportFormController extends Controller
16 {
17 private FormImportExportService $importExportService;
18
19 public function __construct(FormImportExportService $importExportService)
20 {
21 $this->importExportService = $importExportService;
22 }
23
24 /**
25 * Handle the import request
26 *
27 * @param WP_REST_Request<array<string, mixed>> $data
28 * @return WP_REST_Response
29 * @throws ForbiddenException
30 * @throws InvalidArgumentException
31 */
32 public function handle(WP_REST_Request $data): WP_REST_Response
33 {
34 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
35 $formsData = FormImportValidator::validateImportPayload($data->get_param('importData'));
36
37 // Import forms using service
38 $importedForms = $this->importExportService->importForms($formsData);
39 if (empty($importedForms)) {
40 throw new InvalidArgumentException(
41 BackendStrings::getCommonStrings()['no_forms_imported']
42 );
43 }
44
45 return new WP_REST_Response([
46 'success' => true,
47 'data' => $importedForms,
48 'message' => BackendStrings::getCommonStrings()['forms_imported_successfully']
49 ], 200);
50 }
51 }
52