| 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 |
preg_match_all($fieldPattern, $stringToReplaceField, $matchedField); |
| 18 |
if (empty($matchedField)) { |
| 19 |
return $stringToReplaceField; |
| 20 |
} |
| 21 |
$uniqueFieldsInStr = array_unique($matchedField[0]); |
| 22 |
foreach ($uniqueFieldsInStr as $key => $value) { |
| 23 |
$fieldName = substr($value, 2, strlen($value) - 3); |
| 24 |
$fieldValue = null; |
| 25 |
if (isset($fieldValues[$fieldName])) { |
| 26 |
$targetFieldValue = isset($fieldValues[$fieldName]['value']) ? $fieldValues[$fieldName]['value'] : $fieldValues[$fieldName]; |
| 27 |
if ('array' === gettype($targetFieldValue) || 'object' === gettype($targetFieldValue)) { |
| 28 |
foreach ((array)$targetFieldValue as $singleTargetVal) { |
| 29 |
if (isset($fieldValue)) { |
| 30 |
if (is_numeric($fieldValue) && is_numeric($singleTargetVal)) { |
| 31 |
$fieldValue = $fieldValue + $singleTargetVal; |
| 32 |
} else { |
| 33 |
$fieldValue = "$fieldValue, $singleTargetVal"; |
| 34 |
} |
| 35 |
} else { |
| 36 |
$fieldValue = $singleTargetVal; |
| 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 |
|
| 49 |
// check if the string is a function like : "${_bf_calc(${b27-5}*10)}" |
| 50 |
// TO DO: Implement the function properly |
| 51 |
// if (self::isFunction($stringToReplaceField)) { |
| 52 |
// $functionName = self::getFunctionName($stringToReplaceField); |
| 53 |
|
| 54 |
// switch ($functionName) { |
| 55 |
// case '_bf_calc': |
| 56 |
// return self::getFunctionParameter($stringToReplaceField); |
| 57 |
// case '_bf_count': |
| 58 |
// return self::getCountValue($stringToReplaceField); |
| 59 |
// default: |
| 60 |
// return 0; |
| 61 |
// } |
| 62 |
// } |
| 63 |
return $stringToReplaceField; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Summary of getCountValue - get the count value from the function string "${_bf_count(item-1, item-2)}" => 2 |
| 68 |
* |
| 69 |
* @param string $functionString |
| 70 |
* @return int |
| 71 |
*/ |
| 72 |
private static function getCountValue(string $functionString): int |
| 73 |
{ |
| 74 |
$options = self::getFunctionParameter($functionString); |
| 75 |
$option = explode(',', $options); |
| 76 |
return count($option); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Summary of getFunctionParameter - get the function parameter from the function string "${_bf_calc(2*10)}" => 2*10 |
| 81 |
* |
| 82 |
* @param string $functionString |
| 83 |
* @return string |
| 84 |
*/ |
| 85 |
private static function getFunctionParameter(string $functionString): string |
| 86 |
{ |
| 87 |
$regexPattern = '/\(([^)]*)\)/'; |
| 88 |
preg_match($regexPattern, $functionString, $matches); |
| 89 |
return $matches[1]; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Summary of isFunction - check if the string is a function "${_bf_calc(${b27-5}*10)}" or not "${_bf_date}" |
| 94 |
* |
| 95 |
* @param string $functionString |
| 96 |
* @return bool true if the string is a function else false |
| 97 |
*/ |
| 98 |
private static function isFunction(string $functionString): bool |
| 99 |
{ |
| 100 |
$regexPattern = '/\([^)]*\)/'; |
| 101 |
return preg_match($regexPattern, $functionString); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Summary of getFunctionName - get the function name from the function string "${_bf_calc(${b27-5}*10)}" => _bf_calc |
| 106 |
* |
| 107 |
* @param string $functionString |
| 108 |
* @return string |
| 109 |
*/ |
| 110 |
private static function getFunctionName(string $functionString): string |
| 111 |
{ |
| 112 |
$regexPattern = '/\b([a-zA-Z_][a-zA-Z0-9_]*)\(/'; |
| 113 |
preg_match($regexPattern, $functionString, $matches); |
| 114 |
return $matches[1]; |
| 115 |
} |
| 116 |
|
| 117 |
public static function validateMailArry($emailAddresses, $fieldValues) |
| 118 |
{ |
| 119 |
if (!is_array($emailAddresses)) { |
| 120 |
return [FieldValueHandler::replaceFieldWithValue($emailAddresses, $fieldValues)]; |
| 121 |
} |
| 122 |
foreach ($emailAddresses as $key => $email) { |
| 123 |
if (!is_email($email)) { |
| 124 |
$email = FieldValueHandler::replaceFieldWithValue($email, $fieldValues); |
| 125 |
if (is_email($email)) { |
| 126 |
$emailAddresses[$key] = $email; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
return $emailAddresses; |
| 131 |
} |
| 132 |
|
| 133 |
public static function replaceSmartTagWithValue($fieldValues) |
| 134 |
{ |
| 135 |
$fieldPattern = '/(\${_[^{]*?)(?=\})}/'; |
| 136 |
$matchPattern = preg_match_all($fieldPattern, $fieldValues, $matchedField); |
| 137 |
if (!$matchPattern) { |
| 138 |
return $fieldValues; |
| 139 |
} |
| 140 |
|
| 141 |
$ajaxRequest = false; |
| 142 |
if (isset($_REQUEST['action']) && 'bitforms_trigger_workflow' === $_REQUEST['action']) { |
| 143 |
$ajaxRequest = true; |
| 144 |
} |
| 145 |
|
| 146 |
foreach (array_unique($matchedField[0]) as $value) { |
| 147 |
$fieldName = trim(substr($value, 2, strlen($value) - 3)); |
| 148 |
|
| 149 |
$matches = preg_match('/\("*([^\)]+"*)\)/', $value, $matchCustomFormat); |
| 150 |
|
| 151 |
$customValue = ''; |
| 152 |
if ($matches) { |
| 153 |
$removeQuote = ["'", '"']; |
| 154 |
$customValue = str_replace($removeQuote, '', $matchCustomFormat[1]); |
| 155 |
$fieldName = str_replace($matchCustomFormat[0], '', $fieldName); |
| 156 |
} |
| 157 |
|
| 158 |
$tagFieldValues = SmartTags::getSmartTagValue($fieldName, $ajaxRequest, $customValue); |
| 159 |
$fieldValues = str_replace($value, $tagFieldValues, $fieldValues); |
| 160 |
} |
| 161 |
return $fieldValues; |
| 162 |
} |
| 163 |
|
| 164 |
public static function isEmpty($val) |
| 165 |
{ |
| 166 |
if (empty($val) && !in_array($val, ['0', 0, 0.0], true)) { |
| 167 |
return true; |
| 168 |
} |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
public static function formatFieldValueForMail($fields, $fieldValues) |
| 173 |
{ |
| 174 |
$formattedFldValues = $fieldValues; |
| 175 |
$file_upload_types = ['file-up', 'advanced-file-up']; |
| 176 |
|
| 177 |
foreach ($fields as $fldKey => $fldData) { |
| 178 |
if (in_array($fldData->typ, $file_upload_types)) { |
| 179 |
continue; |
| 180 |
} |
| 181 |
if (array_key_exists($fldKey, $fieldValues)) { |
| 182 |
$value = $fieldValues[$fldKey]; |
| 183 |
// if (is_array($value)) { |
| 184 |
// $formattedFldValues[$fldKey] = htmlspecialchars(implode(', ', $value)); |
| 185 |
// } else { |
| 186 |
// $formattedFldValues[$fldKey] = htmlspecialchars($value); |
| 187 |
// } |
| 188 |
if (is_array($value)) { |
| 189 |
foreach ($value as $subValue) { |
| 190 |
if (is_array($subValue)) { |
| 191 |
foreach ($subValue as $subSubKey => $subSubValue) { |
| 192 |
$stringValue = is_array($subSubValue) ? implode(', ', $subSubValue) : $subSubValue . ', '; |
| 193 |
$stringValue = htmlspecialchars($stringValue); |
| 194 |
if (array_key_exists($subSubKey, $formattedFldValues)) { |
| 195 |
$formattedFldValues[$subSubKey] .= $stringValue; |
| 196 |
} else { |
| 197 |
$formattedFldValues[$subSubKey] = $stringValue; |
| 198 |
} |
| 199 |
} |
| 200 |
} else { |
| 201 |
if (array_key_exists($fldKey, $formattedFldValues)) { |
| 202 |
if (is_array($formattedFldValues[$fldKey])) { |
| 203 |
$formattedFldValues[$fldKey] = htmlspecialchars(implode(', ', $formattedFldValues[$fldKey]) . ', '); |
| 204 |
} else { |
| 205 |
$formattedFldValues[$fldKey] .= htmlspecialchars($subValue); |
| 206 |
} |
| 207 |
} else { |
| 208 |
$formattedFldValues[$fldKey] = htmlspecialchars($subValue . ', '); |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
} else { |
| 213 |
$formattedFldValues[$fldKey] = htmlspecialchars($value); |
| 214 |
} |
| 215 |
if ('textarea' === $fldData->typ) { |
| 216 |
$formattedFldValues[$fldKey] = nl2br(htmlspecialchars($value)); |
| 217 |
} |
| 218 |
if ('date' === $fldData->typ) { |
| 219 |
$formattedFldValues[$fldKey] = date_i18n(get_option('date_format'), strtotime(htmlspecialchars($value))); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
return $formattedFldValues; |
| 225 |
} |
| 226 |
} |
| 227 |
|