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-text.php
359 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Text field. |
| 4 | * |
| 5 | * @package EverestForms\Fields |
| 6 | * @since 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * EVF_Field_Text class. |
| 13 | */ |
| 14 | class EVF_Field_Text extends EVF_Form_Fields { |
| 15 | |
| 16 | /** |
| 17 | * Constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | $this->name = esc_html__( 'Single Line Text', 'everest-forms' ); |
| 21 | $this->type = 'text'; |
| 22 | $this->icon = 'evf-icon evf-icon-text'; |
| 23 | $this->order = 30; |
| 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 | 'placeholder', |
| 39 | 'label_hide', |
| 40 | 'limit_length', |
| 41 | 'min_length', |
| 42 | 'default_value', |
| 43 | 'css', |
| 44 | 'input_mask', |
| 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_action( 'everest_forms_shortcode_scripts', array( $this, 'load_assets' ) ); |
| 60 | add_filter( 'everest_forms_field_properties_' . $this->type, array( $this, 'field_properties' ), 5, 3 ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Limit length field option. |
| 65 | * |
| 66 | * @param array $field Field settings. |
| 67 | */ |
| 68 | public function limit_length( $field ) { |
| 69 | // Limit length. |
| 70 | $args = array( |
| 71 | 'slug' => 'limit_enabled', |
| 72 | 'content' => $this->field_element( |
| 73 | 'checkbox', |
| 74 | $field, |
| 75 | array( |
| 76 | 'slug' => 'limit_enabled', |
| 77 | 'value' => isset( $field['limit_enabled'] ), |
| 78 | 'desc' => esc_html__( 'Limit Length', 'everest-forms' ), |
| 79 | 'tooltip' => esc_html__( 'Check this option to specify maximum text length by characters or word count.', 'everest-forms' ), |
| 80 | ), |
| 81 | false |
| 82 | ), |
| 83 | ); |
| 84 | $this->field_element( 'row', $field, $args ); |
| 85 | |
| 86 | // Limit controls. |
| 87 | $count = $this->field_element( |
| 88 | 'text', |
| 89 | $field, |
| 90 | array( |
| 91 | 'type' => 'number', |
| 92 | 'class' => 'small-text', |
| 93 | 'slug' => 'limit_count', |
| 94 | 'attrs' => array( |
| 95 | 'min' => 1, |
| 96 | 'step' => 1, |
| 97 | 'pattern' => '[0-9]', |
| 98 | ), |
| 99 | 'value' => ! empty( $field['limit_count'] ) ? absint( $field['limit_count'] ) : 1, |
| 100 | ), |
| 101 | false |
| 102 | ); |
| 103 | |
| 104 | $mode = $this->field_element( |
| 105 | 'select', |
| 106 | $field, |
| 107 | array( |
| 108 | 'slug' => 'limit_mode', |
| 109 | 'class' => 'limit-select', |
| 110 | 'value' => ! empty( $field['limit_mode'] ) ? esc_attr( $field['limit_mode'] ) : 'characters', |
| 111 | 'options' => array( |
| 112 | 'characters' => esc_html__( 'Characters', 'everest-forms' ), |
| 113 | 'words' => esc_html__( 'Words Count', 'everest-forms' ), |
| 114 | ), |
| 115 | ), |
| 116 | false |
| 117 | ); |
| 118 | $args = array( |
| 119 | 'slug' => 'limit_controls', |
| 120 | 'class' => ! isset( $field['limit_enabled'] ) ? 'everest-forms-hidden' : '', |
| 121 | 'content' => $count . $mode, |
| 122 | ); |
| 123 | $this->field_element( 'row', $field, $args ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Minimum Length length field option. |
| 128 | * |
| 129 | * @param array $field Field settings. |
| 130 | */ |
| 131 | public function min_length( $field ) { |
| 132 | // Minimum length. |
| 133 | $args = array( |
| 134 | 'slug' => 'min_length_enabled', |
| 135 | 'content' => $this->field_element( |
| 136 | 'checkbox', |
| 137 | $field, |
| 138 | array( |
| 139 | 'slug' => 'min_length_enabled', |
| 140 | 'value' => isset( $field['min_length_enabled'] ), |
| 141 | 'desc' => esc_html__( 'Minimum Length', 'everest-forms' ), |
| 142 | 'tooltip' => esc_html__( 'Check this option to specify minimum text length by characters or word count.', 'everest-forms' ), |
| 143 | ), |
| 144 | false |
| 145 | ), |
| 146 | ); |
| 147 | $this->field_element( 'row', $field, $args ); |
| 148 | |
| 149 | // Minimum length controls. |
| 150 | $count = $this->field_element( |
| 151 | 'text', |
| 152 | $field, |
| 153 | array( |
| 154 | 'type' => 'number', |
| 155 | 'class' => 'small-text', |
| 156 | 'slug' => 'min_length_count', |
| 157 | 'attrs' => array( |
| 158 | 'min' => 1, |
| 159 | 'step' => 1, |
| 160 | 'pattern' => '[0-9]', |
| 161 | ), |
| 162 | 'value' => ! empty( $field['min_length_count'] ) ? absint( $field['min_length_count'] ) : 1, |
| 163 | ), |
| 164 | false |
| 165 | ); |
| 166 | |
| 167 | $mode = $this->field_element( |
| 168 | 'select', |
| 169 | $field, |
| 170 | array( |
| 171 | 'slug' => 'min_length_mode', |
| 172 | 'class' => 'min-length-select', |
| 173 | 'value' => ! empty( $field['min_length_mode'] ) ? esc_attr( $field['min_length_mode'] ) : 'characters', |
| 174 | 'options' => array( |
| 175 | 'characters' => esc_html__( 'Characters', 'everest-forms' ), |
| 176 | 'words' => esc_html__( 'Words Count', 'everest-forms' ), |
| 177 | ), |
| 178 | ), |
| 179 | false |
| 180 | ); |
| 181 | $args = array( |
| 182 | 'slug' => 'min_length_controls', |
| 183 | 'class' => ! isset( $field['min_length_enabled'] ) ? 'everest-forms-hidden' : '', |
| 184 | 'content' => $count . $mode, |
| 185 | ); |
| 186 | $this->field_element( 'row', $field, $args ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Input mask field option. |
| 191 | * |
| 192 | * @param array $field Field settings. |
| 193 | */ |
| 194 | public function input_mask( $field ) { |
| 195 | // Input Mask. |
| 196 | $lbl = $this->field_element( |
| 197 | 'label', |
| 198 | $field, |
| 199 | array( |
| 200 | 'slug' => 'input_mask', |
| 201 | 'value' => esc_html__( 'Input Mask', 'everest-forms' ), |
| 202 | 'tooltip' => esc_html__( 'Enter your custom input mask.', 'everest-forms' ), |
| 203 | 'after_tooltip' => '<a href="https://docs.everestforms.net/docs/how-to-use-custom-input-mask/" class="after-label-description" target="_blank" rel="noopener noreferrer">' . esc_html__( 'See Examples & Docs', 'everest-forms' ) . '</a>', |
| 204 | ), |
| 205 | false |
| 206 | ); |
| 207 | $fld = $this->field_element( |
| 208 | 'text', |
| 209 | $field, |
| 210 | array( |
| 211 | 'slug' => 'input_mask', |
| 212 | 'value' => ! empty( $field['input_mask'] ) ? esc_attr( $field['input_mask'] ) : '', |
| 213 | ), |
| 214 | false |
| 215 | ); |
| 216 | $this->field_element( |
| 217 | 'row', |
| 218 | $field, |
| 219 | array( |
| 220 | 'slug' => 'input_mask', |
| 221 | 'content' => $lbl . $fld, |
| 222 | ) |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Enqueue shortcode scripts. |
| 228 | * |
| 229 | * @param array $atts Shortcode Attributes. |
| 230 | */ |
| 231 | public function load_assets( $atts ) { |
| 232 | $form_id = isset( $atts['id'] ) ? wp_unslash( $atts['id'] ) : ''; // WPCS: CSRF ok, input var ok, sanitization ok. |
| 233 | $form_obj = evf()->form->get( $form_id ); |
| 234 | $form_data = ! empty( $form_obj->post_content ) ? evf_decode( $form_obj->post_content ) : ''; |
| 235 | |
| 236 | // Leave only fields with limit. |
| 237 | if ( ! empty( $form_data['form_fields'] ) ) { |
| 238 | $form_fields = array_filter( $form_data['form_fields'], array( $this, 'field_is_limit' ) ); |
| 239 | |
| 240 | if ( count( $form_fields ) ) { |
| 241 | wp_enqueue_script( 'everest-forms-text-limit' ); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Define additional field properties. |
| 248 | * |
| 249 | * @since 1.0.0 |
| 250 | * |
| 251 | * @param array $properties Field properties. |
| 252 | * @param array $field Field settings. |
| 253 | * @param array $form_data Form data and settings. |
| 254 | * |
| 255 | * @return array of additional field properties. |
| 256 | */ |
| 257 | public function field_properties( $properties, $field, $form_data ) { |
| 258 | // Input primary: Detect custom input mask. |
| 259 | if ( ! empty( $field['input_mask'] ) ) { |
| 260 | // Add class that will trigger custom mask. |
| 261 | $properties['inputs']['primary']['class'][] = 'evf-masked-input'; |
| 262 | |
| 263 | // Register string for translation. |
| 264 | $field['input_mask'] = evf_string_translation( $form_data['id'], $field['id'], $field['input_mask'], '-input-mask' ); |
| 265 | |
| 266 | if ( false !== strpos( $field['input_mask'], 'alias:' ) ) { |
| 267 | $mask = str_replace( 'alias:', '', $field['input_mask'] ); |
| 268 | $properties['inputs']['primary']['data']['inputmask-alias'] = $mask; |
| 269 | } elseif ( false !== strpos( $field['input_mask'], 'regex:' ) ) { |
| 270 | $mask = str_replace( 'regex:', '', $field['input_mask'] ); |
| 271 | $properties['inputs']['primary']['data']['inputmask-regex'] = $mask; |
| 272 | } elseif ( false !== strpos( $field['input_mask'], 'date:' ) ) { |
| 273 | $mask = str_replace( 'date:', '', $field['input_mask'] ); |
| 274 | $properties['inputs']['primary']['data']['inputmask-alias'] = 'datetime'; |
| 275 | $properties['inputs']['primary']['data']['inputmask-inputformat'] = $mask; |
| 276 | } else { |
| 277 | $properties['inputs']['primary']['data']['inputmask-mask'] = $field['input_mask']; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | return $properties; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Field preview inside the builder. |
| 286 | * |
| 287 | * @param array $field Field data and settings. |
| 288 | */ |
| 289 | public function field_preview( $field ) { |
| 290 | // Define data. |
| 291 | $placeholder = ! empty( $field['placeholder'] ) ? esc_attr( $field['placeholder'] ) : ''; |
| 292 | |
| 293 | // Label. |
| 294 | $this->field_preview_option( 'label', $field ); |
| 295 | |
| 296 | // Primary input. |
| 297 | echo '<input type="text" placeholder="' . esc_attr( $placeholder ) . '" class="widefat" disabled>'; |
| 298 | |
| 299 | // Description. |
| 300 | $this->field_preview_option( 'description', $field ); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Field display on the form front-end. |
| 305 | * |
| 306 | * @since 1.0.0 |
| 307 | * |
| 308 | * @param array $field Field Data. |
| 309 | * @param array $field_atts Field attributes. |
| 310 | * @param array $form_data All Form Data. |
| 311 | */ |
| 312 | public function field_display( $field, $field_atts, $form_data ) { |
| 313 | // Define data. |
| 314 | $primary = apply_filters( 'everest_forms_default_values', $field['properties']['inputs']['primary'] ); |
| 315 | |
| 316 | // Limit length. |
| 317 | if ( isset( $field['limit_enabled'] ) ) { |
| 318 | $limit_count = isset( $field['limit_count'] ) ? absint( $field['limit_count'] ) : 0; |
| 319 | $limit_mode = isset( $field['limit_mode'] ) ? sanitize_key( $field['limit_mode'] ) : 'characters'; |
| 320 | |
| 321 | $primary['data']['form-id'] = $form_data['id']; |
| 322 | $primary['data']['field-id'] = $field['id']; |
| 323 | |
| 324 | if ( 'characters' === $limit_mode ) { |
| 325 | $primary['class'][] = 'everest-forms-limit-characters-enabled'; |
| 326 | $primary['attr']['maxlength'] = $limit_count; |
| 327 | $primary['data']['text-limit'] = $limit_count; |
| 328 | } else { |
| 329 | $primary['class'][] = 'everest-forms-limit-words-enabled'; |
| 330 | $primary['data']['text-limit'] = $limit_count; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // Minimum length. |
| 335 | if ( isset( $field['min_length_enabled'] ) ) { |
| 336 | $min_length_count = isset( $field['min_length_count'] ) ? absint( $field['min_length_count'] ) : 0; |
| 337 | $min_length_mode = isset( $field['min_length_mode'] ) ? sanitize_key( $field['min_length_mode'] ) : 'characters'; |
| 338 | |
| 339 | $primary['data']['form-id'] = $form_data['id']; |
| 340 | $primary['data']['field-id'] = $field['id']; |
| 341 | |
| 342 | if ( 'characters' === $min_length_mode ) { |
| 343 | $primary['class'][] = 'everest-forms-min-characters-length-enabled'; |
| 344 | $primary['attr']['minlength'] = $min_length_count; |
| 345 | $primary['data']['text-min-length'] = $min_length_count; |
| 346 | } else { |
| 347 | $primary['class'][] = 'everest-forms-min-words-length-enabled'; |
| 348 | $primary['data']['text-min-length'] = $min_length_count; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | printf( |
| 353 | '<input type="text" %s %s>', |
| 354 | evf_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), |
| 355 | esc_attr( $primary['required'] ) |
| 356 | ); |
| 357 | } |
| 358 | } |
| 359 |