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/Repository/Field/FieldRepository.php +168 -121 1.1 → 1.5.1 View file →
@@ -52,68 +52,9 @@
52 52 'columnIndex' => $data['columnIndex'] ?? 0,
53 53 'width' => $data['width'] ?? 100,
54 54 'parentId' => $data['parentId'] ?? null,
55 55 'pageId' => $data['pageId'] ?? null,
56 - 'settings' => json_encode(
57 - /**
58 - * Allows modification of the settings data before saving to database.
59 - * @since 0.1.0
60 - *
61 - * @param mixed[] $settings The current settings array.
62 - * @param mixed[] $data The full field data array.
63 - * @param Field $entity The Field entity instance.
64 - * @return mixed[] The modified settings array.
65 - */
66 - apply_filters(
67 - 'ivyforms/repository/field/settings',
68 - [
69 - 'hideLabel' => (int)$data['hideLabel'],
70 - 'readOnly' => (int)$data['readOnly'],
71 - 'description' => $data['description'],
72 - 'requiredMessage' => $data['requiredMessage'],
73 - 'cssClasses' => $data['cssClasses'],
74 - 'limitMaxLength' => (int)$data['limitMaxLength'],
75 - 'limitRange' => (int)$data['limitRange'],
76 - 'maxLength' => $data['maxLength'],
77 - 'labelPosition' => $data['labelPosition'],
78 - 'noDuplicates' => (int)$data['noDuplicates'],
79 - 'shuffleOptions' => (int)$data['shuffleOptions'],
80 - 'showValues' => (int)$data['showValues'],
81 - 'enableSearch' => (int)$data['enableSearch'],
82 - 'rows' => (int)$data['rows'],
83 - 'confirmFieldEnabled' => (int)($data['confirmFieldEnabled'] ?? 0),
84 - 'confirmFieldLabel' => $data['confirmFieldLabel'] ?? '',
85 - 'confirmFieldPlaceholder' => $data['confirmFieldPlaceholder'] ?? '',
86 - 'confirmFieldHideLabel' => (int)($data['confirmFieldHideLabel'] ?? 0),
87 - 'phoneFormat' => $data['phoneFormat'],
88 - 'phoneAutoDetect' => $data['phoneAutoDetect'],
89 - 'minValue' => isset($data['minValue']) ? (float)$data['minValue'] : null,
90 - 'maxValue' => isset($data['maxValue']) ? (float)$data['maxValue'] : null,
91 - 'step' => (float)$data['step'],
92 - 'showGrid' => (int)($data['showGrid'] ?? 0),
93 - 'numberFormat' => $data['numberFormat'] ?? '',
94 - 'visible' => (int)$data['visible'],
95 - 'inputPrefix' => $data['inputPrefix'] ?? '',
96 - 'inputSuffix' => $data['inputSuffix'] ?? '',
97 - 'timeFieldType' => $data['timeFieldType'] ?? '',
98 - 'timeFormat' => $data['timeFormat'] ?? '',
99 - 'dateFieldType' => $data['dateFieldType'] ?? '',
100 - 'dateFormat' => $data['dateFormat'] ?? '',
101 - 'minDateValue' => $data['minDateValue'] ?? '',
102 - 'maxDateValue' => $data['maxDateValue'] ?? '',
103 - 'showRatingText' => (int)($data['showRatingText'] ?? 0),
104 - 'ratingIcon' => $data['ratingIcon'] ?? 'star',
105 - 'htmlContent' => $data['htmlContent'] ?? '',
106 - 'agreementText' => $data['agreementText'] ?? '',
107 - 'limitDateRange' => (int)$data['limitDateRange'],
108 - 'minTimeValue' => $data['minTimeValue'] ?? '',
109 - 'maxTimeValue' => $data['maxTimeValue'] ?? '',
110 - 'limitTimeRange' => (int)$data['limitTimeRange'],
111 - ],
112 - $data,
113 - $entity
114 - )
115 - ),
56 + 'settings' => $this->encodeFieldSettings($data, $entity, false),
116 57 'dateCreated' => current_time('mysql')
117 58 ],
118 59 ['%d', '%d', '%s', '%s', '%d', '%s', '%s', '%d', '%d', '%d', '%d', '%d', '%s', '%s', '%s']
119 60 );
@@ -138,8 +79,11 @@
138 79 * @throws QueryExecutionException
139 80 */
140 81 public function update(int $id, $entity): bool
141 82 {
83 + if ($id < 1) {
84 + throw new InvalidArgumentException('Field id must be a positive integer.');
85 + }
142 86
143 87 $data = $entity->toArray();
144 88
145 89 $result = $this->wpdb->update(
@@ -157,70 +101,11 @@
157 101 'columnIndex' => $data['columnIndex'] ?? 0,
158 102 'width' => $data['width'] ?? 100,
159 103 'parentId' => $data['parentId'] ?? null,
160 104 'pageId' => $data['pageId'] ?? null,
161 - 'settings' => json_encode(
162 - /**
163 - * Allows modification of the settings data before updating database.
164 - * @since 0.1.0
165 - *
166 - * @param mixed[] $settings The current settings array.
167 - * @param mixed[] $data The full field data array.
168 - * @param Field $entity The Field entity instance.
169 - * @return mixed[] The modified settings array.
170 - */
171 - apply_filters(
172 - 'ivyforms/repository/field/settings',
173 - [
174 - 'hideLabel' => (int)$data['hideLabel'],
175 - 'readOnly' => (int)$data['readOnly'],
176 - 'description' => $data['description'],
177 - 'requiredMessage' => $data['requiredMessage'],
178 - 'cssClasses' => $data['cssClasses'],
179 - 'limitMaxLength' => (int)$data['limitMaxLength'],
180 - 'limitRange' => filter_var($data['limitRange'], FILTER_VALIDATE_BOOLEAN) ? 1 : 0,
181 - 'maxLength' => $data['maxLength'],
182 - 'labelPosition' => $data['labelPosition'],
183 - 'noDuplicates' => (int)$data['noDuplicates'],
184 - 'shuffleOptions' => (int)$data['shuffleOptions'],
185 - 'showValues' => (int)$data['showValues'],
186 - 'enableSearch' => (int)$data['enableSearch'],
187 - 'rows' => (int)$data['rows'],
188 - 'confirmFieldEnabled' => (int)($data['confirmFieldEnabled'] ?? 0),
189 - 'confirmFieldLabel' => $data['confirmFieldLabel'] ?? '',
190 - 'confirmFieldPlaceholder' => $data['confirmFieldPlaceholder'] ?? '',
191 - 'confirmFieldHideLabel' => (int)($data['confirmFieldHideLabel'] ?? 0),
192 - 'phoneFormat' => $data['phoneFormat'],
193 - 'phoneAutoDetect' => $data['phoneAutoDetect'],
194 - 'minValue' => isset($data['minValue']) ? (float)$data['minValue'] : null,
195 - 'maxValue' => isset($data['maxValue']) ? (float)$data['maxValue'] : null,
196 - 'step' => (float)$data['step'],
197 - 'showGrid' => (int)($data['showGrid'] ?? 0),
198 - 'numberFormat' => $data['numberFormat'] ?? '',
199 - 'visible' => array_key_exists('visible', $data) ? (int)$data['visible'] : 1,
200 - 'inputPrefix' => $data['inputPrefix'] ?? '',
201 - 'inputSuffix' => $data['inputSuffix'] ?? '',
202 - 'timeFieldType' => $data['timeFieldType'] ?? '',
203 - 'timeFormat' => $data['timeFormat'] ?? '',
204 - 'dateFieldType' => $data['dateFieldType'] ?? '',
205 - 'dateFormat' => $data['dateFormat'] ?? '',
206 - 'minDateValue' => $data['minDateValue'] ?? '',
207 - 'maxDateValue' => $data['maxDateValue'] ?? '',
208 - 'showRatingText' => (int)($data['showRatingText'] ?? 0),
209 - 'ratingIcon' => $data['ratingIcon'] ?? 'star',
210 - 'htmlContent' => $data['htmlContent'] ?? '',
211 - 'agreementText' => $data['agreementText'] ?? '',
212 - 'limitDateRange' => (int)$data['limitDateRange'],
213 - 'minTimeValue' => $data['minTimeValue'] ?? '',
214 - 'maxTimeValue' => $data['maxTimeValue'] ?? '',
215 - 'limitTimeRange' => (int)$data['limitTimeRange'],
216 - ],
217 - $data,
218 - $entity
219 - )
220 - ),
105 + 'settings' => $this->encodeFieldSettings($data, $entity, true),
221 106 ],
222 - ['id' => $data['id']],
107 + ['id' => $id],
223 108 ['%d', '%d', '%s', '%s', '%d', '%s', '%s', '%d', '%d', '%d', '%d', '%d', '%s', '%s'],
224 109 ['%d']
225 110 );
226 111
@@ -228,8 +113,170 @@
228 113 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__);
229 114 }
230 115
231 116 return $result;
117 + }
118 +
119 + /**
120 + * @param mixed[] $data
121 + */
122 + private function encodeFieldSettings(array $data, Field $entity, bool $forUpdate): string
123 + {
124 + $settings = $this->buildFieldSettingsPayload($data, $forUpdate);
125 +
126 + /**
127 + * Allows modification of the settings data before saving to database.
128 + *
129 + * @param mixed[] $settings The current settings array.
130 + * @param mixed[] $data The full field data array.
131 + * @param Field $entity The Field entity instance.
132 + * @return mixed[] The modified settings array.
133 + */
134 + $settings = apply_filters('ivyforms/repository/field/settings', $settings, $data, $entity);
135 +
136 + $encoded = wp_json_encode($settings);
137 + if ($encoded === false) {
138 + throw new QueryExecutionException(
139 + 'Failed to encode field settings: ' . json_last_error_msg()
140 + );
141 + }
142 +
143 + return $encoded;
144 + }
145 +
146 + /**
147 + * @param mixed[] $data
148 + *
149 + * @return mixed[]
150 + */
151 + private function buildFieldSettingsPayload(array $data, bool $forUpdate): array
152 + {
153 + return array_merge(
154 + $this->buildCoreFieldSettings($data, $forUpdate),
155 + $this->buildFileUploadFieldSettings($data)
156 + );
157 + }
158 +
159 + /**
160 + * @param mixed[] $data
161 + *
162 + * @return mixed[]
163 + */
164 + private function buildCoreFieldSettings(array $data, bool $forUpdate): array
165 + {
166 + return array_merge(
167 + $this->buildBaseFieldSettings($data),
168 + [
169 + 'limitRange' => $this->resolveLimitRangeFlag($data, $forUpdate),
170 + 'visible' => $this->resolveVisibleFlag($data, $forUpdate),
171 + ],
172 + $this->buildExtendedFieldSettings($data)
173 + );
174 + }
175 +
176 + /**
177 + * @param mixed[] $data
178 + *
179 + * @return mixed[]
180 + */
181 + private function buildBaseFieldSettings(array $data): array
182 + {
183 + return [
184 + 'hideLabel' => (int) $data['hideLabel'],
185 + 'readOnly' => (int) $data['readOnly'],
186 + 'description' => $data['description'],
187 + 'requiredMessage' => $data['requiredMessage'],
188 + 'cssClasses' => $data['cssClasses'],
189 + 'limitMaxLength' => (int) $data['limitMaxLength'],
190 + 'maxLength' => $data['maxLength'],
191 + 'labelPosition' => $data['labelPosition'],
192 + 'noDuplicates' => (int) $data['noDuplicates'],
193 + 'shuffleOptions' => (int) $data['shuffleOptions'],
194 + 'showValues' => (int) $data['showValues'],
195 + 'enableSearch' => (int) $data['enableSearch'],
196 + 'rows' => (int) $data['rows'],
197 + ];
198 + }
199 +
200 + /**
201 + * @param mixed[] $data
202 + *
203 + * @return mixed[]
204 + */
205 + private function buildExtendedFieldSettings(array $data): array
206 + {
207 + return [
208 + 'confirmFieldEnabled' => (int) ($data['confirmFieldEnabled'] ?? 0),
209 + 'confirmFieldLabel' => $data['confirmFieldLabel'] ?? '',
210 + 'confirmFieldPlaceholder' => $data['confirmFieldPlaceholder'] ?? '',
211 + 'confirmFieldHideLabel' => (int) ($data['confirmFieldHideLabel'] ?? 0),
212 + 'phoneFormat' => $data['phoneFormat'],
213 + 'phoneAutoDetect' => $data['phoneAutoDetect'],
214 + 'minValue' => isset($data['minValue']) ? (float) $data['minValue'] : null,
215 + 'maxValue' => isset($data['maxValue']) ? (float) $data['maxValue'] : null,
216 + 'step' => (float) $data['step'],
217 + 'showGrid' => (int) ($data['showGrid'] ?? 0),
218 + 'numberFormat' => $data['numberFormat'] ?? '',
219 + 'inputPrefix' => $data['inputPrefix'] ?? '',
220 + 'inputSuffix' => $data['inputSuffix'] ?? '',
221 + 'timeFieldType' => $data['timeFieldType'] ?? '',
222 + 'timeFormat' => $data['timeFormat'] ?? '',
223 + 'dateFieldType' => $data['dateFieldType'] ?? '',
224 + 'dateFormat' => $data['dateFormat'] ?? '',
225 + 'minDateValue' => $data['minDateValue'] ?? '',
226 + 'maxDateValue' => $data['maxDateValue'] ?? '',
227 + 'showRatingText' => (int) ($data['showRatingText'] ?? 0),
228 + 'ratingIcon' => $data['ratingIcon'] ?? 'star',
229 + 'htmlContent' => $data['htmlContent'] ?? '',
230 + 'agreementText' => $data['agreementText'] ?? '',
231 + 'limitDateRange' => (int) $data['limitDateRange'],
232 + 'minTimeValue' => $data['minTimeValue'] ?? '',
233 + 'maxTimeValue' => $data['maxTimeValue'] ?? '',
234 + 'limitTimeRange' => (int) $data['limitTimeRange'],
235 + 'nameFieldType' => $data['nameFieldType'] ?? '',
236 + 'subFieldIndex' => isset($data['subFieldIndex']) ? (int) $data['subFieldIndex'] : null,
237 + ];
238 + }
239 +
240 + /**
241 + * @param mixed[] $data
242 + */
243 + private function resolveLimitRangeFlag(array $data, bool $forUpdate): int
244 + {
245 + if (!$forUpdate) {
246 + return (int) $data['limitRange'];
247 + }
248 +
249 + return filter_var($data['limitRange'], FILTER_VALIDATE_BOOLEAN) ? 1 : 0;
250 + }
251 +
252 + /**
253 + * @param mixed[] $data
254 + */
255 + private function resolveVisibleFlag(array $data, bool $forUpdate): int
256 + {
257 + if (!$forUpdate) {
258 + return (int) $data['visible'];
259 + }
260 +
261 + return array_key_exists('visible', $data) ? (int) $data['visible'] : 1;
262 + }
263 +
264 + /**
265 + * @param mixed[] $data
266 + *
267 + * @return mixed[]
268 + */
269 + private function buildFileUploadFieldSettings(array $data): array
270 + {
271 + return [
272 + 'maxFileSizeMb' => isset($data['maxFileSizeMb']) ? (int) $data['maxFileSizeMb'] : 0,
273 + 'saveUploadsTo' => (string) ($data['saveUploadsTo'] ?? 'ivyforms'),
274 + 'allowedFileExtensions' => isset($data['allowedFileExtensions'])
275 + && is_array($data['allowedFileExtensions'])
276 + ? array_values($data['allowedFileExtensions'])
277 + : [],
278 + ];
232 279 }
233 280
234 281 /**
235 282 * Get fields by form ID