PluginProbe
Contact Forms by Cimatti / 2.3.0
Contact Forms by Cimatti v2.3.0
2.3.6 2.3.5 2.3.0 2.2.32 2.2.4 2.2.0 2.1.2 2.1.1 trunk 1.0 1.1 1.2 1.2.1 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 All 62 releases
contact-forms / classes / Element / Telephone.php

Telephone.php in Contact Forms by Cimatti 2.3.0, at classes/Element/Telephone.php

101 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Telephone Element for Contact Forms
4 *
5 * Renders an accessible telephone input with proper HTML5 attributes.
6 * Uses type="tel" for mobile keyboard optimization, proper autocomplete,
7 * and integrates with the Phone validation class.
8 *
9 * Data is stored as plain text in the database - no changes to existing data.
10 *
11 * @package Contact Forms
12 * @subpackage Element
13 * @since 2.0.0-beta.20
14 */
15
16 class AccuaForm_Element_Telephone extends Element_Textbox {
17
18 /**
19 * Country code for validation (ISO 3166-1 alpha-2)
20 *
21 * @var string
22 */
23 protected $country_code = 'IT';
24
25 /**
26 * Constructor
27 *
28 * Sets up telephone-specific HTML5 attributes for accessibility and UX:
29 * - type="tel" for mobile numeric keyboard
30 * - inputmode="tel" for better mobile support
31 * - autocomplete="tel" for browser autofill
32 * - data-country for libphonenumber validation
33 * - Automatic phone validation
34 *
35 * @param string $label The field label
36 * @param string $name The field name attribute
37 * @param array|null $properties Optional element properties. Supports 'country_code' for validation.
38 */
39 public function __construct($label, $name, ?array $properties = null) {
40 // Extract country_code before parent constructor
41 if (isset($properties['country_code'])) {
42 $this->country_code = strtoupper(sanitize_text_field($properties['country_code']));
43 unset($properties['country_code']); // Don't pass to parent
44 }
45
46 // Extract custom format message before parent constructor
47 $custom_format_message = '';
48 if (isset($properties['custom_format_message'])) {
49 $custom_format_message = $properties['custom_format_message'];
50 unset($properties['custom_format_message']);
51 }
52
53 parent::__construct($label, $name, $properties);
54
55 // Set HTML5 telephone input type for mobile keyboard optimization
56 $this->attributes['type'] = 'tel';
57
58 // Input mode for better mobile keyboard support
59 $this->attributes['inputmode'] = 'tel';
60
61 // Autocomplete hint for browser autofill
62 $this->attributes['autocomplete'] = 'tel';
63
64 // Data attribute for libphonenumber-js validation (used by phone-validation.js)
65 $this->attributes['data-country'] = esc_attr($this->country_code);
66
67 // Add CSS class for styling and JS targeting
68 $this->setClass('accuaform-telephone');
69
70 // Attach phone validation with translated or custom error message
71 if ($custom_format_message !== '') {
72 $phone_error_msg = str_replace(array('%s', '%element%'), $label, $custom_format_message);
73 } else {
74 // translators: %element% is the field label.
75 $phone_error_msg = str_replace('%element%', $label, __( "Please enter a valid phone number in %element% (e.g. +39 333 1234567)", 'contact-forms' ));
76 }
77 $this->setValidation(new AccuaForm_Validation_Phone($phone_error_msg));
78 }
79
80 /**
81 * Override to normalize prefix-only values (e.g. "+39") to empty before validation.
82 *
83 * This ensures Required validation correctly treats prefix-only values as empty
84 * (the user has not entered an actual phone number, only the default country prefix).
85 *
86 * @param string|null $value The raw POST value.
87 * @return bool True if all validators pass.
88 */
89 public function isValid($value) {
90 if (is_string($value) && $value !== '') {
91 $trimmed = trim($value);
92 $digitCount = strlen(preg_replace('/\D/', '', $trimmed));
93 if (strpos($trimmed, '+') === 0 && $digitCount <= 4) {
94 $value = '';
95 }
96 }
97 return parent::isValid($value);
98 }
99
100 }
101