| 1 |
<?php |
| 2 |
/** |
| 3 |
* PHP render for the Campaign Donations block. |
| 4 |
* |
| 5 |
* @package SureDonation |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SureDonation\Inc\Blocks\Campaign_Donations; |
| 10 |
|
| 11 |
use SureDonation\Inc\Blocks\Base; |
| 12 |
use SureDonation\Inc\Campaigns\Campaign_Page; |
| 13 |
use SureDonation\Inc\Campaigns\Campaign_Stats; |
| 14 |
use SureDonation\Inc\Payments\Payment_Helper; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; // Exit if accessed directly. |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Campaign Donations block. |
| 22 |
* |
| 23 |
* @since 1.0.0 |
| 24 |
*/ |
| 25 |
class Block extends Base { |
| 26 |
/** |
| 27 |
* Render the block. |
| 28 |
* |
| 29 |
* @param array<string, mixed> $attributes Block attributes. |
| 30 |
* @param string $content Block content. |
| 31 |
* @return string |
| 32 |
* @since 1.0.0 |
| 33 |
*/ |
| 34 |
public function render( $attributes, $content = '' ) { |
| 35 |
unset( $content ); |
| 36 |
|
| 37 |
$campaign_id = Campaign_Page::resolve_campaign_id( $attributes ); |
| 38 |
if ( ! $campaign_id ) { |
| 39 |
return ''; |
| 40 |
} |
| 41 |
|
| 42 |
wp_enqueue_style( 'suredonation-campaign-blocks' ); |
| 43 |
|
| 44 |
$limit = isset( $attributes['donationsToShow'] ) ? absint( $attributes['donationsToShow'] ) : 5; |
| 45 |
$limit = max( 1, $limit ); |
| 46 |
$show_anon = ! isset( $attributes['showAnonymous'] ) || $attributes['showAnonymous']; |
| 47 |
|
| 48 |
// Over-fetch when anonymous donations are hidden so the visible list can |
| 49 |
// still fill even if many recent donations are anonymous. Capped to keep |
| 50 |
// the query bounded. |
| 51 |
$fetch = $show_anon ? $limit : min( $limit * 5, 100 ); |
| 52 |
$donations = Campaign_Stats::get_cached_recent_donations( $campaign_id, $fetch ); |
| 53 |
|
| 54 |
if ( ! is_array( $donations ) ) { |
| 55 |
$donations = []; |
| 56 |
} |
| 57 |
|
| 58 |
if ( ! $show_anon ) { |
| 59 |
$donations = array_filter( |
| 60 |
$donations, |
| 61 |
static function ( $donation ) { |
| 62 |
return empty( $donation['is_anonymous'] ); |
| 63 |
} |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
$donations = array_slice( $donations, 0, $limit ); |
| 68 |
|
| 69 |
$wrapper = get_block_wrapper_attributes( [ 'class' => 'suredonation-campaign-donations' ] ); |
| 70 |
|
| 71 |
ob_start(); |
| 72 |
?> |
| 73 |
<?php |
| 74 |
$show_button = ! isset( $attributes['showButton'] ) || $attributes['showButton']; |
| 75 |
$button_text = ! empty( $attributes['buttonText'] ) ? (string) $attributes['buttonText'] : __( 'Donate', 'suredonation' ); |
| 76 |
?> |
| 77 |
<div <?php echo $wrapper; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- get_block_wrapper_attributes() returns escaped markup. ?>> |
| 78 |
<div class="suredonation-campaign-donations__header"> |
| 79 |
<h2 class="suredonation-campaign-donations__title"><?php esc_html_e( 'Recent Donations', 'suredonation' ); ?></h2> |
| 80 |
<?php if ( $show_button ) : ?> |
| 81 |
<a class="suredonation-campaign-donations__donate-link" href="#<?php echo esc_attr( Campaign_Page::FORM_ANCHOR ); ?>"><?php echo esc_html( $button_text ); ?></a> |
| 82 |
<?php endif; ?> |
| 83 |
</div> |
| 84 |
<?php if ( empty( $donations ) ) : ?> |
| 85 |
<p class="suredonation-campaign-donations__empty"><?php esc_html_e( 'Be the first to donate.', 'suredonation' ); ?></p> |
| 86 |
<?php else : ?> |
| 87 |
<ul class="suredonation-campaign-donations__list"> |
| 88 |
<?php foreach ( $donations as $donation ) : ?> |
| 89 |
<?php |
| 90 |
$is_anon = ! empty( $donation['is_anonymous'] ); |
| 91 |
$name = $is_anon ? __( 'Anonymous', 'suredonation' ) : ( $donation['donor_name'] ?? __( 'Anonymous', 'suredonation' ) ); |
| 92 |
$amount = Payment_Helper::format_amount( $donation['amount'] ?? 0 ); |
| 93 |
$time = ! empty( $donation['created_at'] ) ? strtotime( (string) $donation['created_at'] ) : false; |
| 94 |
$avatar_url = Campaign_Page::donor_avatar_url( $donation['donor_email'] ?? '', $is_anon ); |
| 95 |
?> |
| 96 |
<li class="suredonation-campaign-donations__item"> |
| 97 |
<?php if ( $avatar_url ) : ?> |
| 98 |
<div class="suredonation-campaign-donations__avatar"> |
| 99 |
<img src="<?php echo esc_url( $avatar_url ); ?>" alt="" loading="lazy" /> |
| 100 |
</div> |
| 101 |
<?php endif; ?> |
| 102 |
<div class="suredonation-campaign-donations__info"> |
| 103 |
<div class="suredonation-campaign-donations__description"> |
| 104 |
<?php |
| 105 |
echo wp_kses_post( |
| 106 |
sprintf( |
| 107 |
/* translators: 1: donor name, 2: donation amount. */ |
| 108 |
__( '%1$s donated %2$s', 'suredonation' ), |
| 109 |
'<strong>' . esc_html( $name ) . '</strong>', |
| 110 |
'<strong>' . esc_html( $amount ) . '</strong>' |
| 111 |
) |
| 112 |
); |
| 113 |
?> |
| 114 |
</div> |
| 115 |
<?php if ( $time ) : ?> |
| 116 |
<span class="suredonation-campaign-donations__date"> |
| 117 |
<?php |
| 118 |
/* translators: %s: human-readable time difference, e.g. "3 weeks". */ |
| 119 |
printf( esc_html__( '%s ago', 'suredonation' ), esc_html( human_time_diff( $time ) ) ); |
| 120 |
?> |
| 121 |
</span> |
| 122 |
<?php endif; ?> |
| 123 |
</div> |
| 124 |
</li> |
| 125 |
<?php endforeach; ?> |
| 126 |
</ul> |
| 127 |
<?php endif; ?> |
| 128 |
</div> |
| 129 |
<?php |
| 130 |
$output = ob_get_clean(); |
| 131 |
|
| 132 |
return false !== $output ? $output : ''; |
| 133 |
} |
| 134 |
} |
| 135 |
|