PluginProbe ʕ •ᴥ•ʔ
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI / 1.9.9
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI v1.9.9
3.5.2 3.5.1 3.5.0 3.4.8 3.4.7 3.4.6 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5.1 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.10 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.6.1 1.6.7 1.7.0 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.2 1.7.2.1 1.7.2.2 1.7.3 1.7.4 1.7.5 1.7.5.1 1.7.5.2 1.7.6 1.7.7 1.7.7.1 1.7.7.2 1.7.8 1.7.9 1.8.0 1.8.0.1 1.8.1 1.8.2 1.8.2.1 1.8.2.2 1.8.2.3 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 1.9.0.1 1.9.1 1.9.2 1.9.3 1.9.4 1.9.4.1 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.0.1 2.0.1 2.0.2 2.0.3 2.0.3.1 2.0.4 2.0.4.1 2.0.5 2.0.6 2.0.7 2.0.8 2.0.8.1 2.0.9 3.0.0 3.0.0.1 3.0.1 3.0.2 3.0.3 3.0.3.1 3.0.4 3.0.4.1 3.0.4.2 3.0.5 3.0.5.1 3.0.5.2 3.0.6 3.0.6.1 3.0.7.1 3.0.8 3.0.8.1 3.0.9 3.0.9.1 3.0.9.2 3.0.9.3 3.0.9.4 3.0.9.5 3.1.0 3.1.1 3.1.2 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.3.0 3.4.0 3.4.1 3.4.2 3.4.2.1 3.4.3 3.4.4 3.4.5 trunk 1.0 1.0.1 1.0.2 1.0.3
everest-forms / includes / fields / class-evf-field-number.php
everest-forms / includes / fields Last commit date
class-evf-field-address.php 7 years ago class-evf-field-captcha.php 6 years ago class-evf-field-checkbox.php 3 years ago class-evf-field-color.php 3 years ago class-evf-field-country.php 7 years ago class-evf-field-credit-card.php 7 years ago class-evf-field-date-time.php 3 years ago class-evf-field-divider.php 4 years ago class-evf-field-email.php 3 years ago class-evf-field-file-upload.php 6 years ago class-evf-field-first-name.php 3 years ago class-evf-field-hidden.php 7 years ago class-evf-field-html.php 7 years ago class-evf-field-image-upload.php 7 years ago class-evf-field-last-name.php 3 years ago class-evf-field-likert.php 7 years ago class-evf-field-number.php 3 years ago class-evf-field-password.php 7 years ago class-evf-field-payment-checkbox.php 7 years ago class-evf-field-payment-coupon.php 4 years ago class-evf-field-payment-quantity.php 5 years ago class-evf-field-payment-radio.php 7 years ago class-evf-field-payment-single.php 7 years ago class-evf-field-payment-subtotal.php 3 years ago class-evf-field-payment-total.php 6 years ago class-evf-field-phone.php 7 years ago class-evf-field-privacy-policy.php 4 years ago class-evf-field-progress.php 3 years ago class-evf-field-radio.php 3 years ago class-evf-field-range-slider.php 6 years ago class-evf-field-rating.php 7 years ago class-evf-field-repeater.php 4 years ago class-evf-field-reset.php 3 years ago class-evf-field-scale-rating.php 7 years ago class-evf-field-select.php 3 years ago class-evf-field-signature.php 7 years ago class-evf-field-text.php 3 years ago class-evf-field-textarea.php 3 years ago class-evf-field-title.php 6 years ago class-evf-field-url.php 3 years ago class-evf-field-wysiwyg.php 4 years ago class-evf-field-yes-no.php 3 years ago
class-evf-field-number.php
327 lines
1 <?php
2 /**
3 * Number field.
4 *
5 * @package EverestForms\Fields
6 * @since 1.0.0
7 */
8
9 defined( 'ABSPATH' ) || exit;
10
11 /**
12 * EVF_Field_Number class.
13 */
14 class EVF_Field_Number extends EVF_Form_Fields {
15
16 /**
17 * Constructor.
18 */
19 public function __construct() {
20 $this->name = esc_html__( 'Number', 'everest-forms' );
21 $this->type = 'number';
22 $this->icon = 'evf-icon evf-icon-number';
23 $this->order = 80;
24 $this->group = 'general';
25 $this->settings = array(
26 'basic-options' => array(
27 'field_options' => array(
28 'label',
29 'meta',
30 'description',
31 'required',
32 'required_field_message_setting',
33 'required_field_message',
34 ),
35 ),
36 'advanced-options' => array(
37 'field_options' => array(
38 'step',
39 'min_value',
40 'max_value',
41 'default_value',
42 'placeholder',
43 'label_hide',
44 'css',
45 ),
46 ),
47 );
48
49 parent::__construct();
50 }
51
52 /**
53 * Hook in tabs.
54 */
55 public function init_hooks() {
56 add_filter( 'everest_forms_field_properties_' . $this->type, array( $this, 'field_properties' ), 5, 3 );
57 add_filter( 'everest_forms_field_exporter_' . $this->type, array( $this, 'field_exporter' ) );
58 }
59
60 /**
61 * Step field option.
62 *
63 * @since 1.4.9
64 * @param array $field Field Data.
65 */
66 public function step( $field ) {
67 $label = $this->field_element(
68 'label',
69 $field,
70 array(
71 'slug' => 'step',
72 'value' => esc_html__( 'Step', 'everest-forms' ),
73 'tooltip' => esc_html__( 'Allows users to enter specific legal number intervals.', 'everest-forms' ),
74 ),
75 false
76 );
77 $input_field = $this->field_element(
78 'text',
79 $field,
80 array(
81 'type' => 'number',
82 'slug' => 'step',
83 'class' => 'evf-input-number-step',
84 'value' => isset( $field['step'] ) ? $field['step'] : 0,
85 ),
86 false
87 );
88 $this->field_element(
89 'row',
90 $field,
91 array(
92 'slug' => 'step',
93 'content' => $label . $input_field,
94 )
95 );
96 }
97
98 /**
99 * Minimum value field option.
100 *
101 * @since 1.4.9
102 * @param array $field Field Data.
103 */
104 public function min_value( $field ) {
105 $label = $this->field_element(
106 'label',
107 $field,
108 array(
109 'slug' => 'min_value',
110 'value' => esc_html__( 'Min Value', 'everest-forms' ),
111 'tooltip' => esc_html__( 'Minimum value user is allowed to enter.', 'everest-forms' ),
112 ),
113 false
114 );
115 $input_field = $this->field_element(
116 'text',
117 $field,
118 array(
119 'type' => 'number',
120 'slug' => 'min_value',
121 'class' => 'evf-input-number',
122 'value' => isset( $field['min_value'] ) ? $field['min_value'] : '',
123 ),
124 false
125 );
126 $this->field_element(
127 'row',
128 $field,
129 array(
130 'slug' => 'min_value',
131 'content' => $label . $input_field,
132 )
133 );
134 }
135
136 /**
137 * Maximum value field option.
138 *
139 * @since 1.4.9
140 * @param array $field Field Data.
141 */
142 public function max_value( $field ) {
143 $label = $this->field_element(
144 'label',
145 $field,
146 array(
147 'slug' => 'max_value',
148 'value' => esc_html__( 'Max Value', 'everest-forms' ),
149 'tooltip' => esc_html__( 'Maximum value user is allowed to enter.', 'everest-forms' ),
150 ),
151 false
152 );
153 $input_field = $this->field_element(
154 'text',
155 $field,
156 array(
157 'type' => 'number',
158 'slug' => 'max_value',
159 'class' => 'evf-input-number',
160 'value' => isset( $field['max_value'] ) ? $field['max_value'] : '',
161 ),
162 false
163 );
164 $this->field_element(
165 'row',
166 $field,
167 array(
168 'slug' => 'max_value',
169 'content' => $label . $input_field,
170 )
171 );
172 }
173
174 /**
175 * Define additional field properties.
176 *
177 * @since 1.0.0
178 *
179 * @param array $properties Field properties.
180 * @param array $field Field settings.
181 * @param array $form_data Form data and settings.
182 *
183 * @return array of additional field properties.
184 */
185 public function field_properties( $properties, $field, $form_data ) {
186 // Input primary: step interval.
187 if ( ! empty( $field['step'] ) ) {
188 $properties['inputs']['primary']['attr']['step'] = (float) $field['step'];
189 }
190
191 // Input primary: minimum value.
192 if ( ! empty( $field['min_value'] ) ) {
193 $properties['inputs']['primary']['attr']['min'] = (float) $field['min_value'];
194 }
195
196 // Input primary: maximum value.
197 if ( ! empty( $field['max_value'] ) ) {
198 $properties['inputs']['primary']['attr']['max'] = (float) $field['max_value'];
199 }
200
201 return $properties;
202 }
203
204 /**
205 * Filter callback for outputting formatted data.
206 *
207 * @param array $field Field Data.
208 * @return array Data for field exporter PDF or Email.
209 */
210 public function field_exporter( $field ) {
211 return array(
212 'label' => ! empty( $field['name'] ) ? $field['name'] : ucfirst( str_replace( '_', ' ', $field['type'] ) ) . " - {$field['id']}",
213 'value' => ! empty( $field['value'] ) || is_numeric( $field['value'] ) ? sanitize_text_field( $field['value'] ) : false,
214 );
215 }
216
217 /**
218 * Field preview inside the builder.
219 *
220 * @since 1.0.0
221 *
222 * @param array $field Field data and settings.
223 */
224 public function field_preview( $field ) {
225
226 // Define data.
227 $placeholder = ! empty( $field['placeholder'] ) ? esc_attr( $field['placeholder'] ) : '';
228
229 // Label.
230 $this->field_preview_option( 'label', $field );
231
232 // Primary input.
233 echo '<input type="number" placeholder="' . esc_attr($placeholder) . '" class="widefat" disabled>'; // @codingStandardsIgnoreLine.
234
235 // Description.
236 $this->field_preview_option( 'description', $field );
237 }
238
239 /**
240 * Field display on the form front-end.
241 *
242 * @since 1.0.0
243 *
244 * @param array $field Field Data.
245 * @param array $field_atts Field attributes.
246 * @param array $form_data All Form Data.
247 */
248 public function field_display( $field, $field_atts, $form_data ) {
249 // Define data.
250 $primary = $field['properties']['inputs']['primary'];
251
252 // Primary field.
253 printf(
254 '<input type="number" %s %s/>',
255 evf_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ),
256 esc_attr( $primary['required'] )
257 );
258 }
259
260 /**
261 * Formats and sanitizes field.
262 *
263 * @param string $field_id Field Id.
264 * @param array $field_submit Submitted Field.
265 * @param array $form_data All Form Data.
266 * @param string $meta_key Field Meta Key.
267 */
268 public function format( $field_id, $field_submit, $form_data, $meta_key ) {
269 // Define data.
270 $name = ! empty( $form_data['form_fields'][ $field_id ]['label'] ) ? $form_data['form_fields'][ $field_id ]['label'] : '';
271 $value = preg_replace( '/[^0-9.]/', '', $field_submit );
272
273 // Set final field details.
274 evf()->task->form_fields[ $field_id ] = array(
275 'name' => make_clickable( $name ),
276 'value' => sanitize_text_field( $value ),
277 'id' => $field_id,
278 'type' => $this->type,
279 'meta_key' => $meta_key,
280 );
281 }
282
283 /**
284 * Validates field for minimum and maximum data input.
285 *
286 * @since 1.4.9
287 * @param string $field_id Field Id.
288 * @param array $field_submit Submitted Data.
289 * @param array $form_data All Form Data.
290 */
291 public function validate( $field_id, $field_submit, $form_data ) {
292 $form_id = absint( $form_data['id'] );
293 $min_value = isset( $form_data['form_fields'][ $field_id ]['min_value'] ) ? floatval( $form_data['form_fields'][ $field_id ]['min_value'] ) : 0;
294 $max_value = isset( $form_data['form_fields'][ $field_id ]['max_value'] ) ? floatval( $form_data['form_fields'][ $field_id ]['max_value'] ) : 0;
295 $entry = $form_data['entry'];
296 $visible = apply_filters( 'everest_forms_visible_fields', true, $form_data['form_fields'][ $field_id ], $entry, $form_data );
297 $field_type = isset( $form_data['form_fields'][ $field_id ]['type'] ) ? $form_data['form_fields'][ $field_id ]['type'] : '';
298 $required_message = isset( $form_data['form_fields'][ $field_id ]['required-field-message'], $form_data['form_fields'][ $field_id ]['required_field_message_setting'] ) && ! empty( $form_data['form_fields'][ $field_id ]['required-field-message'] ) && 'individual' == $form_data['form_fields'][ $field_id ]['required_field_message_setting'] ? $form_data['form_fields'][ $field_id ]['required-field-message'] : get_option( 'everest_forms_' . $field_type . '_validation' );
299 if ( false === $visible ) {
300 return;
301 }
302
303 // Basic required check - If field is marked as required, check for entry data.
304 if ( ! empty( $form_data['form_fields'][ $field_id ]['required'] ) && empty( $field_submit ) && '0' !== $field_submit ) {
305 evf()->task->errors[ $form_id ][ $field_id ] = $required_message;
306 update_option( 'evf_validation_error', 'yes' );
307 }
308
309 // Check if value is numeric.
310 if ( ! empty( $field_submit ) && ! is_numeric( $field_submit ) ) {
311 evf()->task->errors[ $form_id ][ $field_id ] = apply_filters( 'everest_forms_valid_number_label', esc_html__( 'Please enter a valid number.', 'everest-forms' ) );
312 update_option( 'evf_validation_error', 'yes' );
313 }
314
315 // Check if minimum and maximum value is valid.
316 if ( ! empty( $form_data['form_fields'][ $field_id ]['min_value'] ) && ! empty( $field_submit ) && floatval( $field_submit ) < $min_value ) {
317 /* translators: %s - minimum value. */
318 evf()->task->errors[ $form_id ][ $field_id ] = sprintf( esc_html__( 'Please enter a value greater than or equal to %s', 'everest-forms' ), absint( $min_value ) );
319 update_option( 'evf_validation_error', 'yes' );
320 } elseif ( ! empty( $form_data['form_fields'][ $field_id ]['max_value'] ) && ! empty( $field_submit ) && floatval( $field_submit ) > $max_value ) {
321 /* translators: %s - maximum value. */
322 evf()->task->errors[ $form_id ][ $field_id ] = sprintf( esc_html__( 'Please enter a value less than or equal to %s', 'everest-forms' ), absint( $max_value ) );
323 update_option( 'evf_validation_error', 'yes' );
324 }
325 }
326 }
327