PluginProbe
The Innovative Form Builder – IvyForms / 1.5.1
The Innovative Form Builder – IvyForms v1.5.1
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 / WpFormsNameFieldMapper.php

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

84 lines 2.9 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 WPForms name fields into IvyForms compound name fields.
7 */
8 class WpFormsNameFieldMapper
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['required']);
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 self::appendSubFields($rawField, $fields, $context, $parentIndex, $required);
37 }
38
39 /**
40 * @param array<string, mixed> $rawField
41 * @param array<int, array<string, mixed>> $fields
42 * @param array<string, int> $context
43 * @param int $parentIndex
44 * @param bool $required
45 * @return void
46 */
47 private static function appendSubFields(
48 array $rawField,
49 array &$fields,
50 array &$context,
51 int $parentIndex,
52 bool $required
53 ): void {
54 $format = (string) ($rawField['format'] ?? 'first-last');
55 $parts = $format === 'simple'
56 ? [['simple', 'nameField1']]
57 : [['first', 'nameField1'], ['last', 'nameField2']];
58
59 if ($format === 'first-middle-last') {
60 array_splice($parts, 1, 0, [['middle', 'nameField3']]);
61 }
62
63 foreach ($parts as $position => $part) {
64 [$key, $nameFieldType] = $part;
65 $subFieldIndex = $position + 1;
66 $fields[] = [
67 'id' => $context['id']++,
68 'fieldIndex' => $parentIndex,
69 'type' => 'text',
70 'label' => sanitize_text_field(ucfirst($key)),
71 'placeholder' => sanitize_text_field((string) ($rawField[$key . '_placeholder'] ?? '')),
72 'required' => $key === 'middle' ? false : $required,
73 'defaultValue' => sanitize_text_field((string) ($rawField[$key . '_default'] ?? '')),
74 'parentId' => $parentIndex,
75 'position' => $subFieldIndex,
76 'settings' => wp_json_encode([
77 'nameFieldType' => $nameFieldType,
78 'subFieldIndex' => $subFieldIndex,
79 ]) ?: '',
80 ];
81 }
82 }
83 }
84