| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Frontend\Form\View; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Util\FrontendHelpers; |
| 6 |
|
| 7 |
class InputWrapper |
| 8 |
{ |
| 9 |
private $_fieldData; |
| 10 |
private $_fieldKey; |
| 11 |
private $_fieldName; |
| 12 |
private $_formID; |
| 13 |
private $_error; |
| 14 |
private $_value; |
| 15 |
private $_fieldHelpers; |
| 16 |
|
| 17 |
public function __construct($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error = null, $value = null) |
| 18 |
{ |
| 19 |
// Corrupt/partial form JSON can hand us a null field; normalize so |
| 20 |
// property_exists() calls never receive null (PHP 8 TypeError). |
| 21 |
$field = is_object($field) ? $field : new \stdClass(); |
| 22 |
$this->_fieldData = $field; |
| 23 |
$this->_fieldKey = $rowID; |
| 24 |
$this->_fieldName = $field_name; |
| 25 |
$this->_formID = $formID; |
| 26 |
$this->_error = $error; |
| 27 |
$this->_value = $value; |
| 28 |
$this->_fieldHelpers = new FieldHelpers($field, $rowID, $form_atomic_Cls_map); |
| 29 |
} |
| 30 |
|
| 31 |
public function wrapper($field, $noLabel = false, $noErrMsg = false) |
| 32 |
{ |
| 33 |
$labelWrapper = !$noLabel ? $this->labelWrapper() : ''; |
| 34 |
$helperText = $this->helperText(); |
| 35 |
$error = !$noErrMsg ? $this->errorMessages() : ''; |
| 36 |
$fieldTypeClass = empty($this->_fieldData->typ) ? 'bf-default-field' : 'bf-' . $this->_fieldData->typ . '-field'; |
| 37 |
|
| 38 |
return sprintf( |
| 39 |
'<div %1$s class="%2$s %3$s %4$s"> |
| 40 |
%5$s |
| 41 |
<div %6$s class="%7$s %8$s"> |
| 42 |
%9$s |
| 43 |
%10$s |
| 44 |
%11$s |
| 45 |
</div> |
| 46 |
</div>', |
| 47 |
$this->_fieldHelpers->getCustomAttributes('fld-wrp'), |
| 48 |
$fieldTypeClass, |
| 49 |
$this->_fieldHelpers->getAtomicCls('fld-wrp'), |
| 50 |
$this->_fieldHelpers->getCustomClasses('fld-wrp'), |
| 51 |
$labelWrapper, |
| 52 |
$this->_fieldHelpers->getCustomAttributes('inp-wrp'), |
| 53 |
$this->_fieldHelpers->getAtomicCls('inp-wrp'), |
| 54 |
$this->_fieldHelpers->getCustomClasses('inp-wrp'), |
| 55 |
$field, |
| 56 |
$helperText, |
| 57 |
$error |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
private function labelWrapper() |
| 62 |
{ |
| 63 |
$_label = ''; |
| 64 |
$_lblPreIcn = $this->_fieldHelpers->icon('lblPreIcn', 'lbl-pre-i'); |
| 65 |
$_lblSufIcn = $this->_fieldHelpers->icon('lblSufIcn', 'lbl-suf-i'); |
| 66 |
$_reqPre = ''; |
| 67 |
$_reqPost = ''; |
| 68 |
$_lbl = ''; |
| 69 |
$_subtitle = ''; |
| 70 |
$_subtitleText = ''; |
| 71 |
$_subtitlePreIcn = $this->_fieldHelpers->icon('subTlePreIcn', 'sub-titl-pre-i'); |
| 72 |
$_subtitlePostIcn = $this->_fieldHelpers->icon('subTleSufIcn', 'sub-titl-suf-i'); |
| 73 |
if (property_exists($this->_fieldData, 'lbl')) { |
| 74 |
$replaceToBackSlash = $this->_fieldHelpers->replaceToBackSlash($this->_fieldData->lbl); |
| 75 |
$_lbl = wp_kses_post($this->_fieldHelpers->renderHTMR($replaceToBackSlash)); |
| 76 |
} |
| 77 |
|
| 78 |
if (property_exists($this->_fieldData, 'subtitle')) { |
| 79 |
$replaceToBackSlash = $this->_fieldHelpers->replaceToBackSlash($this->_fieldData->subtitle); |
| 80 |
$_subtitleText = wp_kses_post($this->_fieldHelpers->renderHTMR($replaceToBackSlash)); |
| 81 |
} |
| 82 |
|
| 83 |
// for pre required icon |
| 84 |
// aria-hidden="true" is used because the asterisk is purely decorative/visual. |
| 85 |
// Screen readers will announce the field as required via aria-required="true" on the input element. |
| 86 |
// This prevents redundant "asterisk" or "star" announcements. |
| 87 |
if ( |
| 88 |
$this->_fieldHelpers->property_exists_nested($this->_fieldData, 'valid->req') |
| 89 |
&& $this->_fieldHelpers->property_exists_nested($this->_fieldData, 'valid->reqShow', true) |
| 90 |
&& $this->_fieldHelpers->property_exists_nested($this->_fieldData, 'valid->reqPos', 'before') |
| 91 |
) { |
| 92 |
$_reqPre = sprintf( |
| 93 |
'<span %1$s class="%2$s %3$s" aria-hidden="true">*</span>', |
| 94 |
$this->_fieldHelpers->getCustomAttributes('req-smbl'), |
| 95 |
$this->_fieldHelpers->getAtomicCls('req-smbl'), |
| 96 |
$this->_fieldHelpers->getCustomClasses('req-smbl') |
| 97 |
); |
| 98 |
} |
| 99 |
// for post required icon |
| 100 |
// Same accessibility reasoning as pre required icon - hide from screen readers |
| 101 |
// to avoid redundant announcements since aria-required is on the input. |
| 102 |
if ( |
| 103 |
$this->_fieldHelpers->property_exists_nested($this->_fieldData, 'valid->req') |
| 104 |
&& $this->_fieldHelpers->property_exists_nested($this->_fieldData, 'valid->reqShow', true) |
| 105 |
&& $this->_fieldHelpers->property_exists_nested($this->_fieldData, 'valid->reqPos', 'before', 1) |
| 106 |
) { |
| 107 |
$_reqPost = sprintf( |
| 108 |
'<span %1$s class="%2$s %3$s" aria-hidden="true">*</span>', |
| 109 |
$this->_fieldHelpers->getCustomAttributes('req-smbl'), |
| 110 |
$this->_fieldHelpers->getAtomicCls('req-smbl'), |
| 111 |
$this->_fieldHelpers->getCustomClasses('req-smbl') |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
// for Label |
| 116 |
if ( |
| 117 |
property_exists($this->_fieldData, 'lbl') |
| 118 |
// && isset($this->_fieldData->valid->hideLbl) |
| 119 |
// && $this->_fieldData->valid->hideLbl !== true |
| 120 |
) { |
| 121 |
$bfFrontendFormIds = FrontendHelpers::$bfFrontendFormIds; |
| 122 |
$contentCount = count($bfFrontendFormIds); |
| 123 |
$labelId = $this->_fieldHelpers->getLabelId(); |
| 124 |
$_label = sprintf( |
| 125 |
'<label |
| 126 |
id="%1$s" |
| 127 |
%2$s |
| 128 |
class="%3$s %4$s" |
| 129 |
for="%5$s-%6$d"> |
| 130 |
%7$s |
| 131 |
%8$s |
| 132 |
%9$s |
| 133 |
%10$s |
| 134 |
%11$s |
| 135 |
</label>', |
| 136 |
$labelId, |
| 137 |
$this->_fieldHelpers->getCustomAttributes('lbl'), |
| 138 |
$this->_fieldHelpers->getAtomicCls('lbl'), |
| 139 |
$this->_fieldHelpers->getCustomClasses('lbl'), |
| 140 |
$this->_fieldKey, |
| 141 |
$contentCount, |
| 142 |
$_reqPre, |
| 143 |
$_lblPreIcn, |
| 144 |
$_lbl, |
| 145 |
$_lblSufIcn, |
| 146 |
$_reqPost |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
// for subtitle |
| 151 |
if (property_exists($this->_fieldData, 'subtitle') && !empty($this->_fieldData->subtitleHide)) { |
| 152 |
$_subtitle = sprintf( |
| 153 |
'<div %1$s class="%2$s %3$s">%4$s%5$s%6$s</div>', |
| 154 |
$this->_fieldHelpers->getCustomAttributes('sub-titl'), |
| 155 |
$this->_fieldHelpers->getAtomicCls('sub-titl'), |
| 156 |
$this->_fieldHelpers->getCustomClasses('sub-titl'), |
| 157 |
$_subtitlePreIcn, |
| 158 |
$_subtitleText, |
| 159 |
$_subtitlePostIcn |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
return sprintf( |
| 164 |
'<div %1$s class="%2$s %3$s">%4$s%5$s</div>', |
| 165 |
$this->_fieldHelpers->getCustomAttributes('lbl-wrp'), |
| 166 |
$this->_fieldHelpers->getAtomicCls('lbl-wrp'), |
| 167 |
$this->_fieldHelpers->getCustomClasses('lbl-wrp'), |
| 168 |
$_label, |
| 169 |
$_subtitle |
| 170 |
); |
| 171 |
} |
| 172 |
|
| 173 |
private function helperText() |
| 174 |
{ |
| 175 |
$_helperTxtPreIcn = $this->_fieldHelpers->icon('hlpPreIcn', 'hlp-txt-pre-i'); |
| 176 |
$_helperTxtSufIcn = $this->_fieldHelpers->icon('hlpSufIcn', 'hlp-txt-suf-i'); |
| 177 |
$_helperTxt = ''; |
| 178 |
|
| 179 |
if (isset($this->_fieldData->helperTxt)) { |
| 180 |
$_helperTxt = wp_kses_post($this->_fieldHelpers->renderHTMR($this->_fieldData->helperTxt)); |
| 181 |
} |
| 182 |
|
| 183 |
if (property_exists($this->_fieldData, 'helperTxt') && $this->_fieldData->helperTxt) { |
| 184 |
$helperTextId = $this->_fieldHelpers->getHelperTextId(); |
| 185 |
$_helperTxt = sprintf( |
| 186 |
'<div |
| 187 |
id="%1$s" |
| 188 |
%2$s |
| 189 |
class="%3$s %4$s" |
| 190 |
> |
| 191 |
%5$s |
| 192 |
%6$s |
| 193 |
%7$s |
| 194 |
</div>', |
| 195 |
$helperTextId, |
| 196 |
$this->_fieldHelpers->getCustomAttributes('hlp-txt'), |
| 197 |
$this->_fieldHelpers->getAtomicCls('hlp-txt'), |
| 198 |
$this->_fieldHelpers->getCustomClasses('hlp-txt'), |
| 199 |
$_helperTxtPreIcn, |
| 200 |
$_helperTxt, |
| 201 |
$_helperTxtSufIcn |
| 202 |
); |
| 203 |
} |
| 204 |
|
| 205 |
// for helper text |
| 206 |
return $_helperTxt; |
| 207 |
} |
| 208 |
|
| 209 |
public function errorMessages() |
| 210 |
{ |
| 211 |
$_errorPreIcn = $this->_fieldHelpers->icon('errPreIcn', 'err-txt-pre-i'); |
| 212 |
$_errorSufIcn = $this->_fieldHelpers->icon('errSufIcn', 'err-txt-suf-i'); |
| 213 |
$_error = ''; |
| 214 |
$_style = 'opacity: 0 !important; height: 0px !important;'; |
| 215 |
|
| 216 |
if ($this->_error) { |
| 217 |
$_error = wp_kses_post($this->_error); |
| 218 |
$_style = 'margin-top: 5px !important; height: 9px !important;'; |
| 219 |
} |
| 220 |
|
| 221 |
$errorId = $this->_fieldHelpers->getErrorId(); |
| 222 |
$errMsgStart = ''; |
| 223 |
$errMsgEnd = ''; |
| 224 |
$msgStyle = "style='display: none !important;'"; |
| 225 |
$errMsgAtomicCls = $this->_fieldHelpers->getAtomicCls('err-msg'); |
| 226 |
$errMsgCustomCls = $this->_fieldHelpers->getCustomClasses('err-msg'); |
| 227 |
$errMsgCustomAttr = $this->_fieldHelpers->getCustomAttributes('err-msg'); |
| 228 |
if (!(empty($_errorPreIcn) && empty($_errorSufIcn))) { |
| 229 |
$errMsgStart = "<div {$errMsgCustomAttr} class='{$errMsgAtomicCls} {$errMsgCustomCls}' {$msgStyle}>"; |
| 230 |
$errMsgEnd = '</div>'; |
| 231 |
$msgStyle = ''; |
| 232 |
$errMsgAtomicCls = ''; |
| 233 |
$errMsgCustomCls = ''; |
| 234 |
$errMsgCustomAttr = ''; |
| 235 |
} |
| 236 |
|
| 237 |
return sprintf( |
| 238 |
'<div class="%1$s" role="alert" aria-atomic="true" aria-live="assertive" style="%2$s"> |
| 239 |
<div class="%3$s"> |
| 240 |
%4$s |
| 241 |
%5$s |
| 242 |
<div |
| 243 |
id="%6$s" |
| 244 |
%7$s |
| 245 |
%8$s |
| 246 |
class="%9$s %10$s %11$s %12$s " |
| 247 |
%13$s |
| 248 |
> |
| 249 |
%14$s |
| 250 |
</div> |
| 251 |
%15$s |
| 252 |
%16$s |
| 253 |
</div> |
| 254 |
</div>', |
| 255 |
$this->_fieldHelpers->getAtomicCls('err-wrp'), // 1 |
| 256 |
$_style, // 2 |
| 257 |
$this->_fieldHelpers->getAtomicCls('err-inner'), // 3 |
| 258 |
$errMsgStart, // 4 |
| 259 |
$_errorPreIcn, // 5 |
| 260 |
$errorId, // 6 |
| 261 |
$this->_fieldHelpers->getCustomAttributes('err-txt'), // 7 |
| 262 |
$errMsgCustomAttr, // 8 |
| 263 |
$errMsgAtomicCls, // 9 |
| 264 |
$errMsgCustomCls, // 10 |
| 265 |
$this->_fieldHelpers->getAtomicCls('err-txt'), // 11 |
| 266 |
$this->_fieldHelpers->getCustomClasses('err-txt'), // 12 |
| 267 |
$msgStyle, // 13 |
| 268 |
$_error, // 14 |
| 269 |
$_errorSufIcn, // 15 |
| 270 |
$errMsgEnd // 16 |
| 271 |
); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Get the field helpers instance for external access |
| 276 |
* @return FieldHelpers |
| 277 |
*/ |
| 278 |
public function getFieldHelpers() |
| 279 |
{ |
| 280 |
return $this->_fieldHelpers; |
| 281 |
} |
| 282 |
|
| 283 |
public function parentFieldWrapper($childFieldsInput) |
| 284 |
{ |
| 285 |
return '<div ' |
| 286 |
. $this->_fieldHelpers->getCustomAttributes('parent-fld-wrp') |
| 287 |
. ' class="' . $this->_fieldHelpers->getAtomicCls('parent-fld-wrp') . ' ' . $this->_fieldHelpers->getCustomClasses('parent-fld-wrp') . '">' |
| 288 |
. $childFieldsInput |
| 289 |
. '</div>'; |
| 290 |
} |
| 291 |
|
| 292 |
public function childFieldWrapper($fieldInput, $subFieldName = '', $colSpan = null, $childKey = '') |
| 293 |
{ |
| 294 |
$dataAttr = $subFieldName ? ' data-bf-subfield="' . esc_attr($subFieldName) . '"' : ''; |
| 295 |
$styleAttr = null !== $colSpan ? ' style="--bfv-addr-col:' . (int) $colSpan . '"' : ''; |
| 296 |
// Per-child class so conditional actions (show/hide) can collapse the whole column. |
| 297 |
$childKeyCls = $childKey ? ' ' . esc_attr($childKey) . '-sub-fld-wrp' : ''; |
| 298 |
return '<div' |
| 299 |
. $dataAttr |
| 300 |
. $styleAttr |
| 301 |
. ' ' |
| 302 |
. $this->_fieldHelpers->getCustomAttributes('child-fld-wrp') |
| 303 |
. ' class="' . $this->_fieldHelpers->getAtomicCls('child-fld-wrp') . ' ' . $this->_fieldHelpers->getCustomClasses('child-fld-wrp') . $childKeyCls . '">' |
| 304 |
. $fieldInput |
| 305 |
. '</div>'; |
| 306 |
} |
| 307 |
} |
| 308 |
|