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
← All changes | backend/src/Factory/Field/FieldFactory.php +109 -11 0.9.1 → 1.5.1 View file →
@@ -1,19 +1,16 @@
1 1 <?php
2 2
3 3 namespace IvyForms\Factory\Field;
4 4
5 -// phpcs:disable PSR1.Files.SideEffects
6 -if (!defined('ABSPATH')) {
7 - exit; // Exit if accessed directly
8 -}
9 -
10 5 use IvyForms\Common\Exceptions\ValidationException;
6 +use IvyForms\Common\Helpers\NameFieldKeyHelper;
11 7 use IvyForms\Entity\Field\Field as FieldEntity;
12 8 use IvyForms\ValueObjects\Field\Field;
13 9 use IvyForms\ValueObjects\Field\FieldGeneralSettings;
14 10 use IvyForms\ValueObjects\Field\FieldOptions;
15 11 use IvyForms\ValueObjects\Field\FieldAdvancedSettings;
12 +use IvyForms\ValueObjects\Field\FileUploadFieldAdvancedSettings;
16 13 use IvyForms\ValueObjects\Field\ConfirmationGeneralSettings;
17 14 use IvyForms\ValueObjects\Field\TimeFieldGeneralSettings;
18 15 use IvyForms\ValueObjects\Field\DateFieldGeneralSettings;
19 16
@@ -32,9 +29,9 @@
32 29 * @throws ValidationException
33 30 */
34 31 public static function create(array $data): FieldEntity
35 32 {
36 - $settings = isset($data['settings']) ? json_decode($data['settings'], true) : [];
33 + $settings = self::parseSettings($data['settings'] ?? null);
37 34
38 35 if (!empty($settings)) {
39 36 $data['hideLabel'] = $settings['hideLabel'] ?? false;
40 37 $data['readOnly'] = $settings['readOnly'] ?? false;
@@ -58,8 +55,9 @@
58 55 $data['phoneAutoDetect'] = $settings['phoneAutoDetect'] ?? false;
59 56 $data['minValue'] = $settings['minValue'] ?? null;
60 57 $data['maxValue'] = $settings['maxValue'] ?? null;
61 58 $data['step'] = $settings['step'] ?? 1;
59 + $data['showGrid'] = $settings['showGrid'] ?? false;
62 60 $data['numberFormat'] = $settings['numberFormat'] ?? '';
63 61 $data['inputPrefix'] = $settings['inputPrefix'] ?? '';
64 62 $data['inputSuffix'] = $settings['inputSuffix'] ?? '';
65 63 $data['timeFieldType'] = $settings['timeFieldType'] ?? '';
@@ -72,10 +70,18 @@
72 70 $data['maxDateValue'] = $settings['maxDateValue'] ?? '';
73 71 $data['showRatingText'] = $settings['showRatingText'] ?? false;
74 72 $data['ratingIcon'] = $settings['ratingIcon'] ?? 'star';
75 73 $data['htmlContent'] = $settings['htmlContent'] ?? '';
74 + $data['agreementText'] = $settings['agreementText'] ?? '';
76 75 $data['limitDateRange'] = $settings['limitDateRange'] ?? false;
77 76 $data['limitTimeRange'] = $settings['limitTimeRange'] ?? false;
77 + $data['maxFileSizeMb'] = isset($settings['maxFileSizeMb']) ? (int) $settings['maxFileSizeMb'] : 0;
78 + $data['saveUploadsTo'] = isset($settings['saveUploadsTo'])
79 + ? (string) $settings['saveUploadsTo']
80 + : 'ivyforms';
81 + if (!array_key_exists('allowedFileExtensions', $data) && isset($settings['allowedFileExtensions'])) {
82 + $data['allowedFileExtensions'] = $settings['allowedFileExtensions'];
83 + }
78 84 // Grid layout: prioritize direct database columns over settings JSON
79 85 // This ensures backward compatibility while preferring the dedicated columns
80 86 $data['rowIndex'] = $data['rowIndex'] ?? ($settings['rowIndex'] ?? 0);
81 87 $data['columnIndex'] = $data['columnIndex'] ?? ($settings['columnIndex'] ?? 0);
@@ -85,10 +91,34 @@
85 91 : true;
86 92 if ($data['visible'] === null) {
87 93 $data['visible'] = true;
88 94 }
95 + if (isset($settings['nameFieldType'])) {
96 + $data['nameFieldType'] = (string)$settings['nameFieldType'];
97 + }
98 + if (array_key_exists('subFieldIndex', $settings)) {
99 + $data['subFieldIndex'] = is_numeric($settings['subFieldIndex'])
100 + ? (int)$settings['subFieldIndex']
101 + : null;
102 + }
89 103 }
90 104
105 + if (isset($data['nameFieldType'])) {
106 + $data['nameFieldType'] = (string)$data['nameFieldType'];
107 + }
108 + if (array_key_exists('subFieldIndex', $data)) {
109 + $data['subFieldIndex'] = is_numeric($data['subFieldIndex'])
110 + ? (int)$data['subFieldIndex']
111 + : null;
112 + }
113 +
114 + if (empty($data['nameFieldType'])) {
115 + $inferredNameFieldType = NameFieldKeyHelper::inferNameFieldKeyFromLabel($data['label'] ?? '');
116 + if ($inferredNameFieldType !== null) {
117 + $data['nameFieldType'] = $inferredNameFieldType;
118 + }
119 + }
120 +
91 121 /**
92 122 * Allows modification of the settings data before creating the Field entity.
93 123 * This filter runs for both:
94 124 * - Fields loaded from database (settings from JSON string)
@@ -99,8 +129,9 @@
99 129 * @param mixed[] $settings The settings array (from database or empty for API).
100 130 * @return mixed[] The modified field data.
101 131 */
102 132 $data = apply_filters('ivyforms/field/factory/extract_settings', $data, $settings);
133 +
103 134 $generalSettings = new FieldGeneralSettings(
104 135 self::validateString($data['label'] ?? '', 255, 'label'),
105 136 self::validateBool($data['required'] ?? false),
106 137 self::validateString($data['placeholder'] ?? '', 255, 'placeholder'),
@@ -105,9 +136,9 @@
105 136 self::validateBool($data['required'] ?? false),
106 137 self::validateString($data['placeholder'] ?? '', 255, 'placeholder'),
107 138 self::validateBool($data['hideLabel'] ?? false),
108 139 self::validateBool($data['readOnly'] ?? false),
109 - self::validateString($data['description'] ?? '', 255, 'description'),
140 + self::validateString($data['description'] ?? '', 1000, 'description'),
110 141 self::validateString($data['requiredMessage'] ?? '', 255, 'requiredMessage'),
111 142 self::validateString($data['cssClasses'] ?? '', 255, 'cssClasses'),
112 143 self::validateBool($data['shuffleOptions'] ?? false),
113 144 self::validateBool($data['showValues'] ?? false),
@@ -116,8 +147,9 @@
116 147 self::validateBool($data['phoneAutoDetect'] ?? false),
117 148 self::validateNumberValue($data['minValue'] ?? null, 'minValue'),
118 149 self::validateNumberValue($data['maxValue'] ?? null, 'maxValue'),
119 150 self::validateStep($data['step'] ?? 1),
151 + self::validateBool($data['showGrid'] ?? false),
120 152 self::validateNumberFormat($data['numberFormat'] ?? ''),
121 153 new ConfirmationGeneralSettings(
122 154 self::validateBool($data['confirmFieldEnabled'] ?? false),
123 155 self::validateString(
@@ -152,12 +184,13 @@
152 184 ),
153 185 self::validateBool($data['showRatingText'] ?? false),
154 186 self::validateBool($data['visible'] ?? true),
155 187 self::validateString($data['htmlContent'] ?? '', 4294967295, 'htmlContent'),
188 + self::validateString($data['agreementText'] ?? '', 1000, 'agreementText'),
156 189 self::validateBool($data['limitRange'] ?? false),
157 190 );
158 191 $advancedSettings = new FieldAdvancedSettings(
159 - self::validateString($data['defaultValue'] ?? '', 255, 'defaultValue'),
192 + self::resolveDefaultValue($data),
160 193 self::validateBool($data['limitMaxLength'] ?? false),
161 194 (int)($data['maxLength'] ?? 255),
162 195 self::validateString($data['labelPosition'] ?? '', 255, 'labelPosition'),
163 196 self::validateBool($data['noDuplicates'] ?? false),
@@ -162,9 +195,16 @@
162 195 self::validateString($data['labelPosition'] ?? '', 255, 'labelPosition'),
163 196 self::validateBool($data['noDuplicates'] ?? false),
164 197 self::validateString($data['inputPrefix'] ?? '', 255, 'inputPrefix'),
165 198 self::validateString($data['inputSuffix'] ?? '', 255, 'inputSuffix'),
166 - self::validateString($data['ratingIcon'] ?? 'star', 50, 'ratingIcon')
199 + self::validateString($data['ratingIcon'] ?? 'star', 50, 'ratingIcon'),
200 + new FileUploadFieldAdvancedSettings(
201 + (int) ($data['maxFileSizeMb'] ?? 0),
202 + (string) ($data['saveUploadsTo'] ?? 'ivyforms'),
203 + is_array($data['allowedFileExtensions'] ?? null)
204 + ? $data['allowedFileExtensions']
205 + : []
206 + )
167 207 );
168 208 $fieldOptions = new FieldOptions($data['options'] ?? []);
169 209
170 210 $fieldObject = new Field(
@@ -179,9 +219,10 @@
179 219 $data['parentId'] ?? null,
180 220 $data['rows'] ?? 0,
181 221 $data['rowIndex'] ?? 0,
182 222 $data['columnIndex'] ?? 0,
183 - $data['width'] ?? 100
223 + $data['width'] ?? 100,
224 + $data['pageId'] ?? null
184 225 );
185 226
186 227 /**
187 228 * Allows setting of additional properties for the Field value object.
@@ -218,8 +259,12 @@
218 259 if (isset($data['parentId'])) {
219 260 $field->setParentId($data['parentId']);
220 261 }
221 262
263 + if (isset($data['pageId'])) {
264 + $field->setPageId($data['pageId']);
265 + }
266 +
222 267 if (isset($data['rows'])) {
223 268 $field->setRows((int)$data['rows']);
224 269 }
225 270
@@ -230,12 +275,40 @@
230 275 * @param FieldEntity $field The current field data.
231 276 * @param mixed[] $data The settings array from the database.
232 277 * @return mixed[] The modified field data.
233 278 */
234 - return apply_filters('ivyforms/field/entity/set_properties', $field, $data);
279 + $field = apply_filters('ivyforms/field/entity/set_properties', $field, $data);
280 +
281 + if (!empty($data['nameFieldType'])) {
282 + $field->setAdditionalProperty('nameFieldType', (string)$data['nameFieldType']);
283 + }
284 + if (array_key_exists('subFieldIndex', $data) && $data['subFieldIndex'] !== null) {
285 + $field->setAdditionalProperty('subFieldIndex', (int)$data['subFieldIndex']);
286 + }
287 +
288 + return $field;
235 289 }
236 290
237 291 /**
292 + * @param mixed $rawSettings
293 + * @return array<string, mixed>
294 + */
295 + private static function parseSettings($rawSettings): array
296 + {
297 + if (is_array($rawSettings)) {
298 + return $rawSettings;
299 + }
300 +
301 + if (!is_string($rawSettings) || $rawSettings === '') {
302 + return [];
303 + }
304 +
305 + $decoded = json_decode($rawSettings, true);
306 +
307 + return is_array($decoded) ? $decoded : [];
308 + }
309 +
310 + /**
238 311 * Validate and convert a value to boolean.
239 312 *
240 313 * @param mixed $value The value to validate.
241 314 *
@@ -243,8 +316,33 @@
243 316 */
244 317 private static function validateBool($value): bool
245 318 {
246 319 return (bool)$value;
320 + }
321 +
322 + /**
323 + * Resolve and validate field default value (Pro may override for rich_text, etc.).
324 + *
325 + * @param array<string, mixed> $data
326 + *
327 + * @return string
328 + *
329 + * @throws ValidationException
330 + */
331 + private static function resolveDefaultValue(array $data): string
332 + {
333 + $filtered = apply_filters(
334 + 'ivyforms/field/validate_default_value',
335 + null,
336 + $data['defaultValue'] ?? '',
337 + $data
338 + );
339 +
340 + if (is_string($filtered)) {
341 + return $filtered;
342 + }
343 +
344 + return self::validateString($data['defaultValue'] ?? '', 255, 'defaultValue');
247 345 }
248 346
249 347 /**
250 348 * Validate a string's length.