'class FormHelper { //*** starts the cllass private static function prepareCondition($condition) { $controlling_field = $condition['field']; $current_value = $condition['current_value'] ?? ''; $controlling_values = implode('|', (array) ($condition['values'])); $condition_attributes = "data-controlling-field='{$controlling_field}'"; $condition_attributes .= "data-controlling-values='{$controlling_values}'"; $class_value = ''; if (($current_value !=='')) { if (in_array($current_value, (array) ($condition['values']))) { $class_value = "visible"; } if (!in_array($current_value, (array) ($condition['values']))) { $class_value = "hidden"; } } return ['condition_attributes'=>$condition_attributes, 'condition_class'=>$class_value]; } private static function prepareField($field_id, $condition, $base_class, $label ='') { $condition = is_array($condition) ? $condition : []; $name_to_class = str_replace('_', '-', $field_id); $data_name = "data-name='{$name_to_class}'"; $class_list = "class='{$base_class} {$name_to_class}'"; $label_for =''; if($label !==''){ $label_for = ucwords(str_replace('_', '-', $label)); $label_for = ""; } $condition_attributes = ''; if (!empty($condition)) { $prepared = self::prepareCondition($condition); $condition_attributes = $prepared['condition_attributes']; $condition_class = $prepared['condition_class']; if (!empty($condition_class)){ $class_list = "class='{$base_class} {$name_to_class} {$condition_class}'"; } } $start_div = "
"; return [ 'class_string' => $class_list, 'data_name' => $data_name, 'condition_attributes' => $condition_attributes, 'start_div' => $start_div, 'label_for' => $label_for, ]; } private static function prepareDiv($field_id, $condition, $base_class, $label='') { $condition = is_array($condition) ? $condition : []; $name_to_class = str_replace('_', '-', $field_id); $data_name = "data-name='{$name_to_class}'"; $class_list = "class='{$base_class} {$name_to_class}'"; $label_for =''; if($label !==''){ $label_for = ucwords(str_replace('_', '-', $label)); $label_for = ""; } $condition_attributes = ''; if (!empty($condition)) { $prepared = self::prepareCondition($condition); $condition_attributes = $prepared['condition_attributes']; $condition_class = $prepared['condition_class']; if (!empty($condition_class)){ $class_list = "class='{$base_class} {$name_to_class} {$condition_class}'"; } } $start_div = "
"; return [ 'start_div' => $start_div, 'label_for' => $label_for, 'data_name' => $data_name, ]; } public static function generateButtonField($field_id, $label, $class, $condition = []) { extract(self::prepareDiv($field_id, $condition, "dibraco-button")); $button_text = ucwords(str_replace(['_', '-'], ' ',$label)); return "{$start_div}
"; } public static function generateToggleSwitch($field_id, $label, $value, $options_label = [], $condition = []) { extract(self::prepareField($field_id, $condition, "dibraco-toggle")); $label = ucwords(str_replace(['_', '-'], ' ', $label)); if (empty($options_label)) { $options_label = ["0" => 'ON', "1" => 'OFF']; } if ($value !== "1" && $value !== "0") { $value = "1"; } return "
{$label}
"; } public static function generateCheckBox($field_id, $label, $value, $condition = []) { extract(self::prepareDiv($field_id, $condition, "dibraco-checkbox", $label)); $value = ($value == "1") ? "1" : "0"; $checked_attr = ($value === "1") ? 'checked' : ''; return "{$start_div} {$label_for}
"; } public static function generateCheckboxGroup($field_id, $label, $selected_values = [], $options = [], $condition = []) { extract(self::prepareField($field_id, $condition, "dibraco-checkbox-group", $label)); $selected_values = is_array($selected_values) ? array_map('strval', $selected_values) : []; $options_html = ''; foreach ($options as $option_value => $option_label) { $checkbox_id = "{$field_id}_{$option_value}"; $checked = ''; if (in_array((string)$option_value, $selected_values, true)) { $checked = 'checked'; } else if (empty($selected_values)) { $checked = 'checked'; } $options_html .= " "; } return "

" . esc_html($label) . "

