class-evf-field-address.php
7 years ago
class-evf-field-ai.php
2 years ago
class-evf-field-captcha.php
2 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
2 years ago
class-evf-field-date-time.php
2 years ago
class-evf-field-divider.php
4 years ago
class-evf-field-email.php
2 years ago
class-evf-field-file-upload.php
6 years ago
class-evf-field-first-name.php
2 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
2 years ago
class-evf-field-likert.php
2 years ago
class-evf-field-lookup.php
2 years ago
class-evf-field-number.php
2 years ago
class-evf-field-password.php
7 years ago
class-evf-field-payment-authorize-net.php
2 years ago
class-evf-field-payment-checkbox.php
7 years ago
class-evf-field-payment-coupon.php
2 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
2 years ago
class-evf-field-reset.php
3 years ago
class-evf-field-scale-rating.php
2 years ago
class-evf-field-select.php
3 years ago
class-evf-field-signature.php
7 years ago
class-evf-field-text.php
2 years ago
class-evf-field-textarea.php
3 years ago
class-evf-field-title.php
6 years ago
class-evf-field-url.php
2 years ago
class-evf-field-wysiwyg.php
4 years ago
class-evf-field-yes-no.php
2 years ago
class-evf-field-number.php
330 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 | 'regex_validation', |
| 46 | 'regex_value', |
| 47 | 'regex_message', |
| 48 | ), |
| 49 | ), |
| 50 | ); |
| 51 | |
| 52 | parent::__construct(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Hook in tabs. |
| 57 | */ |
| 58 | public function init_hooks() { |
| 59 | add_filter( 'everest_forms_field_properties_' . $this->type, array( $this, 'field_properties' ), 5, 3 ); |
| 60 | add_filter( 'everest_forms_field_exporter_' . $this->type, array( $this, 'field_exporter' ) ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Step field option. |
| 65 | * |
| 66 | * @since 1.4.9 |
| 67 | * @param array $field Field Data. |
| 68 | */ |
| 69 | public function step( $field ) { |
| 70 | $label = $this->field_element( |
| 71 | 'label', |
| 72 | $field, |
| 73 | array( |
| 74 | 'slug' => 'step', |
| 75 | 'value' => esc_html__( 'Step', 'everest-forms' ), |
| 76 | 'tooltip' => esc_html__( 'Allows users to enter specific legal number intervals.', 'everest-forms' ), |
| 77 | ), |
| 78 | false |
| 79 | ); |
| 80 | $input_field = $this->field_element( |
| 81 | 'text', |
| 82 | $field, |
| 83 | array( |
| 84 | 'type' => 'number', |
| 85 | 'slug' => 'step', |
| 86 | 'class' => 'evf-input-number-step', |
| 87 | 'value' => isset( $field['step'] ) ? $field['step'] : 0, |
| 88 | ), |
| 89 | false |
| 90 | ); |
| 91 | $this->field_element( |
| 92 | 'row', |
| 93 | $field, |
| 94 | array( |
| 95 | 'slug' => 'step', |
| 96 | 'content' => $label . $input_field, |
| 97 | ) |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Minimum value field option. |
| 103 | * |
| 104 | * @since 1.4.9 |
| 105 | * @param array $field Field Data. |
| 106 | */ |
| 107 | public function min_value( $field ) { |
| 108 | $label = $this->field_element( |
| 109 | 'label', |
| 110 | $field, |
| 111 | array( |
| 112 | 'slug' => 'min_value', |
| 113 | 'value' => esc_html__( 'Min Value', 'everest-forms' ), |
| 114 | 'tooltip' => esc_html__( 'Minimum value user is allowed to enter.', 'everest-forms' ), |
| 115 | ), |
| 116 | false |
| 117 | ); |
| 118 | $input_field = $this->field_element( |
| 119 | 'text', |
| 120 | $field, |
| 121 | array( |
| 122 | 'type' => 'number', |
| 123 | 'slug' => 'min_value', |
| 124 | 'class' => 'evf-input-number', |
| 125 | 'value' => isset( $field['min_value'] ) ? $field['min_value'] : '', |
| 126 | ), |
| 127 | false |
| 128 | ); |
| 129 | $this->field_element( |
| 130 | 'row', |
| 131 | $field, |
| 132 | array( |
| 133 | 'slug' => 'min_value', |
| 134 | 'content' => $label . $input_field, |
| 135 | ) |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Maximum value field option. |
| 141 | * |
| 142 | * @since 1.4.9 |
| 143 | * @param array $field Field Data. |
| 144 | */ |
| 145 | public function max_value( $field ) { |
| 146 | $label = $this->field_element( |
| 147 | 'label', |
| 148 | $field, |
| 149 | array( |
| 150 | 'slug' => 'max_value', |
| 151 | 'value' => esc_html__( 'Max Value', 'everest-forms' ), |
| 152 | 'tooltip' => esc_html__( 'Maximum value user is allowed to enter.', 'everest-forms' ), |
| 153 | ), |
| 154 | false |
| 155 | ); |
| 156 | $input_field = $this->field_element( |
| 157 | 'text', |
| 158 | $field, |
| 159 | array( |
| 160 | 'type' => 'number', |
| 161 | 'slug' => 'max_value', |
| 162 | 'class' => 'evf-input-number', |
| 163 | 'value' => isset( $field['max_value'] ) ? $field['max_value'] : '', |
| 164 | ), |
| 165 | false |
| 166 | ); |
| 167 | $this->field_element( |
| 168 | 'row', |
| 169 | $field, |
| 170 | array( |
| 171 | 'slug' => 'max_value', |
| 172 | 'content' => $label . $input_field, |
| 173 | ) |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Define additional field properties. |
| 179 | * |
| 180 | * @since 1.0.0 |
| 181 | * |
| 182 | * @param array $properties Field properties. |
| 183 | * @param array $field Field settings. |
| 184 | * @param array $form_data Form data and settings. |
| 185 | * |
| 186 | * @return array of additional field properties. |
| 187 | */ |
| 188 | public function field_properties( $properties, $field, $form_data ) { |
| 189 | // Input primary: step interval. |
| 190 | if ( ! empty( $field['step'] ) ) { |
| 191 | $properties['inputs']['primary']['attr']['step'] = (float) $field['step']; |
| 192 | } |
| 193 | |
| 194 | // Input primary: minimum value. |
| 195 | if ( '' !== $field['min_value'] ) { |
| 196 | $properties['inputs']['primary']['attr']['min'] = ( '0' === $field['min_value'] ) ? '0' : (float) $field['min_value']; |
| 197 | } |
| 198 | |
| 199 | // Input primary: maximum value. |
| 200 | if ( ! empty( $field['max_value'] ) ) { |
| 201 | $properties['inputs']['primary']['attr']['max'] = (float) $field['max_value']; |
| 202 | } |
| 203 | |
| 204 | return $properties; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Filter callback for outputting formatted data. |
| 209 | * |
| 210 | * @param array $field Field Data. |
| 211 | * @return array Data for field exporter PDF or Email. |
| 212 | */ |
| 213 | public function field_exporter( $field ) { |
| 214 | return array( |
| 215 | 'label' => ! empty( $field['name'] ) ? $field['name'] : ucfirst( str_replace( '_', ' ', $field['type'] ) ) . " - {$field['id']}", |
| 216 | 'value' => ! empty( $field['value'] ) || is_numeric( $field['value'] ) ? sanitize_text_field( $field['value'] ) : false, |
| 217 | ); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Field preview inside the builder. |
| 222 | * |
| 223 | * @since 1.0.0 |
| 224 | * |
| 225 | * @param array $field Field data and settings. |
| 226 | */ |
| 227 | public function field_preview( $field ) { |
| 228 | |
| 229 | // Define data. |
| 230 | $placeholder = ! empty( $field['placeholder'] ) ? esc_attr( $field['placeholder'] ) : ''; |
| 231 | |
| 232 | // Label. |
| 233 | $this->field_preview_option( 'label', $field ); |
| 234 | |
| 235 | // Primary input. |
| 236 | echo '<input type="number" placeholder="' . esc_attr($placeholder) . '" class="widefat" disabled>'; // @codingStandardsIgnoreLine. |
| 237 | |
| 238 | // Description. |
| 239 | $this->field_preview_option( 'description', $field ); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Field display on the form front-end. |
| 244 | * |
| 245 | * @since 1.0.0 |
| 246 | * |
| 247 | * @param array $field Field Data. |
| 248 | * @param array $field_atts Field attributes. |
| 249 | * @param array $form_data All Form Data. |
| 250 | */ |
| 251 | public function field_display( $field, $field_atts, $form_data ) { |
| 252 | // Define data. |
| 253 | $primary = $field['properties']['inputs']['primary']; |
| 254 | |
| 255 | // Primary field. |
| 256 | printf( |
| 257 | '<input type="number" %s %s/>', |
| 258 | evf_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), |
| 259 | esc_attr( $primary['required'] ) |
| 260 | ); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Formats and sanitizes field. |
| 265 | * |
| 266 | * @param string $field_id Field Id. |
| 267 | * @param array $field_submit Submitted Field. |
| 268 | * @param array $form_data All Form Data. |
| 269 | * @param string $meta_key Field Meta Key. |
| 270 | */ |
| 271 | public function format( $field_id, $field_submit, $form_data, $meta_key ) { |
| 272 | // Define data. |
| 273 | $name = ! empty( $form_data['form_fields'][ $field_id ]['label'] ) ? $form_data['form_fields'][ $field_id ]['label'] : ''; |
| 274 | $value = preg_replace( '/[^0-9.]/', '', $field_submit ); |
| 275 | |
| 276 | // Set final field details. |
| 277 | evf()->task->form_fields[ $field_id ] = array( |
| 278 | 'name' => make_clickable( $name ), |
| 279 | 'value' => sanitize_text_field( $value ), |
| 280 | 'id' => $field_id, |
| 281 | 'type' => $this->type, |
| 282 | 'meta_key' => $meta_key, |
| 283 | ); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Validates field for minimum and maximum data input. |
| 288 | * |
| 289 | * @since 1.4.9 |
| 290 | * @param string $field_id Field Id. |
| 291 | * @param array $field_submit Submitted Data. |
| 292 | * @param array $form_data All Form Data. |
| 293 | */ |
| 294 | public function validate( $field_id, $field_submit, $form_data ) { |
| 295 | $form_id = absint( $form_data['id'] ); |
| 296 | $min_value = isset( $form_data['form_fields'][ $field_id ]['min_value'] ) ? floatval( $form_data['form_fields'][ $field_id ]['min_value'] ) : 0; |
| 297 | $max_value = isset( $form_data['form_fields'][ $field_id ]['max_value'] ) ? floatval( $form_data['form_fields'][ $field_id ]['max_value'] ) : 0; |
| 298 | $entry = $form_data['entry']; |
| 299 | $visible = apply_filters( 'everest_forms_visible_fields', true, $form_data['form_fields'][ $field_id ], $entry, $form_data ); |
| 300 | $field_type = isset( $form_data['form_fields'][ $field_id ]['type'] ) ? $form_data['form_fields'][ $field_id ]['type'] : ''; |
| 301 | $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' ); |
| 302 | if ( false === $visible ) { |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | // Basic required check - If field is marked as required, check for entry data. |
| 307 | if ( ! empty( $form_data['form_fields'][ $field_id ]['required'] ) && empty( $field_submit ) && '0' !== $field_submit ) { |
| 308 | evf()->task->errors[ $form_id ][ $field_id ] = $required_message; |
| 309 | update_option( 'evf_validation_error', 'yes' ); |
| 310 | } |
| 311 | |
| 312 | // Check if value is numeric. |
| 313 | if ( ! empty( $field_submit ) && ! is_numeric( $field_submit ) ) { |
| 314 | evf()->task->errors[ $form_id ][ $field_id ] = apply_filters( 'everest_forms_valid_number_label', esc_html__( 'Please enter a valid number.', 'everest-forms' ) ); |
| 315 | update_option( 'evf_validation_error', 'yes' ); |
| 316 | } |
| 317 | |
| 318 | // Check if minimum and maximum value is valid. |
| 319 | if ( ! empty( $form_data['form_fields'][ $field_id ]['min_value'] ) && ! empty( $field_submit ) && floatval( $field_submit ) < $min_value ) { |
| 320 | /* translators: %s - minimum value. */ |
| 321 | 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 ) ); |
| 322 | update_option( 'evf_validation_error', 'yes' ); |
| 323 | } elseif ( ! empty( $form_data['form_fields'][ $field_id ]['max_value'] ) && ! empty( $field_submit ) && floatval( $field_submit ) > $max_value ) { |
| 324 | /* translators: %s - maximum value. */ |
| 325 | 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 ) ); |
| 326 | update_option( 'evf_validation_error', 'yes' ); |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 |