| 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 |
* CHOOSE Control |
| 10 |
* Handles Elementor CHOOSE control type |
| 11 |
* Visual chooser with icon options (like alignment, position selector) |
| 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 with icon and title (key => ['title' => '', 'icon' => '']) |
| 19 |
* - toggle: Allow deselecting by clicking again (boolean) |
| 20 |
* |
| 21 |
* Common settings (handled by base class): |
| 22 |
* - show_label: Show/hide label |
| 23 |
* - label_block: Display label on separate line |
| 24 |
* - responsive: Enable responsive control |
| 25 |
* - separator: Control separator position |
| 26 |
* - condition: Conditional display logic |
| 27 |
* - selectors: CSS selectors for styling |
| 28 |
*/ |
| 29 |
class Choose extends Control_Base { |
| 30 |
|
| 31 |
public function get_type() { |
| 32 |
return 'CHOOSE'; |
| 33 |
} |
| 34 |
|
| 35 |
public function build($control_key, $field) { |
| 36 |
$label = !empty($field['label']) ? $field['label'] : 'Choose'; |
| 37 |
|
| 38 |
// Check if responsive control |
| 39 |
$is_responsive = isset($field['responsive']) && $field['responsive']; |
| 40 |
|
| 41 |
// Build control header |
| 42 |
$content = $this->build_control_header($control_key, $label, 'CHOOSE', $is_responsive); |
| 43 |
|
| 44 |
// Add CHOOSE-specific properties FIRST |
| 45 |
$content .= $this->build_choose_properties($field); |
| 46 |
|
| 47 |
// Add common properties |
| 48 |
$content .= $this->build_common_properties($field); |
| 49 |
|
| 50 |
// Close control |
| 51 |
$content .= $this->build_control_footer(); |
| 52 |
|
| 53 |
return $content; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Build CHOOSE-specific properties |
| 58 |
* |
| 59 |
* @param array $field Field configuration |
| 60 |
* @return string |
| 61 |
*/ |
| 62 |
protected function build_choose_properties($field) { |
| 63 |
$content = ''; |
| 64 |
|
| 65 |
// Options - chooser options with icons |
| 66 |
if (!empty($field['options']) && is_array($field['options'])) { |
| 67 |
$content .= $this->build_options($field['options']); |
| 68 |
} |
| 69 |
|
| 70 |
// Toggle - allow deselecting |
| 71 |
if (isset($field['toggle']) && $field['toggle']) { |
| 72 |
$content .= "\t\t\t\t'toggle' => true,\n"; |
| 73 |
} |
| 74 |
|
| 75 |
return $content; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Build options array with icons |
| 80 |
* |
| 81 |
* @param array $options |
| 82 |
* @return string |
| 83 |
*/ |
| 84 |
private function build_options($options) { |
| 85 |
// Convert array format to keyed object format |
| 86 |
// If options is [{key: 'left', title: '...', icon: '...'}, ...] |
| 87 |
// Convert to {'left': {title: '...', icon: '...'}, ...} |
| 88 |
$normalized_options = []; |
| 89 |
|
| 90 |
foreach ($options as $index => $option) { |
| 91 |
// If option has a 'key' property, use it; otherwise use the array index |
| 92 |
if (isset($option['key']) && !empty($option['key'])) { |
| 93 |
$key = $option['key']; |
| 94 |
$normalized_options[$key] = [ |
| 95 |
'title' => $option['title'] ?? '', |
| 96 |
'icon' => $option['icon'] ?? '' |
| 97 |
]; |
| 98 |
} else { |
| 99 |
// Fallback to using array index if no key is specified |
| 100 |
$normalized_options[$index] = $option; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
$content = "\t\t\t\t'options' => [\n"; |
| 105 |
foreach ($normalized_options as $key => $option) { |
| 106 |
$content .= "\t\t\t\t\t'" . esc_js($key) . "' => [\n"; |
| 107 |
if (isset($option['title'])) { |
| 108 |
$content .= "\t\t\t\t\t\t'title' => esc_html__('" . esc_js($option['title']) . "', 'master-addons'),\n"; |
| 109 |
} |
| 110 |
if (isset($option['icon'])) { |
| 111 |
$content .= "\t\t\t\t\t\t'icon' => '" . esc_js($option['icon']) . "',\n"; |
| 112 |
} |
| 113 |
$content .= "\t\t\t\t\t],\n"; |
| 114 |
} |
| 115 |
$content .= "\t\t\t\t],\n"; |
| 116 |
return $content; |
| 117 |
} |
| 118 |
|
| 119 |
protected function default_label() { |
| 120 |
return 'Choose'; |
| 121 |
} |
| 122 |
|
| 123 |
protected function get_type_specific_config($field) { |
| 124 |
$config = []; |
| 125 |
if (!empty($field['options']) && is_array($field['options'])) { |
| 126 |
$config['options'] = $this->normalize_choose_options($field['options']); |
| 127 |
} |
| 128 |
if (!empty($field['toggle'])) { |
| 129 |
$config['toggle'] = true; |
| 130 |
} |
| 131 |
return $config; |
| 132 |
} |
| 133 |
|
| 134 |
private function normalize_choose_options($options) { |
| 135 |
$out = []; |
| 136 |
foreach ($options as $index => $option) { |
| 137 |
if (isset($option['key']) && !empty($option['key'])) { |
| 138 |
$out[$option['key']] = [ |
| 139 |
// translators: dynamic user-defined choose option title. |
| 140 |
'title' => esc_html($option['title'] ?? ''), |
| 141 |
'icon' => $option['icon'] ?? '', |
| 142 |
]; |
| 143 |
} else { |
| 144 |
$entry = []; |
| 145 |
if (isset($option['title'])) { |
| 146 |
// translators: dynamic user-defined choose option title. |
| 147 |
$entry['title'] = esc_html($option['title']); |
| 148 |
} |
| 149 |
if (isset($option['icon'])) { |
| 150 |
$entry['icon'] = $option['icon']; |
| 151 |
} |
| 152 |
$out[$index] = $entry; |
| 153 |
} |
| 154 |
} |
| 155 |
return $out; |
| 156 |
} |
| 157 |
} |
| 158 |
|