| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Frontend\Form\View; |
| 4 |
|
| 5 |
use BitCode\BitForm\Admin\Form\Helpers; |
| 6 |
use BitCode\BitForm\Core\Util\FieldValueHandler; |
| 7 |
use BitCode\BitForm\Core\Util\FrontendHelpers; |
| 8 |
|
| 9 |
class FieldHelpers |
| 10 |
{ |
| 11 |
private $_fld; |
| 12 |
private $_fk; |
| 13 |
private $_form_atomic_Cls_map; |
| 14 |
|
| 15 |
public function __construct($field, $fk, $form_atomic_Cls_map = null) |
| 16 |
{ |
| 17 |
$this->_fld = $field; |
| 18 |
$this->_fk = $fk; |
| 19 |
$this->_form_atomic_Cls_map = $form_atomic_Cls_map; |
| 20 |
} |
| 21 |
|
| 22 |
public function getCustomAttributes($element) |
| 23 |
{ |
| 24 |
if ($this->property_exists_nested($this->_fld, "customAttributes->{$element}")) { |
| 25 |
$attrString = ''; |
| 26 |
$customAttributs = $this->_fld->customAttributes->{$element}; |
| 27 |
|
| 28 |
foreach ($customAttributs as $attr) { |
| 29 |
$attrString .= " {$this->esc_attr($attr->key)}='{$this->esc_attr($attr->value)}'"; |
| 30 |
} |
| 31 |
return $attrString; |
| 32 |
} |
| 33 |
return ''; |
| 34 |
} |
| 35 |
|
| 36 |
public function getCustomClasses($element) |
| 37 |
{ |
| 38 |
if ($this->property_exists_nested($this->_fld, "customClasses->{$element}")) { |
| 39 |
return $this->esc_attr($this->_fld->customClasses->{$element}); |
| 40 |
} |
| 41 |
return ''; |
| 42 |
} |
| 43 |
|
| 44 |
public function renderHTMR($content) |
| 45 |
{ |
| 46 |
$safeContent = Helpers::sanitizeUserHTML($content); |
| 47 |
return $safeContent; |
| 48 |
} |
| 49 |
|
| 50 |
public function getAtomicCls($element) |
| 51 |
{ |
| 52 |
if (empty($this->_fld) && empty($this->_fk)) { |
| 53 |
if (property_exists($this->_form_atomic_Cls_map, ".$element")) { |
| 54 |
$getAtomicCls = $this->_form_atomic_Cls_map->{".$element"}; |
| 55 |
return implode(' ', $getAtomicCls) . " $element"; |
| 56 |
} |
| 57 |
return $element; |
| 58 |
} |
| 59 |
if ('advanced-file-up' === $this->_fld->typ && isset($this->_form_atomic_Cls_map->{".$element"})) { |
| 60 |
$getAtomicCls = $this->_form_atomic_Cls_map->{".$element"}; |
| 61 |
return implode(' ', $getAtomicCls) . " {$this->_fk}-{$element}"; |
| 62 |
} |
| 63 |
$cls = ".$this->_fk-$element"; |
| 64 |
if (isset($this->_form_atomic_Cls_map->{$cls})) { |
| 65 |
$getAtomicCls = $this->_form_atomic_Cls_map->{$cls}; |
| 66 |
return implode(' ', $getAtomicCls) . " {$this->_fk}-{$element} bf-{$element}"; |
| 67 |
} |
| 68 |
return "{$this->_fk}-{$element} bf-{$element}"; |
| 69 |
} |
| 70 |
|
| 71 |
public function replaceToBackSlash($str) |
| 72 |
{ |
| 73 |
$phReplaceToBackslash = str_replace('$_bf_$', '\\', $str); // Replace React inputs value "$_bf_$" to '\\' |
| 74 |
return FieldValueHandler::replaceSmartTagWithValue($phReplaceToBackslash); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @param $element @exmp $element = 'fld-wrp' |
| 79 |
*/ |
| 80 |
public function icon($icnPropName, $element) |
| 81 |
{ |
| 82 |
if ($this->property_exists_nested($this->_fld, $icnPropName, '', 1)) { |
| 83 |
return sprintf( |
| 84 |
'<img |
| 85 |
%1$s |
| 86 |
class="%2$s %3$s" |
| 87 |
src="%4$s" |
| 88 |
alt="" |
| 89 |
/>', |
| 90 |
$this->getCustomAttributes($element), |
| 91 |
$this->getAtomicCls($element), |
| 92 |
$this->getCustomClasses($element), |
| 93 |
$this->esc_url($this->_fld->{$icnPropName}) |
| 94 |
); |
| 95 |
} |
| 96 |
return ''; |
| 97 |
} |
| 98 |
|
| 99 |
public function property_exists_nested($obj, $path = '', $valToCheck = null, $checkNegativeVal = 0) |
| 100 |
{ |
| 101 |
$path = explode('->', $path); |
| 102 |
$current = $obj; |
| 103 |
foreach ($path as $key) { |
| 104 |
if (is_object($current)) { |
| 105 |
if (property_exists($current, $key)) { |
| 106 |
$current = $current->{$key}; |
| 107 |
} else { |
| 108 |
return false; |
| 109 |
} |
| 110 |
} else { |
| 111 |
break; |
| 112 |
} |
| 113 |
} |
| 114 |
if (isset($valToCheck)) { |
| 115 |
if ($checkNegativeVal) { |
| 116 |
return $current !== $valToCheck; |
| 117 |
} |
| 118 |
return $current === $valToCheck; |
| 119 |
} |
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
public function required() |
| 124 |
{ |
| 125 |
if ($this->property_exists_nested($this->_fld, 'valid->req', true) && $this->property_exists_nested($this->_fld, 'err->req->show', true)) { |
| 126 |
return 'required'; |
| 127 |
} |
| 128 |
|
| 129 |
return ''; |
| 130 |
} |
| 131 |
|
| 132 |
public function disabled() |
| 133 |
{ |
| 134 |
if ($this->property_exists_nested($this->_fld, 'valid->disabled', true)) { |
| 135 |
return 'disabled'; |
| 136 |
} |
| 137 |
return ''; |
| 138 |
} |
| 139 |
|
| 140 |
public function readonly() |
| 141 |
{ |
| 142 |
if ($this->property_exists_nested($this->_fld, 'readonly', true)) { |
| 143 |
return 'readonly'; |
| 144 |
} |
| 145 |
if ($this->property_exists_nested($this->_fld, 'valid->readonly', true)) { |
| 146 |
return 'readonly'; |
| 147 |
} |
| 148 |
return ''; |
| 149 |
} |
| 150 |
|
| 151 |
public function placeholder() |
| 152 |
{ |
| 153 |
if ($this->property_exists_nested($this->_fld, 'ph', '', 1)) { |
| 154 |
return "placeholder='{$this->esc_attr($this->replaceToBackSlash($this->_fld->ph))}'"; |
| 155 |
} |
| 156 |
return ''; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* @param $str |
| 161 |
* for attribute value |
| 162 |
* @return string |
| 163 |
*/ |
| 164 |
public function esc_attr($str) |
| 165 |
{ |
| 166 |
return esc_attr($str); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* @param $str |
| 171 |
* for html content that don't need any html markup |
| 172 |
* @return string |
| 173 |
*/ |
| 174 |
public function esc_html($str) |
| 175 |
{ |
| 176 |
return esc_html($str); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* @param $str |
| 181 |
* for textarea content |
| 182 |
* @return string |
| 183 |
*/ |
| 184 |
public function esc_textarea($str) |
| 185 |
{ |
| 186 |
return esc_textarea($str); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* @param $str |
| 191 |
* for url |
| 192 |
* @return string |
| 193 |
*/ |
| 194 |
public function esc_url($str) |
| 195 |
{ |
| 196 |
return esc_url($str); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* @param $str |
| 201 |
* for content that needs html markup but escapes all scripts |
| 202 |
* @return string |
| 203 |
*/ |
| 204 |
public function kses_post($str) |
| 205 |
{ |
| 206 |
return wp_kses_post($str); |
| 207 |
} |
| 208 |
|
| 209 |
public function name() |
| 210 |
{ |
| 211 |
if ($this->property_exists_nested($this->_fld, 'fieldName', '', 1)) { |
| 212 |
if ($this->property_exists_nested($this->_fld, 'typ', 'check')) { |
| 213 |
return "name='{$this->esc_attr($this->_fld->fieldName)}[]'"; |
| 214 |
} |
| 215 |
if ($this->property_exists_nested($this->_fld, 'typ', 'file-up') && $this->property_exists_nested($this->_fld, 'config->multiple', true)) { |
| 216 |
return "name='{$this->esc_attr($this->_fld->fieldName)}[]'"; |
| 217 |
} |
| 218 |
if ($this->property_exists_nested($this->_fld, 'typ', 'image-select') |
| 219 |
&& $this->property_exists_nested($this->_fld, 'inpType', 'checkbox')) { |
| 220 |
return "name='{$this->esc_attr($this->_fld->fieldName)}[]'"; |
| 221 |
} |
| 222 |
return "name='{$this->esc_attr($this->_fld->fieldName)}'"; |
| 223 |
} |
| 224 |
return ''; |
| 225 |
} |
| 226 |
|
| 227 |
public function value() |
| 228 |
{ |
| 229 |
$val = ''; |
| 230 |
if ($this->property_exists_nested($this->_fld, 'val', '', 1)) { |
| 231 |
$val = $this->_fld->val; |
| 232 |
} elseif ($this->property_exists_nested($this->_fld, 'defaultValue', '', 1)) { |
| 233 |
$val = $this->_fld->defaultValue; |
| 234 |
} |
| 235 |
if (isset($val) && '' !== $val && 'textarea' !== $this->_fld->typ) { |
| 236 |
return "value='{$this->esc_attr($val)}'"; |
| 237 |
} |
| 238 |
return $val; |
| 239 |
} |
| 240 |
|
| 241 |
public function autoComplete() |
| 242 |
{ |
| 243 |
if ($this->property_exists_nested($this->_fld, 'ac', '', 1)) { |
| 244 |
return "autocomplete='{$this->esc_attr($this->_fld->ac)}'"; |
| 245 |
} |
| 246 |
return ''; |
| 247 |
} |
| 248 |
|
| 249 |
// getter & setter function for $_fk |
| 250 |
public function getFk() |
| 251 |
{ |
| 252 |
return $this->_fk; |
| 253 |
} |
| 254 |
|
| 255 |
public function setFk($fk) |
| 256 |
{ |
| 257 |
$this->_fk = $fk; |
| 258 |
} |
| 259 |
|
| 260 |
public function getFieldKeyWithContentCount() |
| 261 |
{ |
| 262 |
$bfFrontendFormIds = FrontendHelpers::$bfFrontendFormIds; |
| 263 |
$contentCount = count($bfFrontendFormIds); |
| 264 |
return "{$this->_fk}-{$contentCount}"; |
| 265 |
} |
| 266 |
} |
| 267 |
|