PluginProbe
The Innovative Form Builder – IvyForms / 1.5
The Innovative Form Builder – IvyForms v1.5
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 / GravityFormsNameFieldMapper.php

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

156 lines 4.8 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 * Map Gravity Forms name fields into IvyForms compound name fields.
7 */
8 class GravityFormsNameFieldMapper
9 {
10 /**
11 * @param array<string, mixed> $rawField
12 * @param array<int, array<string, mixed>> $fields
13 * @param array<string, int> $context
14 * @return void
15 */
16 public static function append(array $rawField, array &$fields, array &$context): void
17 {
18 $parentIndex = $context['fieldIndex']++;
19 $required = !empty($rawField['isRequired']);
20 $fields[] = [
21 'id' => $context['id']++,
22 'fieldIndex' => $parentIndex,
23 'type' => 'name',
24 'label' => sanitize_text_field((string) ($rawField['label'] ?? '')),
25 'placeholder' => '',
26 'description' => sanitize_text_field((string) ($rawField['description'] ?? '')),
27 'required' => $required,
28 'defaultValue' => '',
29 'parentId' => null,
30 'position' => $context['position']++,
31 'rowIndex' => $context['rowIndex']++,
32 'columnIndex' => 0,
33 'width' => 100,
34 ];
35
36 $inputs = is_array($rawField['inputs'] ?? null) ? $rawField['inputs'] : [];
37 if (empty($inputs) || ($rawField['nameFormat'] ?? '') === 'simple') {
38 self::appendSimpleField($rawField, $fields, $context, $parentIndex, $required);
39 return;
40 }
41
42 self::appendAdvancedFields($inputs, $fields, $context, $parentIndex, $required);
43 }
44
45 /**
46 * @param array<string, mixed> $rawField
47 * @param array<int, array<string, mixed>> $fields
48 * @param array<string, int> $context
49 * @param int $parentIndex
50 * @param bool $required
51 * @return void
52 */
53 private static function appendSimpleField(
54 array $rawField,
55 array &$fields,
56 array &$context,
57 int $parentIndex,
58 bool $required
59 ): void {
60 self::appendChild(
61 [
62 'label' => $rawField['label'] ?? '',
63 'placeholder' => $rawField['placeholder'] ?? '',
64 'defaultValue' => $rawField['defaultValue'] ?? '',
65 ],
66 'nameField1',
67 1,
68 $fields,
69 $context,
70 $parentIndex,
71 $required
72 );
73 }
74
75 /**
76 * @param array<int, mixed> $inputs
77 * @param array<int, array<string, mixed>> $fields
78 * @param array<string, int> $context
79 * @param int $parentIndex
80 * @param bool $required
81 * @return void
82 */
83 private static function appendAdvancedFields(
84 array $inputs,
85 array &$fields,
86 array &$context,
87 int $parentIndex,
88 bool $required
89 ): void {
90 $nameTypeMap = [
91 '3' => 'nameField1',
92 '6' => 'nameField2',
93 '4' => 'nameField3',
94 ];
95 $position = 1;
96
97 foreach ($inputs as $input) {
98 if (!is_array($input) || !empty($input['isHidden'])) {
99 continue;
100 }
101
102 $inputIdParts = explode('.', (string) ($input['id'] ?? ''));
103 $suffix = (string) end($inputIdParts);
104 if (!isset($nameTypeMap[$suffix])) {
105 continue;
106 }
107
108 self::appendChild(
109 $input,
110 $nameTypeMap[$suffix],
111 $position++,
112 $fields,
113 $context,
114 $parentIndex,
115 $required
116 );
117 }
118 }
119
120 /**
121 * @param array<string, mixed> $input
122 * @param string $nameFieldType
123 * @param int $position
124 * @param array<int, array<string, mixed>> $fields
125 * @param array<string, int> $context
126 * @param int $parentIndex
127 * @param bool $required
128 * @return void
129 */
130 private static function appendChild(
131 array $input,
132 string $nameFieldType,
133 int $position,
134 array &$fields,
135 array &$context,
136 int $parentIndex,
137 bool $required
138 ): void {
139 $fields[] = [
140 'id' => $context['id']++,
141 'fieldIndex' => $parentIndex,
142 'type' => 'text',
143 'label' => sanitize_text_field((string) ($input['customLabel'] ?? $input['label'] ?? '')),
144 'placeholder' => sanitize_text_field((string) ($input['placeholder'] ?? '')),
145 'required' => $required,
146 'defaultValue' => sanitize_text_field((string) ($input['defaultValue'] ?? '')),
147 'parentId' => $parentIndex,
148 'position' => $position,
149 'settings' => wp_json_encode([
150 'nameFieldType' => $nameFieldType,
151 'subFieldIndex' => $position,
152 ]) ?: '',
153 ];
154 }
155 }
156