| 1 |
<?php |
| 2 |
namespace MasterAddons\Inc\Admin\WidgetBuilder\Controls; |
| 3 |
|
| 4 |
use MasterAddons\Inc\Admin\WidgetBuilder\Controls\Control_Base; |
| 5 |
|
| 6 |
defined('ABSPATH') || exit; |
| 7 |
|
| 8 |
/** |
| 9 |
* SELECT Control |
| 10 |
* Handles Elementor SELECT control type |
| 11 |
* Dropdown select control with options |
| 12 |
* |
| 13 |
* Supported properties: |
| 14 |
* - label: Control label |
| 15 |
* - name: Field name (used for control key) |
| 16 |
* - description: Description below field |
| 17 |
* - default: Default selected value |
| 18 |
* - options: Array of options (key => label pairs) |
| 19 |
* |
| 20 |
* Common settings (handled by base class): |
| 21 |
* - show_label: Show/hide label |
| 22 |
* - label_block: Display label on separate line |
| 23 |
* - responsive: Enable responsive control |
| 24 |
* - dynamic: Enable dynamic content support |
| 25 |
* - separator: Control separator position |
| 26 |
* - condition: Conditional display logic |
| 27 |
*/ |
| 28 |
class Select extends Control_Base { |
| 29 |
|
| 30 |
public function get_type() { |
| 31 |
return 'SELECT'; |
| 32 |
} |
| 33 |
|
| 34 |
public function build($control_key, $field) { |
| 35 |
// Set first option as default if no default is set |
| 36 |
if (!isset($field['default']) || $field['default'] === '') { |
| 37 |
if (!empty($field['options']) && is_array($field['options'])) { |
| 38 |
// Handle array of objects from UI |
| 39 |
if (isset($field['options'][0]) && is_array($field['options'][0]) && isset($field['options'][0]['value'])) { |
| 40 |
$field['default'] = $field['options'][0]['value']; |
| 41 |
} |
| 42 |
// Handle associative array |
| 43 |
else if (!isset($field['options'][0])) { |
| 44 |
$firstKey = array_key_first($field['options']); |
| 45 |
if ($firstKey !== null) { |
| 46 |
$field['default'] = $firstKey; |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
$label = !empty($field['label']) ? $field['label'] : 'Select'; |
| 53 |
|
| 54 |
// Check if responsive control |
| 55 |
$is_responsive = isset($field['responsive']) && $field['responsive']; |
| 56 |
|
| 57 |
// Build control header |
| 58 |
$content = $this->build_control_header($control_key, $label, 'SELECT', $is_responsive); |
| 59 |
|
| 60 |
// Add SELECT-specific properties FIRST |
| 61 |
$content .= $this->build_select_properties($field); |
| 62 |
|
| 63 |
// Add common properties |
| 64 |
$content .= $this->build_common_properties($field); |
| 65 |
|
| 66 |
// Close control |
| 67 |
$content .= $this->build_control_footer(); |
| 68 |
|
| 69 |
return $content; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Build SELECT-specific properties |
| 74 |
* |
| 75 |
* @param array $field Field configuration |
| 76 |
* @return string |
| 77 |
*/ |
| 78 |
protected function build_select_properties($field) { |
| 79 |
$content = ''; |
| 80 |
|
| 81 |
// Options - dropdown options |
| 82 |
if (!empty($field['options']) && is_array($field['options'])) { |
| 83 |
$content .= $this->build_options($field['options']); |
| 84 |
} |
| 85 |
|
| 86 |
return $content; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Build options array |
| 91 |
* |
| 92 |
* @param array $options |
| 93 |
* @return string |
| 94 |
*/ |
| 95 |
private function build_options($options) { |
| 96 |
$content = "\t\t\t\t'options' => [\n"; |
| 97 |
|
| 98 |
// Check if options is an array of objects (from the UI) or key-value pairs |
| 99 |
if (!empty($options) && is_array($options)) { |
| 100 |
// Check if this is a numeric array (array of objects from UI) |
| 101 |
if (isset($options[0]) && is_array($options[0])) { |
| 102 |
// Handle array of objects [{value: 'val1', label: 'Label 1'}, ...] |
| 103 |
foreach ($options as $option) { |
| 104 |
if (isset($option['value']) && isset($option['label'])) { |
| 105 |
$value = esc_js($option['value']); |
| 106 |
$label = esc_js($option['label']); |
| 107 |
$content .= "\t\t\t\t\t'" . $value . "' => esc_html__('" . $label . "', 'master-addons'),\n"; |
| 108 |
} |
| 109 |
} |
| 110 |
} else { |
| 111 |
// Handle associative array (key => label pairs) |
| 112 |
foreach ($options as $key => $label) { |
| 113 |
// If label is an array (shouldn't happen but let's be safe) |
| 114 |
if (is_array($label)) { |
| 115 |
if (isset($label['label'])) { |
| 116 |
$label = $label['label']; |
| 117 |
} else { |
| 118 |
$label = 'Option ' . $key; |
| 119 |
} |
| 120 |
} |
| 121 |
$content .= "\t\t\t\t\t'" . esc_js($key) . "' => esc_html__('" . esc_js($label) . "', 'master-addons'),\n"; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
$content .= "\t\t\t\t],\n"; |
| 127 |
return $content; |
| 128 |
} |
| 129 |
|
| 130 |
protected function default_label() { |
| 131 |
return 'Select'; |
| 132 |
} |
| 133 |
|
| 134 |
public function get_config($control_key, $field) { |
| 135 |
// First option becomes the default when none is set (mirrors build()). |
| 136 |
if (!isset($field['default']) || $field['default'] === '') { |
| 137 |
if (!empty($field['options']) && is_array($field['options'])) { |
| 138 |
if (isset($field['options'][0]) && is_array($field['options'][0]) && isset($field['options'][0]['value'])) { |
| 139 |
$field['default'] = $field['options'][0]['value']; |
| 140 |
} elseif (!isset($field['options'][0])) { |
| 141 |
$firstKey = array_key_first($field['options']); |
| 142 |
if ($firstKey !== null) { |
| 143 |
$field['default'] = $firstKey; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
return parent::get_config($control_key, $field); |
| 149 |
} |
| 150 |
|
| 151 |
protected function get_type_specific_config($field) { |
| 152 |
$config = []; |
| 153 |
if (!empty($field['options']) && is_array($field['options'])) { |
| 154 |
$config['options'] = $this->normalize_select_options($field['options']); |
| 155 |
} |
| 156 |
return $config; |
| 157 |
} |
| 158 |
|
| 159 |
private function normalize_select_options($options) { |
| 160 |
$out = []; |
| 161 |
if (isset($options[0]) && is_array($options[0])) { |
| 162 |
foreach ($options as $option) { |
| 163 |
if (isset($option['value']) && isset($option['label'])) { |
| 164 |
// translators: dynamic user-defined select option label. |
| 165 |
$out[$option['value']] = esc_html($option['label']); |
| 166 |
} |
| 167 |
} |
| 168 |
} else { |
| 169 |
foreach ($options as $key => $label) { |
| 170 |
if (is_array($label)) { |
| 171 |
$label = isset($label['label']) ? $label['label'] : 'Option ' . $key; |
| 172 |
} |
| 173 |
// translators: dynamic user-defined select option label. |
| 174 |
$out[$key] = esc_html($label); |
| 175 |
} |
| 176 |
} |
| 177 |
return $out; |
| 178 |
} |
| 179 |
} |
| 180 |
|