PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.1.3
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.1.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 / WorkFlow / Helper.php

Helper.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 3.1.3, at includes/Core/WorkFlow/Helper.php

283 lines 8.8 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\WorkFlow;
4
5 use BitCode\BitForm\Core\Integration\IntegrationHandler;
6 use BitCode\BitForm\Core\Messages\SuccessMessageHandler;
7 use BitCode\BitForm\Core\Util\FieldValueHandler;
8 use BitCode\BitForm\Core\Util\SmartTags;
9
10 final class Helper
11 {
12 public static function getFieldData($fields)
13 {
14 $fieldData = [];
15
16 foreach ($fields as $fieldKey => $fieldDetail) {
17 $value = isset($fieldDetail->val) ? $fieldDetail->val : (isset($fieldDetail->defaultValue) ? $fieldDetail->defaultValue : '');
18 $fieldData[$fieldKey] = [
19 'key' => $fieldKey,
20 'value' => $value,
21 'type' => $fieldDetail->typ,
22 ];
23 if (isset($fieldDetail->mul)) {
24 $fieldData[$fieldKey] =
25 array_merge(
26 $fieldData[$fieldKey],
27 [
28 'mul' => $fieldDetail->mul,
29 ]
30 );
31 }
32 }
33 return $fieldData;
34 }
35
36 public static function smartFldMargeFormFld($logics, $fieldData)
37 {
38 $fieldKeys = SmartTags::smartTagFieldKeys();
39
40 foreach ($logics as $logic) {
41 $removeSpecialStr = ['${', '}', '()'];
42 if (isset($logic->field) && in_array(str_replace($removeSpecialStr, '', $logic->field), $fieldKeys)) {
43 $field = str_replace($removeSpecialStr, '', $logic->field);
44
45 $fldKey = '${' . $field . '}';
46 $customValue = isset($logic->smartKey) ? $logic->smartKey : '';
47 $val = SmartTags::getSmartTagValue($field, false, $customValue);
48
49 $fieldData[$fldKey] = [
50 'key' => $fldKey,
51 'value' => empty($val) ? '' : $val,
52 'type' => is_string($val) ? 'text' : 'array',
53 ];
54 }
55 }
56 return $fieldData;
57 }
58
59 public static function replaceFieldWithValue($stringToReplaceField, $fieldValues, $evalMathExpr = true, $formID = null)
60 {
61 $stringToReplaceField = FieldValueHandler::replaceFieldWithValue($stringToReplaceField, $fieldValues, $formID);
62 if ($evalMathExpr) {
63 return self::evalMathExpression($stringToReplaceField);
64 }
65 return $stringToReplaceField;
66 }
67
68 public static function setDefaultSubmitConfirmation($confirmationType, $fieldValue, $formId, $logID = 0)
69 {
70 $messageId = 0;
71 $returnableData = null;
72 return [
73 'msg_id' => $messageId,
74 'confirmation' => $returnableData,
75 ];
76 $integrationHandler = new IntegrationHandler($formId);
77 switch ($confirmationType) {
78 case 'successMsg':
79 $successMessageHandler
80 = new SuccessMessageHandler($formId);
81 $successMessage = $successMessageHandler->getAllMessage();
82 if (!is_wp_error($successMessage) && !empty($successMessage) && count($successMessage) > 0) {
83 $returnableData = self::replaceFieldWithValue($successMessage[0]->message_content, $fieldValue);
84 $messageId = $successMessage[0]->id;
85 }
86 break;
87 case 'redirectPage':
88 $redirectPage = $integrationHandler->getAllIntegration('form', 'redirectPage');
89 if (!is_wp_error($redirectPage) && !empty($redirectPage) && count($redirectPage) > 0) {
90 $url = json_decode($redirectPage[0]->integration_details)->url;
91 if (!empty($url)) {
92 $url = self::replaceFieldWithValue($url, $fieldValue);
93 }
94 $returnableData = empty($url) ? '' : esc_url_raw($url);
95 }
96 break;
97 case 'webHooks':
98 $webHooks = $integrationHandler->getAllIntegration('form', 'webHooks');
99 if (!is_wp_error($webHooks) && !empty($webHooks) && 1 === count($webHooks)) {
100 $returnableData = ["{\"id\":{$webHooks[0]->id}}"];
101 }
102 break;
103 default:
104 break;
105 }
106
107 return [
108 'msg_id' => $messageId,
109 'confirmation' => $returnableData,
110 ];
111 }
112
113 public static function calculte($firstOperand, $secondOperand, $operator)
114 {
115 switch ($operator) {
116 case '+':
117 return $firstOperand + $secondOperand;
118 case '-':
119 return $firstOperand - $secondOperand;
120 case '*':
121 return $firstOperand * $secondOperand;
122 case '/':
123 return $firstOperand / $secondOperand;
124 case '^':
125 return $firstOperand ** $secondOperand;
126 }
127 }
128
129 public static function filterMailContentType()
130 {
131 return 'text/html';
132 }
133
134 public static function evalMathExpression($stringWithFieldValue)
135 {
136 $mathExpr = $stringWithFieldValue;
137 if (empty($mathExpr)) {
138 return $stringWithFieldValue;
139 }
140 preg_match_all('/[\+\-\*\/\s]+/', $mathExpr, $isMathExpr);
141 if (empty($isMathExpr[0])) {
142 return $stringWithFieldValue;
143 }
144 preg_match_all('/\w+/', $mathExpr, $exprValues);
145 if (empty($exprValues[0])) {
146 return $stringWithFieldValue;
147 }
148 foreach ($exprValues[0] as $opreands) {
149 if (!is_numeric($opreands)) {
150 return $stringWithFieldValue;
151 }
152 }
153 $validOperator = ['+', '-', '*', '^', '/'];
154 foreach ($isMathExpr[0] as $value) {
155 if (!in_array(trim($value), $validOperator)) {
156 return $stringWithFieldValue;
157 }
158 }
159 $mathExpr = str_replace(' ', '', $mathExpr);
160 $mathExpr = preg_replace('/\{|\[|\(/', '(', $mathExpr);
161 $mathExpr = preg_replace('/\}|\]/', ')', $mathExpr);
162 $calculated = self::infixToPostfixEvalute($mathExpr);
163 if (!is_null($calculated)) {
164 return (string) $calculated[0];
165 }
166
167 return (string) $stringWithFieldValue;
168 }
169
170 public static function infixToPostfixEvalute($expression)
171 {
172 $operatorStack = [];
173 $outputQueue = [];
174 $numTemp = null;
175 for ($strIndex = 0; $strIndex < strlen($expression); $strIndex++) {
176 $token = $expression[$strIndex];
177 if ('+' === $token || '-' === $token || '*' === $token || '/' === $token || '^' === $token || '(' === $token || ')' === $token) {
178 if (!is_null($numTemp)) {
179 $outputQueue[] = $numTemp;
180 $numTemp = null;
181 }
182 $stackSize = count($operatorStack);
183 if ($stackSize) {
184 $stackTop = $operatorStack[$stackSize - 1];
185 }
186 if ('(' === $token) {
187 $operatorStack[] = $token;
188 } elseif (')' === $token) {
189 while ('(' !== $operatorStack[count($operatorStack) - 1]) {
190 $outputQueue[] = array_pop($operatorStack);
191 if ('(' === $operatorStack[count($operatorStack) - 1]) {
192 array_pop($operatorStack);
193 break;
194 }
195 }
196 } elseif (isset($stackTop) && self::operatorPrecedence($token) > self::operatorPrecedence($stackTop)) {
197 $operatorStack[] = $token;
198 } elseif ('^' !== $token && $stackSize) {
199 $operatorStack[$stackSize - 1] = $token;
200 $outputQueue[] = $stackTop;
201 } else {
202 $operatorStack[] = $token;
203 }
204 continue;
205 }
206 $numTemp .= $token;
207 if ($strIndex === strlen($expression) - 1 && !is_null($numTemp)) {
208 $outputQueue[] = $numTemp;
209 }
210 }
211
212 if (!is_null($operatorStack)) {
213 $outputQueue = array_merge($outputQueue, array_reverse($operatorStack));
214 }
215 $resultStack = [];
216 foreach ($outputQueue as $value) {
217 if (is_numeric($value)) {
218 $resultStack[] = $value;
219 continue;
220 }
221 $secondOperand = array_pop($resultStack);
222 $firstOperand = array_pop($resultStack);
223 $resultStack[] = self::calculate($firstOperand, $secondOperand, $value);
224 }
225 return $resultStack;
226 }
227
228 public static function operatorPrecedence($operator)
229 {
230 $precedence = [
231 '+' => 2,
232 '-' => 2,
233 '*' => 3,
234 '/' => 3,
235 '^' => 4,
236 ];
237
238 return isset($precedence[$operator]) ? $precedence[$operator] : 0;
239 }
240
241 public static function calculate($firstOperand, $secondOperand, $operator)
242 {
243 $calculated = [
244 '+' => $firstOperand + $secondOperand,
245 '-' => $firstOperand - $secondOperand,
246 '*' => $firstOperand * $secondOperand,
247 '/' => $firstOperand / $secondOperand,
248 '^' => $firstOperand ** $secondOperand,
249 ];
250
251 return isset($calculated[$operator]) ? $calculated[$operator] : '';
252 }
253
254 /**
255 * Recursively sets a nested property in a given object.
256 *
257 * This function takes an object, a string path representing nested properties
258 * (e.g., "fk->valid->hide"), and a value to assign. It ensures that all
259 * intermediate properties exist as objects before setting the final value.
260 *
261 * @param object $object The main object where properties should be set.
262 * @param string $path The nested property path, with keys separated by "->".
263 * @param mixed $value The value to assign to the final property.
264 *
265 * @return void
266 */
267 public static function setNestedProperty(&$object, $path, $value)
268 {
269 $keys = explode('->', $path);
270 $key = array_shift($keys);
271
272 if (!isset($object->$key) || !is_object($object->$key)) {
273 $object->$key = new \stdClass();
274 }
275
276 if (!empty($keys)) {
277 self::setNestedProperty($object->$key, implode('->', $keys), $value);
278 } else {
279 $object->$key = $value;
280 }
281 }
282 }
283