aristath /
kirki
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Override field methods |
||
| 4 | * |
||
| 5 | * @package Kirki |
||
| 6 | * @subpackage Controls |
||
| 7 | * @copyright Copyright (c) 2017, Aristeides Stathopoulos |
||
| 8 | * @license 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 | $this->sanitize_callback = array( $this, 'sanitize' ); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Sets the $choices |
||
| 39 | * |
||
| 40 | * @access protected |
||
| 41 | */ |
||
| 42 | protected function set_choices() { |
||
| 43 | $this->choices = wp_parse_args( |
||
| 44 | $this->choices, |
||
| 45 | array( |
||
| 46 | 'min' => -999999999, |
||
| 47 | 'max' => 999999999, |
||
| 48 | 'step' => 1, |
||
| 49 | ) |
||
| 50 | ); |
||
| 51 | // Make sure min, max & step are all numeric. |
||
| 52 | $this->choices['min'] = filter_var( $this->choices['min'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
||
| 53 | $this->choices['max'] = filter_var( $this->choices['max'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
||
| 54 | $this->choices['step'] = filter_var( $this->choices['step'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Sanitizes numeric values. |
||
| 59 | * |
||
| 60 | * @access public |
||
| 61 | * @param integer|string $value The checkbox value. |
||
| 62 | * @return bool |
||
| 63 | */ |
||
| 64 | public function sanitize( $value = 0 ) { |
||
| 65 | $this->set_choices(); |
||
| 66 | |||
| 67 | $value = filter_var( $value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
||
| 68 | |||
| 69 | // Minimum & maximum value limits. |
||
| 70 | if ( $value < $this->choices['min'] || $value > $this->choices['max'] ) { |
||
| 71 | return max( min( $value, $this->choices['max'] ), $this->choices['min'] ); |
||
| 72 | } |
||
| 73 | |||
| 74 | // Only multiple of steps. |
||
| 75 | $steps = ( $value - $this->choices['min'] ) / $this->choices['step']; |
||
| 76 | if ( ! is_int( $steps ) ) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 77 | $value = $this->choices['min'] + ( round( $steps ) * $this->choices['step'] ); |
||
| 78 | } |
||
| 79 | return $value; |
||
| 80 | } |
||
| 81 | } |
||
| 82 |