| 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 |
* SWITCHER Control |
| 10 |
* Handles Elementor SWITCHER control type |
| 11 |
* Toggle switch control with On/Off states |
| 12 |
* |
| 13 |
* Supported properties: |
| 14 |
* - label: Control label |
| 15 |
* - name: Field name (used for control key) |
| 16 |
* - description: Description below field |
| 17 |
* - default: Default value (yes/no or custom return_value) |
| 18 |
* - label_on: Text for ON state (default: 'Yes') |
| 19 |
* - label_off: Text for OFF state (default: 'No') |
| 20 |
* - return_value: Value when ON (default: 'yes') |
| 21 |
* |
| 22 |
* Common settings (handled by base class): |
| 23 |
* - show_label: Show/hide label |
| 24 |
* - label_block: Display label on separate line |
| 25 |
* - dynamic: Enable dynamic content support |
| 26 |
* - separator: Control separator position |
| 27 |
* - condition: Conditional display logic |
| 28 |
*/ |
| 29 |
class Switcher extends Control_Base { |
| 30 |
|
| 31 |
public function get_type() { |
| 32 |
return 'SWITCHER'; |
| 33 |
} |
| 34 |
|
| 35 |
public function build($control_key, $field) { |
| 36 |
$label = !empty($field['label']) ? $field['label'] : 'Switcher'; |
| 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, 'SWITCHER', $is_responsive); |
| 43 |
|
| 44 |
// Add SWITCHER-specific properties FIRST |
| 45 |
$content .= $this->build_switcher_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 SWITCHER-specific properties |
| 58 |
* |
| 59 |
* @param array $field Field configuration |
| 60 |
* @return string |
| 61 |
*/ |
| 62 |
protected function build_switcher_properties($field) { |
| 63 |
$content = ''; |
| 64 |
|
| 65 |
// Label ON - text displayed for ON state |
| 66 |
if (!empty($field['label_on'])) { |
| 67 |
$content .= $this->format_string_property('label_on', $field['label_on']); |
| 68 |
} |
| 69 |
|
| 70 |
// Label OFF - text displayed for OFF state |
| 71 |
if (!empty($field['label_off'])) { |
| 72 |
$content .= $this->format_string_property('label_off', $field['label_off']); |
| 73 |
} |
| 74 |
|
| 75 |
// Return value - value returned when switcher is ON |
| 76 |
if (isset($field['return_value'])) { |
| 77 |
$content .= $this->format_plain_string_property('return_value', $field['return_value']); |
| 78 |
} |
| 79 |
|
| 80 |
return $content; |
| 81 |
} |
| 82 |
|
| 83 |
protected function default_label() { |
| 84 |
return 'Switcher'; |
| 85 |
} |
| 86 |
|
| 87 |
protected function get_type_specific_config($field) { |
| 88 |
$config = []; |
| 89 |
if (!empty($field['label_on'])) { |
| 90 |
// translators: dynamic user-defined switcher ON label. |
| 91 |
$config['label_on'] = esc_html($field['label_on']); |
| 92 |
} |
| 93 |
if (!empty($field['label_off'])) { |
| 94 |
// translators: dynamic user-defined switcher OFF label. |
| 95 |
$config['label_off'] = esc_html($field['label_off']); |
| 96 |
} |
| 97 |
if (isset($field['return_value'])) { |
| 98 |
$config['return_value'] = $field['return_value']; |
| 99 |
} |
| 100 |
return $config; |
| 101 |
} |
| 102 |
} |
| 103 |
|