| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Common\Helpers\Import; |
| 4 |
|
| 5 |
/** |
| 6 |
* Apply WPForms type-specific settings to mapped IvyForms fields. |
| 7 |
*/ |
| 8 |
class WpFormsFieldSettingsMapper |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Resolve source settings that refine the direct type map. |
| 12 |
* |
| 13 |
* @param string $wpFormsType |
| 14 |
* @param string $ivyType |
| 15 |
* @param array<string, mixed> $rawField |
| 16 |
* @return string |
| 17 |
*/ |
| 18 |
public static function resolveType(string $wpFormsType, string $ivyType, array $rawField): string |
| 19 |
{ |
| 20 |
if ($wpFormsType === 'select' && !empty($rawField['multiple'])) { |
| 21 |
return 'multi-select'; |
| 22 |
} |
| 23 |
|
| 24 |
if ($wpFormsType === 'date-time' && ($rawField['format'] ?? '') === 'time') { |
| 25 |
return 'time'; |
| 26 |
} |
| 27 |
|
| 28 |
return $ivyType; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @param array<string, mixed> $field |
| 33 |
* @param array<string, mixed> $rawField |
| 34 |
* @param string $wpFormsType |
| 35 |
* @param string $ivyType |
| 36 |
* @param array<int, array<string, mixed>> $options |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public static function apply( |
| 40 |
array &$field, |
| 41 |
array $rawField, |
| 42 |
string $wpFormsType, |
| 43 |
string $ivyType, |
| 44 |
array $options |
| 45 |
): void { |
| 46 |
self::applySliderSettings($field, $rawField, $ivyType); |
| 47 |
self::applyGdprSettings($field, $rawField, $ivyType); |
| 48 |
self::applyDateTimeSettings($field, $rawField, $wpFormsType, $ivyType); |
| 49 |
self::applyFileSettings($field, $rawField, $ivyType); |
| 50 |
self::applyRichTextSettings($field, $rawField, $wpFormsType); |
| 51 |
self::applyPaymentSettings($field, $rawField, $wpFormsType, $ivyType, $options); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @param array<string, mixed> $field |
| 56 |
* @param array<string, mixed> $rawField |
| 57 |
* @param string $wpFormsType |
| 58 |
* @param string $ivyType |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
private static function applyDateTimeSettings( |
| 62 |
array &$field, |
| 63 |
array $rawField, |
| 64 |
string $wpFormsType, |
| 65 |
string $ivyType |
| 66 |
): void { |
| 67 |
if ($wpFormsType !== 'date-time') { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
$isTime = $ivyType === 'time'; |
| 72 |
$field['placeholder'] = sanitize_text_field( |
| 73 |
(string) ($rawField[$isTime ? 'time_placeholder' : 'date_placeholder'] ?? '') |
| 74 |
); |
| 75 |
if ($isTime) { |
| 76 |
$sourceFormat = (string) ($rawField['time_format'] ?? ''); |
| 77 |
$field['timeFieldType'] = 'time-picker'; |
| 78 |
$field['timeFormat'] = preg_match('/[aA]/', $sourceFormat) === 1 ? 'ampm' : '24h'; |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$dateFormats = [ |
| 83 |
'm/d/Y' => 'MM/DD/YYYY', |
| 84 |
'd/m/Y' => 'DD/MM/YYYY', |
| 85 |
'Y-m-d' => 'YYYY-MM-DD', |
| 86 |
'm.d.Y' => 'MM.DD.YYYY', |
| 87 |
'd.m.Y' => 'DD.MM.YYYY', |
| 88 |
]; |
| 89 |
$sourceFormat = (string) ($rawField['date_format'] ?? 'm/d/Y'); |
| 90 |
$field['dateFieldType'] = 'date-picker'; |
| 91 |
$field['dateFormat'] = $dateFormats[$sourceFormat] ?? 'MM/DD/YYYY'; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @param array<string, mixed> $field |
| 96 |
* @param array<string, mixed> $rawField |
| 97 |
* @param string $ivyType |
| 98 |
* @return void |
| 99 |
*/ |
| 100 |
private static function applyFileSettings(array &$field, array $rawField, string $ivyType): void |
| 101 |
{ |
| 102 |
if ($ivyType !== 'file-upload') { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
$extensions = preg_split('/[\s,]+/', (string) ($rawField['extensions'] ?? ''), -1, PREG_SPLIT_NO_EMPTY); |
| 107 |
$field['allowedFileExtensions'] = is_array($extensions) ? array_values($extensions) : []; |
| 108 |
$field['maxFileSizeMb'] = is_numeric($rawField['max_size'] ?? null) ? (int) $rawField['max_size'] : 0; |
| 109 |
$field['allowMultiple'] = !empty($rawField['max_file_number']); |
| 110 |
$field['maxUploadCount'] = is_numeric($rawField['max_file_number'] ?? null) |
| 111 |
? (int) $rawField['max_file_number'] |
| 112 |
: 0; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* @param array<string, mixed> $field |
| 117 |
* @param array<string, mixed> $rawField |
| 118 |
* @param string $wpFormsType |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
private static function applyRichTextSettings(array &$field, array $rawField, string $wpFormsType): void |
| 122 |
{ |
| 123 |
if ($wpFormsType !== 'richtext') { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
$field['defaultValue'] = wp_kses_post((string) ($rawField['content'] ?? '')); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* @param array<string, mixed> $field |
| 132 |
* @param array<string, mixed> $rawField |
| 133 |
* @param string $ivyType |
| 134 |
* @return void |
| 135 |
*/ |
| 136 |
private static function applySliderSettings(array &$field, array $rawField, string $ivyType): void |
| 137 |
{ |
| 138 |
if ($ivyType !== 'slider') { |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
$field['minValue'] = is_numeric($rawField['min'] ?? null) ? (float) $rawField['min'] : 0; |
| 143 |
$field['maxValue'] = is_numeric($rawField['max'] ?? null) ? (float) $rawField['max'] : 100; |
| 144 |
$field['step'] = is_numeric($rawField['step'] ?? null) ? (float) $rawField['step'] : 1; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* @param array<string, mixed> $field |
| 149 |
* @param array<string, mixed> $rawField |
| 150 |
* @param string $ivyType |
| 151 |
* @return void |
| 152 |
*/ |
| 153 |
private static function applyGdprSettings(array &$field, array $rawField, string $ivyType): void |
| 154 |
{ |
| 155 |
if ($ivyType !== 'gdpr') { |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
$rawOptions = $rawField['choices'] ?? []; |
| 160 |
$agreement = is_array($rawOptions) ? reset($rawOptions) : null; |
| 161 |
$field['agreementText'] = is_array($agreement) |
| 162 |
? sanitize_text_field((string) ($agreement['label'] ?? '')) |
| 163 |
: ''; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* @param array<string, mixed> $field |
| 168 |
* @param array<string, mixed> $rawField |
| 169 |
* @param string $wpFormsType |
| 170 |
* @param string $ivyType |
| 171 |
* @param array<int, array<string, mixed>> $options |
| 172 |
* @return void |
| 173 |
*/ |
| 174 |
private static function applyPaymentSettings( |
| 175 |
array &$field, |
| 176 |
array $rawField, |
| 177 |
string $wpFormsType, |
| 178 |
string $ivyType, |
| 179 |
array $options |
| 180 |
): void { |
| 181 |
if ($ivyType === 'product') { |
| 182 |
$field['htmlContent'] = self::buildProductSettings($rawField, $wpFormsType, $options); |
| 183 |
} |
| 184 |
|
| 185 |
if ($ivyType === 'total') { |
| 186 |
$field['readOnly'] = true; |
| 187 |
$field['htmlContent'] = self::totalSettings(); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* @param array<string, mixed> $rawField |
| 193 |
* @param string $wpFormsType |
| 194 |
* @param array<int, array<string, mixed>> $options |
| 195 |
* @return string |
| 196 |
*/ |
| 197 |
private static function buildProductSettings(array $rawField, string $wpFormsType, array $options): string |
| 198 |
{ |
| 199 |
$productTypeMap = [ |
| 200 |
'payment-checkbox' => 'checkbox', |
| 201 |
'payment-multiple' => 'radio', |
| 202 |
'payment-select' => 'select', |
| 203 |
]; |
| 204 |
$productType = $productTypeMap[$wpFormsType] ?? 'single'; |
| 205 |
if ($wpFormsType === 'payment-single' && ($rawField['format'] ?? '') === 'user_defined') { |
| 206 |
$productType = 'user_defined'; |
| 207 |
} |
| 208 |
|
| 209 |
$rawOptions = is_array($rawField['choices'] ?? null) ? array_values($rawField['choices']) : []; |
| 210 |
$prices = []; |
| 211 |
foreach ($options as $index => $option) { |
| 212 |
$rawOption = is_array($rawOptions[$index] ?? null) ? $rawOptions[$index] : []; |
| 213 |
$prices[(string) $option['value']] = sanitize_text_field((string) ($rawOption['value'] ?? '')); |
| 214 |
} |
| 215 |
|
| 216 |
return wp_json_encode([ |
| 217 |
'configVersion' => 1, |
| 218 |
'productType' => $productType, |
| 219 |
'paymentAmount' => sanitize_text_field((string) ($rawField['price'] ?? '')), |
| 220 |
'amountLabel' => sanitize_text_field((string) ($rawField['price_label'] ?? '')), |
| 221 |
'showPriceAfterItemLabel' => !empty($prices), |
| 222 |
'prices' => $prices, |
| 223 |
'choiceLayout' => 'inline', |
| 224 |
'columnsCount' => 3, |
| 225 |
], JSON_UNESCAPED_UNICODE) ?: ''; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* @return string |
| 230 |
*/ |
| 231 |
private static function totalSettings(): string |
| 232 |
{ |
| 233 |
return wp_json_encode([ |
| 234 |
'totalConfigVersion' => 1, |
| 235 |
'enablePaymentSummary' => false, |
| 236 |
'showPaymentSummaryCloseButton' => true, |
| 237 |
'emptyPaymentMessageHtml' => '', |
| 238 |
'enableAutocomplete' => false, |
| 239 |
], JSON_UNESCAPED_UNICODE) ?: ''; |
| 240 |
} |
| 241 |
} |
| 242 |
|