| @@ -1,17 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace IvyForms\Common\Helpers; |
| 4 | 4 | |
| 5 | -// phpcs:disable PSR1.Files.SideEffects | |
| 6 | -if (!defined('ABSPATH')) { | |
| 7 | - exit; // Exit if accessed directly | |
| 8 | -} | |
| 9 | - | |
| 10 | 5 | use IvyForms\Common\Exceptions\ValidationException; |
| 11 | 6 | use IvyForms\Entity\FieldOptions\FieldOptions; |
| 12 | 7 | use IvyForms\Factory\FieldOptions\FieldOptionsFactory; |
| 13 | 8 | use IvyForms\Repository\FieldOptions\FieldOptionsRepositoryInterface; |
| 9 | +use IvyForms\Services\Media\ImageService; | |
| 14 | 10 | |
| 15 | 11 | /** |
| 16 | 12 | * General helper utilities related to Field processing. |
| 17 | 13 | */ |
| @@ -29,12 +25,24 @@ | ||
| 29 | 25 | return; |
| 30 | 26 | } |
| 31 | 27 | |
| 32 | 28 | $parentId = $data['parentId']; |
| 33 | - $needsResolve = ($parentId === 0 || $parentId === '0' || $parentId === null); | |
| 34 | - if ($needsResolve && isset($data['fieldIndex']) && isset($parentMap[$data['fieldIndex']])) { | |
| 35 | - $data['parentId'] = $parentMap[$data['fieldIndex']]; | |
| 29 | + | |
| 30 | + //parentId is 0 or null → use current field's fieldIndex | |
| 31 | + if (($parentId === 0 || $parentId === '0' || $parentId === null) && isset($data['fieldIndex'])) { | |
| 32 | + $parentIndex = $data['fieldIndex']; | |
| 33 | + if (isset($parentMap[$parentIndex])) { | |
| 34 | + $data['parentId'] = $parentMap[$parentIndex]; | |
| 35 | + } | |
| 36 | + return; | |
| 36 | 37 | } |
| 38 | + | |
| 39 | + //parentId is numeric → use it as parent's fieldIndex | |
| 40 | + if (is_numeric($parentId)) { | |
| 41 | + if (isset($parentMap[$parentId])) { | |
| 42 | + $data['parentId'] = $parentMap[$parentId]; | |
| 43 | + } | |
| 44 | + } | |
| 37 | 45 | } |
| 38 | 46 | |
| 39 | 47 | /** |
| 40 | 48 | * Remap parentId for duplicated field using old-to-new field ID map. |
| @@ -92,12 +100,27 @@ | ||
| 92 | 100 | * @return bool |
| 93 | 101 | */ |
| 94 | 102 | public static function fieldTypeHasOptions(string $fieldType): bool |
| 95 | 103 | { |
| 96 | - return in_array($fieldType, ['radio', 'checkbox', 'select', 'multi-select'], true); | |
| 104 | + return in_array($fieldType, ['radio', 'checkbox', 'select', 'multi-select', 'product', 'rating'], true); | |
| 97 | 105 | } |
| 98 | 106 | |
| 99 | 107 | /** |
| 108 | + * Check if a field type is a security/CAPTCHA field. | |
| 109 | + * | |
| 110 | + * Security fields are used for bot/spam protection and should be excluded | |
| 111 | + * from stored entries, integration payloads, placeholder lists, and any | |
| 112 | + * other processing that targets user-submitted data. | |
| 113 | + * | |
| 114 | + * @param string $fieldType | |
| 115 | + * @return bool | |
| 116 | + */ | |
| 117 | + public static function isSecurityField(string $fieldType): bool | |
| 118 | + { | |
| 119 | + return in_array($fieldType, ['recaptcha', 'turnstile', 'hcaptcha'], true); | |
| 120 | + } | |
| 121 | + | |
| 122 | + /** | |
| 100 | 123 | * Duplicate field options from one field to another. |
| 101 | 124 | * |
| 102 | 125 | * @param int $oldFieldId |
| 103 | 126 | * @param int $newFieldId |
| @@ -116,6 +139,43 @@ | ||
| 116 | 139 | unset($optionData['id']); |
| 117 | 140 | $optionData['fieldId'] = $newFieldId; |
| 118 | 141 | $repository->add(FieldOptionsFactory::create($optionData)); |
| 119 | 142 | } |
| 143 | + } | |
| 144 | + | |
| 145 | + /** | |
| 146 | + * Process htmlContent to upload base64 images | |
| 147 | + * | |
| 148 | + * @param array<string, mixed> $fieldData Passed by reference; htmlContent modified in place | |
| 149 | + * @param ImageService $imageService | |
| 150 | + * @return void | |
| 151 | + */ | |
| 152 | + public static function processFieldHtmlContent(array &$fieldData, ImageService $imageService): void | |
| 153 | + { | |
| 154 | + if (!empty($fieldData['htmlContent'])) { | |
| 155 | + $processedContent = $imageService->processImagesInContent( | |
| 156 | + $fieldData['htmlContent'], | |
| 157 | + 'field' | |
| 158 | + ); | |
| 159 | + $fieldData['htmlContent'] = $processedContent; | |
| 160 | + } | |
| 161 | + } | |
| 162 | + | |
| 163 | + /** | |
| 164 | + * Upload base64 images in rich text default values before form save. | |
| 165 | + * | |
| 166 | + * @param array<string, mixed> $fieldData Passed by reference; defaultValue modified in place | |
| 167 | + * @param ImageService $imageService | |
| 168 | + * @return void | |
| 169 | + */ | |
| 170 | + public static function processRichTextDefaultValue(array &$fieldData, ImageService $imageService): void | |
| 171 | + { | |
| 172 | + if (($fieldData['type'] ?? '') !== 'rich_text' || empty($fieldData['defaultValue'])) { | |
| 173 | + return; | |
| 174 | + } | |
| 175 | + | |
| 176 | + $fieldData['defaultValue'] = $imageService->processImagesInContent( | |
| 177 | + (string) $fieldData['defaultValue'], | |
| 178 | + 'field' | |
| 179 | + ); | |
| 120 | 180 | } |
| 121 | 181 | } |