1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Override field methods |
4
|
|
|
* |
5
|
|
|
* @package Kirki |
6
|
|
|
* @subpackage Controls |
7
|
|
|
* @copyright Copyright (c) 2017, Aristeides Stathopoulos |
8
|
|
|
* @license http://opensource.org/licenses/https://opensource.org/licenses/MIT |
9
|
|
|
* @since 2.2.7 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Field overrides. |
14
|
|
|
*/ |
15
|
|
|
class Kirki_Field_Number extends Kirki_Field { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Sets the control type. |
19
|
|
|
* |
20
|
|
|
* @access protected |
21
|
|
|
*/ |
22
|
|
|
protected function set_type() { |
23
|
|
|
|
24
|
|
|
$this->type = 'kirki-number'; |
25
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Sets the $sanitize_callback |
30
|
|
|
* |
31
|
|
|
* @access protected |
32
|
|
|
*/ |
33
|
|
|
protected function set_sanitize_callback() { |
34
|
|
|
|
35
|
|
|
$this->sanitize_callback = array( $this, 'sanitize' ); |
36
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Sets the $choices |
41
|
|
|
* |
42
|
|
|
* @access protected |
43
|
|
|
*/ |
44
|
|
|
protected function set_choices() { |
45
|
|
|
|
46
|
|
|
$this->choices = wp_parse_args( |
47
|
|
|
$this->choices, |
48
|
|
|
array( |
49
|
|
|
'min' => -999999999, |
50
|
|
|
'max' => 999999999, |
51
|
|
|
'step' => 1, |
52
|
|
|
) |
53
|
|
|
); |
54
|
|
|
// Make sure min, max & step are all numeric. |
55
|
|
|
$this->choices['min'] = filter_var( $this->choices['min'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
56
|
|
|
$this->choices['max'] = filter_var( $this->choices['max'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
57
|
|
|
$this->choices['step'] = filter_var( $this->choices['step'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Sanitizes numeric values. |
62
|
|
|
* |
63
|
|
|
* @access public |
64
|
|
|
* @param integer|string $value The checkbox value. |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function sanitize( $value = 0 ) { |
68
|
|
|
|
69
|
|
|
$this->set_choices(); |
70
|
|
|
|
71
|
|
|
$value = filter_var( $value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
72
|
|
|
|
73
|
|
|
// Minimum & maximum value limits. |
74
|
|
|
if ( $value < $this->choices['min'] || $value > $this->choices['max'] ) { |
75
|
|
|
return max( min( $value, $this->choices['max'] ), $this->choices['min'] ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Only multiple of steps. |
79
|
|
|
$steps = ( $value - $this->choices['min'] ) / $this->choices['step']; |
80
|
|
|
if ( ! is_int( $steps ) ) { |
81
|
|
|
$value = $this->choices['min'] + ( round( $steps ) * $this->choices['step'] ); |
82
|
|
|
} |
83
|
|
|
return $value; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|