| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Frontend\Form\View\Theme\Fields; |
| 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 |
return $title; |
| 46 |
} |
| 47 |
|
| 48 |
public function getAtomicCls($element) |
| 49 |
{ |
| 50 |
if (empty($this->_fld) && empty($this->_fk)) { |
| 51 |
if (property_exists($this->_form_atomic_Cls_map, ".$element")) { |
| 52 |
$getAtomicCls = $this->_form_atomic_Cls_map->{".$element"}; |
| 53 |
return implode(' ', $getAtomicCls) . " $element"; |
| 54 |
} |
| 55 |
return $element; |
| 56 |
} |
| 57 |
if ('advanced-file-up' === $this->_fld->typ && isset($this->_form_atomic_Cls_map->{".$element"})) { |
| 58 |
$getAtomicCls = $this->_form_atomic_Cls_map->{".$element"}; |
| 59 |
return implode(' ', $getAtomicCls) . " {$this->_fk}-{$element}"; |
| 60 |
} |
| 61 |
$cls = ".$this->_fk-$element"; |
| 62 |
if (isset($this->_form_atomic_Cls_map->{$cls})) { |
| 63 |
$getAtomicCls = $this->_form_atomic_Cls_map->{$cls}; |
| 64 |
return implode(' ', $getAtomicCls) . " {$this->_fk}-{$element}"; |
| 65 |
} |
| 66 |
return "{$this->_fk}-{$element}"; |
| 67 |
} |
| 68 |
|
| 69 |
public function replaceToBackSlash($str) |
| 70 |
{ |
| 71 |
$phReplaceToBackslash = str_replace('$_bf_$', '\\', $str); // Replace React inputs value "$_bf_$" to '\\' |
| 72 |
return FieldValueHandler::replaceSmartTagWithValue($phReplaceToBackslash); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @param $element @exmp $element = 'fld-wrp' |
| 77 |
*/ |
| 78 |
public function icon($icnPropName, $element) |
| 79 |
{ |
| 80 |
if ($this->property_exists_nested($this->_fld, $icnPropName, '', 1)) { |
| 81 |
return <<<ICON |
| 82 |
<img |
| 83 |
{$this->getCustomAttributes($element)} |
| 84 |
class="{$this->getAtomicCls($element)} {$this->getCustomClasses($element)}" |
| 85 |
src="{$this->esc_url($this->_fld->{$icnPropName})}" |
| 86 |
alt="" |
| 87 |
/> |
| 88 |
ICON; |
| 89 |
} |
| 90 |
return ''; |
| 91 |
} |
| 92 |
|
| 93 |
public function property_exists_nested($obj, $path = '', $valToCheck = null, $checkNegativeVal = 0) |
| 94 |
{ |
| 95 |
$path = explode('->', $path); |
| 96 |
$current = $obj; |
| 97 |
foreach ($path as $key) { |
| 98 |
if (is_object($current)) { |
| 99 |
if (property_exists($current, $key)) { |
| 100 |
$current = $current->{$key}; |
| 101 |
} else { |
| 102 |
return false; |
| 103 |
} |
| 104 |
} else { |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
if (isset($valToCheck)) { |
| 109 |
if ($checkNegativeVal) { |
| 110 |
return $current !== $valToCheck; |
| 111 |
} |
| 112 |
return $current === $valToCheck; |
| 113 |
} |
| 114 |
return true; |
| 115 |
} |
| 116 |
|
| 117 |
public function required() |
| 118 |
{ |
| 119 |
if ($this->property_exists_nested($this->_fld, 'valid->req', true) && $this->property_exists_nested($this->_fld, 'err->req->show', true)) { |
| 120 |
return 'required'; |
| 121 |
} |
| 122 |
|
| 123 |
return ''; |
| 124 |
} |
| 125 |
|
| 126 |
public function disabled() |
| 127 |
{ |
| 128 |
if ($this->property_exists_nested($this->_fld, 'valid->disabled', true)) { |
| 129 |
return 'disabled'; |
| 130 |
} |
| 131 |
return ''; |
| 132 |
} |
| 133 |
|
| 134 |
public function readonly() |
| 135 |
{ |
| 136 |
if ($this->property_exists_nested($this->_fld, 'readonly', true)) { |
| 137 |
return 'readonly'; |
| 138 |
} |
| 139 |
if ($this->property_exists_nested($this->_fld, 'valid->readonly', true)) { |
| 140 |
return 'readonly'; |
| 141 |
} |
| 142 |
return ''; |
| 143 |
} |
| 144 |
|
| 145 |
public function placeholder() |
| 146 |
{ |
| 147 |
if ($this->property_exists_nested($this->_fld, 'ph', '', 1)) { |
| 148 |
return "placeholder='{$this->esc_attr($this->replaceToBackSlash($this->_fld->ph))}'"; |
| 149 |
} |
| 150 |
return ''; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* @param $str |
| 155 |
* for attribute value |
| 156 |
* @return string |
| 157 |
*/ |
| 158 |
public function esc_attr($str) |
| 159 |
{ |
| 160 |
return esc_attr($str); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* @param $str |
| 165 |
* for html content that don't need any html markup |
| 166 |
* @return string |
| 167 |
*/ |
| 168 |
public function esc_html($str) |
| 169 |
{ |
| 170 |
return esc_html($str); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* @param $str |
| 175 |
* for textarea content |
| 176 |
* @return string |
| 177 |
*/ |
| 178 |
public function esc_textarea($str) |
| 179 |
{ |
| 180 |
return esc_textarea($str); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* @param $str |
| 185 |
* for url |
| 186 |
* @return string |
| 187 |
*/ |
| 188 |
public function esc_url($str) |
| 189 |
{ |
| 190 |
return esc_url($str); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* @param $str |
| 195 |
* for content that needs html markup but escapes all scripts |
| 196 |
* @return string |
| 197 |
*/ |
| 198 |
public function kses_post($str) |
| 199 |
{ |
| 200 |
return wp_kses_post($str); |
| 201 |
} |
| 202 |
|
| 203 |
public function name() |
| 204 |
{ |
| 205 |
if ($this->property_exists_nested($this->_fld, 'fieldName', '', 1)) { |
| 206 |
if ($this->property_exists_nested($this->_fld, 'typ', 'check')) { |
| 207 |
return "name='{$this->esc_attr($this->_fld->fieldName)}[]'"; |
| 208 |
} |
| 209 |
if ($this->property_exists_nested($this->_fld, 'typ', 'file-up') && $this->property_exists_nested($this->_fld, 'config->multiple', true)) { |
| 210 |
return "name='{$this->esc_attr($this->_fld->fieldName)}[]'"; |
| 211 |
} |
| 212 |
if ($this->property_exists_nested($this->_fld, 'typ', 'image-select') |
| 213 |
&& $this->property_exists_nested($this->_fld, 'inpType', 'checkbox')) { |
| 214 |
return "name='{$this->esc_attr($this->_fld->fieldName)}[]'"; |
| 215 |
} |
| 216 |
return "name='{$this->esc_attr($this->_fld->fieldName)}'"; |
| 217 |
} |
| 218 |
return ''; |
| 219 |
} |
| 220 |
|
| 221 |
public function value() |
| 222 |
{ |
| 223 |
$val = ''; |
| 224 |
if ($this->property_exists_nested($this->_fld, 'val', '', 1)) { |
| 225 |
$val = $this->_fld->val; |
| 226 |
} elseif ($this->property_exists_nested($this->_fld, 'defaultValue', '', 1)) { |
| 227 |
$val = $this->_fld->defaultValue; |
| 228 |
} |
| 229 |
if (!empty($val) && 'textarea' !== $this->_fld->typ) { |
| 230 |
return "value='{$this->esc_attr($val)}'"; |
| 231 |
} |
| 232 |
return $val; |
| 233 |
} |
| 234 |
|
| 235 |
public function autoComplete() |
| 236 |
{ |
| 237 |
if ($this->property_exists_nested($this->_fld, 'ac', '', 1)) { |
| 238 |
return "autocomplete='{$this->esc_attr($this->_fld->ac)}'"; |
| 239 |
} |
| 240 |
return ''; |
| 241 |
} |
| 242 |
|
| 243 |
public function getFieldKeyWithContentCount() |
| 244 |
{ |
| 245 |
$bfFrontendFormIds = FrontendHelpers::$bfFrontendFormIds; |
| 246 |
$contentCount = count($bfFrontendFormIds); |
| 247 |
return "{$this->_fk}-{$contentCount}"; |
| 248 |
} |
| 249 |
} |
| 250 |
|