PluginProbe
The Innovative Form Builder – IvyForms / 0.8
The Innovative Form Builder – IvyForms v0.8
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 1.2 1.3
ivyforms / backend / src / Controllers / Form / DuplicateFormController.php

DuplicateFormController.php in The Innovative Form Builder – IvyForms 0.8, at backend/src/Controllers/Form/DuplicateFormController.php

92 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\NotFoundException;
18 use IvyForms\Common\Exceptions\QueryExecutionException;
19 use IvyForms\Common\Exceptions\ValidationException;
20 use IvyForms\Common\Sanitizer\Sanitizer;
21 use IvyForms\Controllers\Controller;
22 use IvyForms\Factory\Form\FormFactory;
23 use IvyForms\Services\Confirmation\ConfirmationService;
24 use IvyForms\Services\Field\FieldService;
25 use IvyForms\Services\Form\FormService;
26 use IvyForms\Services\Notification\NotificationService;
27 use IvyForms\Services\Translations\BackendStrings;
28 use WP_REST_Request;
29 use WP_REST_Response;
30
31 /**
32 * Class DuplicateFormController
33 *
34 * @package IvyForms\Controllers\Form
35 */
36 class DuplicateFormController extends Controller
37 {
38 private FormService $formService;
39 private FieldService $fieldService;
40 private NotificationService $notificationService;
41 private ConfirmationService $confirmationService;
42
43 public function __construct(
44 FormService $formService,
45 FieldService $fieldService,
46 NotificationService $notificationService,
47 ConfirmationService $confirmationService
48 ) {
49 $this->formService = $formService;
50 $this->fieldService = $fieldService;
51 $this->notificationService = $notificationService;
52 $this->confirmationService = $confirmationService;
53 }
54
55 /**
56 * @param WP_REST_Request $data
57 *
58 * @return WP_REST_Response
59 *
60 * @throws QueryExecutionException
61 * @throws InvalidArgumentException
62 * @throws NotFoundException
63 * @throws ValidationException
64 * @throws ForbiddenException
65 */
66 public function handle(WP_REST_Request $data): WP_REST_Response
67 {
68 // Verify the nonce
69 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
70
71 // Check if the form ID is provided
72 if (empty($data->get_params())) {
73 throw new InvalidArgumentException(
74 BackendStrings::getExceptionStrings()['invalid_request_data']
75 );
76 }
77
78 $formId = Sanitizer::sanitizeId($data->get_param('id')) ;
79
80 $newFormId = $this->formService->duplicateForm($formId);
81
82 $this->fieldService->duplicateFields($formId, $newFormId);
83 $this->notificationService->duplicateNotifications($formId, $newFormId);
84 $this->confirmationService->duplicateConfirmations($formId, $newFormId);
85
86 return new WP_REST_Response([
87 'message' => BackendStrings::getCommonStrings()['ok'],
88 'data' => $this->formService->getFormById($newFormId)->toArray()
89 ], 200);
90 }
91 }
92