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