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