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