PluginProbe ʕ •ᴥ•ʔ
WPForms – Easy Form Builder for WordPress – Contact Forms, Payment Forms, Surveys, & More / 1.9.9.4
WPForms – Easy Form Builder for WordPress – Contact Forms, Payment Forms, Surveys, & More v1.9.9.4
1.10.1.1 1.10.1 1.10.0.5 trunk 1.1.4 1.1.4.2 1.1.5 1.1.5.1 1.1.6 1.1.6.1 1.1.7 1.1.7.1 1.1.7.2 1.1.8 1.1.8.1 1.1.8.2 1.1.8.3 1.1.8.4 1.10.0.1 1.10.0.2 1.10.0.3 1.10.0.4 1.2.0 1.2.0.1 1.2.1 1.2.2 1.2.2.1 1.2.2.2 1.2.3 1.2.3.1 1.2.3.2 1.2.4 1.2.4.1 1.2.5 1.2.5.1 1.2.6 1.2.7 1.2.8 1.2.8.1 1.2.9 1.3.0 1.3.1 1.3.1.1 1.3.1.2 1.3.2 1.3.3 1.3.5 1.3.6 1.3.6.1 1.3.6.2 1.3.7.2 1.3.7.3 1.3.7.4 1.3.8 1.3.9.1 1.4.0.1 1.4.1.1 1.4.2 1.4.2.1 1.4.2.2 1.4.3 1.4.4 1.4.4.1 1.4.5 1.4.5.1 1.4.5.2 1.4.5.3 1.4.6 1.4.7.1 1.4.7.2 1.4.8.1 1.4.9 1.5.0.1 1.5.0.3 1.5.0.4 1.5.1 1.5.1.1 1.5.1.3 1.5.2.1 1.5.2.2 1.5.2.3 1.5.3 1.5.3.1 1.5.4.1 1.5.4.2 1.5.5 1.5.5.1 1.5.6 1.5.6.2 1.5.7 1.5.8.2 1.5.9.1 1.5.9.4 1.5.9.5 1.6.0.1 1.6.0.2 1.6.1 1.6.2.2 1.6.2.3 1.6.3.1 1.6.4 1.6.4.1 1.6.5 1.6.6 1.6.7 1.6.7.1 1.6.7.2 1.6.7.3 1.6.8 1.6.8.1 1.6.9 1.7.0 1.7.1.1 1.7.1.2 1.7.2 1.7.2.1 1.7.3 1.7.4 1.7.4.1 1.7.4.2 1.7.5.1 1.7.5.2 1.7.5.3 1.7.5.5 1.7.6 1.7.7 1.7.7.1 1.7.7.2 1.7.8 1.7.9 1.7.9.1 1.8.0.1 1.8.0.2 1.8.1.1 1.8.1.2 1.8.1.3 1.8.2.1 1.8.2.2 1.8.2.3 1.8.3 1.8.3.1 1.8.4 1.8.4.1 1.8.5.2 1.8.5.3 1.8.5.4 1.8.6.2 1.8.6.3 1.8.6.4 1.8.7.2 1.8.8.2 1.8.8.3 1.8.9.1 1.8.9.2 1.8.9.4 1.8.9.5 1.8.9.6 1.9.0.1 1.9.0.2 1.9.0.3 1.9.0.4 1.9.1.1 1.9.1.2 1.9.1.3 1.9.1.4 1.9.1.5 1.9.1.6 1.9.2.1 1.9.2.2 1.9.2.3 1.9.3.1 1.9.3.2 1.9.4.1 1.9.4.2 1.9.5 1.9.5.1 1.9.5.2 1.9.6 1.9.6.1 1.9.6.2 1.9.7.1 1.9.7.2 1.9.7.3 1.9.8.1 1.9.8.2 1.9.8.4 1.9.8.7 1.9.9.2 1.9.9.3 1.9.9.4
wpforms-lite / includes / fields / class-number-slider.php
wpforms-lite / includes / fields Last commit date
class-base.php 4 months ago class-checkbox.php 6 months ago class-email.php 8 months ago class-gdpr-checkbox.php 8 months ago class-internal-information.php 10 months ago class-name.php 10 months ago class-number-slider.php 1 year ago class-number.php 1 year ago class-radio.php 6 months ago class-select.php 5 months ago class-text.php 1 year ago class-textarea.php 1 year ago
class-number-slider.php
453 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 use WPForms\Forms\Fields\Traits\NumberField as NumberFieldTrait;
8
9 /**
10 * Number Slider field.
11 *
12 * @since 1.5.7
13 */
14 class WPForms_Field_Number_Slider extends WPForms_Field {
15
16 use NumberFieldTrait;
17
18 /**
19 * Default minimum value of the field.
20 *
21 * @since 1.5.7
22 */
23 const SLIDER_MIN = 0;
24
25 /**
26 * Default maximum value of the field.
27 *
28 * @since 1.5.7
29 */
30 const SLIDER_MAX = 10;
31
32 /**
33 * Default step value of the field.
34 *
35 * @since 1.5.7
36 */
37 const SLIDER_STEP = 1;
38
39 /**
40 * Primary class constructor.
41 *
42 * @since 1.5.7
43 */
44 public function init() {
45
46 // Define field type information.
47 $this->name = esc_html__( 'Number Slider', 'wpforms-lite' );
48 $this->type = 'number-slider';
49 $this->icon = 'fa-sliders';
50 $this->order = 180;
51
52 // Customize value format for HTML emails.
53 add_filter( 'wpforms_html_field_value', [ $this, 'html_email_value' ], 10, 4 );
54
55 // Builder strings.
56 add_filter( 'wpforms_builder_strings', [ $this, 'add_builder_strings' ] );
57 }
58
59 /**
60 * Add Builder strings.
61 *
62 * @since 1.6.2.3
63 *
64 * @param array $strings Form Builder strings.
65 *
66 * @return array Form Builder strings.
67 */
68 public function add_builder_strings( $strings ) {
69
70 $strings['error_number_slider_increment'] = esc_html__( 'Increment value should be greater than zero. Decimal fractions allowed.', 'wpforms-lite' );
71
72 return $strings;
73 }
74
75 /**
76 * Customize format for HTML email notifications.
77 *
78 * @since 1.5.7
79 *
80 * @param string $val Field value.
81 * @param array $field Field settings.
82 * @param array $form_data Form data and settings.
83 * @param string $context Value display context.
84 *
85 * @return string
86 */
87 public function html_email_value( $val, $field, $form_data = [], $context = '' ) {
88
89 if ( empty( $field['value_raw'] ) || $field['type'] !== $this->type ) {
90 return $val;
91 }
92
93 $value = isset( $field['value_raw']['value'] ) ? (float) $field['value_raw']['value'] : 0;
94 $min = isset( $field['value_raw']['min'] ) ? (float) $field['value_raw']['min'] : self::SLIDER_MIN;
95 $max = isset( $field['value_raw']['max'] ) ? (float) $field['value_raw']['max'] : self::SLIDER_MAX;
96
97 $html_value = $value;
98 if ( strpos( $field['value_raw']['value_display'], '{value}' ) !== false ) {
99 $html_value = str_replace(
100 '{value}',
101 /* translators: %1$s - Number slider selected value, %2$s - its minimum value, %3$s - its maximum value. */
102 sprintf( esc_html__( '%1$s (%2$s min / %3$s max)', 'wpforms-lite' ), $value, $min, $max ),
103 $field['value_raw']['value_display']
104 );
105 }
106
107 return $html_value;
108 }
109
110 /**
111 * Field options panel inside the builder.
112 *
113 * @since 1.5.7
114 *
115 * @param array $field Field settings.
116 */
117 public function field_options( $field ) {
118 /*
119 * Basic field options.
120 */
121
122 // Set default values for Min, Max, Step, Default Value Options.
123 $field = $this->set_default_field_args( $field );
124
125 // Options open markup.
126 $args = [
127 'markup' => 'open',
128 ];
129
130 $this->field_option( 'basic-options', $field, $args );
131
132 // Label.
133 $this->field_option( 'label', $field );
134
135 // Description.
136 $this->field_option( 'description', $field );
137
138 // Required toggle disabled.
139 $this->field_element(
140 'text',
141 $field,
142 [
143 'slug' => 'required',
144 'value' => '',
145 'type' => 'hidden',
146 ]
147 );
148
149 // Min/Max.
150 $min_max_args = [
151 'class' => 'wpforms-number-slider',
152 'label' => esc_html__( 'Value Range', 'wpforms-lite' ),
153 'tooltip' => esc_html__( 'Define the minimum and the maximum values for the slider.', 'wpforms-lite' ),
154 ];
155 $min_max = $this->field_number_option_min_max( $field, $min_max_args, false );
156
157 // Default value.
158 $default_value_args = [
159 'class' => 'wpforms-number-slider-default-value',
160 ];
161 $default_value = $this->field_number_option_default_value( $field, $default_value_args, false );
162
163 // Increment.
164 $step_args = [
165 'class' => 'wpforms-number-slider-step',
166 'tooltip' => esc_html__( 'Determines the increment between selectable values on the slider.', 'wpforms-lite' ),
167 ];
168 $step = $this->field_number_option_step( $field, $step_args, false );
169
170 // Print of options markup: Minimum, Maximum, Increment, Default Value.
171 $this->field_element(
172 'row',
173 $field,
174 [
175 'slug' => 'number_min_max_step_dependent',
176 'content' => $min_max . $default_value . $step,
177 'class' => 'wpforms-field-number-slider-option',
178 ],
179 true
180 );
181
182 // Options close markup.
183 $args = [
184 'markup' => 'close',
185 ];
186
187 $this->field_option( 'basic-options', $field, $args );
188
189 /*
190 * Advanced field options.
191 */
192
193 // Options open markup.
194 $args = [
195 'markup' => 'open',
196 ];
197
198 $this->field_option( 'advanced-options', $field, $args );
199
200 // Size.
201 $this->field_option( 'size', $field );
202
203 // Value display.
204 $lbl = $this->field_element(
205 'label',
206 $field,
207 [
208 'slug' => 'value_display',
209 'value' => esc_html__( 'Value Display', 'wpforms-lite' ),
210 'tooltip' => esc_html__( 'Displays the currently selected value below the slider.', 'wpforms-lite' ),
211 ],
212 false
213 );
214
215 $fld = $this->field_element(
216 'text',
217 $field,
218 [
219 'slug' => 'value_display',
220 'class' => 'wpforms-number-slider-value-display',
221 'value' => isset( $field['value_display'] ) ? $field['value_display'] : $this->get_default_display_value(),
222 ],
223 false
224 );
225
226 $this->field_element(
227 'row',
228 $field,
229 [
230 'slug' => 'value_display',
231 'content' => $lbl . $fld,
232 ]
233 );
234
235 // Custom CSS classes.
236 $this->field_option( 'css', $field );
237
238 // Hide label.
239 $this->field_option( 'label_hide', $field );
240
241 // Options close markup.
242 $args = [
243 'markup' => 'close',
244 ];
245
246 $this->field_option( 'advanced-options', $field, $args );
247 }
248
249 /**
250 * Get default display value.
251 *
252 * @since 1.7.1
253 *
254 * @return string
255 */
256 private function get_default_display_value() {
257
258 return sprintf( /* translators: %s - value. */
259 esc_html__( 'Selected Value: %s', 'wpforms-lite' ),
260 '{value}'
261 );
262 }
263
264 /**
265 * Field preview inside the builder.
266 *
267 * @since 1.5.7
268 *
269 * @param array $field Field data.
270 */
271 public function field_preview( $field ) {
272
273 // Label.
274 $this->field_preview_option( 'label', $field );
275
276 $value_display = isset( $field['value_display'] ) ? esc_attr( $field['value_display'] ) : $this->get_default_display_value();
277 $default_value = ! empty( $field['default_value'] ) ? (float) $field['default_value'] : 0;
278
279 echo wpforms_render( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
280 'fields/number-slider/builder-preview',
281 [
282 'min' => isset( $field['min'] ) && is_numeric( $field['min'] ) ? (float) $field['min'] : self::SLIDER_MIN,
283 'max' => isset( $field['max'] ) && is_numeric( $field['max'] ) ? (float) $field['max'] : self::SLIDER_MAX,
284 'step' => isset( $field['step'] ) && is_numeric( $field['step'] ) ? (float) $field['step'] : self::SLIDER_STEP,
285 'value_display' => $value_display,
286 'default_value' => $default_value,
287 'value_hint' => str_replace( '{value}', '<b>' . $default_value . '</b>', wp_kses( $value_display, wpforms_builder_preview_get_allowed_tags() ) ),
288 'field_id' => $field['id'],
289 ],
290 true
291 );
292
293 // Description.
294 $this->field_preview_option( 'description', $field );
295 }
296
297 /**
298 * Field display on the form front-end.
299 *
300 * @since 1.5.7
301 *
302 * @param array $field Field data and settings.
303 * @param array $deprecated Deprecated field attributes. Use $field['properties'] instead.
304 * @param array $form_data Form data and settings.
305 */
306 public function field_display( $field, $deprecated, $form_data ) {
307
308 // Define data.
309 $primary = $field['properties']['inputs']['primary'];
310
311 $value_display = isset( $field['value_display'] ) ? esc_attr( $field['value_display'] ) : esc_html__( 'Selected Value: {value}', 'wpforms-lite' );
312 $hint_value = ! empty( $primary['attr']['value'] ) ? (float) $primary['attr']['value'] : 0;
313
314 $hint = str_replace( '{value}', '<b>' . $hint_value . '</b>', $value_display );
315
316 // phpcs:ignore
317 echo wpforms_render(
318 'fields/number-slider/frontend',
319 [
320 'atts' => $primary['attr'],
321 'class' => $primary['class'],
322 'datas' => $primary['data'],
323 'id' => $primary['id'],
324 'max' => isset( $field['max'] ) && is_numeric( $field['max'] ) ? (float) $field['max'] : self::SLIDER_MAX,
325 'min' => isset( $field['min'] ) && is_numeric( $field['min'] ) ? (float) $field['min'] : self::SLIDER_MIN,
326 'required' => $primary['required'],
327 'step' => isset( $field['step'] ) && is_numeric( $field['step'] ) ? (float) $field['step'] : self::SLIDER_STEP,
328 'value_display' => $value_display,
329 'value_hint' => $hint,
330 ],
331 true
332 );
333 }
334
335 /**
336 * Validate field on form submit.
337 *
338 * @since 1.5.7
339 *
340 * @param int $field_id Field ID.
341 * @param int|float|string $field_submit Submitted field value (raw data).
342 * @param array $form_data Form data and settings.
343 */
344 public function validate( $field_id, $field_submit, $form_data ) {
345
346 $form_id = $form_data['id'];
347
348 $field_submit = (float) $this->sanitize_value( $field_submit );
349
350 // Basic required check - if field is marked as required, check for entry data.
351 if (
352 ! empty( $form_data['fields'][ $field_id ]['required'] ) &&
353 empty( $field_submit ) &&
354 (string) $field_submit !== '0'
355 ) {
356 wpforms()->obj( 'process' )->errors[ $form_id ][ $field_id ] = wpforms_get_required_label();
357 }
358
359 // Check if value is numeric.
360 if ( ! empty( $field_submit ) && ! is_numeric( $field_submit ) ) {
361 /**
362 * Filter the error message for the number field.
363 *
364 * @since 1.0.0
365 *
366 * @param string $message Error message.
367 */
368 wpforms()->obj( 'process' )->errors[ $form_id ][ $field_id ] = apply_filters( 'wpforms_valid_number_label', esc_html__( 'Please provide a valid value.', 'wpforms-lite' ) ); // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
369 }
370 }
371
372 /**
373 * Format and sanitize field.
374 *
375 * @since 1.5.7
376 *
377 * @param int $field_id Field ID.
378 * @param int|string|float $field_submit Submitted field value.
379 * @param array $form_data Form data and settings.
380 */
381 public function format( $field_id, $field_submit, $form_data ) {
382
383 // Define data.
384 $name = ! empty( $form_data['fields'][ $field_id ]['label'] ) ? $form_data['fields'][ $field_id ]['label'] : '';
385 $value = (float) $this->sanitize_value( $field_submit );
386
387 $value_raw = [
388 'value' => $value,
389 'min' => (float) $form_data['fields'][ $field_id ]['min'],
390 'max' => (float) $form_data['fields'][ $field_id ]['max'],
391 'value_display' => wp_kses_post( $form_data['fields'][ $field_id ]['value_display'] ),
392 ];
393
394 // Set final field details.
395 wpforms()->obj( 'process' )->fields[ $field_id ] = [
396 'name' => sanitize_text_field( $name ),
397 'value' => $value,
398 'value_raw' => $value_raw,
399 'id' => wpforms_validate_field_id( $field_id ),
400 'type' => $this->type,
401 ];
402 }
403
404 /**
405 * Sanitize the value.
406 *
407 * @since 1.5.7
408 *
409 * @param string $value The number field submitted value.
410 *
411 * @return float|int|string
412 */
413 private function sanitize_value( $value ) {
414
415 // Some browsers allow other non-digit/decimal characters to be submitted
416 // with the num input, which then trips the is_numeric validation below.
417 // To get around this we remove all chars that are not expected.
418 $signed_value = preg_replace( '/[^-0-9.]/', '', $value );
419
420 // If there's no number on the signed value we return zero.
421 // We have to do that because since PHP 8.0, the abs() function is allowed an argument with int|float type.
422 if ( ! is_numeric( $signed_value ) ) {
423 return 0;
424 }
425
426 $abs_value = abs( $signed_value );
427 $value = strpos( $signed_value, '-' ) === 0 ? '-' . $abs_value : $abs_value;
428
429 return $value;
430 }
431
432 /**
433 * Sets default field settings.
434 *
435 * @since 1.9.4
436 *
437 * @param array $field Field settings.
438 *
439 * @return array Modified array.
440 */
441 private function set_default_field_args( $field ) {
442
443 $field['min'] = empty( $field['min'] ) ? self::SLIDER_MIN : $field['min'];
444 $field['max'] = empty( $field['max'] ) ? self::SLIDER_MAX : $field['max'];
445 $field['step'] = empty( $field['step'] ) ? self::SLIDER_STEP : $field['step'];
446 $field['default_value'] = empty( $field['default_value'] ) ? self::SLIDER_MIN : $field['default_value'];
447
448 return $field;
449 }
450 }
451
452 new WPForms_Field_Number_Slider();
453