PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.10.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.10.0
3.3.1 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 All 138 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.10.0, at includes/Core/Util/FieldValueHandler.php

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