PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.15.3
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.15.3
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / Util / FieldValueHandler.php

FieldValueHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.15.3, at includes/Core/Util/FieldValueHandler.php

239 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Core\Util;
4
5 use BitCode\BitForm\Admin\Form\Helpers;
6
7 final class FieldValueHandler
8 {
9 public static function replaceFieldWithValue($stringToReplaceField, $fieldValues)
10 {
11 if (empty($stringToReplaceField)) {
12 return $stringToReplaceField;
13 }
14 if (!is_string($stringToReplaceField)) {
15 $stringToReplaceField = wp_json_encode($stringToReplaceField);
16 }
17 $stringToReplaceField = static::replaceSmartTagWithValue($stringToReplaceField);
18 $fieldPattern = '/\${\w[^ ${}]*}/';
19 preg_match_all($fieldPattern, $stringToReplaceField, $matchedField);
20 if (empty($matchedField)) {
21 return $stringToReplaceField;
22 }
23 $uniqueFieldsInStr = array_unique($matchedField[0]);
24 foreach ($uniqueFieldsInStr as $key => $value) {
25 $fieldName = substr($value, 2, strlen($value) - 3);
26 $fieldValue = null;
27 if (isset($fieldValues[$fieldName])) {
28 $targetFieldValue = isset($fieldValues[$fieldName]['value']) ? $fieldValues[$fieldName]['value'] : $fieldValues[$fieldName];
29 if ('array' === gettype($targetFieldValue) || 'object' === gettype($targetFieldValue)) {
30 foreach ((array)$targetFieldValue as $singleTargetVal) {
31 if (isset($fieldValue)) {
32 if (is_numeric($fieldValue) && is_numeric($singleTargetVal)) {
33 $fieldValue = $fieldValue + $singleTargetVal;
34 } else {
35 $fieldValue = "$fieldValue, $singleTargetVal";
36 }
37 } else {
38 $fieldValue = $singleTargetVal;
39 }
40 }
41 // $fieldValue = wp_json_encode($targetFieldValue);
42 } else {
43 $fieldValue = strval($targetFieldValue);
44 }
45 $stringToReplaceField = str_replace($value, $fieldValue, $stringToReplaceField);
46 } else {
47 $stringToReplaceField = str_replace($value, '', $stringToReplaceField);
48 }
49 }
50
51 // check if the string is a function like : "${_bf_calc(${b27-5}*10)}"
52 // TO DO: Implement the function properly
53 // if (self::isFunction($stringToReplaceField)) {
54 // $functionName = self::getFunctionName($stringToReplaceField);
55
56 // switch ($functionName) {
57 // case '_bf_calc':
58 // return self::getFunctionParameter($stringToReplaceField);
59 // case '_bf_count':
60 // return self::getCountValue($stringToReplaceField);
61 // default:
62 // return 0;
63 // }
64 // }
65 return $stringToReplaceField;
66 }
67
68 /**
69 * Summary of getCountValue - get the count value from the function string "${_bf_count(item-1, item-2)}" => 2
70 *
71 * @param string $functionString
72 * @return int
73 */
74 private static function getCountValue(string $functionString): int
75 {
76 $options = self::getFunctionParameter($functionString);
77 $option = explode(',', $options);
78 return count($option);
79 }
80
81 /**
82 * Summary of getFunctionParameter - get the function parameter from the function string "${_bf_calc(2*10)}" => 2*10
83 *
84 * @param string $functionString
85 * @return string
86 */
87 private static function getFunctionParameter(string $functionString): string
88 {
89 $regexPattern = '/\(([^)]*)\)/';
90 preg_match($regexPattern, $functionString, $matches);
91 return $matches[1];
92 }
93
94 /**
95 * Summary of isFunction - check if the string is a function "${_bf_calc(${b27-5}*10)}" or not "${_bf_date}"
96 *
97 * @param string $functionString
98 * @return bool true if the string is a function else false
99 */
100 private static function isFunction(string $functionString): bool
101 {
102 $regexPattern = '/\([^)]*\)/';
103 return preg_match($regexPattern, $functionString);
104 }
105
106 /**
107 * Summary of getFunctionName - get the function name from the function string "${_bf_calc(${b27-5}*10)}" => _bf_calc
108 *
109 * @param string $functionString
110 * @return string
111 */
112 private static function getFunctionName(string $functionString): string
113 {
114 $regexPattern = '/\b([a-zA-Z_][a-zA-Z0-9_]*)\(/';
115 preg_match($regexPattern, $functionString, $matches);
116 return $matches[1];
117 }
118
119 public static function validateMailArry($emailAddresses, $fieldValues)
120 {
121 if (!is_array($emailAddresses)) {
122 return [FieldValueHandler::replaceFieldWithValue($emailAddresses, $fieldValues)];
123 }
124 foreach ($emailAddresses as $key => $email) {
125 if (!is_email($email)) {
126 $email = FieldValueHandler::replaceFieldWithValue($email, $fieldValues);
127 if (is_email($email)) {
128 $emailAddresses[$key] = $email;
129 }
130 }
131 }
132 return $emailAddresses;
133 }
134
135 public static function replaceSmartTagWithValue($fieldValues)
136 {
137 $fieldPattern = '/(\${_[^{]*?)(?=\})}/';
138 $matchPattern = preg_match_all($fieldPattern, $fieldValues, $matchedField);
139 if (!$matchPattern) {
140 return $fieldValues;
141 }
142
143 $ajaxRequest = false;
144 if (isset($_REQUEST['action']) && 'bitforms_trigger_workflow' === $_REQUEST['action']) {
145 $ajaxRequest = true;
146 }
147
148 foreach (array_unique($matchedField[0]) as $value) {
149 $fieldName = trim(substr($value, 2, strlen($value) - 3));
150
151 $matches = preg_match('/\("*([^\)]+"*)\)/', $value, $matchCustomFormat);
152
153 $customValue = '';
154 if ($matches) {
155 $removeQuote = ["'", '"'];
156 $customValue = str_replace($removeQuote, '', $matchCustomFormat[1]);
157 $fieldName = str_replace($matchCustomFormat[0], '', $fieldName);
158 }
159
160 $tagFieldValues = SmartTags::getSmartTagValue($fieldName, $ajaxRequest, $customValue);
161 $fieldValues = str_replace($value, $tagFieldValues, $fieldValues);
162 }
163 return $fieldValues;
164 }
165
166 public static function isEmpty($val)
167 {
168 if (empty($val) && !in_array($val, ['0', 0, 0.0], true)) {
169 return true;
170 }
171 return false;
172 }
173
174 public static function formatFieldValueForMail($fields, $fieldValues)
175 {
176 $formattedFldValues = $fieldValues;
177 $repeaterFieldKey = [];
178 $file_upload_types = Helpers::$file_upload_types;
179 $repeated_array_type_data_fields = Helpers::$repeated_array_type_data_fields;
180 foreach ($fields as $fldKey => $fldData) {
181 if (in_array($fldData->typ, $file_upload_types)) {
182 continue;
183 }
184 if (array_key_exists($fldKey, $fieldValues)) {
185 $value = $fieldValues[$fldKey];
186 // if (is_array($value)) {
187 // $formattedFldValues[$fldKey] = htmlspecialchars(implode(', ', $value));
188 // } else {
189 // $formattedFldValues[$fldKey] = htmlspecialchars($value);
190 // }
191
192 if (is_array($value)) {
193 $arrValue = '';
194 foreach ($value as $v) {
195 if (is_array($v)) {
196 foreach ($v as $k1 => $v1) {
197 if (array_key_exists($k1, $repeaterFieldKey)) {
198 $oldValue = $repeaterFieldKey[$k1];
199 if (is_array($v1) && in_array($fields->{$k1}->typ, $repeated_array_type_data_fields)) {
200 $newValues = '[' . implode(', ', $v1) . '] ';
201 if (!preg_match('/\[.*\]/', $oldValue)) {
202 $oldValue = '[' . $oldValue . '] ';
203 }
204 } else {
205 $newValues = $v1;
206 }
207 $repeaterFieldKey[$k1] = $oldValue . ', ' . $newValues;
208 } else {
209 if (!empty($v1) && is_array($v1)) {
210 $repeaterFieldKey[$k1] = htmlspecialchars(implode(', ', $v1));
211 } else {
212 $repeaterFieldKey[$k1] = htmlspecialchars($v1);
213 }
214 }
215 }
216 } else {
217 $arrValue .= $v . ', ';
218 }
219 }
220 $formattedFldValues[$fldKey] = htmlspecialchars(rtrim($arrValue, ', '));
221 $arrValue = '';
222 } else {
223 $formattedFldValues[$fldKey] = htmlspecialchars($value);
224 }
225 if ('textarea' === $fldData->typ) {
226 $formattedFldValues[$fldKey] = nl2br(htmlspecialchars($value));
227 }
228 if ('date' === $fldData->typ) {
229 $formattedFldValues[$fldKey] = date_i18n(get_option('date_format'), strtotime(htmlspecialchars($value)));
230 }
231 }
232 }
233
234 $merge_values = array_merge($fieldValues, $formattedFldValues);
235 $merge_values = array_merge($merge_values, $repeaterFieldKey);
236 return $merge_values;
237 }
238 }
239