{$options_html}
"; } public static function generateSelect($field_id, $label, $value, $options, $condition = []) { extract(self::prepareDiv($field_id, $condition, "dibraco-select", $label)); if (isset($options['__no_change__'])) { $value = '__no_change__'; } $options_html = ''; $options_html .= ''; foreach ($options as $option_value => $option_label) { $option_label = ucwords(str_replace(['_', '-', '[', ']', '(', ')'], ' ', $option_label)); $selected = ($value == $option_value) ? 'selected' : ''; $options_html .= ""; } return "{$start_div} {$label_for} "; } public static function generateRadioFieldsetWithIntegerValues($field_id, $label, $value, $options_array = [], $condition = []) { extract(self::prepareDiv($field_id, $condition, "dibraco-radio-fieldset", $label)); $options_array = ['' => 'none'] + $options_array; $options_html = ''; foreach ($options_array as $option_value => $option_label) { $option_id = "{$field_id}_{$option_value}"; $checked = ''; if ($value === '' && $option_value === '') { $checked = 'checked'; } elseif ($value !== '' && intval($value) === intval($option_value)) { $checked = 'checked'; } $options_html .= " "; } $fieldset = 'fieldset'; if ($field_id === 'area_parent_location_term'){ $fieldset = 'div'; } return "{$start_div}

{$label}

<$fieldset id='{$field_id}'> {$options_html} "; } public static function generateRadioFieldset($field_id, $label, $value, $options_array = [], $condition = []) { extract(self::prepareDiv($field_id, $condition, "dibraco-radio")); $options_html = ""; foreach ($options_array as $option_value => $option_label) { $option_id = "{$field_id}_{$option_value}"; $checked = ($value === (string)$option_value) ? 'checked' : ''; $options_html .= " "; } return "{$start_div}

{$label}

{$options_html}
"; } public static function generateNoEditField($field_id, $label, $value, $condition = []) { extract(self::prepareDiv($field_id, $condition, "dibraco-no-edit", $label)); $value=$label; return "{$start_div} {$label_for}

" . esc_html($value) . "

"; } public static function generateHiddenField($field_id, $value, $condition = []) { extract(self::prepareField($field_id, $condition, "dibraco-hidden-field", $label)); return "
"; } public static function generateTextInput($field_id, $label, $value, $condition = []) { extract(self::prepareDiv($field_id, $condition, "dibraco-text", $label)); extract(self::prepareInputString($field_id, $value, 'text')); return "{$start_div} {$label_for} {$input_string} "; } private static $shortcodes = [ ['text' => 'Address', 'code' => '[da_address]'], ['text' => 'Phone', 'code' => '[telephonelink]'], ['text' => 'Hours', 'code' => '[da_opening_hours]'], ['text' => 'Service Areas', 'code' => '[service_areas]'] ]; private static function renderShortcodeHelpers($field_id) { $helpers = "
"; foreach (self::$shortcodes as $sc) { $label = $sc['text']; $code = $sc['code']; $helpers .= "{$label}"; } $helpers .= "
"; return $helpers; } public static function generateWysiwyg($field_id, $label, $value, $rows, $condition = [], $field_config = []) { extract(self::prepareField($field_id, $condition, "dibraco-wysiwyg", $label)); if ($rows ===''){ $rows = 6; } extract(self::prepareInputString($field_id, $value, 'textarea', ['rows'=>$rows])); $shortcode_buttons = self::renderShortcodeHelpers($field_id); return "{$start_div} {$label} {$shortcode_buttons} {$data_name}{$text_area_input} "; } public static function generateTextarea($field_id, $label, $value, $rows ='', $condition = []) { extract(self::prepareDiv($field_id, $condition, "dibraco-textarea", $label)); if ($rows ===''){ $rows = 6; } extract(self::prepareInputString($field_id, $value, 'textarea', ['rows'=>$rows])); return "{$start_div} {$label_for} {$text_area_input} "; } public static function generateNumberInput($field_id, $label, $value, $step = 1, $condition = []) { extract(self::prepareDiv($field_id, $condition, "dibraco-number", $label)); extract(self::prepareInputString($field_id, $value, 'number', ['step' => $step])); return "{$start_div} {$label_for} {$input_string} "; } public static function generateDateInput($field_id, $label, $value, $condition = []) { extract(self::prepareDiv($field_id, $condition, "dibraco-date", $label)); extract(self::prepareInputString($field_id, $value, 'date')); return "{$start_div} {$label_for} {$input_string} "; } private static function prepareInputString($field_id, $value, $type, $extra_attr=[]){ $name_to_class = str_replace('_', '-', $field_id); if ($type==='number'){ $step = $extra_attr['step']; $string_step = "step='{$step}'"; } $string_type = "type='{$type}'"; $string_id = "id='{$field_id}'"; $string_name = "name='{$field_id}'"; $string_value = "value='{$value}'"; $data_name = "data-name='{$name_to_class}'"; if ($type==='textarea'){ $rows= $extra_attr['rows']; $rows_string = "rows='{$rows}'"; return ['text_area_input' => ""]; } if ($type==='number'){ return ['input_string' => ""]; } if ($type==='time'){ return ['input_string' => ""]; } return ['input_string'=> ""]; } public static function generateTimeInput($field_id, $label, $value, $condition = []) { extract(self::prepareDiv($field_id, $condition, "dibraco-time", $label)); extract(self::prepareInputString($field_id, $value, 'time')); return "{$start_div} {$label_for} {$input_string} "; } public static function generateColorPicker($field_id, $label, $value, $condition = []) { extract(self::prepareField($field_id, $condition, "dibraco-color-picker", $label)); $color = '#FFFFFF'; $alpha = 1.0; $value = trim((string) $value); $value = ltrim($value, '#'); $value = '#' . substr($value, 0, 8); if (((strlen($value) === 7) || (strlen($value) === 9)) && (substr($value, 0, 1) === '#')) { if (strlen($value) === 9) { $color = substr($value, 0, 7); $alpha = round(hexdec(substr($value, 7, 2)) / 255.0, 2); } else { $color = $value; } } return "{$start_div}
"; } public static function generateImageField($field_id, $label, $value, $condition = []) { extract(self::prepareDiv($field_id, $condition, "dibraco-image-field")); $title = $label; if ($value !==''){ $value = (int)$value; } $image_url = $value ? wp_get_attachment_url($value) : ''; return "{$start_div}

