bit-form
/
includes
/
Frontend
/
Form
/
View
/
Conversational
/
Fields
/
ConversationalInputWrapper.php
ConversationalInputWrapper.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 3.3.1, at includes/Frontend/Form/View/Conversational/Fields/ConversationalInputWrapper.php
| 1 | <?php |
| 2 | |
| 3 | namespace BitCode\BitForm\Frontend\Form\View\Conversational\Fields; |
| 4 | |
| 5 | use BitCode\BitForm\Core\Util\FrontendHelpers; |
| 6 | use BitCode\BitForm\Frontend\Form\View\InputWrapper; |
| 7 | |
| 8 | class ConversationalInputWrapper extends InputWrapper |
| 9 | { |
| 10 | private $_fieldData; |
| 11 | private $_fieldKey; |
| 12 | private $_error; |
| 13 | private $_fieldHelpers; |
| 14 | |
| 15 | public function __construct($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error = null, $value = null) |
| 16 | { |
| 17 | parent::__construct($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error, $value); |
| 18 | // Same normalization as parent — this class shadows _fieldData with its own copy. |
| 19 | $field = is_object($field) ? $field : new \stdClass(); |
| 20 | $this->_fieldData = $field; |
| 21 | $this->_fieldKey = $rowID; |
| 22 | $this->_error = $error; |
| 23 | $this->_fieldHelpers = new ConversationalFieldHelpers($formID, $field, $rowID, $form_atomic_Cls_map); |
| 24 | } |
| 25 | |
| 26 | public function wrapper($field, $noLabel = false, $noErrMsg = false) |
| 27 | { |
| 28 | $labelWrapper = !$noLabel ? $this->labelWrapper() : ''; |
| 29 | $helperText = $this->helperText(); |
| 30 | $error = !$noErrMsg ? $this->errorMessages() : ''; |
| 31 | $inputWrapper = sprintf( |
| 32 | '<div |
| 33 | %1$s |
| 34 | class="%2$s %3$s" |
| 35 | > |
| 36 | |
| 37 | %4$s |
| 38 | <div |
| 39 | %5$s |
| 40 | class="%6$s %7$s" |
| 41 | > |
| 42 | %8$s |
| 43 | %9$s |
| 44 | %10$s |
| 45 | </div> |
| 46 | </div>', |
| 47 | $this->_fieldHelpers->getCustomAttributes('fld-wrp'), // 1 |
| 48 | $this->_fieldHelpers->getConversationalCls('fld-wrp'), // 2 |
| 49 | $this->_fieldHelpers->getCustomClasses('fld-wrp'), // 3 |
| 50 | $labelWrapper, // 4 |
| 51 | $this->_fieldHelpers->getCustomAttributes('inp-wrp'), // 5 |
| 52 | $this->_fieldHelpers->getConversationalCls('inp-wrp'), // 6 |
| 53 | $this->_fieldHelpers->getCustomClasses('inp-wrp'), // 7 |
| 54 | $field, // 8 |
| 55 | $helperText, // 9 |
| 56 | $error // 10 |
| 57 | ); |
| 58 | |
| 59 | return $inputWrapper; |
| 60 | } |
| 61 | |
| 62 | private function labelWrapper() |
| 63 | { |
| 64 | $_label = ''; |
| 65 | $_lblPreIcn = $this->conversationalIcon('lblPreIcn', 'lbl-pre-i'); |
| 66 | $_lblSufIcn = $this->conversationalIcon('lblSufIcn', 'lbl-suf-i'); |
| 67 | $_reqPre = ''; |
| 68 | $_reqPost = ''; |
| 69 | $_lbl = ''; |
| 70 | $_subtitle = ''; |
| 71 | $_subtitleText = ''; |
| 72 | $_subtitlePreIcn = $this->conversationalIcon('subTlePreIcn', 'sub-titl-pre-i'); |
| 73 | $_subtitlePostIcn = $this->conversationalIcon('subTleSufIcn', 'sub-titl-suf-i'); |
| 74 | if (property_exists($this->_fieldData, 'lbl')) { |
| 75 | $replaceToBackSlash = $this->_fieldHelpers->replaceToBackSlash($this->_fieldData->lbl); |
| 76 | $_lbl = wp_kses_post($this->_fieldHelpers->renderHTMR($replaceToBackSlash)); |
| 77 | } |
| 78 | |
| 79 | if (property_exists($this->_fieldData, 'subtitle')) { |
| 80 | $replaceToBackSlash = $this->_fieldHelpers->replaceToBackSlash($this->_fieldData->subtitle); |
| 81 | $_subtitleText = wp_kses_post($this->_fieldHelpers->renderHTMR($replaceToBackSlash)); |
| 82 | } |
| 83 | |
| 84 | // for pre required icon |
| 85 | // aria-hidden="true" hides the asterisk from screen readers since the input |
| 86 | // already has aria-required="true" which properly announces the required state. |
| 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 |
| 94 | %1$s |
| 95 | class="%2$s %3$s" |
| 96 | aria-hidden="true" |
| 97 | > |
| 98 | * |
| 99 | </span>', |
| 100 | $this->_fieldHelpers->getCustomAttributes('req-smbl'), |
| 101 | $this->_fieldHelpers->getConversationalCls('req-smbl'), |
| 102 | $this->_fieldHelpers->getCustomClasses('req-smbl') |
| 103 | ); |
| 104 | } |
| 105 | // for post required icon |
| 106 | // Same as pre required icon - hide from assistive technology to prevent redundancy. |
| 107 | if ( |
| 108 | $this->_fieldHelpers->property_exists_nested($this->_fieldData, 'valid->req') |
| 109 | && $this->_fieldHelpers->property_exists_nested($this->_fieldData, 'valid->reqShow', true) |
| 110 | && $this->_fieldHelpers->property_exists_nested($this->_fieldData, 'valid->reqPos', 'before', 1) |
| 111 | ) { |
| 112 | $_reqPost = sprintf( |
| 113 | '<span |
| 114 | %1$s |
| 115 | class="%2$s %3$s" |
| 116 | aria-hidden="true" |
| 117 | > |
| 118 | * |
| 119 | </span>', |
| 120 | $this->_fieldHelpers->getCustomAttributes('req-smbl'), |
| 121 | $this->_fieldHelpers->getConversationalCls('req-smbl'), |
| 122 | $this->_fieldHelpers->getCustomClasses('req-smbl') |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | // for Label |
| 127 | if ( |
| 128 | property_exists($this->_fieldData, 'lbl') |
| 129 | // && isset($this->_fieldData->valid->hideLbl) |
| 130 | // && $this->_fieldData->valid->hideLbl !== true |
| 131 | ) { |
| 132 | $bfFrontendFormIds = FrontendHelpers::$bfFrontendFormIds; |
| 133 | $contentCount = count($bfFrontendFormIds); |
| 134 | $labelId = $this->_fieldHelpers->getLabelId(); |
| 135 | $_label = sprintf( |
| 136 | '<label |
| 137 | id="%1$s" |
| 138 | %2$s |
| 139 | class="%3$s %4$s" |
| 140 | for="%5$s-%6$s"> |
| 141 | %7$s |
| 142 | %8$s |
| 143 | %9$s |
| 144 | %10$s |
| 145 | %11$s |
| 146 | </label>', |
| 147 | $labelId, // 1 |
| 148 | $this->_fieldHelpers->getCustomAttributes('lbl'), // 2 |
| 149 | $this->_fieldHelpers->getConversationalMultiCls('lbl'), // 3 |
| 150 | $this->_fieldHelpers->getCustomClasses('lbl'), // 4 |
| 151 | $this->_fieldKey, // 5 |
| 152 | $contentCount, // 6 |
| 153 | $_reqPre, // 7 |
| 154 | $_lblPreIcn, // 8 |
| 155 | $_lbl, // 9 |
| 156 | $_lblSufIcn, // 10 |
| 157 | $_reqPost // 11 |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | // for subtitle |
| 162 | if (property_exists($this->_fieldData, 'subtitle') && $this->_fieldData->subtitleHide) { |
| 163 | $_subtitle = sprintf( |
| 164 | '<div |
| 165 | %1$s |
| 166 | class="%2$s %3$s" |
| 167 | > |
| 168 | %4$s |
| 169 | %5$s |
| 170 | %6$s |
| 171 | </div>', |
| 172 | $this->_fieldHelpers->getCustomAttributes('sub-titl'), |
| 173 | $this->_fieldHelpers->getConversationalMultiCls('sub-titl'), |
| 174 | $this->_fieldHelpers->getCustomClasses('sub-titl'), |
| 175 | $_subtitlePreIcn, |
| 176 | $_subtitleText, |
| 177 | $_subtitlePostIcn |
| 178 | ); |
| 179 | } |
| 180 | return sprintf( |
| 181 | ' <div |
| 182 | %1$s |
| 183 | class="%2$s %3$s" |
| 184 | > |
| 185 | %4$s |
| 186 | %5$s |
| 187 | </div>', |
| 188 | $this->_fieldHelpers->getCustomAttributes('lbl-wrp'), |
| 189 | $this->_fieldHelpers->getConversationalCls('lbl-wrp'), |
| 190 | $this->_fieldHelpers->getCustomClasses('lbl-wrp'), |
| 191 | $_label, |
| 192 | $_subtitle |
| 193 | ); |
| 194 | } |
| 195 | |
| 196 | private function helperText() |
| 197 | { |
| 198 | $_helperTxtPreIcn = $this->conversationalIcon('hlpPreIcn', 'hlp-txt-pre-i'); |
| 199 | $_helperTxtSufIcn = $this->conversationalIcon('hlpSufIcn', 'hlp-txt-suf-i'); |
| 200 | $_helperTxt = ''; |
| 201 | |
| 202 | if (isset($this->_fieldData->helperTxt)) { |
| 203 | $_helperTxt = wp_kses_post($this->_fieldHelpers->renderHTMR($this->_fieldData->helperTxt)); |
| 204 | } |
| 205 | |
| 206 | if (property_exists($this->_fieldData, 'helperTxt') && $this->_fieldData->helperTxt) { |
| 207 | $helperTextId = $this->_fieldHelpers->getHelperTextId(); |
| 208 | $_helperTxt = sprintf( |
| 209 | '<div |
| 210 | id="%1$s" |
| 211 | %2$s |
| 212 | class="%3$s %4$s" |
| 213 | > |
| 214 | %5$s |
| 215 | %6$s |
| 216 | %7$s |
| 217 | </div>', |
| 218 | $helperTextId, |
| 219 | $this->_fieldHelpers->getCustomAttributes('hlp-txt'), |
| 220 | $this->_fieldHelpers->getConversationalMultiCls('hlp-txt'), |
| 221 | $this->_fieldHelpers->getCustomClasses('hlp-txt'), |
| 222 | $_helperTxtPreIcn, |
| 223 | $_helperTxt, |
| 224 | $_helperTxtSufIcn |
| 225 | ); |
| 226 | } |
| 227 | |
| 228 | // for helper text |
| 229 | return $_helperTxt; |
| 230 | } |
| 231 | |
| 232 | public function errorMessages() |
| 233 | { |
| 234 | $_errorPreIcn = $this->conversationalIcon('errPreIcn', 'err-txt-pre-i'); |
| 235 | $_errorSufIcn = $this->conversationalIcon('errSufIcn', 'err-txt-suf-i'); |
| 236 | $_error = ''; |
| 237 | $_style = 'opacity: 0 !important; height: 0px !important;'; |
| 238 | |
| 239 | if ($this->_error) { |
| 240 | $_error = wp_kses_post($this->_error); |
| 241 | $_style = 'margin-top: 5px !important; height: 9px !important;'; |
| 242 | } |
| 243 | |
| 244 | $errorId = $this->_fieldHelpers->getErrorId(); |
| 245 | $errMsgStart = ''; |
| 246 | $errMsgEnd = ''; |
| 247 | $msgStyle = "style='display: none !important;'"; |
| 248 | $errMsgAtomicCls = $this->_fieldHelpers->getConversationalMultiCls('err-msg'); |
| 249 | $errMsgCustomCls = $this->_fieldHelpers->getCustomClasses('err-msg'); |
| 250 | $errMsgCustomAttr = $this->_fieldHelpers->getCustomAttributes('err-msg'); |
| 251 | if (!(empty($_errorPreIcn) && empty($_errorSufIcn))) { |
| 252 | $errMsgStart = "<div {$errMsgCustomAttr} class='{$errMsgAtomicCls} {$errMsgCustomCls}' {$msgStyle}>"; |
| 253 | $errMsgEnd = '</div>'; |
| 254 | $msgStyle = ''; |
| 255 | $errMsgAtomicCls = ''; |
| 256 | $errMsgCustomCls = ''; |
| 257 | $errMsgCustomAttr = ''; |
| 258 | } |
| 259 | |
| 260 | return sprintf( |
| 261 | '<div class="%1$s" role="alert" aria-atomic="true" aria-live="assertive" style="%2$s"> |
| 262 | <div class="%3$s"> |
| 263 | %4$s |
| 264 | %5$s |
| 265 | <div id="%6$s" %7$s %8$s class="%9$s %10$s %11$s %12$s " %13$s> |
| 266 | %14$s |
| 267 | </div> |
| 268 | %15$s |
| 269 | %16$s |
| 270 | </div> |
| 271 | </div>', |
| 272 | $this->_fieldHelpers->getConversationalMultiCls('err-wrp'), // 1 |
| 273 | $_style, // 2 |
| 274 | $this->_fieldHelpers->getConversationalCls('err-inner'), // 3 |
| 275 | $errMsgStart, // 4 |
| 276 | $_errorPreIcn, // 5 |
| 277 | $errorId, // 6 |
| 278 | $this->_fieldHelpers->getCustomAttributes('err-txt'), // 7 |
| 279 | $errMsgCustomAttr, // 8 |
| 280 | $errMsgAtomicCls, // 9 |
| 281 | $errMsgCustomCls, // 10 |
| 282 | $this->_fieldHelpers->getConversationalMultiCls('err-txt'), // 11 |
| 283 | $this->_fieldHelpers->getCustomClasses('err-txt'), // 12 |
| 284 | $msgStyle, // 13 |
| 285 | $_error, // 14 |
| 286 | $_errorSufIcn, // 15 |
| 287 | $errMsgEnd // 16 |
| 288 | ); |
| 289 | } |
| 290 | |
| 291 | public function conversationalIcon($icnPropName, $element) |
| 292 | { |
| 293 | if ($this->_fieldHelpers->property_exists_nested($this->_fieldData, $icnPropName, '', 1)) { |
| 294 | return sprintf( |
| 295 | '<img |
| 296 | %1$s |
| 297 | class="%2$s %3$s" |
| 298 | src="%4$s" |
| 299 | alt="" |
| 300 | aria-hidden="true" |
| 301 | /> |
| 302 | ', |
| 303 | $this->_fieldHelpers->getCustomAttributes($element), |
| 304 | $this->_fieldHelpers->getConversationalCls($element), |
| 305 | $this->_fieldHelpers->getCustomClasses($element), |
| 306 | $this->_fieldHelpers->esc_url($this->_fieldData->{$icnPropName}) |
| 307 | ); |
| 308 | } |
| 309 | return ''; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Get the field helpers instance for external access |
| 314 | * @return ConversationalFieldHelpers |
| 315 | */ |
| 316 | public function getFieldHelpers() |
| 317 | { |
| 318 | return $this->_fieldHelpers; |
| 319 | } |
| 320 | } |
| 321 |