Checkbox_Switch.php
125 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Customizer Control: switch. |
| 4 | * |
| 5 | * @package kirki-framework/checkbox |
| 6 | * @copyright Copyright (c) 2023, Themeum |
| 7 | * @license https://opensource.org/licenses/MIT |
| 8 | * @since 1.0 |
| 9 | */ |
| 10 | |
| 11 | namespace Kirki\Control; |
| 12 | |
| 13 | use Kirki\Control\Base; |
| 14 | use Kirki\URL; |
| 15 | |
| 16 | // Exit if accessed directly. |
| 17 | if ( ! defined( 'ABSPATH' ) ) { |
| 18 | exit; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Switch control (modified checkbox). |
| 23 | * |
| 24 | * @since 1.0 |
| 25 | */ |
| 26 | class Checkbox_Switch extends Base { |
| 27 | |
| 28 | /** |
| 29 | * The control type. |
| 30 | * |
| 31 | * @access public |
| 32 | * @since 1.0 |
| 33 | * @var string |
| 34 | */ |
| 35 | public $type = 'kirki-switch'; |
| 36 | |
| 37 | /** |
| 38 | * The control version. |
| 39 | * |
| 40 | * @static |
| 41 | * @access public |
| 42 | * @since 1.0 |
| 43 | * @var string |
| 44 | */ |
| 45 | public static $control_ver = '1.0.3'; |
| 46 | |
| 47 | /** |
| 48 | * Enqueue control related scripts/styles. |
| 49 | * |
| 50 | * @access public |
| 51 | * @since 1.0 |
| 52 | * @return void |
| 53 | */ |
| 54 | |
| 55 | |
| 56 | /** |
| 57 | * Refresh the parameters passed to the JavaScript via JSON. |
| 58 | * |
| 59 | * @since 3.4.0 |
| 60 | */ |
| 61 | public function to_json() { |
| 62 | |
| 63 | // Get the basics from the parent class. |
| 64 | parent::to_json(); |
| 65 | |
| 66 | $this->json['checkboxType'] = str_ireplace( 'kirki-', '', $this->type ); |
| 67 | |
| 68 | $this->json['defaultChoices'] = [ |
| 69 | 'on' => __( 'On', 'kirki' ), |
| 70 | 'off' => __( 'Off', 'kirki' ), |
| 71 | ]; |
| 72 | |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * An Underscore (JS) template for this control's content (but not its container). |
| 77 | * |
| 78 | * Class variables for this control class are available in the `data` JS object; |
| 79 | * export custom variables by overriding {@see WP_Customize_Control::to_json()}. |
| 80 | * |
| 81 | * @see WP_Customize_Control::print_template() |
| 82 | * |
| 83 | * @access protected |
| 84 | * @since 1.0 |
| 85 | * @return void |
| 86 | */ |
| 87 | protected function content_template() { |
| 88 | ?> |
| 89 | |
| 90 | <div class="kirki-{{ data.checkboxType }}-control kirki-{{ data.checkboxType }}"> |
| 91 | <# if ( data.label || data.description ) { #> |
| 92 | <div class="kirki-control-label"> |
| 93 | <# if ( data.label ) { #> |
| 94 | <label class="customize-control-title" for="kirki_{{ data.checkboxType }}_{{ data.id }}"> |
| 95 | {{{ data.label }}} |
| 96 | </label> |
| 97 | <# } #> |
| 98 | |
| 99 | <# if ( data.description ) { #> |
| 100 | <span class="description customize-control-description">{{{ data.description }}}</span> |
| 101 | <# } #> |
| 102 | </div> |
| 103 | <# } #> |
| 104 | |
| 105 | <div class="kirki-control-form"> |
| 106 | <input class="screen-reader-text kirki-toggle-switch-input" {{{ data.inputAttrs }}} name="kirki_{{ data.checkboxType }}_{{ data.id }}" id="kirki_{{ data.checkboxType }}_{{ data.id }}" type="checkbox" value="{{ data.value }}" {{{ data.link }}}<# if ( '1' == data.value ) { #> checked<# } #> /> |
| 107 | <label class="kirki-toggle-switch-label" for="kirki_{{ data.checkboxType }}_{{ data.id }}"> |
| 108 | <# if ('switch' === data.checkboxType) { #> |
| 109 | <span class="toggle-on"> |
| 110 | <# data.choices.on = data.choices.on || data.defaultChoices.on #> |
| 111 | {{ data.choices.on }} |
| 112 | </span> |
| 113 | <span class="toggle-off"> |
| 114 | <# data.choices.off = data.choices.off || data.defaultChoices.off #> |
| 115 | {{ data.choices.off }} |
| 116 | </span> |
| 117 | <# } #> |
| 118 | </label> |
| 119 | </div> |
| 120 | </div> |
| 121 | |
| 122 | <?php |
| 123 | } |
| 124 | } |
| 125 |