| 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 |
if (isset($_REQUEST['action']) && 'bitforms_trigger_workflow' === $_REQUEST['action']) { |
| 79 |
$ajaxRequest = true; |
| 80 |
} |
| 81 |
|
| 82 |
foreach (array_unique($matchedField[0]) as $value) { |
| 83 |
$fieldName = trim(substr($value, 2, strlen($value) - 3)); |
| 84 |
|
| 85 |
$matches = preg_match('/\("*([^\)]+"*)\)/', $value, $matchCustomFormat); |
| 86 |
|
| 87 |
$customValue = ''; |
| 88 |
if ($matches) { |
| 89 |
$removeQuote = ["'", '"']; |
| 90 |
$customValue = str_replace($removeQuote, '', $matchCustomFormat[1]); |
| 91 |
$fieldName = str_replace($matchCustomFormat[0], '', $fieldName); |
| 92 |
} |
| 93 |
|
| 94 |
$tagFieldValues = SmartTags::getSmartTagValue($fieldName, $ajaxRequest, $customValue); |
| 95 |
$fieldValues = str_replace($value, $tagFieldValues, $fieldValues); |
| 96 |
} |
| 97 |
return $fieldValues; |
| 98 |
} |
| 99 |
|
| 100 |
public static function isEmpty($val) |
| 101 |
{ |
| 102 |
if (empty($val) && !in_array($val, ['0', 0, 0.0], true)) { |
| 103 |
return true; |
| 104 |
} |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
public static function formatFieldValueForMail($fields, $fieldValues) |
| 109 |
{ |
| 110 |
$formattedFldValues = $fieldValues; |
| 111 |
$file_upload_types = ['file-up', 'advanced-file-up']; |
| 112 |
|
| 113 |
foreach ($fields as $fldKey => $fldData) { |
| 114 |
if (in_array($fldData->typ, $file_upload_types)) { |
| 115 |
continue; |
| 116 |
} |
| 117 |
if (array_key_exists($fldKey, $fieldValues)) { |
| 118 |
if (is_array($fieldValues[$fldKey])) { |
| 119 |
$formattedFldValues[$fldKey] = htmlspecialchars(implode(', ', $fieldValues[$fldKey])); |
| 120 |
} else { |
| 121 |
$formattedFldValues[$fldKey] = htmlspecialchars($fieldValues[$fldKey]); |
| 122 |
} |
| 123 |
if ('textarea' === $fldData->typ) { |
| 124 |
$formattedFldValues[$fldKey] = nl2br(htmlspecialchars($fieldValues[$fldKey])); |
| 125 |
} |
| 126 |
if ('date' === $fldData->typ) { |
| 127 |
$formattedFldValues[$fldKey] = date_i18n(get_option('date_format'), strtotime(htmlspecialchars($fieldValues[$fldKey]))); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return $formattedFldValues; |
| 133 |
} |
| 134 |
} |
| 135 |
|