| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Common\Helpers\Import; |
| 4 |
|
| 5 |
/** |
| 6 |
* Apply Gravity Forms type-specific settings to mapped IvyForms fields. |
| 7 |
*/ |
| 8 |
class GravityFormsFieldSettingsMapper |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Resolve fields whose rendered input type is configurable. |
| 12 |
* |
| 13 |
* @param string $gravityType |
| 14 |
* @param string $ivyType |
| 15 |
* @param array<string, mixed> $rawField |
| 16 |
* @return string |
| 17 |
*/ |
| 18 |
public static function resolveType(string $gravityType, string $ivyType, array $rawField): string |
| 19 |
{ |
| 20 |
if (in_array($gravityType, ['multi_choice', 'image_choice'], true)) { |
| 21 |
return ($rawField['inputType'] ?? 'radio') === 'checkbox' ? 'checkbox' : 'radio'; |
| 22 |
} |
| 23 |
|
| 24 |
return $ivyType; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* @param array<int, mixed> $confirmations |
| 29 |
* @return array<string, mixed>|null |
| 30 |
*/ |
| 31 |
public static function findDefaultConfirmation(array $confirmations): ?array |
| 32 |
{ |
| 33 |
foreach ($confirmations as $confirmation) { |
| 34 |
if ( |
| 35 |
is_array($confirmation) |
| 36 |
&& !empty($confirmation['isDefault']) |
| 37 |
&& empty($confirmation['event']) |
| 38 |
) { |
| 39 |
return $confirmation; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
foreach ($confirmations as $confirmation) { |
| 44 |
if (is_array($confirmation) && empty($confirmation['event'])) { |
| 45 |
return $confirmation; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return null; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* @param array<string, mixed> $field |
| 54 |
* @param array<string, mixed> $rawField |
| 55 |
* @param string $gravityType |
| 56 |
* @param string $ivyType |
| 57 |
* @param array<int, array<string, mixed>> $options |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
public static function apply( |
| 61 |
array &$field, |
| 62 |
array $rawField, |
| 63 |
string $gravityType, |
| 64 |
string $ivyType, |
| 65 |
array $options |
| 66 |
): void { |
| 67 |
if ($ivyType === 'gdpr') { |
| 68 |
$field['agreementText'] = sanitize_text_field((string) ($rawField['checkboxLabel'] ?? '')); |
| 69 |
} |
| 70 |
|
| 71 |
self::applyDateTimeSettings($field, $rawField, $ivyType); |
| 72 |
|
| 73 |
if ($ivyType === 'product') { |
| 74 |
$field['htmlContent'] = self::productSettings($rawField, $gravityType, $options); |
| 75 |
} |
| 76 |
|
| 77 |
if ($ivyType === 'quantity') { |
| 78 |
$field['minValue'] = 1; |
| 79 |
$field['step'] = 1; |
| 80 |
$field['limitRange'] = true; |
| 81 |
$field['numberFormat'] = 'us_int'; |
| 82 |
$field['htmlContent'] = wp_json_encode([ |
| 83 |
'quantityConfigVersion' => 1, |
| 84 |
'linkedProductFieldIndex' => null, |
| 85 |
], JSON_UNESCAPED_UNICODE) ?: ''; |
| 86 |
} |
| 87 |
|
| 88 |
if ($ivyType === 'total') { |
| 89 |
$field['readOnly'] = true; |
| 90 |
$field['htmlContent'] = wp_json_encode([ |
| 91 |
'totalConfigVersion' => 1, |
| 92 |
'enablePaymentSummary' => false, |
| 93 |
'showPaymentSummaryCloseButton' => true, |
| 94 |
'emptyPaymentMessageHtml' => '', |
| 95 |
'enableAutocomplete' => false, |
| 96 |
], JSON_UNESCAPED_UNICODE) ?: ''; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @param array<string, mixed> $field |
| 102 |
* @param array<string, mixed> $rawField |
| 103 |
* @param string $ivyType |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
private static function applyDateTimeSettings(array &$field, array $rawField, string $ivyType): void |
| 107 |
{ |
| 108 |
if ($ivyType === 'time') { |
| 109 |
$field['timeFieldType'] = 'time-picker'; |
| 110 |
$field['timeFormat'] = (string) ($rawField['timeFormat'] ?? '12') === '24' ? '24h' : 'ampm'; |
| 111 |
} |
| 112 |
|
| 113 |
if ($ivyType !== 'date') { |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
$dateFormats = [ |
| 118 |
'mdy' => 'MM/DD/YYYY', |
| 119 |
'dmy' => 'DD/MM/YYYY', |
| 120 |
'dmy_dash' => 'DD-MM-YYYY', |
| 121 |
'dmy_dot' => 'DD.MM.YYYY', |
| 122 |
'ymd_slash' => 'YYYY/MM/DD', |
| 123 |
'ymd_dash' => 'YYYY-MM-DD', |
| 124 |
'ymd_dot' => 'YYYY.MM.DD', |
| 125 |
]; |
| 126 |
$field['dateFieldType'] = 'date-picker'; |
| 127 |
$field['dateFormat'] = $dateFormats[(string) ($rawField['dateFormat'] ?? 'mdy')] ?? 'MM/DD/YYYY'; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* @param array<string, mixed> $rawField |
| 132 |
* @param string $gravityType |
| 133 |
* @param array<int, array<string, mixed>> $options |
| 134 |
* @return string |
| 135 |
*/ |
| 136 |
private static function productSettings(array $rawField, string $gravityType, array $options): string |
| 137 |
{ |
| 138 |
$inputType = (string) ($rawField['inputType'] ?? $gravityType); |
| 139 |
$productTypeMap = [ |
| 140 |
'checkbox' => 'checkbox', |
| 141 |
'radio' => 'radio', |
| 142 |
'select' => 'select', |
| 143 |
'donation' => 'user_defined', |
| 144 |
'singleproduct' => 'single', |
| 145 |
]; |
| 146 |
$productType = $productTypeMap[$inputType] ?? 'single'; |
| 147 |
$rawOptions = is_array($rawField['choices'] ?? null) ? array_values($rawField['choices']) : []; |
| 148 |
$prices = []; |
| 149 |
|
| 150 |
foreach ($options as $index => $option) { |
| 151 |
$rawOption = is_array($rawOptions[$index] ?? null) ? $rawOptions[$index] : []; |
| 152 |
$prices[(string) $option['value']] = sanitize_text_field((string) ($rawOption['price'] ?? '')); |
| 153 |
} |
| 154 |
|
| 155 |
return wp_json_encode([ |
| 156 |
'configVersion' => 1, |
| 157 |
'productType' => $productType, |
| 158 |
'paymentAmount' => sanitize_text_field((string) ($rawField['basePrice'] ?? '')), |
| 159 |
'amountLabel' => '', |
| 160 |
'showPriceAfterItemLabel' => !empty($prices), |
| 161 |
'prices' => $prices, |
| 162 |
'choiceLayout' => 'inline', |
| 163 |
'columnsCount' => 3, |
| 164 |
], JSON_UNESCAPED_UNICODE) ?: ''; |
| 165 |
} |
| 166 |
} |
| 167 |
|