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