PluginProbe
The Innovative Form Builder – IvyForms / 1.4
The Innovative Form Builder – IvyForms v1.4
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Common / Helpers / Import / FluentFormsImportConverter.php

FluentFormsImportConverter.php in The Innovative Form Builder – IvyForms 1.4, at backend/src/Common/Helpers/Import/FluentFormsImportConverter.php

214 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Common\Helpers\Import;
4
5 /**
6 * Convert a Fluent Forms JSON export into the IvyForms import payload structure.
7 *
8 * A Fluent Forms export may contain multiple forms. Unsupported elements
9 * (submit buttons, section breaks, payment fields, etc.) and platform-specific
10 * settings are skipped gracefully so the import never fails because of them.
11 */
12 class FluentFormsImportConverter
13 {
14 /**
15 * Convert a raw Fluent Forms export into an IvyForms import payload.
16 *
17 * @param array<string|int, mixed> $rawData
18 * @return array<string, mixed>
19 */
20 public static function convert(array $rawData): array
21 {
22 $forms = [];
23 foreach (self::normalizeToList($rawData) as $formRaw) {
24 if (is_array($formRaw)) {
25 $forms[] = self::convertSingleForm($formRaw);
26 }
27 }
28
29 return ['forms' => $forms];
30 }
31
32 /**
33 * Normalize the export into a list of form objects.
34 *
35 * @param array<string|int, mixed> $rawData
36 * @return array<int, mixed>
37 */
38 private static function normalizeToList(array $rawData): array
39 {
40 if (isset($rawData['form_fields'])) {
41 return [$rawData];
42 }
43
44 return array_values($rawData);
45 }
46
47 /**
48 * Convert a single Fluent Forms form object.
49 *
50 * @param array<string, mixed> $formRaw
51 * @return array<string, mixed>
52 */
53 private static function convertSingleForm(array $formRaw): array
54 {
55 $name = sanitize_text_field((string) ($formRaw['title'] ?? ''));
56 $published = (($formRaw['status'] ?? '') === 'published');
57
58 return [
59 'form' => [
60 'name' => $name,
61 'published' => $published,
62 'showTitle' => true,
63 'showDescription' => false,
64 'storeEntries' => true,
65 ],
66 'fields' => self::extractFields($formRaw),
67 'notifications' => [],
68 'confirmation' => self::extractConfirmation($formRaw),
69 ];
70 }
71
72 /**
73 * Map all Fluent Forms fields into IvyForms fields.
74 *
75 * @param array<string, mixed> $formRaw
76 * @return array<int, array<string, mixed>>
77 */
78 private static function extractFields(array $formRaw): array
79 {
80 $formFields = $formRaw['form_fields'] ?? [];
81 if (is_string($formFields)) {
82 $decoded = json_decode($formFields, true);
83 $formFields = is_array($decoded) ? $decoded : [];
84 }
85
86 $context = ['id' => 1, 'fieldIndex' => 1, 'position' => 1, 'rowIndex' => 0];
87 $fields = [];
88 foreach (($formFields['fields'] ?? []) as $rawField) {
89 if (is_array($rawField)) {
90 FluentFormsFieldMapper::append($rawField, $fields, $context);
91 }
92 }
93
94 return $fields;
95 }
96
97 /**
98 * Build the IvyForms confirmation from Fluent Forms form settings.
99 *
100 * @param array<string, mixed> $formRaw
101 * @return array<string, mixed>
102 */
103 private static function extractConfirmation(array $formRaw): array
104 {
105 $formSettings = self::findMetaValue($formRaw, 'formSettings');
106 $confirmation = is_array($formSettings['confirmation'] ?? null) ? $formSettings['confirmation'] : null;
107 if ($confirmation === null) {
108 return [];
109 }
110
111 $redirectTo = (string) ($confirmation['redirectTo'] ?? 'samePage');
112
113 if ($redirectTo === 'customUrl' && !empty($confirmation['customUrl'])) {
114 $url = esc_url_raw((string) $confirmation['customUrl']);
115 return self::redirectConfirmation('redirectToCustomUrl', 'url', $url);
116 }
117
118 if ($redirectTo === 'customPage' && !empty($confirmation['customPage'])) {
119 return self::redirectConfirmation('redirectToPage', 'page', (string) $confirmation['customPage']);
120 }
121
122 return self::messageConfirmation($confirmation);
123 }
124
125 /**
126 * Build a redirect-style confirmation payload.
127 *
128 * @param string $type
129 * @param string $targetKey 'url' or 'page'
130 * @param string $targetValue
131 * @return array<string, mixed>
132 */
133 private static function redirectConfirmation(string $type, string $targetKey, string $targetValue): array
134 {
135 return array_merge(
136 [
137 'type' => $type,
138 'enabled' => true,
139 'showForm' => false,
140 'message' => '',
141 'url' => '',
142 'page' => '',
143 ],
144 [$targetKey => $targetValue]
145 );
146 }
147
148 /**
149 * Build a success-message confirmation payload.
150 *
151 * @param array<string, mixed> $confirmation
152 * @return array<string, mixed>
153 */
154 private static function messageConfirmation(array $confirmation): array
155 {
156 $message = (string) ($confirmation['messageToShow'] ?? '');
157 if ($message === '') {
158 return [];
159 }
160
161 return [
162 'type' => 'successMessage',
163 'enabled' => true,
164 'showForm' => (($confirmation['samePageFormBehavior'] ?? '') !== 'hide_form'),
165 'message' => wp_kses_post($message),
166 'url' => '',
167 'page' => '',
168 ];
169 }
170
171 /**
172 * Find and decode a Fluent Forms meta value by key.
173 *
174 * @param array<string, mixed> $formRaw
175 * @param string $metaKey
176 * @return array<string, mixed>
177 */
178 private static function findMetaValue(array $formRaw, string $metaKey): array
179 {
180 foreach (['metas', 'form_meta'] as $metaSection) {
181 $metas = $formRaw[$metaSection] ?? null;
182 if (!is_array($metas)) {
183 continue;
184 }
185 foreach ($metas as $meta) {
186 if (is_array($meta) && ($meta['meta_key'] ?? '') === $metaKey) {
187 return self::decodeMetaValue($meta['value'] ?? null);
188 }
189 }
190 }
191
192 return [];
193 }
194
195 /**
196 * Decode a Fluent Forms meta value into an array.
197 *
198 * @param mixed $value
199 * @return array<string, mixed>
200 */
201 private static function decodeMetaValue($value): array
202 {
203 if (is_array($value)) {
204 return $value;
205 }
206 if (is_string($value)) {
207 $decoded = json_decode($value, true);
208 return is_array($decoded) ? $decoded : [];
209 }
210
211 return [];
212 }
213 }
214