1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
|
<?php
/** * Single line text field. * * @since 1.0.0 */ class WPForms_Field_Text extends WPForms_Field {
/** * Primary class constructor. * * @since 1.0.0 */ public function init() {
// Define field type information. $this->name = esc_html__( 'Single Line Text', 'wpforms-lite' ); $this->type = 'text'; $this->icon = 'fa-text-width'; $this->order = 30;
// Define additional field properties. add_filter( 'wpforms_field_properties_text', array( $this, 'field_properties' ), 5, 3 ); add_action( 'wpforms_frontend_js', array( $this, 'frontend_js' ) ); }
/** * Convert mask formatted for jquery.inputmask into the format used by amp-inputmask. * * Note that amp-inputmask does not yet support all of the options that jquery.inputmask provides. * In particular, amp-inputmask doesn't provides: * - Upper-alphabetical mask. * - Upper-alphanumeric mask. * - Advanced Input Masks with arbitrary repeating groups. * * @link https://amp.dev/documentation/components/amp-inputmask * @link https://wpforms.com/docs/how-to-use-custom-input-masks/ * * @param string $mask Mask formatted for jquery.inputmask. * @return array { * Mask and placeholder. * * @type string $mask Mask for amp-inputmask. * @type string $placeholder Placeholder derived from mask if one is not supplied. * } */ protected function convert_mask_to_amp_inputmask( $mask ) { $placeholder = '';
// Convert jquery.inputmask format into amp-inputmask format. $amp_mask = ''; $req_mask_mapping = array( '9' => '0', // Numeric. 'a' => 'L', // Alphabetical (a-z or A-Z). 'A' => 'L', // Upper-alphabetical (A-Z). Note: AMP does not have an uppercase-alphabetical mask type, so same as previous. '*' => 'A', // Alphanumeric (0-9, a-z, A-Z). '&' => 'A', // Upper-alphanumeric (A-Z, 0-9). Note: AMP does not have an uppercase-alphanumeric mask type, so same as previous. ' ' => '_', // Automatically insert spaces. ); $opt_mask_mapping = array( '9' => '9', // The user may optionally add a numeric character. 'a' => 'l', // The user may optionally add an alphabetical character. 'A' => 'l', // The user may optionally add an alphabetical character. '*' => 'a', // The user may optionally add an alphanumeric character. '&' => 'a', // The user may optionally add an alphanumeric character. ); $placeholder_mapping = array( '9' => '0', 'a' => 'a', 'A' => 'a', '*' => '_', '&' => '_', ); $is_inside_optional = false; $last_mask_token = null; for ( $i = 0, $len = strlen( $mask ); $i < $len; $i++ ) { if ( '[' === $mask[ $i ] ) { $is_inside_optional = true; $placeholder .= $mask[ $i ]; continue; } elseif ( ']' === $mask[ $i ] ) { $is_inside_optional = false; $placeholder .= $mask[ $i ]; continue; } elseif ( isset( $last_mask_token ) && preg_match( '/^\{(?P<n>\d+)(?:,(?P<m>\d+))?\}/', substr( $mask, $i ), $matches ) ) { $amp_mask .= str_repeat( $req_mask_mapping[ $last_mask_token ], $matches['n'] ); $placeholder .= str_repeat( $placeholder_mapping[ $last_mask_token ], $matches['n'] ); if ( isset( $matches['m'] ) ) { $amp_mask .= str_repeat( $opt_mask_mapping[ $last_mask_token ], $matches['m'] ); $placeholder .= str_repeat( $placeholder_mapping[ $last_mask_token ], $matches['m'] ); } $i += strlen( $matches[0] ) - 1;
$last_mask_token = null; // Reset. continue; }
if ( '\\' === $mask[ $i ] ) { $amp_mask .= '\\'; $i++; if ( ! isset( $mask[ $i ] ) ) { continue; } $amp_mask .= $mask[ $i ]; } else { // Remember this token in case it is a mask. if ( isset( $opt_mask_mapping[ $mask[ $i ] ] ) ) { $last_mask_token = $mask[ $i ]; }
if ( $is_inside_optional && isset( $opt_mask_mapping[ $mask[ $i ] ] ) ) { $amp_mask .= $opt_mask_mapping[ $mask[ $i ] ]; } elseif ( isset( $req_mask_mapping[ $mask[ $i ] ] ) ) { $amp_mask .= $req_mask_mapping[ $mask[ $i ] ]; } else { $amp_mask .= '\\' . $mask[ $i ]; } }
if ( isset( $placeholder_mapping[ $mask[ $i ] ] ) ) { $placeholder .= $placeholder_mapping[ $mask[ $i ] ]; } else { $placeholder .= $mask[ $i ]; } } return array( $amp_mask, $placeholder ); }
/** * Define additional field properties. * * @since 1.4.5 * * @param array $properties Field properties. * @param array $field Field settings. * @param array $form_data Form data and settings. * * @return array */ public function field_properties( $properties, $field, $form_data ) {
// Input primary: Detect custom input mask. if ( ! empty( $field['input_mask'] ) ) {
// Add class that will trigger custom mask. $properties['inputs']['primary']['class'][] = 'wpforms-masked-input';
if ( wpforms_is_amp() ) { list( $amp_mask, $placeholder ) = $this->convert_mask_to_amp_inputmask( $field['input_mask'] );
$properties['inputs']['primary']['attr']['mask'] = $amp_mask; if ( empty( $properties['inputs']['primary']['attr']['placeholder'] ) ) { $properties['inputs']['primary']['attr']['placeholder'] = $placeholder; } } elseif ( false !== strpos( $field['input_mask'], 'alias:' ) ) { $mask = str_replace( 'alias:', '', $field['input_mask'] ); $properties['inputs']['primary']['data']['inputmask-alias'] = $mask; } elseif ( false !== strpos( $field['input_mask'], 'regex:' ) ) { $mask = str_replace( 'regex:', '', $field['input_mask'] ); $properties['inputs']['primary']['data']['inputmask-regex'] = $mask; } elseif ( false !== strpos( $field['input_mask'], 'date:' ) ) { $mask = str_replace( 'date:', '', $field['input_mask'] ); $properties['inputs']['primary']['data']['inputmask-alias'] = 'datetime'; $properties['inputs']['primary']['data']['inputmask-inputformat'] = $mask;
} else { $properties['inputs']['primary']['data']['inputmask-mask'] = $field['input_mask']; $properties['inputs']['primary']['data']['rule-empty-blanks'] = true; } }
return $properties; }
/** * Field options panel inside the builder. * * @since 1.0.0 * * @param array $field Field settings. */ public function field_options( $field ) { /* * Basic field options. */
// Options open markup. $this->field_option( 'basic-options', $field, array( 'markup' => 'open', ) );
// Label. $this->field_option( 'label', $field );
// Description. $this->field_option( 'description', $field );
// Required toggle. $this->field_option( 'required', $field );
// Options close markup. $this->field_option( 'basic-options', $field, array( 'markup' => 'close', ) );
/* * Advanced field options. */
// Options open markup. $this->field_option( 'advanced-options', $field, array( 'markup' => 'open', ) );
// Size. $this->field_option( 'size', $field );
// Placeholder. $this->field_option( 'placeholder', $field );
// Hide label. $this->field_option( 'label_hide', $field );
// Limit length. $args = array( 'slug' => 'limit_enabled', 'content' => $this->field_element( 'checkbox', $field, array( 'slug' => 'limit_enabled', 'value' => isset( $field['limit_enabled'] ), 'desc' => esc_html__( 'Limit Length', 'wpforms-lite' ), 'tooltip' => esc_html__( 'Check this option to limit text length by characters or words count.', 'wpforms-lite' ), ), false ), ); $this->field_element( 'row', $field, $args );
$count = $this->field_element( 'text', $field, array( 'type' => 'number', 'slug' => 'limit_count', 'attrs' => array( 'min' => 1, 'step' => 1, 'pattern' => '[0-9]', ), 'value' => ! empty( $field['limit_count'] ) ? absint( $field['limit_count'] ) : 1, ), false );
$mode = $this->field_element( 'select', $field, array( 'slug' => 'limit_mode', 'value' => ! empty( $field['limit_mode'] ) ? esc_attr( $field['limit_mode'] ) : 'characters', 'options' => array( 'characters' => esc_html__( 'Characters', 'wpforms-lite' ), 'words' => esc_html__( 'Words', 'wpforms-lite' ), ), ), false ); $args = array( 'slug' => 'limit_controls', 'class' => ! isset( $field['limit_enabled'] ) ? 'wpforms-hide' : '', 'content' => $count . $mode, ); $this->field_element( 'row', $field, $args );
// Default value. $this->field_option( 'default_value', $field );
// Custom CSS classes. $this->field_option( 'css', $field );
// Input Mask. $lbl = $this->field_element( 'label', $field, array( 'slug' => 'input_mask', 'value' => esc_html__( 'Input Mask', 'wpforms-lite' ), 'tooltip' => esc_html__( 'Enter your custom input mask.', 'wpforms-lite' ), 'after_tooltip' => '<a href="https://wpforms.com/how-to-use-custom-input-masks/" class="after-label-description" target="_blank" rel="noopener noreferrer">' . esc_html__( 'See Examples & Docs', 'wpforms-lite' ) . '</a>', ), false ); $fld = $this->field_element( 'text', $field, array( 'slug' => 'input_mask', 'value' => ! empty( $field['input_mask'] ) ? esc_attr( $field['input_mask'] ) : '', ), false ); $this->field_element( 'row', $field, array( 'slug' => 'input_mask', 'content' => $lbl . $fld, ) );
// Options close markup. $this->field_option( 'advanced-options', $field, array( 'markup' => 'close', ) ); }
/** * Field preview inside the builder. * * @since 1.0.0 * * @param array $field Field settings. */ public function field_preview( $field ) {
// Define data. $placeholder = ! empty( $field['placeholder'] ) ? esc_attr( $field['placeholder'] ) : '';
// Label. $this->field_preview_option( 'label', $field );
// Primary input. echo '<input type="text" placeholder="' . esc_attr( $placeholder ) . '" class="primary-input" disabled>';
// Description. $this->field_preview_option( 'description', $field ); }
/** * Field display on the form front-end. * * @since 1.0.0 * * @param array $field Field settings. * @param array $deprecated Deprecated. * @param array $form_data Form data and settings. */ public function field_display( $field, $deprecated, $form_data ) {
// Define data. $primary = $field['properties']['inputs']['primary'];
if ( isset( $field['limit_enabled'] ) ) { $limit_count = isset( $field['limit_count'] ) ? absint( $field['limit_count'] ) : 0; $limit_mode = isset( $field['limit_mode'] ) ? sanitize_key( $field['limit_mode'] ) : 'characters';
$primary['data']['form-id'] = $form_data['id']; $primary['data']['field-id'] = $field['id'];
if ( 'characters' === $limit_mode ) { $primary['class'][] = 'wpforms-limit-characters-enabled'; $primary['attr']['maxlength'] = $limit_count; $primary['data']['text-limit'] = $limit_count; } else { $primary['class'][] = 'wpforms-limit-words-enabled'; $primary['data']['text-limit'] = $limit_count; } }
// Primary field. printf( '<input type="text" %s %s>', wpforms_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), $primary['required'] // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ); }
/** * Enqueue frontend limit option js. * * @since 1.5.6 * * @param array $forms Forms on the current page. */ public function frontend_js( $forms ) {
// Get fields. $fields = array_map( function( $form ) { return empty( $form['fields'] ) ? array() : $form['fields']; }, (array) $forms );
// Make fields flat. $fields = array_reduce( $fields, function( $accumulator, $current ) { return array_merge( $accumulator, $current ); }, array() );
// Leave only fields with limit. $fields = array_filter( $fields, function( $field ) { return $field['type'] === $this->type && isset( $field['limit_enabled'] ) && ! empty( $field['limit_count'] ); } );
if ( count( $fields ) ) { $min = \wpforms_get_min_suffix(); wp_enqueue_script( 'wpforms-text-limit', WPFORMS_PLUGIN_URL . "assets/js/text-limit{$min}.js", array(), WPFORMS_VERSION, true ); } }
/** * Format and sanitize field. * * @since 1.5.6 * * @param int $field_id Field ID. * @param mixed $field_submit Field value that was submitted. * @param array $form_data Form data and settings. */ public function format( $field_id, $field_submit, $form_data ) {
$field = $form_data['fields'][ $field_id ]; $name = ! empty( $field['label'] ) ? sanitize_text_field( $field['label'] ) : '';
// Sanitize. $value = sanitize_text_field( $field_submit );
wpforms()->process->fields[ $field_id ] = array( 'name' => $name, 'value' => $value, 'id' => absint( $field_id ), 'type' => $this->type, ); }
/** * Validate field on form submit. * * @since 1.6.2 * * @param int $field_id Field ID. * @param mixed $field_submit Field value that was submitted. * @param array $form_data Form data and settings. */ public function validate( $field_id, $field_submit, $form_data ) {
parent::validate( $field_id, $field_submit, $form_data );
if ( empty( $form_data['fields'][ $field_id ] ) || empty( $form_data['fields'][ $field_id ]['limit_enabled'] ) ) { return; }
$field = $form_data['fields'][ $field_id ]; $limit = absint( $field['limit_count'] ); $mode = ! empty( $field['limit_mode'] ) ? sanitize_key( $field['limit_mode'] ) : 'characters'; $value = sanitize_text_field( $field_submit );
if ( 'characters' === $mode ) { if ( mb_strlen( str_replace( "\r\n", "\n", $value ) ) > $limit ) { /* translators: %s - limit characters number. */ wpforms()->process->errors[ $form_data['id'] ][ $field_id ] = sprintf( _n( 'Text can\'t exceed %d character.', 'Text can\'t exceed %d characters.', $limit, 'wpforms-lite' ), $limit ); return; } } else { if ( wpforms_count_words( $value ) > $limit ) { /* translators: %s - limit words number. */ wpforms()->process->errors[ $form_data['id'] ][ $field_id ] = sprintf( _n( 'Text can\'t exceed %d word.', 'Text can\'t exceed %d words.', $limit, 'wpforms-lite' ), $limit ); return; } } } }
new WPForms_Field_Text();
|