|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Override field methods |
|
4
|
|
|
* |
|
5
|
|
|
* @package Kirki |
|
6
|
|
|
* @subpackage Controls |
|
7
|
|
|
* @copyright Copyright (c) 2016, 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
|
|
|
* Sanitizes numeric values. |
|
41
|
|
|
* |
|
42
|
|
|
* @access public |
|
43
|
|
|
* @param boolean|integer|string|null $value The checkbox value. |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
|
|
public function sanitize( $value = null ) { |
|
47
|
|
|
|
|
48
|
|
|
// Make sure min, max & step are all numeric. |
|
49
|
|
|
$min = ( isset( $this->choices['min'] ) && ! is_numeric( $this->choices['min'] ) ) ? filter_var( $this->choices['min'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) : -999999999; |
|
50
|
|
|
$max = ( isset( $this->choices['max'] ) && ! is_numeric( $this->choices['max'] ) ) ? filter_var( $this->choices['max'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) : 999999999; |
|
51
|
|
|
$step = ( isset( $this->choices['step'] ) && ! is_numeric( $this->choices['step'] ) ) ? filter_var( $this->choices['step'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) : 1; |
|
52
|
|
|
|
|
53
|
|
|
if ( ! is_numeric( $value ) ) { |
|
54
|
|
|
$value = filter_var( $value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Minimum value limit. |
|
58
|
|
|
if ( $value < $min ) { |
|
59
|
|
|
return $min; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// Maximum value limit. |
|
63
|
|
|
if ( $value > $max ) { |
|
64
|
|
|
return $max; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// Step divider. |
|
68
|
|
|
if ( isset( $this->choices['min'] ) && isset( $this->choices['step'] ) ) { |
|
69
|
|
|
$valid = range( $min, $max, $step ); |
|
70
|
|
|
|
|
71
|
|
|
$smallest = array(); |
|
72
|
|
|
foreach ( $valid as $possible_value ) { |
|
73
|
|
|
$smallest[ $possible_value ] = abs( $possible_value - $value ); |
|
74
|
|
|
} |
|
75
|
|
|
asort( $smallest ); |
|
76
|
|
|
$value = key( $smallest ); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $value; |
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|