| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Renderer; |
| 4 |
|
| 5 |
use FluentCart\Framework\Support\Arr; |
| 6 |
|
| 7 |
class FormFieldRenderer |
| 8 |
{ |
| 9 |
public function renderField($fieldData = []) |
| 10 |
{ |
| 11 |
|
| 12 |
if (empty($fieldData) || !is_array($fieldData)) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
$type = Arr::get($fieldData, 'type'); |
| 17 |
|
| 18 |
|
| 19 |
switch ($type) { |
| 20 |
case 'section': |
| 21 |
$this->renderSection($fieldData); |
| 22 |
break; |
| 23 |
case 'sub_section': |
| 24 |
$fields = Arr::get($fieldData, 'fields', []); |
| 25 |
$atts = array_filter([ |
| 26 |
'class' => 'fct_form_sub_section_wrapper ' . Arr::get($fieldData, 'wrapper_class', ''), |
| 27 |
'id' => Arr::get($fieldData, 'id', '') |
| 28 |
]); |
| 29 |
if ($fields) { |
| 30 |
?> |
| 31 |
<div <?php RenderHelper::renderAtts($atts); ?>> |
| 32 |
<?php |
| 33 |
foreach ($fields as $field) { |
| 34 |
$this->renderField($field); |
| 35 |
} |
| 36 |
echo '</div>'; |
| 37 |
} |
| 38 |
break; |
| 39 |
case 'text': |
| 40 |
case 'email': |
| 41 |
case 'tel': |
| 42 |
case 'number': |
| 43 |
$this->renderSingleLineInputField($fieldData); |
| 44 |
break; |
| 45 |
case 'textarea': |
| 46 |
$this->renderTextareaField($fieldData); |
| 47 |
break; |
| 48 |
case 'checkbox': |
| 49 |
$this->renderCheckboxInputField($fieldData); |
| 50 |
break; |
| 51 |
case 'select': |
| 52 |
$this->renderSelectField($fieldData); |
| 53 |
break; |
| 54 |
case 'address_select': |
| 55 |
$this->renderAddressSelect($fieldData); |
| 56 |
break; |
| 57 |
default: |
| 58 |
do_action('fluent_cart/render_custom_form_field', $fieldData); |
| 59 |
break; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public function renderAddressSelect($fieldData = []) |
| 64 |
{ |
| 65 |
|
| 66 |
(new AddressSelectRenderer( |
| 67 |
Arr::get($fieldData, 'options', []), |
| 68 |
Arr::get($fieldData, 'primary_address'), |
| 69 |
Arr::get($fieldData, 'requirements_fields'), |
| 70 |
Arr::get($fieldData, 'address_type') |
| 71 |
)) |
| 72 |
->render(); |
| 73 |
} |
| 74 |
|
| 75 |
public function renderSection($sectionData = []) |
| 76 |
{ |
| 77 |
if (empty($sectionData) || !is_array($sectionData)) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
$label = Arr::get($sectionData, 'heading', ''); |
| 82 |
$fields = Arr::get($sectionData, 'fields', []); |
| 83 |
$sectionId = Arr::get($sectionData, 'id', ''); |
| 84 |
$headingId = $sectionId ? $sectionId . '_heading' : ''; |
| 85 |
|
| 86 |
$wrapperAttributes = array_filter([ |
| 87 |
'class' => 'fct_checkout_form_section', |
| 88 |
'id' => $sectionId |
| 89 |
]); |
| 90 |
|
| 91 |
if (!empty($label) && $headingId) { |
| 92 |
$wrapperAttributes['role'] = 'region'; |
| 93 |
$wrapperAttributes['aria-labelledby'] = $headingId; |
| 94 |
} |
| 95 |
|
| 96 |
if (!empty($sectionData['wrapper_atts'])) { |
| 97 |
$wrapperAttributes = wp_parse_args($sectionData['wrapper_atts'], $wrapperAttributes); |
| 98 |
} |
| 99 |
|
| 100 |
if (empty($fields)) { |
| 101 |
return; |
| 102 |
} |
| 103 |
?> |
| 104 |
<div <?php RenderHelper::renderAtts($wrapperAttributes); ?> data-fct-checkout-form-section> |
| 105 |
<?php |
| 106 |
if (!empty($fieldData['before_callback'])) { |
| 107 |
call_user_func($fieldData['before_callback'], $fieldData); |
| 108 |
} |
| 109 |
?> |
| 110 |
<?php if (!empty($label)) : ?> |
| 111 |
<div class="fct_form_section_header"> |
| 112 |
<h4 class="fct_form_section_header_label" <?php echo $headingId ? 'id="' . esc_attr($headingId) . '"' : ''; ?>> |
| 113 |
<?php echo wp_kses_post($label) ?> |
| 114 |
</h4> |
| 115 |
<?php if (!empty($sectionData['action_callback'])) { |
| 116 |
call_user_func($sectionData['action_callback'], $sectionData); |
| 117 |
} ?> |
| 118 |
</div> |
| 119 |
<?php endif; ?> |
| 120 |
<?php if ($fields): ?> |
| 121 |
<div class="fct_form_section_body"> |
| 122 |
<div class="fct_checkout_input_group"> |
| 123 |
<?php |
| 124 |
foreach ($fields as $field) { |
| 125 |
$this->renderField($field); |
| 126 |
} |
| 127 |
?> |
| 128 |
</div> |
| 129 |
</div> |
| 130 |
<?php endif; ?> |
| 131 |
<div class="fct_form_error fct_error_<?php echo esc_attr($sectionId); ?>"></div> |
| 132 |
<?php |
| 133 |
if (!empty($sectionData['after_callback'])) { |
| 134 |
call_user_func($sectionData['after_callback'], $sectionData); |
| 135 |
} |
| 136 |
?> |
| 137 |
</div> |
| 138 |
<?php |
| 139 |
} |
| 140 |
|
| 141 |
private function renderSingleLineInputField($fieldData) |
| 142 |
{ |
| 143 |
$type = Arr::get($fieldData, 'type', 'text'); |
| 144 |
$fieldId = Arr::get($fieldData, 'id', ''); |
| 145 |
$isRequired = Arr::get($fieldData, 'required', '') === 'yes'; |
| 146 |
|
| 147 |
$inputAttributes = array_filter([ |
| 148 |
'type' => $type, |
| 149 |
'class' => 'fct-input fct-input-' . $type, |
| 150 |
'value' => Arr::get($fieldData, 'value', ''), |
| 151 |
'placeholder' => Arr::get($fieldData, 'placeholder', ''), |
| 152 |
'id' => $fieldId, |
| 153 |
'name' => Arr::get($fieldData, 'name', ''), |
| 154 |
'autocomplete' => Arr::get($fieldData, 'autocomplete', ''), |
| 155 |
'disabled' => Arr::get($fieldData, 'disabled', '') === true ? 'disabled' : '', |
| 156 |
]); |
| 157 |
|
| 158 |
// Add required and aria-required together |
| 159 |
if ($isRequired) { |
| 160 |
$inputAttributes['required'] = 'required'; |
| 161 |
$inputAttributes['aria-required'] = 'true'; |
| 162 |
} |
| 163 |
|
| 164 |
// Only add aria-label if it is not empty |
| 165 |
if (!empty($fieldData['aria-label'])) { |
| 166 |
$inputAttributes['aria-label'] = Arr::get($fieldData, 'aria-label', ''); |
| 167 |
} |
| 168 |
|
| 169 |
if (!empty($fieldData['extra_atts'])) { |
| 170 |
$inputAttributes = wp_parse_args($inputAttributes, $fieldData['extra_atts']); |
| 171 |
} |
| 172 |
|
| 173 |
$label = Arr::get($fieldData, 'label', ''); |
| 174 |
|
| 175 |
$wrapperAttributes = array_filter([ |
| 176 |
'class' => 'fct_input_wrapper fct_input_wrapper_' . $type, |
| 177 |
'id' => 'fct_wrapper_' . $fieldId |
| 178 |
]); |
| 179 |
|
| 180 |
if (!empty($fieldData['wrapper_atts'])) { |
| 181 |
$wrapperAttributes = wp_parse_args($fieldData['wrapper_atts'], $wrapperAttributes); |
| 182 |
} |
| 183 |
?> |
| 184 |
<div <?php RenderHelper::renderAtts($wrapperAttributes); ?>> |
| 185 |
<?php |
| 186 |
if (!empty($fieldData['before_callback'])) { |
| 187 |
call_user_func($fieldData['before_callback'], $fieldData); |
| 188 |
} |
| 189 |
?> |
| 190 |
<?php if (!empty($label)) : ?> |
| 191 |
<label for="<?php echo esc_attr($fieldId) ?>" class="fct_input_label fct_input_label_<?php echo esc_attr($type) ?>"> |
| 192 |
<?php echo wp_kses_post($label) ?> |
| 193 |
</label> |
| 194 |
<?php endif; ?> |
| 195 |
<input <?php RenderHelper::renderAtts($inputAttributes) ?> /> |
| 196 |
<?php |
| 197 |
if (!empty($fieldData['after_callback'])) { |
| 198 |
call_user_func($fieldData['after_callback'], $fieldData); |
| 199 |
} |
| 200 |
?> |
| 201 |
</div> |
| 202 |
<?php |
| 203 |
} |
| 204 |
|
| 205 |
private function renderTextareaField($fieldData) |
| 206 |
{ |
| 207 |
$fieldId = Arr::get($fieldData, 'id', ''); |
| 208 |
$isRequired = Arr::get($fieldData, 'required', '') === 'yes'; |
| 209 |
|
| 210 |
$inputAttributes = array_filter([ |
| 211 |
'type' => 'textarea', |
| 212 |
'class' => 'fct-input fct-input-textarea', |
| 213 |
'placeholder' => Arr::get($fieldData, 'placeholder', ''), |
| 214 |
'id' => $fieldId, |
| 215 |
'name' => Arr::get($fieldData, 'name', ''), |
| 216 |
'autocomplete' => Arr::get($fieldData, 'autocomplete', ''), |
| 217 |
'disabled' => Arr::get($fieldData, 'disabled', '') === true ? 'disabled' : '', |
| 218 |
]); |
| 219 |
|
| 220 |
// Add required and aria-required together |
| 221 |
if ($isRequired) { |
| 222 |
$inputAttributes['required'] = 'required'; |
| 223 |
$inputAttributes['aria-required'] = 'true'; |
| 224 |
} |
| 225 |
|
| 226 |
// Only add aria-label if it is not empty |
| 227 |
if (!empty($fieldData['aria-label'])) { |
| 228 |
$inputAttributes['aria-label'] = Arr::get($fieldData, 'aria-label', ''); |
| 229 |
} |
| 230 |
|
| 231 |
if (!empty($fieldData['extra_atts'])) { |
| 232 |
$inputAttributes = wp_parse_args($inputAttributes, $fieldData['extra_atts']); |
| 233 |
} |
| 234 |
|
| 235 |
$label = Arr::get($fieldData, 'label', ''); |
| 236 |
|
| 237 |
$wrapperAttributes = array_filter([ |
| 238 |
'class' => 'fct_input_wrapper fct_input_wrapper_textarea', |
| 239 |
'id' => 'fct_wrapper_' . $fieldId |
| 240 |
]); |
| 241 |
|
| 242 |
if (!empty($fieldData['wrapper_atts'])) { |
| 243 |
$wrapperAttributes = wp_parse_args($fieldData['wrapper_atts'], $wrapperAttributes); |
| 244 |
} |
| 245 |
?> |
| 246 |
<div <?php RenderHelper::renderAtts($wrapperAttributes); ?>> |
| 247 |
<?php |
| 248 |
if (!empty($fieldData['before_callback'])) { |
| 249 |
call_user_func($fieldData['before_callback'], $fieldData); |
| 250 |
} |
| 251 |
?> |
| 252 |
<?php if (!empty($label)) : ?> |
| 253 |
<label for="<?php echo esc_attr($fieldId) ?>" |
| 254 |
class="fct_input_label fct_input_label_textarea"> |
| 255 |
<?php echo wp_kses_post($label) ?> |
| 256 |
</label> |
| 257 |
<?php endif; ?> |
| 258 |
<textarea <?php RenderHelper::renderAtts($inputAttributes) ?>><?php echo esc_textarea(Arr::get($fieldData, 'value', '')); ?></textarea> |
| 259 |
<?php |
| 260 |
if (!empty($fieldData['after_callback'])) { |
| 261 |
call_user_func($fieldData['after_callback'], $fieldData); |
| 262 |
} |
| 263 |
?> |
| 264 |
</div> |
| 265 |
<?php |
| 266 |
} |
| 267 |
|
| 268 |
private function renderCheckboxInputField($fieldData) |
| 269 |
{ |
| 270 |
$checkboxValue = Arr::get($fieldData, 'checkbox_value', 'yes'); |
| 271 |
$fieldId = Arr::get($fieldData, 'id', ''); |
| 272 |
$isRequired = Arr::get($fieldData, 'required', '') === 'yes'; |
| 273 |
|
| 274 |
$inputAttributes = array_filter([ |
| 275 |
'type' => 'checkbox', |
| 276 |
'class' => 'fct-input fct-input-checkbox', |
| 277 |
'id' => $fieldId, |
| 278 |
'name' => Arr::get($fieldData, 'name', ''), |
| 279 |
'value' => $checkboxValue, |
| 280 |
'checked' => Arr::get($fieldData, 'value', '') == $checkboxValue ? 'checked' : '', |
| 281 |
'disabled' => Arr::get($fieldData, 'disabled', '') === true ? 'disabled' : '', |
| 282 |
]); |
| 283 |
|
| 284 |
// Add required and aria-required together |
| 285 |
if ($isRequired) { |
| 286 |
$inputAttributes['required'] = 'required'; |
| 287 |
$inputAttributes['aria-required'] = 'true'; |
| 288 |
} |
| 289 |
|
| 290 |
if (!empty($fieldData['extra_atts'])) { |
| 291 |
$inputAttributes = wp_parse_args($inputAttributes, $fieldData['extra_atts']); |
| 292 |
} |
| 293 |
|
| 294 |
$label = Arr::get($fieldData, 'label', ''); |
| 295 |
|
| 296 |
$wrapperAttributes = array_filter([ |
| 297 |
'class' => 'fct_input_wrapper fct_input_wrapper_textarea', |
| 298 |
'id' => 'fct_wrapper_' . $fieldId |
| 299 |
]); |
| 300 |
|
| 301 |
if (!empty($fieldData['wrapper_atts'])) { |
| 302 |
$wrapperAttributes = wp_parse_args($fieldData['wrapper_atts'], $wrapperAttributes); |
| 303 |
} |
| 304 |
?> |
| 305 |
<div <?php RenderHelper::renderAtts($wrapperAttributes); ?>> |
| 306 |
<?php |
| 307 |
if (!empty($fieldData['before_callback'])) { |
| 308 |
call_user_func($fieldData['before_callback'], $fieldData); |
| 309 |
} |
| 310 |
?> |
| 311 |
<label for="<?php echo esc_attr(Arr::get($fieldData, 'id', '')); ?>" class="fct_input_label fct_input_label_textarea"> |
| 312 |
<input <?php RenderHelper::renderAtts($inputAttributes) ?> /> |
| 313 |
<?php echo wp_kses_post($label) ?> |
| 314 |
</label> |
| 315 |
<?php |
| 316 |
if (!empty($fieldData['after_callback'])) { |
| 317 |
call_user_func($fieldData['after_callback'], $fieldData); |
| 318 |
} |
| 319 |
?> |
| 320 |
</div> |
| 321 |
<?php |
| 322 |
} |
| 323 |
|
| 324 |
private function renderSelectField($fieldData) |
| 325 |
{ |
| 326 |
$fieldId = Arr::get($fieldData, 'id', ''); |
| 327 |
$isRequired = Arr::get($fieldData, 'required', '') === 'yes'; |
| 328 |
|
| 329 |
$inputAttributes = array_filter([ |
| 330 |
'class' => 'fct-input fct-input-select', |
| 331 |
'id' => $fieldId, |
| 332 |
'name' => Arr::get($fieldData, 'name', ''), |
| 333 |
'disabled' => Arr::get($fieldData, 'disabled', '') === true ? 'disabled' : '', |
| 334 |
]); |
| 335 |
|
| 336 |
// Add required and aria-required together |
| 337 |
if ($isRequired) { |
| 338 |
$inputAttributes['required'] = 'required'; |
| 339 |
$inputAttributes['aria-required'] = 'true'; |
| 340 |
} |
| 341 |
|
| 342 |
if (!empty($fieldData['extra_atts'])) { |
| 343 |
$inputAttributes = wp_parse_args($inputAttributes, $fieldData['extra_atts']); |
| 344 |
} |
| 345 |
|
| 346 |
$label = Arr::get($fieldData, 'label', ''); |
| 347 |
$options = Arr::get($fieldData, 'options', []); |
| 348 |
|
| 349 |
$wrapperAttributes = array_filter([ |
| 350 |
'class' => 'fct_input_wrapper fct_input_wrapper_select', |
| 351 |
'id' => 'fct_wrapper_' . $fieldId |
| 352 |
]); |
| 353 |
|
| 354 |
if (!empty($fieldData['wrapper_atts'])) { |
| 355 |
$wrapperAttributes = wp_parse_args($fieldData['wrapper_atts'], $wrapperAttributes); |
| 356 |
} |
| 357 |
$value = Arr::get($fieldData, 'value', ''); |
| 358 |
?> |
| 359 |
<div <?php RenderHelper::renderAtts($wrapperAttributes); ?>> |
| 360 |
<?php |
| 361 |
if (!empty($fieldData['before_callback'])) { |
| 362 |
call_user_func($fieldData['before_callback'], $fieldData); |
| 363 |
} |
| 364 |
?> |
| 365 |
<?php if (!empty($label)) : ?> |
| 366 |
<label for="<?php echo esc_attr($fieldId) ?>" |
| 367 |
class="fct_input_label fct_input_label_select"> |
| 368 |
<?php echo wp_kses_post($label) ?> |
| 369 |
</label> |
| 370 |
<?php endif; ?> |
| 371 |
<select <?php RenderHelper::renderAtts($inputAttributes) ?>> |
| 372 |
<?php foreach ($options as $option): ?> |
| 373 |
<option |
| 374 |
value="<?php echo esc_attr(Arr::get($option, 'value')); ?>" <?php selected($value, Arr::get($option, 'value')); ?>> |
| 375 |
<?php echo esc_html(Arr::get($option, 'name')); ?> |
| 376 |
</option> |
| 377 |
<?php endforeach; ?> |
| 378 |
</select> |
| 379 |
<?php |
| 380 |
if (!empty($fieldData['after_callback'])) { |
| 381 |
call_user_func($fieldData['after_callback'], $fieldData); |
| 382 |
} |
| 383 |
?> |
| 384 |
</div> |
| 385 |
<?php |
| 386 |
} |
| 387 |
} |
| 388 |
|