| @@ -1,21 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * @copyright © TMS-Plugins. All rights reserved. | |
| 4 | + * @copyright © Melograno Venture Studio. All rights reserved. | |
| 5 | 5 | * @licence See COPYING.md for license details. |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace IvyForms\Controllers\Form; |
| 9 | 9 | |
| 10 | -// phpcs:disable PSR1.Files.SideEffects | |
| 11 | -if (!defined('ABSPATH')) { | |
| 12 | - exit; // Exit if accessed directly | |
| 13 | -} | |
| 14 | - | |
| 15 | 10 | use IvyForms\Common\Exceptions\ForbiddenException; |
| 16 | 11 | use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 17 | 12 | use IvyForms\Common\Exceptions\NotFoundException; |
| 13 | +use IvyForms\Common\Exceptions\QueryExecutionException; | |
| 14 | +use IvyForms\Common\Exceptions\ValidationException; | |
| 18 | 15 | use IvyForms\Common\Sanitizer\Sanitizer; |
| 19 | 16 | use IvyForms\Controllers\Controller; |
| 20 | 17 | use IvyForms\Services\Confirmation\ConfirmationService; |
| 21 | 18 | use IvyForms\Services\Field\FieldService; |
| @@ -33,25 +30,25 @@ | ||
| 33 | 30 | { |
| 34 | 31 | private FormService $formService; |
| 35 | 32 | private FieldService $fieldService; |
| 36 | 33 | private ConfirmationService $confirmationService; |
| 34 | + | |
| 37 | 35 | public function __construct( |
| 38 | 36 | FormService $formService, |
| 39 | 37 | FieldService $fieldService, |
| 40 | 38 | ConfirmationService $confirmationService |
| 41 | 39 | ) { |
| 42 | - $this->formService = $formService; | |
| 43 | - $this->fieldService = $fieldService; | |
| 40 | + $this->formService = $formService; | |
| 41 | + $this->fieldService = $fieldService; | |
| 44 | 42 | $this->confirmationService = $confirmationService; |
| 45 | 43 | } |
| 46 | 44 | |
| 47 | 45 | /** |
| 48 | 46 | * @param WP_REST_Request $data |
| 49 | - * | |
| 50 | 47 | * @return WP_REST_Response |
| 51 | - * | |
| 52 | 48 | * @throws NotFoundException |
| 53 | - * @throws InvalidArgumentException|ForbiddenException | |
| 49 | + * @throws InvalidArgumentException|ForbiddenException|ValidationException | |
| 50 | + * @throws QueryExecutionException | |
| 54 | 51 | */ |
| 55 | 52 | public function handle(WP_REST_Request $data): WP_REST_Response |
| 56 | 53 | { |
| 57 | 54 | // Verify the nonce |
| @@ -56,39 +53,29 @@ | ||
| 56 | 53 | { |
| 57 | 54 | // Verify the nonce |
| 58 | 55 | Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 59 | 56 | |
| 60 | - // Check if the form ID is provided | |
| 57 | + // Check if the form ID is provided | |
| 61 | 58 | if (empty($data->get_params())) { |
| 62 | 59 | throw new InvalidArgumentException( |
| 63 | 60 | BackendStrings::getExceptionStrings()['invalid_request_data'] |
| 64 | 61 | ); |
| 65 | 62 | } |
| 63 | + | |
| 66 | 64 | // Sanitize the input data |
| 67 | 65 | $formId = Sanitizer::sanitizeId($data->get_param('id')); |
| 68 | 66 | |
| 69 | - $form = $this->formService->getFormById($formId); | |
| 67 | + $form = $this->formService->getFormById($formId); | |
| 68 | + $fieldsMap = $this->fieldService->getAllFieldsWithOptions($form->getId()); | |
| 69 | + $form->setFields($fieldsMap); | |
| 70 | 70 | |
| 71 | - $fields = $this->fieldService->getAllFields($form->getId()); | |
| 71 | + $defaultConfirmationId = $this->confirmationService->ensureDefaultConfirmationExists($formId); | |
| 72 | 72 | |
| 73 | - $fieldsMap = []; | |
| 74 | - | |
| 75 | - $confirmationsArr = $this->confirmationService->getConfirmationsById($formId); | |
| 76 | - | |
| 77 | - $confirmations = []; | |
| 78 | - | |
| 79 | - foreach ($confirmationsArr as $confirmation) { | |
| 80 | - $confirmations[] = $confirmation->toArray(); | |
| 81 | - } | |
| 82 | - | |
| 83 | - foreach ($fields as $field) { | |
| 84 | - $fieldsMap[] = $field->toArray(); | |
| 85 | - } | |
| 86 | - | |
| 87 | - $form->setFields($fieldsMap); | |
| 88 | - | |
| 89 | 73 | return new WP_REST_Response([ |
| 90 | 74 | 'message' => BackendStrings::getCommonStrings()['ok'], |
| 91 | - 'data' => array_merge($form->toArray(), ['confirmationId' => $confirmations[0]['id']]) | |
| 75 | + 'data' => array_merge($form->toArray(), [ | |
| 76 | + 'confirmationId' => $defaultConfirmationId, | |
| 77 | + 'submissionNonce' => wp_create_nonce('ivyformsFrontSubmissionNonce_' . $formId), | |
| 78 | + ]), | |
| 92 | 79 | ], 200); |
| 93 | 80 | } |
| 94 | 81 | } |