| 1 |
<?php |
| 2 |
/** |
| 3 |
* SureDonation Donation Amount Markup Class. |
| 4 |
* |
| 5 |
* @package SureDonation |
| 6 |
* @since 0.0.1 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SureDonation\Inc\Fields; |
| 10 |
|
| 11 |
use SureDonation\Inc\Helper; |
| 12 |
use SureDonation\Inc\Payments\Payment_Helper; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly. |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Donation Amount Markup Class. |
| 20 |
* |
| 21 |
* @since 0.0.1 |
| 22 |
*/ |
| 23 |
class Donation_Amount_Markup extends Base { |
| 24 |
/** |
| 25 |
* Choice type (radio or checkbox). |
| 26 |
* |
| 27 |
* @var string |
| 28 |
* @since 0.0.1 |
| 29 |
*/ |
| 30 |
protected $choice_type; |
| 31 |
|
| 32 |
/** |
| 33 |
* Layout (horizontal or vertical). |
| 34 |
* |
| 35 |
* @var string |
| 36 |
* @since 0.0.1 |
| 37 |
*/ |
| 38 |
protected $layout; |
| 39 |
|
| 40 |
/** |
| 41 |
* Choice width (width of each option in horizontal layout). |
| 42 |
* |
| 43 |
* @var string |
| 44 |
* @since 0.0.1 |
| 45 |
*/ |
| 46 |
protected $choice_width; |
| 47 |
|
| 48 |
/** |
| 49 |
* Whether the optional custom amount input is shown after the presets. |
| 50 |
* |
| 51 |
* @var bool |
| 52 |
* @since 1.0.0 |
| 53 |
*/ |
| 54 |
protected $allow_custom_amount; |
| 55 |
|
| 56 |
/** |
| 57 |
* Minimum allowed value for the custom amount input. 0 means no min. |
| 58 |
* |
| 59 |
* @var float |
| 60 |
* @since 1.0.0 |
| 61 |
*/ |
| 62 |
protected $custom_amount_min; |
| 63 |
|
| 64 |
/** |
| 65 |
* Maximum allowed value for the custom amount input. 0 means no max. |
| 66 |
* |
| 67 |
* @var float |
| 68 |
* @since 1.0.0 |
| 69 |
*/ |
| 70 |
protected $custom_amount_max; |
| 71 |
|
| 72 |
/** |
| 73 |
* Initialize the properties based on block attributes. |
| 74 |
* |
| 75 |
* @param array<string, mixed> $attributes Block attributes. |
| 76 |
* @since 0.0.1 |
| 77 |
*/ |
| 78 |
public function __construct( $attributes ) { |
| 79 |
$this->slug = 'donation-amount'; |
| 80 |
$this->choice_type = isset( $attributes['choiceType'] ) ? Helper::get_string_value( $attributes['choiceType'] ) : 'radio'; |
| 81 |
$this->layout = isset( $attributes['layout'] ) ? Helper::get_string_value( $attributes['layout'] ) : 'horizontal'; |
| 82 |
$this->choice_width = isset( $attributes['choiceWidth'] ) ? Helper::get_string_value( $attributes['choiceWidth'] ) : '50'; |
| 83 |
$this->allow_custom_amount = ! isset( $attributes['allowCustomAmount'] ) || (bool) $attributes['allowCustomAmount']; |
| 84 |
$this->custom_amount_min = isset( $attributes['customAmountMin'] ) && is_numeric( $attributes['customAmountMin'] ) |
| 85 |
? (float) $attributes['customAmountMin'] |
| 86 |
: 0.0; |
| 87 |
$this->custom_amount_max = isset( $attributes['customAmountMax'] ) && is_numeric( $attributes['customAmountMax'] ) |
| 88 |
? (float) $attributes['customAmountMax'] |
| 89 |
: 0.0; |
| 90 |
|
| 91 |
$this->set_properties( $attributes ); |
| 92 |
$this->set_unique_slug(); |
| 93 |
$this->set_markup_properties(); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Render donation-amount markup |
| 98 |
* |
| 99 |
* @since 0.0.1 |
| 100 |
* @return string |
| 101 |
*/ |
| 102 |
public function markup() { |
| 103 |
$classes = $this->get_field_classes( [ 'sd-' . $this->choice_type . '-mode' ] ); |
| 104 |
$vertical_class = 'vertical' === $this->layout ? ' sd-vertical-layout' : ''; |
| 105 |
$choice_width_class = ' sd-choice-width-' . str_replace( '.', '-', $this->choice_width ); |
| 106 |
$wrap_class = 'sd-block-wrap sd-donation-amount-wrap' . $choice_width_class . $vertical_class; |
| 107 |
$svg_type = 'radio' === $this->choice_type ? 'circle' : 'square'; |
| 108 |
$checked_svg = $this->get_svg_icon( $svg_type . '-checked', 'sd-donation-amount-icon' ); |
| 109 |
$unchecked_svg = $this->get_svg_icon( $svg_type . '-unchecked', 'sd-donation-amount-icon-unchecked' ); |
| 110 |
$input_name = 'checkbox' === $this->choice_type ? $this->field_name . '[]' : $this->field_name; |
| 111 |
|
| 112 |
// Get the default value for the hidden input (preselected option). |
| 113 |
$hidden_value = ''; |
| 114 |
if ( ! empty( $this->default ) && ! empty( $this->options ) && is_array( $this->options ) ) { |
| 115 |
foreach ( $this->options as $option ) { |
| 116 |
$option_value = $option['value'] ?? ( $option['label'] ?? '' ); |
| 117 |
if ( $this->default === $option_value ) { |
| 118 |
$hidden_value = $option_value; |
| 119 |
break; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
ob_start(); |
| 125 |
?> |
| 126 |
<div data-block-id="<?php echo esc_attr( $this->block_id ); ?>" class="<?php echo esc_attr( $classes ); ?>"> |
| 127 |
<fieldset> |
| 128 |
<!-- Hidden input to store the selected value for use by payment block and form submission --> |
| 129 |
<input |
| 130 |
type="hidden" |
| 131 |
class="sd-input-donation-amount-hidden sd-input-common" |
| 132 |
name="<?php echo esc_attr( $this->field_name ); ?>-value" |
| 133 |
data-slug="<?php echo esc_attr( $this->block_slug ? $this->block_slug : $this->unique_slug ); ?>" |
| 134 |
data-required="<?php echo esc_attr( $this->data_require_attr ); ?>" |
| 135 |
value="<?php echo esc_attr( $hidden_value ); ?>" |
| 136 |
/> |
| 137 |
<legend class="sd-block-legend"> |
| 138 |
<?php echo wp_kses_post( $this->label_markup ); ?> |
| 139 |
</legend> |
| 140 |
<?php echo wp_kses_post( $this->help_markup ); ?> |
| 141 |
<?php |
| 142 |
$show_custom_amount = $this->allow_custom_amount && 'radio' === $this->choice_type; |
| 143 |
?> |
| 144 |
<?php if ( ! empty( $this->options ) && is_array( $this->options ) ) { ?> |
| 145 |
<div class="<?php echo esc_attr( $wrap_class ); ?>" role="group" aria-labelledby="sd-label-<?php echo esc_attr( $this->block_id ); ?>"> |
| 146 |
<?php foreach ( $this->options as $index => $option ) { ?> |
| 147 |
<?php |
| 148 |
$option_label = $option['label'] ?? ''; |
| 149 |
$option_value = $option['value'] ?? $option_label; |
| 150 |
$option_id = $this->unique_slug . '-' . $index; |
| 151 |
$is_checked = $this->default === $option_value; |
| 152 |
$checked_attr = $is_checked ? 'checked' : ''; |
| 153 |
?> |
| 154 |
<div class="sd-donation-amount-single"> |
| 155 |
<input |
| 156 |
type="<?php echo esc_attr( $this->choice_type ); ?>" |
| 157 |
id="<?php echo esc_attr( $option_id ); ?>" |
| 158 |
name="<?php echo esc_attr( $input_name ); ?>" |
| 159 |
value="<?php echo esc_attr( $option_value ); ?>" |
| 160 |
class="sd-input-<?php echo esc_attr( $this->choice_type ); ?>" |
| 161 |
data-required="<?php echo esc_attr( $this->data_require_attr ); ?>" |
| 162 |
<?php echo esc_attr( $checked_attr ); ?> |
| 163 |
/> |
| 164 |
<div class="sd-block-content-wrap"> |
| 165 |
<div class="sd-option-container"> |
| 166 |
<label for="<?php echo esc_attr( $option_id ); ?>"><?php echo esc_html( $this->format_option_label( $option_label ) ); ?></label> |
| 167 |
</div> |
| 168 |
<div class="sd-icon-container"> |
| 169 |
<?php |
| 170 |
$allowed_svg = [ |
| 171 |
'svg' => [ |
| 172 |
'class' => true, |
| 173 |
'width' => true, |
| 174 |
'height' => true, |
| 175 |
'viewbox' => true, |
| 176 |
'fill' => true, |
| 177 |
'xmlns' => true, |
| 178 |
'aria-hidden' => true, |
| 179 |
], |
| 180 |
'circle' => [ |
| 181 |
'cx' => true, |
| 182 |
'cy' => true, |
| 183 |
'r' => true, |
| 184 |
'stroke' => true, |
| 185 |
'stroke-width' => true, |
| 186 |
'fill' => true, |
| 187 |
], |
| 188 |
'rect' => [ |
| 189 |
'x' => true, |
| 190 |
'y' => true, |
| 191 |
'width' => true, |
| 192 |
'height' => true, |
| 193 |
'rx' => true, |
| 194 |
'stroke' => true, |
| 195 |
'stroke-width' => true, |
| 196 |
], |
| 197 |
'path' => [ |
| 198 |
'd' => true, |
| 199 |
'stroke' => true, |
| 200 |
'stroke-width' => true, |
| 201 |
'stroke-linecap' => true, |
| 202 |
'stroke-linejoin' => true, |
| 203 |
], |
| 204 |
]; |
| 205 |
echo wp_kses( $checked_svg, $allowed_svg ); |
| 206 |
echo wp_kses( $unchecked_svg, $allowed_svg ); |
| 207 |
?> |
| 208 |
</div> |
| 209 |
</div> |
| 210 |
</div> |
| 211 |
<?php } ?> |
| 212 |
<?php if ( $show_custom_amount ) { ?> |
| 213 |
<div class="sd-donation-amount-single sd-donation-amount-custom"> |
| 214 |
<input |
| 215 |
type="number" |
| 216 |
id="sd-custom-amount-<?php echo esc_attr( $this->block_id ); ?>" |
| 217 |
class="sd-input-common sd-donation-amount-custom-input" |
| 218 |
data-slug="<?php echo esc_attr( $this->block_slug ? $this->block_slug : $this->unique_slug ); ?>" |
| 219 |
step="0.01" |
| 220 |
<?php if ( $this->custom_amount_min > 0 ) { ?> |
| 221 |
min="<?php echo esc_attr( (string) $this->custom_amount_min ); ?>" |
| 222 |
<?php } ?> |
| 223 |
<?php if ( $this->custom_amount_max > 0 ) { ?> |
| 224 |
max="<?php echo esc_attr( (string) $this->custom_amount_max ); ?>" |
| 225 |
<?php } ?> |
| 226 |
placeholder="<?php esc_attr_e( 'Enter custom amount', 'suredonation' ); ?>" |
| 227 |
aria-label="<?php esc_attr_e( 'Enter custom amount', 'suredonation' ); ?>" |
| 228 |
/> |
| 229 |
</div> |
| 230 |
<?php } ?> |
| 231 |
</div> |
| 232 |
<?php } ?> |
| 233 |
<div class="sd-error-wrap"><?php echo wp_kses_post( $this->error_msg_markup ); ?></div> |
| 234 |
</fieldset> |
| 235 |
</div> |
| 236 |
<?php |
| 237 |
return (string) ob_get_clean(); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Format an option label — if the label is purely numeric, prepend the |
| 242 |
* configured currency symbol. Custom (non-numeric) labels render as-is. |
| 243 |
* |
| 244 |
* @param string $label Raw option label. |
| 245 |
* @return string Display label. |
| 246 |
* @since 1.0.0 |
| 247 |
*/ |
| 248 |
private function format_option_label( $label ) { |
| 249 |
$trimmed = trim( $label ); |
| 250 |
|
| 251 |
if ( '' === $trimmed || ! is_numeric( $trimmed ) ) { |
| 252 |
return $label; |
| 253 |
} |
| 254 |
|
| 255 |
$currency = Payment_Helper::get_global_setting( 'currency', 'USD' ); |
| 256 |
$symbol = Payment_Helper::get_currency_symbol( is_string( $currency ) ? $currency : 'USD' ); |
| 257 |
|
| 258 |
// Position the symbol per the global setting while keeping the raw |
| 259 |
// value (presets intentionally show "$3", not "$3.00"). |
| 260 |
return Payment_Helper::position_currency_symbol( $symbol, $trimmed ); |
| 261 |
} |
| 262 |
} |
| 263 |
|