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

117 lines 3.9 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 public static function replaceFieldWithValue($stringToReplaceField, $fieldValues) {
7 if (empty($stringToReplaceField)) {
8 return $stringToReplaceField;
9 }
10 if (!is_string($stringToReplaceField)) {
11 $stringToReplaceField = wp_json_encode($stringToReplaceField);
12 }
13 $stringToReplaceField = static::replaceSmartTagWithValue($stringToReplaceField);
14 $fieldPattern = '/\${\w[^ ${}]*}/';
15
16 preg_match_all($fieldPattern, $stringToReplaceField, $matchedField);
17 if (empty($matchedField)) {
18 return $stringToReplaceField;
19 }
20 $uniqueFieldsInStr = array_unique($matchedField[0]);
21 foreach ($uniqueFieldsInStr as $key => $value) {
22 $fieldName = substr($value, 2, strlen($value) - 3);
23 $fieldValue = null;
24 if (isset($fieldValues[$fieldName])) {
25 $targetFieldValue = isset($fieldValues[$fieldName]['value']) ? $fieldValues[$fieldName]['value'] : $fieldValues[$fieldName];
26 if ('array' === gettype($targetFieldValue) || 'object' === gettype($targetFieldValue)) {
27 foreach ((array)$targetFieldValue as $singleTargetVal) {
28 if (isset($fieldValue)) {
29 if (is_numeric($fieldValue) && is_numeric($singleTargetVal)) {
30 $fieldValue = $fieldValue + $singleTargetVal;
31 } else {
32 $fieldValue = "$fieldValue, $singleTargetVal";
33 }
34 } else {
35 $fieldValue = $singleTargetVal;
36 }
37 }
38 // $fieldValue = wp_json_encode($targetFieldValue);
39 } else {
40 $fieldValue = strval($targetFieldValue);
41 }
42 $stringToReplaceField = str_replace($value, $fieldValue, $stringToReplaceField);
43 } else {
44 $stringToReplaceField = str_replace($value, '', $stringToReplaceField);
45 }
46 }
47
48 return $stringToReplaceField;
49 }
50
51 public static function validateMailArry($emailAddresses, $fieldValues) {
52 if (!is_array($emailAddresses)) {
53 return [FieldValueHandler::replaceFieldWithValue($emailAddresses, $fieldValues)];
54 }
55 foreach ($emailAddresses as $key => $email) {
56 if (!is_email($email)) {
57 $email = FieldValueHandler::replaceFieldWithValue($email, $fieldValues);
58 if (is_email($email)) {
59 $emailAddresses[$key] = $email;
60 }
61 }
62 }
63 return $emailAddresses;
64 }
65
66 public static function replaceSmartTagWithValue($fieldValues) {
67 $fieldPattern = '/(\${_[^{]*?)(?=\})}/';
68 $matchPattern = preg_match_all($fieldPattern, $fieldValues, $matchedField);
69 if (!$matchPattern) {
70 return $fieldValues;
71 }
72
73 $ajaxRequest = false;
74
75 if ('bitforms_trigger_workflow' === isset($_REQUEST['action'])) {
76 $ajaxRequest = true;
77 }
78
79 foreach (array_unique($matchedField[0]) as $value) {
80 $fieldName = trim(substr($value, 2, strlen($value) - 3));
81
82 $matches = preg_match('/\("*([^\)]+"*)\)/', $value, $matchCustomFormat);
83
84 $customValue = '';
85 if ($matches) {
86 $removeQuote = ["'", '"'];
87 $customValue = str_replace($removeQuote, '', $matchCustomFormat[1]);
88 $fieldName = str_replace($matchCustomFormat[0], '', $fieldName);
89 }
90
91 $tagFieldValues = SmartTags::getSmartTagValue($fieldName, $ajaxRequest, $customValue);
92 $fieldValues = str_replace($value, $tagFieldValues, $fieldValues);
93 }
94 return $fieldValues;
95 }
96
97 public static function isEmpty($val) {
98 if (empty($val) && !in_array($val, ['0', 0, 0.0], true)) {
99 return true;
100 }
101 return false;
102 }
103
104 public static function formatFieldValueForMail($fields, $fieldValues) {
105 $formattedFldValues = $fieldValues;
106
107 foreach ($fields as $fldKey => $fldData) {
108 $formattedFldValues[$fldKey] = htmlspecialchars($formattedFldValues[$fldKey]);
109 if ('textarea' === $fldData->typ) {
110 $formattedFldValues[$fldKey] = htmlspecialchars(nl2br($fieldValues[$fldKey]));
111 }
112 }
113
114 return $formattedFldValues;
115 }
116 }
117