| 1 |
<?php |
| 2 |
/** |
| 3 |
* SureDonation Input Markup Class. |
| 4 |
* |
| 5 |
* @package SureDonation |
| 6 |
* @since 0.0.1 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SureDonation\Inc\Fields; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
use SureDonation\Inc\Helper; |
| 16 |
|
| 17 |
/** |
| 18 |
* Input Markup Class. |
| 19 |
* |
| 20 |
* @since 0.0.1 |
| 21 |
*/ |
| 22 |
class Input_Markup extends Base { |
| 23 |
/** |
| 24 |
* Maximum length of text allowed for an input field. |
| 25 |
* |
| 26 |
* @var int |
| 27 |
* @since 0.0.1 |
| 28 |
*/ |
| 29 |
protected $max_length = 100; |
| 30 |
|
| 31 |
/** |
| 32 |
* Input mask pattern for the input field. |
| 33 |
* |
| 34 |
* @var string |
| 35 |
* @since 1.0.0 |
| 36 |
*/ |
| 37 |
protected $input_mask = ''; |
| 38 |
|
| 39 |
/** |
| 40 |
* Custom input mask pattern for the input field. |
| 41 |
* |
| 42 |
* @var string |
| 43 |
* @since 1.0.0 |
| 44 |
*/ |
| 45 |
protected $custom_input_mask = ''; |
| 46 |
|
| 47 |
/** |
| 48 |
* Initialize the properties based on block attributes. |
| 49 |
* |
| 50 |
* @param array<string, mixed> $attributes Block attributes. |
| 51 |
* @since 0.0.1 |
| 52 |
*/ |
| 53 |
public function __construct( $attributes ) { |
| 54 |
$this->slug = 'input'; |
| 55 |
$this->max_length = isset( $attributes['maxLength'] ) ? absint( Helper::get_string_value( $attributes['maxLength'] ) ) : 100; |
| 56 |
$this->input_mask = isset( $attributes['inputMask'] ) ? Helper::get_string_value( $attributes['inputMask'] ) : 'none'; |
| 57 |
$this->custom_input_mask = 'custom-mask' === $this->input_mask && isset( $attributes['customInputMask'] ) ? Helper::get_string_value( $attributes['customInputMask'] ) : ''; |
| 58 |
|
| 59 |
$this->set_properties( $attributes ); |
| 60 |
$this->set_unique_slug(); |
| 61 |
$this->set_markup_properties(); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Render input markup |
| 66 |
* |
| 67 |
* @since 0.0.1 |
| 68 |
* @return string |
| 69 |
*/ |
| 70 |
public function markup() { |
| 71 |
$classes = $this->get_field_classes(); |
| 72 |
$aria_desc = $this->get_aria_describedby(); |
| 73 |
|
| 74 |
ob_start(); |
| 75 |
?> |
| 76 |
<div data-block-id="<?php echo esc_attr( $this->block_id ); ?>" class="<?php echo esc_attr( $classes ); ?>"> |
| 77 |
<?php echo wp_kses_post( $this->label_markup ); ?> |
| 78 |
<?php echo wp_kses_post( $this->help_markup ); ?> |
| 79 |
<div class="sd-block-wrap"> |
| 80 |
<input |
| 81 |
class="sd-input-common sd-input-<?php echo esc_attr( $this->slug ); ?>" |
| 82 |
type="text" |
| 83 |
name="<?php echo esc_attr( $this->field_name ); ?>" |
| 84 |
id="<?php echo esc_attr( $this->unique_slug ); ?>" |
| 85 |
data-slug="<?php echo esc_attr( $this->block_slug ? $this->block_slug : $this->unique_slug ); ?>" |
| 86 |
<?php if ( ! empty( $aria_desc ) ) { ?> |
| 87 |
aria-describedby="<?php echo esc_attr( $aria_desc ); ?>" |
| 88 |
<?php } ?> |
| 89 |
data-required="<?php echo esc_attr( $this->data_require_attr ); ?>" |
| 90 |
aria-required="<?php echo esc_attr( $this->data_require_attr ); ?>" |
| 91 |
maxlength="<?php echo esc_attr( (string) $this->max_length ); ?>" |
| 92 |
<?php if ( '' !== $this->default ) { ?> |
| 93 |
value="<?php echo esc_attr( $this->default ); ?>" |
| 94 |
<?php } ?> |
| 95 |
<?php if ( '' !== $this->placeholder ) { ?> |
| 96 |
placeholder="<?php echo esc_attr( $this->placeholder ); ?>" |
| 97 |
<?php } ?> |
| 98 |
data-sd-mask="<?php echo esc_attr( $this->input_mask ); ?>" |
| 99 |
<?php if ( ! empty( $this->custom_input_mask ) ) { ?> |
| 100 |
data-custom-sd-mask="<?php echo esc_attr( $this->custom_input_mask ); ?>" |
| 101 |
<?php } ?> |
| 102 |
/> |
| 103 |
</div> |
| 104 |
<div class="sd-error-wrap"><?php echo wp_kses_post( $this->error_msg_markup ); ?></div> |
| 105 |
</div> |
| 106 |
<?php |
| 107 |
return (string) ob_get_clean(); |
| 108 |
} |
| 109 |
} |
| 110 |
|