Generic.php
5 months ago
Number.php
5 months ago
Text.php
5 months ago
Textarea.php
5 months ago
URL.php
5 months ago
Number.php
111 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Override field methods |
| 4 | * |
| 5 | * @package kirki-framework/control-generic |
| 6 | * @copyright Copyright (c) 2023, Themeum |
| 7 | * @license https://opensource.org/licenses/MIT |
| 8 | * @since 1.0 |
| 9 | */ |
| 10 | |
| 11 | namespace Kirki\Field; |
| 12 | |
| 13 | /** |
| 14 | * Field overrides. |
| 15 | * |
| 16 | * @since 1.0 |
| 17 | */ |
| 18 | class Number extends Generic { |
| 19 | |
| 20 | /** |
| 21 | * The field type. |
| 22 | * |
| 23 | * @access public |
| 24 | * @since 1.0 |
| 25 | * @var string |
| 26 | */ |
| 27 | public $type = 'kirki-number'; |
| 28 | |
| 29 | /** |
| 30 | * Filter arguments before creating the setting. |
| 31 | * |
| 32 | * @access public |
| 33 | * @since 0.1 |
| 34 | * @param array $args The field arguments. |
| 35 | * @param WP_Customize_Manager $wp_customize The customizer instance. |
| 36 | * @return array |
| 37 | */ |
| 38 | public function filter_setting_args( $args, $wp_customize ) { |
| 39 | |
| 40 | if ( $args['settings'] !== $this->args['settings'] ) { |
| 41 | return $args; |
| 42 | } |
| 43 | |
| 44 | // Set the sanitize-callback if none is defined. |
| 45 | if ( ! isset( $args['sanitize_callback'] ) || ! $args['sanitize_callback'] ) { |
| 46 | |
| 47 | $args['sanitize_callback'] = function( $value ) use ( $args ) { |
| 48 | $value = filter_var( $value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
| 49 | |
| 50 | if ( isset( $args['choices'] ) && isset( $args['choices']['min'] ) && isset( $args['choices']['max'] ) ) { |
| 51 | // Make sure min & max are all numeric. |
| 52 | $min = filter_var( $args['choices']['min'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
| 53 | $max = filter_var( $args['choices']['max'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
| 54 | |
| 55 | if ( $value < $min ) { |
| 56 | $value = $min; |
| 57 | } elseif ( $value > $max ) { |
| 58 | $value = $max; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return $value; |
| 63 | }; |
| 64 | |
| 65 | } |
| 66 | |
| 67 | return $args; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Filter arguments before creating the control. |
| 72 | * |
| 73 | * @access public |
| 74 | * @since 0.1 |
| 75 | * @param array $args The field arguments. |
| 76 | * @param WP_Customize_Manager $wp_customize The customizer instance. |
| 77 | * @return array |
| 78 | */ |
| 79 | public function filter_control_args( $args, $wp_customize ) { |
| 80 | |
| 81 | if ( $args['settings'] === $this->args['settings'] ) { |
| 82 | $args = parent::filter_control_args( $args, $wp_customize ); |
| 83 | |
| 84 | // Set the control-type. |
| 85 | $args['type'] = 'kirki-generic'; |
| 86 | |
| 87 | // Choices. |
| 88 | $args['choices'] = isset( $args['choices'] ) ? $args['choices'] : []; |
| 89 | $args['choices']['element'] = 'input'; |
| 90 | $args['choices']['type'] = 'number'; |
| 91 | $args['choices'] = wp_parse_args( |
| 92 | $args['choices'], |
| 93 | [ |
| 94 | 'min' => -999999999, |
| 95 | 'max' => 999999999, |
| 96 | 'step' => 1, |
| 97 | ] |
| 98 | ); |
| 99 | |
| 100 | // Make sure min, max & step are all numeric. |
| 101 | $args['choices']['min'] = filter_var( $args['choices']['min'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
| 102 | $args['choices']['max'] = filter_var( $args['choices']['max'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
| 103 | $args['choices']['step'] = filter_var( $args['choices']['step'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
| 104 | } |
| 105 | |
| 106 | return $args; |
| 107 | |
| 108 | } |
| 109 | |
| 110 | } |
| 111 |