{$title}

"; } private static $tracking_started = false; private static $tracked_field_names = []; private static $meta_array = []; public static function generateField($field_name, $field_config) { $type = $field_config['type']; if ($type === "valueinjector") { if (empty($field_config['meta_array'])) { return; } self::$meta_array = $field_config['meta_array']; self::$tracking_started = true; self::$tracked_field_names = array_keys(self::$meta_array); return; } if (self::$tracking_started && $type !== "button" && $type !=='injectionend' && $type !=='valueinjector') { foreach (self::$tracked_field_names as $tracked_field_name) { if ($field_name === $tracked_field_name) { $stored_value = self::$meta_array[$tracked_field_name]; $field_config['value'] = $stored_value; } } } if ($type === "injectionend") { self::$tracking_started = false; self::$tracked_field_names = []; self::$meta_array = []; } if ($type === "injectionend" || $type === "valueinjector") { return; } if (empty($field_name) || empty($type) || empty($field_config)) { return "Error: Missing required parameters ('field_name', 'type', or 'field_config')."; } if ($type !== 'button' && !array_key_exists('value', $field_config)) { $field_config['value'] = ''; } $rows_textarea = $field_config['rows'] ?? ''; $value = $field_config['value'] ?? ''; if($value ===''){ $value = $field_config['value'] = ''; } $legend = $field_config['legend'] ?? ''; $class = $field_config['class'] ?? ''; $image_url = $field_config['data-url'] ?? ''; $label = $field_config['label'] ?? ucwords(str_replace(['_', '-', '[', ']', '(', ')'], ' ', $field_name)); $label = preg_replace('/\s+/', ' ', $label); $label = trim($label); $options_label = $field_config['options_label'] ?? []; if (!array_key_exists('condition', $field_config) || !is_array($field_config['condition'])) { $field_config['condition'] = []; } $condition = $field_config['condition']; $options = $field_config['options'] ?? []; $step = $field_config['step'] ?? '1'; if ($type === 'radio' && empty($options)) { return "Error: The {$type} field '{$field_name}' requires options but none were provided."; } if ($type ==='wysiwyg'){ unset($field_config['rows']); unset($field_config['value']); unset($field_config['label']); } switch ($type) { case 'text': return self::generateTextInput($field_name, $label, $value, $condition); case 'textarea': return self::generateTextarea($field_name, $label, $value, $rows_textarea, $condition); case 'date': return self::generateDateInput($field_name, $label, $value, $condition); case 'time': return self::generateTimeInput($field_name, $label, $value, $condition); case 'colorpicker': return self::generateColorPicker($field_name, $label, $value, $condition); case 'image': return self::generateImageField($field_name, $label, $value, $condition); case 'button': return self::generateButtonField($field_name, $label, $class, $condition); case 'number': return self::generateNumberInput($field_name, $label, $value, $step, $condition); case 'toggle': return self::generateToggleSwitch($field_name, $label, $value, $options_label, $condition); case 'select': return self::generateSelect($field_name, $label, $value, $options, $condition); case 'radio': return self::generateRadioFieldset($field_name, $label, $value, $options, $condition); case 'checkbox': return self::generateCheckBox($field_name, $label, $value, $condition); case 'wysiwyg': return self::generateWysiwyg($field_name, $label, $value, $rows_textarea, $condition, $field_config); case 'checkbox_group': return self::generateCheckboxGroup($field_name, $label, $value, $options, $condition); case 'no-edit': return self::generateNoEditField($field_name, $label, $value, $options, $condition); case 'hidden': return self::generateHiddenField($field_name, $value, $condition = []); } return "Error: Unsupported field type '{$type}'."; } public static function generateVisualSection($field_name, $field_config, $is_recursive = false) { $label = $field_config['label'] ?? ucwords(str_replace(['_', '-'], ' ', $field_name)); $condition = $field_config['condition'] ?? []; $fields = $field_config['fields'] ?? []; $storage_array = $field_config['storage']??''; extract(self::prepareField($field_name, $condition, "dibraco-section")); $hidden = [ 'storage_field' => '', 'storage_end' => '']; if($storage_array ==='1'){ $hidden = self::hiddenFields($field_name); } $subfields_html = ''; foreach ($fields as $subfield_name => $subfield_config) { if (($subfield_config['type'] !=='group') && ($subfield_config['type'] !=='field_group')){ $subfield_config['label'] = $subfield_config['label'] ?? ucwords(str_replace(['_', '-'], ' ', $subfield_name)); } $subfield_config['condition'] = $subfield_config['condition'] ?? []; switch ($subfield_config['type']) { case 'visual_section': $subfields_html .= self::generateVisualSection($subfield_name, $subfield_config, true); break; case 'visual_split': $subfields_html.= self::generateVisualSplit($subfield_name, $subfield_config); break; case 'field_group': case 'visual_group': $subfields_html .= self::generateVisualFieldGroup($subfield_name, $subfield_config); break; case 'repeater': $subfields_html .= self::generateRepeaterField($subfield_name, $subfield_config); break; case 'group': $subfields_html .= self::generateGroup($subfield_name, $subfield_config); break; default: $subfields_html .= self::generateField($subfield_name, $subfield_config); break; } } if ($is_recursive) { return "
{$hidden['storage_field']}

