| 1 |
<?php |
| 2 |
/** |
| 3 |
* Sureforms Email Markup Class file. |
| 4 |
* |
| 5 |
* @package sureforms. |
| 6 |
* @since 0.0.1 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SRFM\Inc\Fields; |
| 10 |
|
| 11 |
use SRFM\Inc\Helper; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; // Exit if accessed directly. |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* SureForms Email Markup Class. |
| 19 |
* |
| 20 |
* @since 0.0.1 |
| 21 |
*/ |
| 22 |
class Email_Markup extends Base { |
| 23 |
/** |
| 24 |
* Flag indicating whether email confirmation is required. |
| 25 |
* |
| 26 |
* @var bool |
| 27 |
* @since 0.0.2 |
| 28 |
*/ |
| 29 |
protected $is_confirm_email; |
| 30 |
|
| 31 |
/** |
| 32 |
* Fallback label for the confirmation input field. |
| 33 |
* |
| 34 |
* @var string |
| 35 |
* @since 0.0.2 |
| 36 |
*/ |
| 37 |
protected $input_confirm_label_fallback; |
| 38 |
|
| 39 |
/** |
| 40 |
* Encoded label for the confirmation input field. |
| 41 |
* |
| 42 |
* @var string |
| 43 |
* @since 0.0.2 |
| 44 |
*/ |
| 45 |
protected $input_confirm_label; |
| 46 |
|
| 47 |
/** |
| 48 |
* Unique slug for the confirmation input field, combining the form slug, block ID, and encoded label. |
| 49 |
* |
| 50 |
* @var string |
| 51 |
* @since 0.0.2 |
| 52 |
*/ |
| 53 |
protected $unique_confirm_slug; |
| 54 |
|
| 55 |
/** |
| 56 |
* Retains a copy of Confirmation Email input label. |
| 57 |
* |
| 58 |
* @var string |
| 59 |
* @since 0.0.7 |
| 60 |
*/ |
| 61 |
protected $confirm_label; |
| 62 |
|
| 63 |
/** |
| 64 |
* Read-only attribute for the email field. |
| 65 |
* |
| 66 |
* @var bool |
| 67 |
* @since 1.7.2 |
| 68 |
*/ |
| 69 |
protected $read_only; |
| 70 |
|
| 71 |
/** |
| 72 |
* Initialize the properties based on block attributes. |
| 73 |
* |
| 74 |
* @param array<mixed> $attributes Block attributes. |
| 75 |
* @since 0.0.2 |
| 76 |
*/ |
| 77 |
public function __construct( $attributes ) { |
| 78 |
$this->set_properties( $attributes ); |
| 79 |
$this->set_input_label( __( 'Email', 'sureforms' ) ); |
| 80 |
$this->set_error_msg( $attributes, 'srfm_email_block_required_text' ); |
| 81 |
$this->set_duplicate_msg( $attributes, 'srfm_email_block_unique_text' ); |
| 82 |
$this->slug = 'email'; |
| 83 |
$this->is_confirm_email = $attributes['isConfirmEmail'] ?? false; |
| 84 |
$this->input_confirm_label_fallback = __( 'Confirm ', 'sureforms' ) . $this->input_label_fallback; |
| 85 |
$this->input_confirm_label = '-lbl-' . Helper::encode( $this->input_confirm_label_fallback ); |
| 86 |
$this->unique_confirm_slug = 'srfm-' . $this->slug . '-confirm-' . $this->block_id . $this->input_confirm_label; |
| 87 |
$this->read_only = ! empty( trim( $this->default ) ) && $attributes['readOnly']; |
| 88 |
$this->set_unique_slug(); |
| 89 |
$this->set_field_name( $this->unique_slug ); |
| 90 |
$this->set_markup_properties( $this->input_label, true ); |
| 91 |
$this->set_aria_described_by(); |
| 92 |
// Translators: %s is label of block. |
| 93 |
$this->confirm_label = ! empty( $attributes['confirmLabel'] ) ? sanitize_text_field( $attributes['confirmLabel'] ) : sprintf( __( 'Confirm %s', 'sureforms' ), $this->label ); |
| 94 |
$this->set_label_as_placeholder( $this->input_label ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Render the sureforms email classic styling |
| 99 |
* |
| 100 |
* @since 0.0.2 |
| 101 |
* @return string|bool |
| 102 |
*/ |
| 103 |
public function markup() { |
| 104 |
$extra_classes = [ |
| 105 |
'srfm-' . $this->slug . '-block-wrap', |
| 106 |
]; |
| 107 |
|
| 108 |
if ( $this->read_only ) { |
| 109 |
$extra_classes[] = 'srfm-read-only'; |
| 110 |
} |
| 111 |
|
| 112 |
$this->class_name = $this->get_field_classes( $extra_classes ); |
| 113 |
ob_start(); ?> |
| 114 |
<div data-block-id="<?php echo esc_attr( $this->block_id ); ?>" class="<?php echo esc_attr( $this->class_name ); ?>"> |
| 115 |
<div class="srfm-<?php echo esc_attr( $this->slug ); ?>-block srf-<?php echo esc_attr( $this->slug ); ?>-<?php echo esc_attr( $this->block_id ); ?>-block"> |
| 116 |
<?php echo wp_kses_post( $this->label_markup ); ?> |
| 117 |
<?php echo wp_kses_post( $this->help_markup ); ?> |
| 118 |
<div class="srfm-block-wrap"> |
| 119 |
<input class="srfm-input-common srfm-input-<?php echo esc_attr( $this->slug ); ?>" type="email" name="<?php echo esc_attr( $this->field_name ); ?>" id="<?php echo esc_attr( $this->unique_slug ); ?>" |
| 120 |
<?php echo ! empty( $this->aria_described_by ) ? "aria-describedby='" . esc_attr( trim( $this->aria_described_by ) ) . "'" : ''; ?> |
| 121 |
data-required="<?php echo esc_attr( strval( $this->data_require_attr ) ); ?>" aria-required="<?php echo esc_attr( $this->data_require_attr ); ?>" data-unique="<?php echo esc_attr( $this->aria_unique ); ?>" value="<?php echo esc_attr( $this->default ); ?>" <?php echo wp_kses_post( $this->placeholder_attr ); ?> <?php echo $this->read_only ? 'readonly' : ''; ?> /> |
| 122 |
</div> |
| 123 |
<div class="srfm-error-wrap"> |
| 124 |
<?php echo wp_kses_post( $this->duplicate_msg_markup ); ?> |
| 125 |
</div> |
| 126 |
</div> |
| 127 |
<?php |
| 128 |
if ( true === $this->is_confirm_email ) { |
| 129 |
$confirm_label_markup = Helper::generate_common_form_markup( $this->form_id, 'label', $this->confirm_label, $this->slug . '-confirm', $this->block_id . $this->input_confirm_label, boolval( $this->required ) ); |
| 130 |
$placeholder = Helper::generate_common_form_markup( $this->form_id, 'placeholder', $this->confirm_label, $this->slug, $this->block_id . $this->block_id . $this->input_confirm_label, boolval( $this->required ) ); |
| 131 |
$this->placeholder_attr = ''; |
| 132 |
if ( ! empty( $placeholder ) ) { |
| 133 |
$confirm_label_markup = ''; |
| 134 |
$this->placeholder_attr = ' placeholder="' . esc_attr( $placeholder ) . '" '; |
| 135 |
} |
| 136 |
|
| 137 |
$confirm_error_markup = Helper::generate_common_form_markup( $this->form_id, 'error', '', '', $this->block_id, boolval( $this->required ), '', $this->error_msg, false, '', true ); |
| 138 |
?> |
| 139 |
<div class="srfm-<?php echo esc_attr( $this->slug ); ?>-confirm-block srf-<?php echo esc_attr( $this->slug ); ?>-<?php echo esc_attr( $this->block_id ); ?>-confirm-block"> |
| 140 |
<?php echo wp_kses_post( $confirm_label_markup ); ?> |
| 141 |
<div class="srfm-block-wrap"> |
| 142 |
<input class="srfm-input-common srfm-input-<?php echo esc_attr( $this->slug ); ?>-confirm" type="email" id="<?php echo esc_attr( $this->unique_confirm_slug ); ?>" |
| 143 |
<?php echo ! empty( $this->aria_described_by ) ? "aria-describedby='" . esc_attr( trim( $this->aria_described_by ) ) . "'" : ''; ?> |
| 144 |
data-required="<?php echo esc_attr( $this->data_require_attr ); ?>" aria-required="<?php echo esc_attr( $this->data_require_attr ); ?>" value="<?php echo esc_attr( $this->default ); ?>" <?php echo wp_kses_post( $this->placeholder_attr ); ?> <?php echo $this->read_only ? 'readonly' : ''; ?> /> |
| 145 |
</div> |
| 146 |
<div class="srfm-error-wrap"> |
| 147 |
<?php echo wp_kses_post( $confirm_error_markup ); ?> |
| 148 |
</div> |
| 149 |
</div> |
| 150 |
<?php } ?> |
| 151 |
</div> |
| 152 |
<?php |
| 153 |
$markup = ob_get_clean(); |
| 154 |
|
| 155 |
return apply_filters( |
| 156 |
'srfm_block_field_markup', |
| 157 |
$markup, |
| 158 |
[ |
| 159 |
'slug' => $this->slug, |
| 160 |
'is_editing' => $this->is_editing, |
| 161 |
'field_name' => $this->field_name, |
| 162 |
'attributes' => $this->attributes, |
| 163 |
] |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
} |
| 168 |
|