InputSlider.php
148 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The input slider control. |
| 4 | * |
| 5 | * Creates an input slider control. |
| 6 | * |
| 7 | * @package kirki-input-slider |
| 8 | */ |
| 9 | |
| 10 | namespace Kirki\Control; |
| 11 | |
| 12 | use Kirki\Control\Base; |
| 13 | use Kirki\URL; |
| 14 | |
| 15 | /** |
| 16 | * Input slider control. |
| 17 | * |
| 18 | * @since 1.0 |
| 19 | */ |
| 20 | class InputSlider extends Base { |
| 21 | |
| 22 | /** |
| 23 | * The control type. |
| 24 | * |
| 25 | * @since 1.0 |
| 26 | * @access public |
| 27 | * @var string |
| 28 | */ |
| 29 | public $type = 'kirki-input-slider'; |
| 30 | |
| 31 | /** |
| 32 | * The control version. |
| 33 | * |
| 34 | * @since 1.0 |
| 35 | * @access public |
| 36 | * @var string |
| 37 | */ |
| 38 | public static $control_ver = '0.1.0'; |
| 39 | |
| 40 | /** |
| 41 | * Control's value unit. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | public $value_unit = ''; |
| 46 | |
| 47 | /** |
| 48 | * Control's value number. |
| 49 | * |
| 50 | * @var string |
| 51 | */ |
| 52 | public $value_number = 0; |
| 53 | |
| 54 | /** |
| 55 | * Control's constructor. |
| 56 | * |
| 57 | * @since 1.0.0 |
| 58 | * |
| 59 | * @param WP_Customize_Manager $wp_customize WP_Customize_Manager instance. |
| 60 | * @param string $id The control's ID. |
| 61 | * @param array $args The control's arguments. |
| 62 | */ |
| 63 | public function __construct( $wp_customize, $id, $args = array() ) { |
| 64 | |
| 65 | parent::__construct( $wp_customize, $id, $args ); |
| 66 | |
| 67 | // If `unit` choice is defined. |
| 68 | if ( ! empty( $this->choices['unit'] ) ) { |
| 69 | $this->value_unit = $this->choices['unit']; |
| 70 | } |
| 71 | |
| 72 | // If the value includes the unit, then replace the `value_unit` (set from choice) with unit from value. |
| 73 | if ( ! is_numeric( $this->value() ) ) { |
| 74 | $this->value_unit = preg_replace( '/\d+/', '', $this->value() ); |
| 75 | $this->value_number = str_ireplace( $this->value_unit, '', $this->value() ); |
| 76 | $this->value_number = (float) $this->value_number; |
| 77 | } else { |
| 78 | $this->value_number = (float) $this->value(); |
| 79 | } |
| 80 | |
| 81 | // Set default choices. |
| 82 | $this->choices = wp_parse_args( |
| 83 | $this->choices, |
| 84 | [ |
| 85 | 'min' => 0, |
| 86 | 'max' => 100, |
| 87 | 'step' => 1, |
| 88 | ] |
| 89 | ); |
| 90 | |
| 91 | $this->choices['min'] = (float) $this->choices['min']; |
| 92 | $this->choices['max'] = (float) $this->choices['max']; |
| 93 | $this->choices['step'] = (float) $this->choices['step']; |
| 94 | |
| 95 | // Value number must not be less than min and must not be greater than max. |
| 96 | $this->value_number = $this->value_number < $this->choices['min'] ? $this->choices['min'] : $this->value_number; |
| 97 | $this->value_number = $this->value_number > $this->choices['max'] ? $this->choices['max'] : $this->value_number; |
| 98 | |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Enqueue control related styles/scripts. |
| 103 | * |
| 104 | * @since 1.0 |
| 105 | * @access public |
| 106 | */ |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * Refresh the parameters passed to the JavaScript via JSON. |
| 111 | * |
| 112 | * @see WP_Customize_Control::to_json() |
| 113 | * |
| 114 | * @since 1.0 |
| 115 | * @access public |
| 116 | */ |
| 117 | public function to_json() { |
| 118 | |
| 119 | parent::to_json(); |
| 120 | |
| 121 | if ( isset( $this->json['label'] ) ) { |
| 122 | $this->json['label'] = html_entity_decode( $this->json['label'] ); |
| 123 | } |
| 124 | |
| 125 | if ( isset( $this->json['description'] ) ) { |
| 126 | $this->json['description'] = html_entity_decode( $this->json['description'] ); |
| 127 | } |
| 128 | |
| 129 | $this->json['value_number'] = $this->value_number; |
| 130 | $this->json['value_unit'] = $this->value_unit; |
| 131 | $this->json['value'] = $this->value_number . $this->value_unit; |
| 132 | |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * An Underscore (JS) template for this control's content (but not its container). |
| 137 | * |
| 138 | * Class variables for this control class are available in the `data` JS object; |
| 139 | * export custom variables by overriding WP_Customize_Control::to_json(). |
| 140 | * |
| 141 | * @see WP_Customize_Control::print_template() |
| 142 | * |
| 143 | * @since 1.0 |
| 144 | */ |
| 145 | protected function content_template() {} |
| 146 | |
| 147 | } |
| 148 |