From 05c1b578d05b1adb40866a0445f57b1c18465a4f Mon Sep 17 00:00:00 2001 From: Jake Jackson Date: Mon, 2 Sep 2024 09:57:28 +1000 Subject: [PATCH] Merge pull request #1570 from GravityPDF/int-and-float-numbers Allow valid integers (10) and floats (10.5) in Number fields --- src/Helper/Helper_Abstract_Options.php | 7 ++++++- tests/phpunit/unit-tests/test-options-api.php | 11 +++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Helper/Helper_Abstract_Options.php b/src/Helper/Helper_Abstract_Options.php index 84dad11c0..0cbc62dbe 100644 --- a/src/Helper/Helper_Abstract_Options.php +++ b/src/Helper/Helper_Abstract_Options.php @@ -1221,7 +1221,12 @@ public function sanitize_trim_field( $input ) { * @since 6.11 Force minimum and maximum values */ public function sanitize_number_field( $value, $key = '', $input = [], $settings = [] ) { - $value = (int) $value; + if ( ! is_numeric( $value ) ) { + return 0; + } + + /* force PHP to convert string to int or float, depending on input */ + $value += 0; /* If number less than the minimum, set to the minimum */ if ( ! empty( $settings['min'] ) && $value < $settings['min'] ) { diff --git a/tests/phpunit/unit-tests/test-options-api.php b/tests/phpunit/unit-tests/test-options-api.php index 85a24ec15..03d321d9d 100644 --- a/tests/phpunit/unit-tests/test-options-api.php +++ b/tests/phpunit/unit-tests/test-options-api.php @@ -899,12 +899,15 @@ public function test_sanitize_number_field( $expected, $input ) { */ public function dataprovider_sanitize_number() { return [ - [ 122, '122.34343The' ], - [ 0, 'The122.34343' ], + [ 0, '122.34343The' ], /* not a number */ + [ 0, 'The122.34343' ], /* not a number */ [ 20, '20' ], + [ 20, 20 ], + [ 30.261, 30.261 ], + [ 30.261, '30.261' ], [ 2000, '2000' ], - [ 20, '20.50' ], - [ 50, '50,20' ], + [ 20.5, '20.50' ], + [ 0, '50,20' ], /* not a number */ ]; }