Skip to content

Commit

Permalink
Merge pull request #1570 from GravityPDF/int-and-float-numbers
Browse files Browse the repository at this point in the history
Allow valid integers (10) and floats (10.5) in Number fields
  • Loading branch information
jakejackson1 committed Sep 2, 2024
1 parent c721a04 commit 05c1b57
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/Helper/Helper_Abstract_Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) {
Expand Down
11 changes: 7 additions & 4 deletions tests/phpunit/unit-tests/test-options-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
];
}

Expand Down

0 comments on commit 05c1b57

Please sign in to comment.