| @@ -1,0 +1,115 @@ | ||
| 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 | +use IvyForms\Common\Exceptions\ForbiddenException; | |
| 11 | +use IvyForms\Common\Exceptions\InvalidArgumentException; | |
| 12 | +use IvyForms\Common\Exceptions\QueryExecutionException; | |
| 13 | +use IvyForms\Common\Exceptions\ValidationException; | |
| 14 | +use IvyForms\Common\Sanitizer\Sanitizer; | |
| 15 | +use IvyForms\Controllers\Controller; | |
| 16 | +use IvyForms\Services\Form\FormCreateService; | |
| 17 | +use IvyForms\Services\Template\TemplateService; | |
| 18 | +use WP_REST_Request; | |
| 19 | +use WP_REST_Response; | |
| 20 | +use IvyForms\Services\Translations\BackendStrings; | |
| 21 | + | |
| 22 | +/** | |
| 23 | + * Class AddFormController | |
| 24 | + * | |
| 25 | + * @package IvyForms\Controllers\Form | |
| 26 | + */ | |
| 27 | +class AddFormController extends Controller | |
| 28 | +{ | |
| 29 | + private FormCreateService $formCreateService; | |
| 30 | + private TemplateService $templateService; | |
| 31 | + | |
| 32 | + public function __construct( | |
| 33 | + FormCreateService $formCreateService, | |
| 34 | + TemplateService $templateService | |
| 35 | + ) { | |
| 36 | + $this->formCreateService = $formCreateService; | |
| 37 | + $this->templateService = $templateService; | |
| 38 | + } | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * @param WP_REST_Request<array<string, mixed>> $data | |
| 42 | + * | |
| 43 | + * @return WP_REST_Response | |
| 44 | + * | |
| 45 | + * @throws QueryExecutionException | |
| 46 | + * @throws InvalidArgumentException | |
| 47 | + * @throws ValidationException | |
| 48 | + * @throws ForbiddenException | |
| 49 | + */ | |
| 50 | + public function handle(WP_REST_Request $data): WP_REST_Response | |
| 51 | + { | |
| 52 | + Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); | |
| 53 | + | |
| 54 | + if (empty($data->get_params())) { | |
| 55 | + throw new InvalidArgumentException( | |
| 56 | + BackendStrings::getExceptionStrings()['invalid_request_data'] | |
| 57 | + ); | |
| 58 | + } | |
| 59 | + | |
| 60 | + $templateId = sanitize_text_field($data->get_param('template_id')); | |
| 61 | + | |
| 62 | + $mergedParams = $data->get_params(); | |
| 63 | + $formData = []; | |
| 64 | + if (!empty($templateId)) { | |
| 65 | + $formData = $this->templateService->getFormDataFromTemplate($templateId); | |
| 66 | + } | |
| 67 | + /** | |
| 68 | + * Keys the client may override on create. When creating from a template, field | |
| 69 | + * structure and layout always come from TemplateService — not the builder store. | |
| 70 | + * Merging frontend fields caused duplicate name/address rows and single-row layout. | |
| 71 | + * | |
| 72 | + * @param list<string> $keys | |
| 73 | + */ | |
| 74 | + $mergeableKeys = apply_filters('ivyforms/form/add/mergeable_keys', [ | |
| 75 | + 'name', | |
| 76 | + 'description', | |
| 77 | + 'published', | |
| 78 | + 'showTitle', | |
| 79 | + 'showDescription', | |
| 80 | + 'storeEntries', | |
| 81 | + 'fields', | |
| 82 | + 'integrationSettings', | |
| 83 | + 'styleSettings', | |
| 84 | + 'formActionButtons', | |
| 85 | + 'formType', | |
| 86 | + ]); | |
| 87 | + | |
| 88 | + $templateLockedKeys = [ | |
| 89 | + 'fields', | |
| 90 | + 'integrationSettings', | |
| 91 | + 'styleSettings', | |
| 92 | + 'formActionButtons', | |
| 93 | + ]; | |
| 94 | + | |
| 95 | + foreach ($mergeableKeys as $key) { | |
| 96 | + if (!array_key_exists($key, $mergedParams)) { | |
| 97 | + continue; | |
| 98 | + } | |
| 99 | + | |
| 100 | + if (!empty($templateId) && in_array($key, $templateLockedKeys, true)) { | |
| 101 | + continue; | |
| 102 | + } | |
| 103 | + | |
| 104 | + $formData[$key] = $mergedParams[$key]; | |
| 105 | + } | |
| 106 | + | |
| 107 | + $params = Sanitizer::sanitizeFormData($formData); | |
| 108 | + $created = $this->formCreateService->createFromSanitizedParams($params); | |
| 109 | + | |
| 110 | + return new WP_REST_Response([ | |
| 111 | + 'message' => BackendStrings::getCommonStrings()['ok'], | |
| 112 | + 'data' => $created, | |
| 113 | + ], 200); | |
| 114 | + } | |
| 115 | +} | |