register_block_from_metadata( 'tel' );
}
public function render( $atts ) {
if ( ! $this->allow_render_now( $atts ) ) {
return '';
}
$label = isset( $atts['label'] ) ? $atts['label'] : 'Phone';
$name = isset( $atts['name'] ) ? $atts['name'] : 'tel';
$name = sanitize_key( $name );
$ph = isset( $atts['placeholder'] ) ? esc_attr( $atts['placeholder'] ) : '';
$required = ! empty( $atts['required'] );
$reqAttr = $this->get_field_boolean_attr( $atts, 'required', 'required' );
$defVal = isset( $atts['defaultValue'] ) ? esc_attr( $atts['defaultValue'] ) : '';
$acomp = isset( $atts['autocomplete'] ) && $atts['autocomplete'] !== '' ? 'autocomplete="'. esc_attr( $atts['autocomplete'] ) .'"' : 'autocomplete="tel"';
$inputMode = isset( $atts['inputMode'] ) && $atts['inputMode'] !== '' ? 'inputmode="'. esc_attr( $atts['inputMode'] ) .'"' : 'inputmode="tel"';
$minLen = isset( $atts['minLength'] ) && is_numeric( $atts['minLength'] ) ? 'minlength="'. intval( $atts['minLength'] ) .'"' : '';
$maxLen = isset( $atts['maxLength'] ) && is_numeric( $atts['maxLength'] ) ? 'maxlength="'. intval( $atts['maxLength'] ) .'"' : '';
$pattern = isset( $atts['pattern'] ) && $atts['pattern'] !== '' ? 'pattern="'. esc_attr( $atts['pattern'] ) .'"' : '';
$disabled= $this->get_field_boolean_attr( $atts, 'disabled', 'disabled' );
$readonly= $this->get_field_boolean_attr( $atts, 'readOnly', 'readonly' );
$labelPos= $this->get_field_label_position( $atts );
$intl_enabled = ! isset( $atts['intlEnabled'] ) || $atts['intlEnabled'];
$intl_default = 'auto';
if ( isset( $atts['intlDefaultCountry'] ) && is_string( $atts['intlDefaultCountry'] ) ) {
$candidate = strtolower( preg_replace( '/[^a-z]/', '', $atts['intlDefaultCountry'] ) );
if ( $candidate === 'auto' || strlen( $candidate ) === 2 ) {
$intl_default = $candidate;
}
}
$sanitize_countries = static function( $value ) {
if ( empty( $value ) ) return array();
$items = is_array( $value ) ? $value : explode( ',', $value );
$out = array();
foreach ( $items as $item ) {
$item = strtolower( preg_replace( '/[^a-z]/', '', (string) $item ) );
if ( strlen( $item ) === 2 ) {
$out[] = $item;
}
}
return array_values( array_unique( $out ) );
};
$intl_preferred = isset( $atts['intlPreferredCountries'] ) ? $sanitize_countries( $atts['intlPreferredCountries'] ) : array();
$intl_only = isset( $atts['intlOnlyCountries'] ) ? $sanitize_countries( $atts['intlOnlyCountries'] ) : array();
$intl_separate = ! empty( $atts['intlSeparateDialCode'] );
$intl_preferred_attr = empty( $intl_preferred ) ? '' : implode( ',', $intl_preferred );
$intl_only_attr = empty( $intl_only ) ? '' : implode( ',', $intl_only );
$intl_validate = ! isset( $atts['intlValidateFormat'] ) || $atts['intlValidateFormat'];
$intl_error_message = isset( $atts['intlInvalidMessage'] ) && $atts['intlInvalidMessage'] !== ''
? wp_strip_all_tags( $atts['intlInvalidMessage'] )
: __( 'Phone number is not valid for the selected country.', 'stacklar-forms' );
$class = $this->build_field_wrapper_class( 'tel', $atts );
$style_rules = $this->get_field_wrapper_style_rules( $atts );
$prefix = isset( $atts['prefix'] ) ? esc_html( $atts['prefix'] ) : '';
$suffix = isset( $atts['suffix'] ) ? esc_html( $atts['suffix'] ) : '';
$help_context = $this->get_field_help_context( $atts, $name );
$ariaLabel = $this->get_field_aria_label_attr( $atts );
$ariaDesc = $help_context['aria_describedby'];
$id = 'stacklar-tel-' . $name . '-' . wp_generate_password( 6, false, false );
$input_rules = $this->get_input_style_rules( $atts );
$inputStyle = $this->build_style_attribute( $input_rules );
$labelHtml = $this->render_field_label( $id, $label, $required, $labelPos, $atts );
$hiddenHtml = '';
$visibleName = $name;
$country_hidden_name = sanitize_key( $name . '_country' );
$national_hidden_name = sanitize_key( $name . '_national' );
if ( $intl_enabled ) {
$hiddenHtml = '';
$hiddenHtml .= '';
$hiddenHtml .= '';
$visibleName = $name . '_intl_display';
if ( $intl_validate ) {
$hiddenHtml .= '';
$hiddenHtml .= '';
}
}
$inputHtml = $this->render_input_wrap(
$hiddenHtml .
( $prefix !== '' ? ''. $prefix .'' : '' ) .
'' .
( $suffix !== '' ? ''. $suffix .'' : '' )
);
$contentHtml = $this->render_field_content( $labelHtml, $inputHtml, $labelPos, $help_context['help_html'] );
$html = $this->render_wrapper_open(
$atts,
$class,
$style_rules,
array(
'label' => '> label, .stacklar__inline > label',
'input' => 'input[type="tel"]',
)
);
$html .= $contentHtml;
$html .= $this->render_wrapper_close();
return $html;
}
}