| 1 |
<?php |
| 2 |
/** |
| 3 |
* SureDonation Checkbox 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 |
/** |
| 16 |
* Checkbox Markup Class. |
| 17 |
* |
| 18 |
* @since 0.0.1 |
| 19 |
*/ |
| 20 |
class Checkbox_Markup extends Base { |
| 21 |
/** |
| 22 |
* Initialize the properties based on block attributes. |
| 23 |
* |
| 24 |
* @param array<string, mixed> $attributes Block attributes. |
| 25 |
* @since 0.0.1 |
| 26 |
*/ |
| 27 |
public function __construct( $attributes ) { |
| 28 |
$this->slug = 'checkbox'; |
| 29 |
|
| 30 |
$this->set_properties( $attributes ); |
| 31 |
$this->set_unique_slug(); |
| 32 |
$this->set_markup_properties(); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Render checkbox markup |
| 37 |
* |
| 38 |
* @since 0.0.1 |
| 39 |
* @return string |
| 40 |
*/ |
| 41 |
public function markup() { |
| 42 |
// `sd-checkbox-field` is this block's own modifier. `sd-checkbox-block` |
| 43 |
// and the `sd-checkbox-*` element classes are a shared contract — the |
| 44 |
// anonymous-donation, cover-fees and privacy-consent fields write the |
| 45 |
// same markup by hand — so anything specific to the authorable field |
| 46 |
// (styling, and the frontend hooks that read its label and validate it) |
| 47 |
// keys off this class instead of the shared ones. |
| 48 |
$classes = $this->get_field_classes( [ 'sd-checkbox-field' ] ); |
| 49 |
$aria_desc = $this->get_aria_describedby(); |
| 50 |
$required_mark = $this->required ? '<span class="sd-required" aria-hidden="true">*</span>' : ''; |
| 51 |
|
| 52 |
// The slug is the key the value is submitted, validated and stored |
| 53 |
// under. Without it the field renders but never reaches the server — |
| 54 |
// the failure mode #273 hit on the anonymous-donation checkbox. |
| 55 |
$data_slug = $this->block_slug ? $this->block_slug : $this->unique_slug; |
| 56 |
|
| 57 |
ob_start(); |
| 58 |
?> |
| 59 |
<div data-block-id="<?php echo esc_attr( $this->block_id ); ?>" class="<?php echo esc_attr( $classes ); ?>"> |
| 60 |
<div class="sd-block-wrap sd-checkbox-wrap"> |
| 61 |
<label class="sd-checkbox-label" for="<?php echo esc_attr( $this->unique_slug ); ?>"> |
| 62 |
<input |
| 63 |
class="sd-input-checkbox" |
| 64 |
type="checkbox" |
| 65 |
name="<?php echo esc_attr( $this->field_name ); ?>" |
| 66 |
id="<?php echo esc_attr( $this->unique_slug ); ?>" |
| 67 |
data-slug="<?php echo esc_attr( $data_slug ); ?>" |
| 68 |
value="1" |
| 69 |
<?php if ( ! empty( $aria_desc ) ) { ?> |
| 70 |
aria-describedby="<?php echo esc_attr( $aria_desc ); ?>" |
| 71 |
<?php } ?> |
| 72 |
data-required="<?php echo esc_attr( $this->data_require_attr ); ?>" |
| 73 |
aria-required="<?php echo esc_attr( $this->data_require_attr ); ?>" |
| 74 |
<?php echo esc_attr( $this->checked_attr ); ?> |
| 75 |
/> |
| 76 |
<span class="sd-checkbox-text"> |
| 77 |
<?php echo wp_kses_post( $this->label ); ?> |
| 78 |
<?php echo wp_kses_post( $required_mark ); ?> |
| 79 |
</span> |
| 80 |
</label> |
| 81 |
</div> |
| 82 |
<?php echo wp_kses_post( $this->help_markup ); ?> |
| 83 |
<div class="sd-error-wrap"><?php echo wp_kses_post( $this->error_msg_markup ); ?></div> |
| 84 |
</div> |
| 85 |
<?php |
| 86 |
return (string) ob_get_clean(); |
| 87 |
} |
| 88 |
} |
| 89 |
|