InputSlider.php
109 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Override field methods. |
| 4 | * |
| 5 | * @package kirki-input-slider |
| 6 | */ |
| 7 | |
| 8 | namespace Kirki\Field; |
| 9 | |
| 10 | use Kirki\Field; |
| 11 | |
| 12 | /** |
| 13 | * Field overrides. |
| 14 | * |
| 15 | * @since 1.0 |
| 16 | */ |
| 17 | class InputSlider extends Field { |
| 18 | |
| 19 | /** |
| 20 | * The field type. |
| 21 | * |
| 22 | * @since 1.0 |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | public $type = 'kirki-input-slider'; |
| 27 | |
| 28 | /** |
| 29 | * The control class-name. |
| 30 | * |
| 31 | * @since 1.0 |
| 32 | * |
| 33 | * @var string |
| 34 | */ |
| 35 | protected $control_class = '\Kirki\Control\InputSlider'; |
| 36 | |
| 37 | /** |
| 38 | * Whether we should register the control class for JS-templating or not. |
| 39 | * |
| 40 | * @since 1.0 |
| 41 | * |
| 42 | * @var bool |
| 43 | */ |
| 44 | protected $control_has_js_template = false; |
| 45 | |
| 46 | /** |
| 47 | * Filter arguments before creating the setting. |
| 48 | * |
| 49 | * @since 1.0.0 |
| 50 | * |
| 51 | * @param array $args The field arguments. |
| 52 | * @param \WP_Customize_Manager $wp_customize The customizer instance. |
| 53 | * |
| 54 | * @return array $args The maybe-filtered arguments. |
| 55 | */ |
| 56 | public function filter_setting_args( $args, $wp_customize ) { |
| 57 | |
| 58 | if ( $args['settings'] === $this->args['settings'] ) { |
| 59 | $args = parent::filter_setting_args( $args, $wp_customize ); |
| 60 | |
| 61 | // Set the sanitize_callback if none is defined. |
| 62 | if ( ! isset( $args['sanitize_callback'] ) || ! $args['sanitize_callback'] ) { |
| 63 | $args['sanitize_callback'] = [ __CLASS__, 'sanitize' ]; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return $args; |
| 68 | |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Sanitize the value. |
| 73 | * |
| 74 | * @param mixed $value The value to sanitize. |
| 75 | * @return mixed |
| 76 | */ |
| 77 | public static function sanitize( $value ) { |
| 78 | |
| 79 | if ( is_numeric( $value ) ) { |
| 80 | return filter_var( $value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
| 81 | } else { |
| 82 | return sanitize_text_field( $value ); |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Filter arguments before creating the control. |
| 89 | * |
| 90 | * @since 1.0.0 |
| 91 | * |
| 92 | * @param array $args The field arguments. |
| 93 | * @param \WP_Customize_Manager $wp_customize The customizer instance. |
| 94 | * |
| 95 | * @return array $args The maybe-filtered arguments. |
| 96 | */ |
| 97 | public function filter_control_args( $args, $wp_customize ) { |
| 98 | |
| 99 | if ( $args['settings'] === $this->args['settings'] ) { |
| 100 | $args = parent::filter_control_args( $args, $wp_customize ); |
| 101 | $args['type'] = 'kirki-input-slider'; |
| 102 | } |
| 103 | |
| 104 | return $args; |
| 105 | |
| 106 | } |
| 107 | |
| 108 | } |
| 109 |