PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 1.4
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v1.4
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 1.4, at includes/Core/Util/FieldValueHandler.php

67 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace BitCode\BitForm\Core\Util;
3
4 final class FieldValueHandler
5 {
6 public static function replaceFieldWithValue($stringToReplaceField, $fieldValues)
7 {
8 if (empty($stringToReplaceField)) {
9 return $stringToReplaceField;
10 }
11 if (!is_string($stringToReplaceField)) {
12 $stringToReplaceField = wp_json_encode($stringToReplaceField);
13 }
14 $fieldPattern = '/\${\w[^ ${}]*}/';
15 preg_match_all($fieldPattern, $stringToReplaceField, $matchedField);
16 if (empty($matchedField)) {
17 return $stringToReplaceField;
18 }
19 $uniqueFieldsInStr = array_unique($matchedField[0]);
20 foreach ($uniqueFieldsInStr as $key => $value) {
21 $fieldName = substr($value, 2, strlen($value)-3);
22 $fieldValue = null;
23 if (isset($fieldValues[$fieldName])) {
24 $targetFieldValue = isset($fieldValues[$fieldName]['value']) ? $fieldValues[$fieldName]['value'] : $fieldValues[$fieldName];
25 if (gettype($targetFieldValue) === 'array' || gettype($targetFieldValue) === 'object') {
26 foreach ((array)$targetFieldValue as $singleTargetVal) {
27 if (isset($fieldValue)) {
28 if (is_numeric($fieldValue) && is_numeric($singleTargetVal)) {
29 $fieldValue = $fieldValue + $singleTargetVal;
30 } else {
31 $fieldValue = "$fieldValue, $singleTargetVal";
32 }
33
34 } else {
35 $fieldValue = $singleTargetVal;
36 }
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 return $stringToReplaceField;
49 }
50
51 public static function validateMailArry($emailAddresses, $fieldValues)
52 {
53 if (!is_array($emailAddresses)) {
54 return [FieldValueHandler::replaceFieldWithValue($emailAddresses, $fieldValues)];
55 }
56 foreach ($emailAddresses as $key=>$email) {
57 if (!is_email($email)) {
58 $email = FieldValueHandler::replaceFieldWithValue($email, $fieldValues);
59 if (is_email($email)) {
60 $emailAddresses[$key] = $email;
61 }
62 }
63 }
64 return $emailAddresses;
65 }
66 }
67