| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Services; |
| 4 |
|
| 5 |
class FormBuilder |
| 6 |
{ |
| 7 |
private $formFields = []; |
| 8 |
|
| 9 |
public function __construct($formFields) |
| 10 |
{ |
| 11 |
$this->formFields = $formFields; |
| 12 |
} |
| 13 |
|
| 14 |
public function render() |
| 15 |
{ |
| 16 |
foreach ($this->formFields as $name => $field) { |
| 17 |
if (empty($field['disabled'])) { |
| 18 |
$field['name'] = $name; |
| 19 |
$this->renderField($field); |
| 20 |
} |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
private function renderField($field) |
| 25 |
{ |
| 26 |
$type = $field['type']; |
| 27 |
$label = $field['label'] ?? ''; |
| 28 |
$name = $field['name']; |
| 29 |
$required = $field['required'] ?? false; |
| 30 |
$options = $field['options'] ?? []; |
| 31 |
$value = $field['value'] ?? ''; |
| 32 |
$readonly = $field['readonly'] ?? false; |
| 33 |
|
| 34 |
$atts = array_filter([ |
| 35 |
'type' => in_array($type, ['text', 'email', 'password']) ? $type : '', |
| 36 |
'id' => 'fcom_' . $name, |
| 37 |
'name' => $name, |
| 38 |
'value' => $value, |
| 39 |
'required' => '',//$required ? 'required' : '', |
| 40 |
'readonly' => $readonly ? 'readonly' : '', |
| 41 |
'placeholder' => $field['placeholder'] ?? '', |
| 42 |
'class' => $field['input_class'] ?? '', |
| 43 |
]); |
| 44 |
|
| 45 |
echo "<div id='fcom_group_" . esc_attr($name) . "' class='fcom_form-group'>"; |
| 46 |
if ($label): |
| 47 |
echo "<div class='fcom_form_label'><label for='" . esc_attr($atts['id']) . "'>" . esc_html($label) . "</label></div>"; |
| 48 |
endif; |
| 49 |
|
| 50 |
echo "<div class='fcom_form_input'>"; |
| 51 |
|
| 52 |
if (isset($atts['type'])) { |
| 53 |
echo "<input".$this->printAtts($atts).">"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 54 |
} elseif ($type === 'select') { |
| 55 |
echo "<select id='" . esc_attr($name) . "' name='" . esc_attr($name) . "' " . ($required ? 'required' : '') . ">"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 56 |
foreach ($options as $option) { |
| 57 |
echo "<option value='" . esc_attr($option) . "'>" . esc_html($option) . "</option>"; |
| 58 |
} |
| 59 |
echo "</select>"; |
| 60 |
} else if ($type === 'inline_checkbox') { |
| 61 |
echo "<div class='fcom_inline_checkbox'>"; |
| 62 |
echo "<input type='checkbox' " . $this->printAtts($atts) . ">"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 63 |
echo "<label for='" . esc_attr($atts['id']) . "'>" . wp_kses_post($field['inline_label']) . "</label>"; |
| 64 |
echo "</div>"; |
| 65 |
} else if ($type === 'textarea') { |
| 66 |
echo "<textarea " . $this->printAtts($atts) . "></textarea>"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 67 |
} |
| 68 |
echo "</div></div>"; |
| 69 |
} |
| 70 |
|
| 71 |
private function printAtts($atts) |
| 72 |
{ |
| 73 |
$result = ''; |
| 74 |
foreach ($atts as $key => $value) { |
| 75 |
$result .= " " . esc_attr($key) . "='" . esc_attr($value) . "'"; |
| 76 |
} |
| 77 |
return $result; |
| 78 |
} |
| 79 |
} |
| 80 |
|