| 1 |
<?php |
| 2 |
/** |
| 3 |
* SureDonation Cover Fees 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 |
use SureDonation\Inc\Payments\Payment_Helper; |
| 17 |
|
| 18 |
/** |
| 19 |
* Cover Fees Markup Class. |
| 20 |
* |
| 21 |
* @since 0.0.1 |
| 22 |
*/ |
| 23 |
class Cover_Fees_Markup extends Base { |
| 24 |
/** |
| 25 |
* Fee percentage (e.g., 2.9 for Stripe). |
| 26 |
* |
| 27 |
* @var float |
| 28 |
*/ |
| 29 |
protected $fee_percentage; |
| 30 |
|
| 31 |
/** |
| 32 |
* Fixed fee amount (e.g., 0.30 for Stripe). |
| 33 |
* |
| 34 |
* @var float |
| 35 |
*/ |
| 36 |
protected $fee_fixed; |
| 37 |
|
| 38 |
/** |
| 39 |
* Fee mode: 'all_gateways' or 'per_gateway'. |
| 40 |
* |
| 41 |
* @var string |
| 42 |
* @since 1.0.0 |
| 43 |
*/ |
| 44 |
protected $fee_mode; |
| 45 |
|
| 46 |
/** |
| 47 |
* Per-gateway fee configuration. |
| 48 |
* |
| 49 |
* @var array<string, mixed> |
| 50 |
* @since 1.0.0 |
| 51 |
*/ |
| 52 |
protected $gateway_fees; |
| 53 |
|
| 54 |
/** |
| 55 |
* Initialize the properties based on block attributes. |
| 56 |
* |
| 57 |
* @param array<string, mixed> $attributes Block attributes. |
| 58 |
* @since 0.0.1 |
| 59 |
*/ |
| 60 |
public function __construct( $attributes ) { |
| 61 |
$this->slug = 'cover-fees'; |
| 62 |
|
| 63 |
$use_global = $attributes['useGlobalDefaults'] ?? true; |
| 64 |
|
| 65 |
if ( $use_global ) { |
| 66 |
$fee_config = Payment_Helper::get_fee_recovery_settings(); |
| 67 |
} else { |
| 68 |
$fee_config = [ |
| 69 |
'fee_percentage' => Helper::get_float_value( $attributes['feePercentage'] ?? 2.9 ), |
| 70 |
'fee_fixed' => Helper::get_float_value( $attributes['feeFixed'] ?? 0.30 ), |
| 71 |
'fee_mode' => $attributes['feeMode'] ?? 'all_gateways', |
| 72 |
'gateways' => $attributes['gatewayFees'] ?? [], |
| 73 |
]; |
| 74 |
} |
| 75 |
|
| 76 |
$this->fee_mode = is_string( $fee_config['fee_mode'] ?? null ) ? $fee_config['fee_mode'] : 'all_gateways'; |
| 77 |
$this->fee_percentage = is_numeric( $fee_config['fee_percentage'] ?? null ) ? (float) $fee_config['fee_percentage'] : 2.9; |
| 78 |
$this->fee_fixed = is_numeric( $fee_config['fee_fixed'] ?? null ) ? (float) $fee_config['fee_fixed'] : 0.30; |
| 79 |
$this->gateway_fees = is_array( $fee_config['gateways'] ?? null ) ? $fee_config['gateways'] : []; |
| 80 |
|
| 81 |
$this->set_properties( $attributes ); |
| 82 |
$this->set_unique_slug(); |
| 83 |
$this->set_markup_properties(); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Render cover fees checkbox markup |
| 88 |
* |
| 89 |
* @since 0.0.1 |
| 90 |
* @return string |
| 91 |
*/ |
| 92 |
public function markup() { |
| 93 |
$classes = $this->get_field_classes(); |
| 94 |
$aria_desc = $this->get_aria_describedby(); |
| 95 |
|
| 96 |
ob_start(); |
| 97 |
?> |
| 98 |
<div data-block-id="<?php echo esc_attr( $this->block_id ); ?>" class="<?php echo esc_attr( $classes ); ?>"> |
| 99 |
<div class="sd-block-wrap sd-checkbox-wrap sd-cover-fees-wrap"> |
| 100 |
<label class="sd-checkbox-label" for="<?php echo esc_attr( $this->unique_slug ); ?>"> |
| 101 |
<input |
| 102 |
class="sd-input-checkbox sd-cover-fees-input" |
| 103 |
type="checkbox" |
| 104 |
name="<?php echo esc_attr( $this->field_name ); ?>" |
| 105 |
id="<?php echo esc_attr( $this->unique_slug ); ?>" |
| 106 |
value="1" |
| 107 |
data-fee-percentage="<?php echo esc_attr( Helper::get_string_value( $this->fee_percentage ) ); ?>" |
| 108 |
data-fee-fixed="<?php echo esc_attr( Helper::get_string_value( $this->fee_fixed ) ); ?>" |
| 109 |
data-fee-mode="<?php echo esc_attr( $this->fee_mode ); ?>" |
| 110 |
<?php if ( 'per_gateway' === $this->fee_mode && ! empty( $this->gateway_fees ) ) { ?> |
| 111 |
data-gateway-fees="<?php echo esc_attr( (string) wp_json_encode( $this->gateway_fees ) ); ?>" |
| 112 |
<?php } ?> |
| 113 |
<?php if ( ! empty( $aria_desc ) ) { ?> |
| 114 |
aria-describedby="<?php echo esc_attr( $aria_desc ); ?>" |
| 115 |
<?php } ?> |
| 116 |
<?php echo esc_attr( $this->checked_attr ); ?> |
| 117 |
/> |
| 118 |
<span class="sd-checkbox-text"> |
| 119 |
<?php echo wp_kses_post( $this->label ); ?> |
| 120 |
<span class="sd-cover-fees-amount"></span> |
| 121 |
</span> |
| 122 |
</label> |
| 123 |
</div> |
| 124 |
<?php echo wp_kses_post( $this->help_markup ); ?> |
| 125 |
</div> |
| 126 |
<?php |
| 127 |
return (string) ob_get_clean(); |
| 128 |
} |
| 129 |
} |
| 130 |
|