| @@ -1,18 +1,13 @@ | ||
| 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\QueryExecutionException; |
| 18 | 13 | use IvyForms\Common\Exceptions\ValidationException; |
| @@ -17,18 +12,13 @@ | ||
| 17 | 12 | use IvyForms\Common\Exceptions\QueryExecutionException; |
| 18 | 13 | use IvyForms\Common\Exceptions\ValidationException; |
| 19 | 14 | use IvyForms\Common\Sanitizer\Sanitizer; |
| 20 | 15 | use IvyForms\Controllers\Controller; |
| 21 | -use IvyForms\Factory\Field\FieldFactory; | |
| 22 | -use IvyForms\Factory\Form\FormFactory; | |
| 23 | -use IvyForms\Factory\Notification\NotificationFactory; | |
| 24 | -use IvyForms\Factory\Confirmation\ConfirmationFactory; | |
| 25 | -use IvyForms\Services\Field\FieldService; | |
| 26 | -use IvyForms\Services\Form\FormService; | |
| 27 | -use IvyForms\Services\Notification\NotificationService; | |
| 28 | -use IvyForms\Services\Confirmation\ConfirmationService; | |
| 16 | +use IvyForms\Services\Form\FormCreateService; | |
| 17 | +use IvyForms\Services\Template\TemplateService; | |
| 29 | 18 | use WP_REST_Request; |
| 30 | 19 | use WP_REST_Response; |
| 20 | +use IvyForms\Services\Translations\BackendStrings; | |
| 31 | 21 | |
| 32 | 22 | /** |
| 33 | 23 | * Class AddFormController |
| 34 | 24 | * |
| @@ -35,27 +25,21 @@ | ||
| 35 | 25 | * @package IvyForms\Controllers\Form |
| 36 | 26 | */ |
| 37 | 27 | class AddFormController extends Controller |
| 38 | 28 | { |
| 39 | - private FormService $formService; | |
| 40 | - private FieldService $fieldService; | |
| 41 | - private NotificationService $notificationService; | |
| 42 | - private ConfirmationService $confirmationService; | |
| 29 | + private FormCreateService $formCreateService; | |
| 30 | + private TemplateService $templateService; | |
| 43 | 31 | |
| 44 | 32 | public function __construct( |
| 45 | - FormService $formService, | |
| 46 | - FieldService $fieldService, | |
| 47 | - NotificationService $notificationService, | |
| 48 | - ConfirmationService $confirmationService | |
| 33 | + FormCreateService $formCreateService, | |
| 34 | + TemplateService $templateService | |
| 49 | 35 | ) { |
| 50 | - $this->formService = $formService; | |
| 51 | - $this->fieldService = $fieldService; | |
| 52 | - $this->notificationService = $notificationService; | |
| 53 | - $this->confirmationService = $confirmationService; | |
| 36 | + $this->formCreateService = $formCreateService; | |
| 37 | + $this->templateService = $templateService; | |
| 54 | 38 | } |
| 55 | 39 | |
| 56 | 40 | /** |
| 57 | - * @param WP_REST_Request $data | |
| 41 | + * @param WP_REST_Request<array<string, mixed>> $data | |
| 58 | 42 | * |
| 59 | 43 | * @return WP_REST_Response |
| 60 | 44 | * |
| 61 | 45 | * @throws QueryExecutionException |
| @@ -64,82 +48,68 @@ | ||
| 64 | 48 | * @throws ForbiddenException |
| 65 | 49 | */ |
| 66 | 50 | public function handle(WP_REST_Request $data): WP_REST_Response |
| 67 | 51 | { |
| 68 | - // Verify the nonce | |
| 69 | 52 | Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 70 | 53 | |
| 71 | - // Check if the form data is provided | |
| 72 | 54 | if (empty($data->get_params())) { |
| 73 | - throw new InvalidArgumentException('Invalid request data.'); | |
| 55 | + throw new InvalidArgumentException( | |
| 56 | + BackendStrings::getExceptionStrings()['invalid_request_data'] | |
| 57 | + ); | |
| 74 | 58 | } |
| 75 | 59 | |
| 76 | - // Sanitize the input data | |
| 77 | - $params = Sanitizer::sanitizeFormData($data->get_params()); | |
| 60 | + $templateId = sanitize_text_field($data->get_param('template_id')); | |
| 78 | 61 | |
| 79 | - $form = FormFactory::create($params); | |
| 80 | - | |
| 81 | - $formId = $this->formService->createForm($form); | |
| 82 | - | |
| 83 | - if (!$formId) { | |
| 84 | - throw new QueryExecutionException('Failed to create form.'); | |
| 62 | + $mergedParams = $data->get_params(); | |
| 63 | + $formData = []; | |
| 64 | + if (!empty($templateId)) { | |
| 65 | + $formData = $this->templateService->getFormDataFromTemplate($templateId); | |
| 85 | 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 | + ]); | |
| 86 | 87 | |
| 87 | - // Create default notification | |
| 88 | - $adminEmail = get_option('admin_email'); | |
| 89 | - | |
| 90 | - $defaultNotification = [ | |
| 91 | - 'formId' => $formId, | |
| 92 | - 'name' => 'Admin Notification Email', | |
| 93 | - 'sender' => $adminEmail, | |
| 94 | - 'receiver' => $adminEmail, | |
| 95 | - 'status' => 1, | |
| 96 | - 'subject' => $params['name'], | |
| 97 | - 'message' => '{all_data}', | |
| 98 | - 'smartLogic' => 0, | |
| 88 | + $templateLockedKeys = [ | |
| 89 | + 'fields', | |
| 90 | + 'integrationSettings', | |
| 91 | + 'styleSettings', | |
| 92 | + 'formActionButtons', | |
| 99 | 93 | ]; |
| 100 | 94 | |
| 101 | - $notification = NotificationFactory::create($defaultNotification); | |
| 102 | - $notificationId = $this->notificationService->createNotification($notification); | |
| 95 | + foreach ($mergeableKeys as $key) { | |
| 96 | + if (!array_key_exists($key, $mergedParams)) { | |
| 97 | + continue; | |
| 98 | + } | |
| 103 | 99 | |
| 104 | - if (!$notificationId) { | |
| 105 | - throw new QueryExecutionException('Failed to create default notification.'); | |
| 106 | - } | |
| 100 | + if (!empty($templateId) && in_array($key, $templateLockedKeys, true)) { | |
| 101 | + continue; | |
| 102 | + } | |
| 107 | 103 | |
| 108 | - $notification->setId($notificationId); | |
| 109 | - | |
| 110 | - $defaultConfirmation = [ | |
| 111 | - 'formId' => $formId, | |
| 112 | - 'type' => 'successMessage', | |
| 113 | - 'status' => 1, | |
| 114 | - 'showForm' => 0, | |
| 115 | - 'message' => 'Thanks for reaching out! We’ve received your message and will get back to you shortly.', | |
| 116 | - 'url' => '', | |
| 117 | - 'page' => '', | |
| 118 | - ]; | |
| 119 | - | |
| 120 | - $confirmation = ConfirmationFactory::create($defaultConfirmation); | |
| 121 | - $confirmationId = $this->confirmationService->createConfirmation($confirmation); | |
| 122 | - | |
| 123 | - if (!$confirmationId) { | |
| 124 | - throw new QueryExecutionException('Failed to create confirmation.'); | |
| 104 | + $formData[$key] = $mergedParams[$key]; | |
| 125 | 105 | } |
| 126 | 106 | |
| 127 | - $confirmation->setId($confirmationId); | |
| 107 | + $params = Sanitizer::sanitizeFormData($formData); | |
| 108 | + $created = $this->formCreateService->createFromSanitizedParams($params); | |
| 128 | 109 | |
| 129 | - // Save form fields | |
| 130 | - if (!empty($params['fields'])) { | |
| 131 | - foreach ($params['fields'] as $fieldData) { | |
| 132 | - $fieldData['formId'] = $fieldData['formId'] ?? $formId; | |
| 133 | - $field = FieldFactory::create($fieldData); | |
| 134 | - $this->fieldService->addField($field); | |
| 135 | - } | |
| 136 | - } | |
| 137 | - | |
| 138 | - $form->setId($formId); | |
| 139 | - | |
| 140 | 110 | return new WP_REST_Response([ |
| 141 | - 'message' => 'OK', | |
| 142 | - 'data' => array_merge($form->toArray(), ['confirmationId' => $confirmationId]) | |
| 111 | + 'message' => BackendStrings::getCommonStrings()['ok'], | |
| 112 | + 'data' => $created, | |
| 143 | 113 | ], 200); |
| 144 | 114 | } |
| 145 | 115 | } |