PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.10
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.10
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / models / fields / FrmFieldNumber.php

FrmFieldNumber.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.10, at classes/models/fields/FrmFieldNumber.php

189 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $frm_settings = FrmAppHelper::get_settings();
34 if ( $frm_settings->use_html ) {
35 $settings['max'] = false;
36 }
37
38 return $settings;
39 }
40
41 /**
42 * @return array
43 */
44 protected function extra_field_opts() {
45 return array(
46 'minnum' => 0,
47 'maxnum' => 9999999,
48 'step' => 'any',
49 );
50 }
51
52 /**
53 * @since 3.01.03
54 *
55 * @return void
56 */
57 protected function add_extra_html_atts( $args, &$input_html ) {
58 $this->add_min_max( $args, $input_html );
59 }
60
61 public function validate( $args ) {
62 $errors = array();
63
64 $this->remove_commas_from_number( $args );
65
66 // Validate the number format.
67 if ( ! is_numeric( $args['value'] ) && '' !== $args['value'] ) {
68 $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $this->field, 'invalid' );
69 }
70
71 // validate number settings
72 if ( $args['value'] != '' ) {
73 $frm_settings = FrmAppHelper::get_settings();
74 // only check if options are available in settings
75 $minnum = FrmField::get_option( $this->field, 'minnum' );
76 $maxnum = FrmField::get_option( $this->field, 'maxnum' );
77 if ( $frm_settings->use_html && $maxnum !== '' && $minnum !== '' ) {
78 $value = (float) $args['value'];
79 if ( $value < $minnum ) {
80 $errors[ 'field' . $args['id'] ] = __( 'Please select a higher number', 'formidable' );
81 } elseif ( $value > $maxnum ) {
82 $errors[ 'field' . $args['id'] ] = __( 'Please select a lower number', 'formidable' );
83 }
84 }
85
86 $this->validate_step( $errors, $args );
87 }
88
89 return $errors;
90 }
91
92 /**
93 * Validates the step setting.
94 *
95 * @since 5.2.06
96 *
97 * @param array $errors Errors array.
98 * @param array $args Validation args.
99 *
100 * @return void
101 */
102 private function validate_step( &$errors, $args ) {
103 if ( isset( $errors[ 'field' . $args['id'] ] ) ) {
104 // Don't need to check if value is invalid before.
105 return;
106 }
107
108 $step = FrmField::get_option( $this->field, 'step' );
109 if ( ! $step || ! is_numeric( $step ) ) {
110 return;
111 }
112
113 $result = $this->check_value_is_valid_with_step( $args['value'], $step );
114 if ( ! $result ) {
115 return;
116 }
117
118 $errors[ 'field' . $args['id'] ] = sprintf(
119 // Translators: %1$s: the first nearest value; %2$s: the second nearest value.
120 __( 'Please enter a valid value. Two nearest valid values are %1$s and %2$s', 'formidable' ),
121 floatval( $result[0] ),
122 floatval( $result[1] )
123 );
124 }
125
126 /**
127 * Checks if value is valid with the given step.
128 *
129 * @since 5.2.07
130 *
131 * @param numeric $value The value.
132 * @param numeric $step The step.
133 * @return array|int Return `0` if valid. Otherwise, return an array contains two nearest values.
134 */
135 private function check_value_is_valid_with_step( $value, $step ) {
136 // Count the number of decimals.
137 $decimals = max( FrmAppHelper::count_decimals( $value ), FrmAppHelper::count_decimals( $step ) );
138
139 // Convert value and step to int to prevent precision problem.
140 $pow = pow( 10, $decimals );
141 $value = intval( $pow * $value );
142 $step = intval( $pow * $step );
143 $div = $value / $step;
144 if ( is_int( $div ) ) {
145 return 0;
146 }
147
148 $div = floor( $div );
149 return array( $div * $step / $pow, ( $div + 1 ) * $step / $pow );
150 }
151
152 /**
153 * IE fallback for number fields
154 * Remove the comma when HTML5 isn't supported
155 *
156 * @since 3.0
157 *
158 * @param array $args
159 *
160 * @return void
161 */
162 private function remove_commas_from_number( &$args ) {
163 if ( strpos( $args['value'], ',' ) ) {
164 $args['value'] = str_replace( ',', '', $args['value'] );
165 FrmEntriesHelper::set_posted_value( $this->field, $args['value'], $args );
166 }
167 }
168
169 /**
170 * Force the value to be numeric before it's saved in the DB
171 */
172 public function set_value_before_save( $value ) {
173 if ( ! is_numeric( $value ) ) {
174 $value = (float) $value;
175 }
176
177 return $value;
178 }
179
180 /**
181 * @since 4.0.04
182 *
183 * @return void
184 */
185 public function sanitize_value( &$value ) {
186 FrmAppHelper::sanitize_value( 'sanitize_text_field', $value );
187 }
188 }
189