| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* @since 3.0 |
| 8 |
*/ |
| 9 |
class FrmFieldNumber extends FrmFieldType { |
| 10 |
|
| 11 |
/** |
| 12 |
* @var string |
| 13 |
* @since 3.0 |
| 14 |
*/ |
| 15 |
protected $type = 'number'; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var bool |
| 19 |
*/ |
| 20 |
protected $array_allowed = false; |
| 21 |
|
| 22 |
/** |
| 23 |
* @return bool[] |
| 24 |
*/ |
| 25 |
protected function field_settings_for_type() { |
| 26 |
$settings = array( |
| 27 |
'size' => true, |
| 28 |
'clear_on_focus' => true, |
| 29 |
'invalid' => true, |
| 30 |
'range' => true, |
| 31 |
); |
| 32 |
|
| 33 |
$settings['max'] = false; |
| 34 |
|
| 35 |
return $settings; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* @return array |
| 40 |
*/ |
| 41 |
protected function extra_field_opts() { |
| 42 |
return array( |
| 43 |
'minnum' => 0, |
| 44 |
'maxnum' => 9999999, |
| 45 |
'step' => 'any', |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @since 3.01.03 |
| 51 |
* |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
protected function add_extra_html_atts( $args, &$input_html ) { |
| 55 |
$this->add_min_max( $args, $input_html ); |
| 56 |
} |
| 57 |
|
| 58 |
public function validate( $args ) { |
| 59 |
$errors = array(); |
| 60 |
|
| 61 |
$this->remove_commas_from_number( $args ); |
| 62 |
|
| 63 |
// Validate the number format. |
| 64 |
if ( ! is_numeric( $args['value'] ) && '' !== $args['value'] ) { |
| 65 |
$errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $this->field, 'invalid' ); |
| 66 |
} |
| 67 |
|
| 68 |
// validate number settings |
| 69 |
if ( $args['value'] != '' ) { |
| 70 |
// only check if options are available in settings |
| 71 |
$minnum = FrmField::get_option( $this->field, 'minnum' ); |
| 72 |
$maxnum = FrmField::get_option( $this->field, 'maxnum' ); |
| 73 |
if ( $maxnum !== '' && $minnum !== '' ) { |
| 74 |
$value = (float) $args['value']; |
| 75 |
if ( $value < $minnum ) { |
| 76 |
$errors[ 'field' . $args['id'] ] = __( 'Please select a higher number', 'formidable' ); |
| 77 |
} elseif ( $value > $maxnum ) { |
| 78 |
$errors[ 'field' . $args['id'] ] = __( 'Please select a lower number', 'formidable' ); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
$this->validate_step( $errors, $args ); |
| 83 |
} |
| 84 |
|
| 85 |
return $errors; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Validates the step setting. |
| 90 |
* |
| 91 |
* @since 5.2.06 |
| 92 |
* |
| 93 |
* @param array $errors Errors array. |
| 94 |
* @param array $args Validation args. |
| 95 |
* |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
private function validate_step( &$errors, $args ) { |
| 99 |
if ( isset( $errors[ 'field' . $args['id'] ] ) ) { |
| 100 |
// Don't need to check if value is invalid before. |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
$step = FrmField::get_option( $this->field, 'step' ); |
| 105 |
if ( ! $step || ! is_numeric( $step ) ) { |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
$result = $this->check_value_is_valid_with_step( $args['value'], $step ); |
| 110 |
if ( ! $result ) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
$errors[ 'field' . $args['id'] ] = sprintf( |
| 115 |
// Translators: %1$s: the first nearest value; %2$s: the second nearest value. |
| 116 |
__( 'Please enter a valid value. Two nearest valid values are %1$s and %2$s', 'formidable' ), |
| 117 |
floatval( $result[0] ), |
| 118 |
floatval( $result[1] ) |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Checks if value is valid with the given step. |
| 124 |
* |
| 125 |
* @since 5.2.07 |
| 126 |
* |
| 127 |
* @param numeric $value The value. |
| 128 |
* @param numeric $step The step. |
| 129 |
* @return array|int Return `0` if valid. Otherwise, return an array contains two nearest values. |
| 130 |
*/ |
| 131 |
private function check_value_is_valid_with_step( $value, $step ) { |
| 132 |
// Count the number of decimals. |
| 133 |
$decimals = max( FrmAppHelper::count_decimals( $value ), FrmAppHelper::count_decimals( $step ) ); |
| 134 |
|
| 135 |
// Convert value and step to int to prevent precision problem. |
| 136 |
$pow = pow( 10, $decimals ); |
| 137 |
$value = intval( $pow * $value ); |
| 138 |
$step = intval( $pow * $step ); |
| 139 |
$div = $value / $step; |
| 140 |
if ( is_int( $div ) ) { |
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
$div = floor( $div ); |
| 145 |
return array( $div * $step / $pow, ( $div + 1 ) * $step / $pow ); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* IE fallback for number fields |
| 150 |
* Remove the comma when HTML5 isn't supported |
| 151 |
* |
| 152 |
* @since 3.0 |
| 153 |
* |
| 154 |
* @param array $args |
| 155 |
* |
| 156 |
* @return void |
| 157 |
*/ |
| 158 |
private function remove_commas_from_number( &$args ) { |
| 159 |
if ( strpos( $args['value'], ',' ) ) { |
| 160 |
$args['value'] = str_replace( ',', '', $args['value'] ); |
| 161 |
FrmEntriesHelper::set_posted_value( $this->field, $args['value'], $args ); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Force the value to be numeric before it's saved in the DB |
| 167 |
*/ |
| 168 |
public function set_value_before_save( $value ) { |
| 169 |
if ( ! is_numeric( $value ) ) { |
| 170 |
$value = (float) $value; |
| 171 |
} |
| 172 |
|
| 173 |
return $value; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* @since 4.0.04 |
| 178 |
* |
| 179 |
* @return void |
| 180 |
*/ |
| 181 |
public function sanitize_value( &$value ) { |
| 182 |
FrmAppHelper::sanitize_value( 'sanitize_text_field', $value ); |
| 183 |
} |
| 184 |
} |
| 185 |
|