|
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 checkbox values. |
|
41
|
|
|
* |
|
42
|
|
|
* @static |
|
43
|
|
|
* @access public |
|
44
|
|
|
* @param boolean|integer|string|null $value The checkbox value. |
|
45
|
|
|
* @return bool |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function sanitize( $value = null ) { |
|
48
|
|
|
|
|
49
|
|
|
$value = ( is_numeric( $value ) ) ? $value : intval( $value ); |
|
50
|
|
|
|
|
51
|
|
|
// Minimum value limit. |
|
52
|
|
|
if ( isset( $this->choices['min'] ) ) { |
|
53
|
|
|
$min = ( is_numeric( $this->choices['min'] ) ) ? $this->choices['min'] : intval( $this->choices['min'] ); |
|
|
|
|
|
|
54
|
|
|
if ( $value < $min ) { |
|
55
|
|
|
$value = $min; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// Maximum value limit. |
|
60
|
|
|
if ( isset( $this->choices['max'] ) ) { |
|
61
|
|
|
$max = ( is_numeric( $this->choices['max'] ) ) ? $this->choices['max'] : intval( $this->choices['max'] ); |
|
62
|
|
|
if ( $value > $max ) { |
|
63
|
|
|
$value = $max; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// Step divider. |
|
68
|
|
|
if ( isset( $this->choices['min'] ) && isset( $this->choices['step'] ) ) { |
|
69
|
|
|
$min = ( is_numeric( $this->choices['min'] ) ) ? $this->choices['min'] : intval( $this->choices['min'] ); |
|
70
|
|
|
$max = ( is_numeric( $this->choices['max'] ) ) ? $this->choices['max'] : intval( $this->choices['max'] ); |
|
71
|
|
|
$step = ( is_numeric( $this->choices['step'] ) ) ? $this->choices['step'] : intval( $this->choices['step'] ); |
|
72
|
|
|
$valid = range( $min, $max, $step ); |
|
73
|
|
|
foreach ( $valid as $possible_value ) { |
|
74
|
|
|
$smallest[ $possible_value ] = abs( $possible_value - $value ); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
asort( $smallest ); |
|
77
|
|
|
$value = key( $smallest ); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $value; |
|
81
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.