{$label}

{$subfields_html} {$hidden['storage_end']}
"; } return "
{$hidden['storage_field']}

{$label}

{$subfields_html}
{$hidden['storage_end']}
"; } private static function hiddenFields($field_name){ return [ 'storage_field' => "", 'storage_end' => "" ]; } public static function generateVisualSplit($field_name, $field_config) { $condition = $field_config['condition'] ?? []; $fields = $field_config['fields']; extract(self::prepareField($field_name, $condition, "dibraco-split")); $subfields_html = ''; $first_subfield_html = ''; $hidden = self::hiddenFields($field_name); $is_first = true; foreach ($fields as $subfield_name => $subfield_config) { $subfield_config['condition'] = $subfield_config['condition'] ?? []; $type=$subfield_config['type']; if ($is_first) { $first_subfield_html = self::generateField($subfield_name, $subfield_config); $is_first = false; } else { if($type ==='field_group'){ $subfields_html .= self::generateVisualFieldGroup($subfield_name, $subfield_config); }elseif ($type !=='field_group'){ $subfields_html .= self::generateField($subfield_name, $subfield_config); } } } return "
{$hidden['storage_field']} {$first_subfield_html}
{$subfields_html}
{$hidden['storage_end']}
"; } public static function generateVisualFieldGroup($field_name, $field_config, $is_recursive = false) { $label = $field_config['label'] ?? null; $condition = $field_config['condition'] ?? []; $fields = $field_config['fields']; extract(self::prepareField($field_name, $condition, "dibraco-group")); $subfields_html = ''; if ($label !==null) { $label = ucwords(str_replace(['_', '-'], ' ', $label)); $label = '

