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

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

141 lines 4.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
18 preg_match_all($fieldPattern, $stringToReplaceField, $matchedField);
19 if (empty($matchedField)) {
20 return $stringToReplaceField;
21 }
22 $uniqueFieldsInStr = array_unique($matchedField[0]);
23 foreach ($uniqueFieldsInStr as $key => $value) {
24 $fieldName = substr($value, 2, strlen($value) - 3);
25 $fieldValue = null;
26 if (isset($fieldValues[$fieldName])) {
27 $targetFieldValue = isset($fieldValues[$fieldName]['value']) ? $fieldValues[$fieldName]['value'] : $fieldValues[$fieldName];
28 if ('array' === gettype($targetFieldValue) || 'object' === gettype($targetFieldValue)) {
29 foreach ((array)$targetFieldValue as $singleTargetVal) {
30 if (isset($fieldValue)) {
31 if (is_numeric($fieldValue) && is_numeric($singleTargetVal)) {
32 $fieldValue = $fieldValue + $singleTargetVal;
33 } else {
34 $fieldValue = "$fieldValue, $singleTargetVal";
35 }
36 } else {
37 $fieldValue = $singleTargetVal;
38 }
39 }
40 // $fieldValue = wp_json_encode($targetFieldValue);
41 } else {
42 $fieldValue = strval($targetFieldValue);
43 }
44 $stringToReplaceField = str_replace($value, $fieldValue, $stringToReplaceField);
45 } else {
46 $stringToReplaceField = str_replace($value, '', $stringToReplaceField);
47 }
48 }
49
50 return $stringToReplaceField;
51 }
52
53 public static function validateMailArry($emailAddresses, $fieldValues)
54 {
55 if (!is_array($emailAddresses)) {
56 return [FieldValueHandler::replaceFieldWithValue($emailAddresses, $fieldValues)];
57 }
58 foreach ($emailAddresses as $key => $email) {
59 if (!is_email($email)) {
60 $email = FieldValueHandler::replaceFieldWithValue($email, $fieldValues);
61 if (is_email($email)) {
62 $emailAddresses[$key] = $email;
63 }
64 }
65 }
66 return $emailAddresses;
67 }
68
69 public static function replaceSmartTagWithValue($fieldValues)
70 {
71 $fieldPattern = '/(\${_[^{]*?)(?=\})}/';
72 $matchPattern = preg_match_all($fieldPattern, $fieldValues, $matchedField);
73 if (!$matchPattern) {
74 return $fieldValues;
75 }
76
77 $ajaxRequest = false;
78
79 if ('bitforms_trigger_workflow' === isset($_REQUEST['action'])) {
80 $ajaxRequest = true;
81 }
82
83 $data = SmartTags::getPostUserData($ajaxRequest);
84 $specialtagFields = SmartTags::smartTagFieldKeys();
85
86 foreach (array_unique($matchedField[0]) as $value) {
87 $fieldName = trim(substr($value, 2, strlen($value) - 3));
88
89 $matches = preg_match('/\("*([^\)]+"*)\)/', $value, $matchCustomFormat);
90
91 $customValue = '';
92 if ($matches) {
93 $removeQuote = ["'", '"'];
94 $customValue = str_replace($removeQuote, '', $matchCustomFormat[1]);
95 $fieldName = str_replace($matchCustomFormat[0], '', $fieldName);
96 }
97
98 if (in_array($fieldName, $specialtagFields)) {
99 $tagFieldValues = SmartTags::getSmartTagValue($fieldName, $data, $customValue);
100 $fieldValues = str_replace($value, $tagFieldValues, $fieldValues);
101 }
102 }
103 return $fieldValues;
104 }
105
106 public static function isEmpty($val)
107 {
108 if (empty($val) && !in_array($val, ['0', 0, 0.0], true)) {
109 return true;
110 }
111 return false;
112 }
113
114 public static function formatFieldValueForMail($fields, $fieldValues)
115 {
116 $formattedFldValues = $fieldValues;
117 $file_upload_types = ['file-up', 'advanced-file-up'];
118
119 foreach ($fields as $fldKey => $fldData) {
120 if (in_array($fldData->typ, $file_upload_types)) {
121 continue;
122 }
123 if (array_key_exists($fldKey, $fieldValues)) {
124 if (is_array($fieldValues[$fldKey])) {
125 $formattedFldValues[$fldKey] = htmlspecialchars(implode(', ', $fieldValues[$fldKey]));
126 } else {
127 $formattedFldValues[$fldKey] = htmlspecialchars($fieldValues[$fldKey]);
128 }
129 if ('textarea' === $fldData->typ) {
130 $formattedFldValues[$fldKey] = nl2br(htmlspecialchars($fieldValues[$fldKey]));
131 }
132 if ('date' === $fldData->typ) {
133 $formattedFldValues[$fldKey] = date_i18n(get_option('date_format'), strtotime(htmlspecialchars($fieldValues[$fldKey])));
134 }
135 }
136 }
137
138 return $formattedFldValues;
139 }
140 }
141