PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 1.0.10
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v1.0.10
3.1.14 3.1.13 3.1.12 3.1.11 3.1.10 3.1.9 3.1.8 3.1.7 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 All 123 releases
firebox / Inc / Framework / Inc / Helpers / FormHelper.php

FormHelper.php in FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment 1.0.10, at Inc/Framework/Inc/Helpers/FormHelper.php

280 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package FirePlugins Framework
4 * @version 1.0.21
5 *
6 * @author FirePlugins <[email protected]>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2022 FirePlugins All Rights Reserved
9 * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
10 */
11
12 namespace FPFramework\Helpers;
13
14 if (!defined('ABSPATH'))
15 {
16 exit; // Exit if accessed directly.
17 }
18
19 class FormHelper {
20 /**
21 * Extra validation on the fields that we are preparing to save
22 *
23 * @param array $fields
24 *
25 * @return array
26 */
27 public static function validateFields(&$fields)
28 {
29 // remove repeater ITEM_ID template items
30 foreach ($fields as $key => &$value)
31 {
32 if (!is_array($value))
33 {
34 continue;
35 }
36
37 if ($key == 'ITEM_ID')
38 {
39 unset($fields[$key]);
40 }
41 else
42 {
43 self::validateFields($value);
44 }
45 }
46 }
47
48 /**
49 * Filters every field based on the form settings
50 *
51 * @param array $fields
52 * @param array $form_settings
53 * @param array $saved_fields The fields that have been already saved in the database for this post. Used to find the updated field values.
54 *
55 * @return array
56 */
57 public static function filterFields(&$fields, $form_settings, $saved_fields = [])
58 {
59 // Validates the fields
60 self::validateFields($fields);
61
62 $previous_fields = [];
63
64 /**
65 * If we were given the previously saved fields,
66 * we filter the $fields by only filtering the values
67 * of the changed fields
68 */
69 if ($saved_fields)
70 {
71 // find all $saved_fields keys that no longer exist in $fields and remove them
72 $removed_keys = array_diff_key($saved_fields, $fields);
73 // remove these keys from $saved_fields
74 foreach ($removed_keys as $key => $value)
75 {
76 unset($saved_fields[$key]);
77 }
78
79 // find updated fields
80 $data = self::findUpdatedFieldsToBeFiltered($fields, $saved_fields);
81
82 $fields = $data['updated_fields'];
83 $previous_fields = $data['previous_fields'];
84 }
85
86 // Filter the fields only if we have entered new values
87 if ($fields)
88 {
89 // filter all fields
90 self::loopFieldsRecursive($fields, $form_settings);
91 }
92
93 $fields = array_merge($fields, $previous_fields);
94 }
95
96 /**
97 * Finds the updated fields and only filters these fields in order to speed up the saving process
98 *
99 * @param array $updated_fields
100 * @param array $saved_fields
101 *
102 * @return array
103 */
104 private static function findUpdatedFieldsToBeFiltered($updated_fields, $saved_fields)
105 {
106 if (!$updated_fields || !$saved_fields || !is_array($updated_fields) || !is_array($saved_fields))
107 {
108 return [
109 'updated_fields' => [],
110 'previous_fields' => []
111 ];
112 }
113
114 // Compare all values by a json_encode
115 $updated_fields = array_diff_assoc(array_map('json_encode', $updated_fields), array_map('json_encode', $saved_fields));
116
117 // Json decode the result
118 $updated_fields = array_map('json_decode', $updated_fields);
119
120 // If we have modified the fields, then get the unmodified form fields
121 if ($updated_fields)
122 {
123 $saved_fields = array_diff_key($saved_fields, $updated_fields);
124 }
125
126 return [
127 'updated_fields' => $updated_fields,
128 'previous_fields' => $saved_fields
129 ];
130 }
131
132 /**
133 * Loop all submitted fields and filter their values
134 *
135 * @param array $fields
136 * @param array $form_settings
137 * @param string $fieldName
138 *
139 * @return void
140 */
141 public static function loopFieldsRecursive(&$fields, $form_settings, $fieldName = '')
142 {
143 foreach ($fields as $key => &$value)
144 {
145 if (is_array($value) || is_object($value))
146 {
147 // loop all fields till we find a field name to sanitize
148 self::loopFieldsRecursive($value, $form_settings, $fieldName . '[' . $key . ']');
149 }
150 else
151 {
152 // sanitize field
153 self::sanitizeFieldRecursive($form_settings, $value, $fieldName . '[' . $key . ']');
154 }
155 }
156 }
157
158 /**
159 * Sanitizes the field value based on its filter or uses a general sanitize filter
160 *
161 * @param array $form_settings
162 * @param string $fieldValue
163 * @param string $fieldName
164 *
165 * @return void
166 */
167 public static function sanitizeFieldRecursive($form_settings, &$fieldValue, $fieldName = '')
168 {
169 foreach ($form_settings as $key => $value)
170 {
171 // Skip if value is not an array
172 if (!is_array($value))
173 {
174 continue;
175 }
176
177 // Filter each
178 if (isset($value['name']) && $value['name'] == $fieldName)
179 {
180 $filterType = isset($value['filter']) ? $value['filter'] : \FPFramework\Base\Filter::defaultFilter;
181
182 // set filter
183 $filter = \FPFramework\Base\Filter::getInstance();
184
185 $fieldValue = $filter->clean($fieldValue, $filterType);
186 return;
187 }
188 else
189 {
190 // else recurse till we find a valid field
191 self::sanitizeFieldRecursive($value, $fieldValue, $fieldName);
192 }
193 }
194 }
195
196 /**
197 * Parse the show on conditions
198 *
199 * @param string $showOn Show on conditions.
200 * @param string $formControl Form name.
201 * @param string $group The dot-separated form group path.
202 *
203 * @return array Array with show on conditions.
204 */
205 public static function parseShowOnConditions($showOn, $formControl = null, $group = null)
206 {
207 // Process the showon data.
208 if (!$showOn)
209 {
210 return [];
211 }
212
213 $formPath = $formControl ?: '';
214
215 if ($group)
216 {
217 $groups = explode('.', $group);
218
219 // An empty formControl leads to invalid shown property
220 // Use the 1st part of the group instead to avoid.
221 if (empty($formPath) && isset($groups[0]))
222 {
223 $formPath = $groups[0];
224 array_shift($groups);
225 }
226
227 foreach ($groups as $group)
228 {
229 $formPath .= '[' . $group . ']';
230 }
231 }
232
233 $showOnData = [];
234 $showOnParts = preg_split('#(\[AND\]|\[OR\])#', $showOn, -1, PREG_SPLIT_DELIM_CAPTURE);
235 $op = '';
236
237 foreach ($showOnParts as $showOnPart)
238 {
239 if (($showOnPart === '[AND]') || $showOnPart === '[OR]')
240 {
241 $op = trim($showOnPart, '[]');
242 continue;
243 }
244
245 $compareEqual = strpos($showOnPart, '!:') === false;
246 $showOnPartBlocks = explode(($compareEqual ? ':' : '!:'), $showOnPart, 2);
247
248 if (strpos($showOnPartBlocks[0], '.') !== false)
249 {
250 if ($formControl)
251 {
252 $field = $formControl . ('[' . str_replace('.', '][', $showOnPartBlocks[0]) . ']');
253 }
254 else
255 {
256 $groupParts = explode('.', $showOnPartBlocks[0]);
257 $field = array_shift($groupParts) . '[' . join('][', $groupParts) . ']';
258 }
259 }
260 else
261 {
262 $field = $formPath ? $formPath . '[' . $showOnPartBlocks[0] . ']' : $showOnPartBlocks[0];
263 }
264
265 $showOnData[] = [
266 'field' => $field,
267 'values' => explode(',', $showOnPartBlocks[1]),
268 'sign' => $compareEqual === true ? '=' : '!=',
269 'op' => $op,
270 ];
271
272 if ($op !== '')
273 {
274 $op = '';
275 }
276 }
277
278 return json_encode($showOnData);
279 }
280 }