' . $label . '

'; $label = "
{$label}
"; } foreach ($fields as $subfield_name => $subfield_config) { $subfield_config['condition'] = $subfield_config['condition'] ?? []; switch ($subfield_config['type']) { case 'field_group': case 'visual_group': $subfields_html .= self::generateVisualFieldGroup($subfield_name, $subfield_config); break; case 'repeater': $subfields_html .= self::generateRepeaterField($subfield_name, $subfield_config); break; case 'group': $subfields_html .= self::generateGroup($subfield_name, $subfield_config); break; default: $subfields_html .= self::generateField($subfield_name, $subfield_config); break; } } return "
{$label}
{$subfields_html}
"; } public static function generateRepeaterField($field_name, $field_config, $parent_name = null) { $label = $field_config['label'] ?? ucwords(str_replace(['_', '-'], ' ', $field_name)); $condition = $field_config['condition'] ?? []; $row_config = $field_config['fields']; $hidden = self::hiddenFields($field_name); if (!array_key_exists('row_count', $field_config)) { $row_count = 1; } if (!array_key_exists("{$field_name}_row_count", $field_config)) { $field_config["{$field_name}_row_count"] = 1; } if (self::$tracking_started){ if (isset(self::$meta_array["{$field_name}_row_count"])){ $row_count = (int)self::$meta_array["{$field_name}_row_count"]; } else { $row_count = 1; } } $row_count = $field_config["{$field_name}_row_count"]; if ($row_count ===''){ $row_count =1; } if ($parent_name !== null) {$parent_name = "data-parent-name='{$parent_name}'";} extract(self::prepareField($field_name, $condition, "repeater-wrapper")); $row_html = ''; for ($index = 0; $index < $row_count; $index++) { $data_attr = "data-row-index={$index}"; $row_html .= "
"; foreach ($row_config as $subfield_name => $subfield_config) { switch ($subfield_config['type']) { case 'repeater': $subfield_name_with_index = "{$field_name}[{$index}][{$subfield_name}]"; $row_html .= self::generateRepeaterField($subfield_name_with_index, $subfield_config, $field_name); break; case 'group': $subfield_name_with_index = "{$field_name}[{$index}][$subfield_name"; $row_html .= self::generateGroup($subfield_name_with_index, $subfield_config); break; default: $sublabel = ucwords(str_replace(['_', '-'], ' ', $subfield_name));; $sublabel = "$sublabel $index"; $subfield_config['label'] = $sublabel; $subfield_name_with_index = "{$field_name}[{$index}][{$subfield_name}]"; $row_html .= self::generateField($subfield_name_with_index, $subfield_config); break; } } if ($index === 0){ $row_html .= "
"; } else { $row_html .= "
"; } } $label = ucwords(str_replace(['_', '-', '[', ']', '(', ')'], ' ', $label)); $label = preg_replace('/\s+/', ' ', $label); $label = trim($label); return "

{$label}

{$hidden['storage_field']}
{$row_html}
{$hidden['storage_end']}"; } public static function generateGroup($field_name, $field_config) { $label = $field_config['label'] ?? null; $condition = $field_config['condition'] ?? []; $fields = $field_config['fields']; if (preg_match('/\[\d+\]\[/', $field_name) && substr($field_name, -1) !== ']') { $normal_field_name = "{$field_name}]"; extract(self::prepareField($normal_field_name, $condition, "dibraco-group")); } else { extract(self::prepareField($field_name, $condition, "dibraco-group")); } if ($label !==null) { $label = ucwords(str_replace(['_', '-'], ' ', $label)); $label = '

' . $label . '

'; } $subfields_html = ''; foreach ($fields as $subfield_name => $subfield_config) { $subfield_config['label'] = $subfield_config['label'] ?? ucwords(str_replace(['_', '-'], ' ', $subfield_name)); $subfield_config['condition'] = $subfield_config['condition'] ?? []; $prefixed_subfield_name = "{$field_name}_{$subfield_name}"; if (preg_match('/\[\d+\]\[/', $field_name) && substr($field_name, -1) !== ']') { $prefixed_subfield_name = "{$field_name}_{$subfield_name}]"; } $subfields_html .= self::generateField($prefixed_subfield_name, $subfield_config); } $field_name = $normal_field_name ?? $field_name; return "
{$label}
{$subfields_html}
"; } }