| 1 |
<?php |
| 2 |
/** |
| 3 |
* PHP render for the Campaign Donors block. |
| 4 |
* |
| 5 |
* @package SureDonation |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SureDonation\Inc\Blocks\Campaign_Donors; |
| 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 Donors 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['donorsToShow'] ) ? absint( $attributes['donorsToShow'] ) : 5; |
| 45 |
$limit = max( 1, $limit ); |
| 46 |
$donors = Campaign_Stats::get_cached_top_donors( $campaign_id, $limit ); |
| 47 |
|
| 48 |
if ( ! is_array( $donors ) ) { |
| 49 |
$donors = []; |
| 50 |
} |
| 51 |
|
| 52 |
$wrapper = get_block_wrapper_attributes( [ 'class' => 'suredonation-campaign-donors' ] ); |
| 53 |
|
| 54 |
ob_start(); |
| 55 |
?> |
| 56 |
<?php |
| 57 |
$show_button = ! isset( $attributes['showButton'] ) || $attributes['showButton']; |
| 58 |
$button_text = ! empty( $attributes['buttonText'] ) ? (string) $attributes['buttonText'] : __( 'Join the list', 'suredonation' ); |
| 59 |
?> |
| 60 |
<div <?php echo $wrapper; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- get_block_wrapper_attributes() returns escaped markup. ?>> |
| 61 |
<div class="suredonation-campaign-donors__header"> |
| 62 |
<h2 class="suredonation-campaign-donors__title"><?php esc_html_e( 'Top Donors', 'suredonation' ); ?></h2> |
| 63 |
<?php if ( $show_button ) : ?> |
| 64 |
<a class="suredonation-campaign-donors__donate-link" href="#<?php echo esc_attr( Campaign_Page::FORM_ANCHOR ); ?>"><?php echo esc_html( $button_text ); ?></a> |
| 65 |
<?php endif; ?> |
| 66 |
</div> |
| 67 |
<?php if ( empty( $donors ) ) : ?> |
| 68 |
<p class="suredonation-campaign-donors__empty"><?php esc_html_e( 'No donors yet.', 'suredonation' ); ?></p> |
| 69 |
<?php else : ?> |
| 70 |
<ul class="suredonation-campaign-donors__list"> |
| 71 |
<?php |
| 72 |
$rank = 0; |
| 73 |
foreach ( $donors as $donor ) : |
| 74 |
++$rank; |
| 75 |
$name = ! empty( $donor['donor_name'] ) ? $donor['donor_name'] : __( 'Anonymous', 'suredonation' ); |
| 76 |
$total = Payment_Helper::format_amount( $donor['total_donated'] ?? 0 ); |
| 77 |
// Top donors are non-anonymous (filtered in the query). |
| 78 |
$avatar_url = Campaign_Page::donor_avatar_url( $donor['donor_email'] ?? '', false ); |
| 79 |
?> |
| 80 |
<li class="suredonation-campaign-donors__item"> |
| 81 |
<?php if ( $avatar_url ) : ?> |
| 82 |
<div class="suredonation-campaign-donors__avatar"> |
| 83 |
<img src="<?php echo esc_url( $avatar_url ); ?>" alt="" loading="lazy" /> |
| 84 |
</div> |
| 85 |
<?php endif; ?> |
| 86 |
<div class="suredonation-campaign-donors__info"> |
| 87 |
<span class="suredonation-campaign-donors__name"><?php echo esc_html( $name ); ?></span> |
| 88 |
<?php if ( $rank <= 3 ) : ?> |
| 89 |
<span class="suredonation-campaign-donors__ribbon" data-position="<?php echo esc_attr( (string) $rank ); ?>" aria-hidden="true"> |
| 90 |
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="8" r="6"></circle><path d="M15.477 12.89 17 22l-5-3-5 3 1.523-9.11"></path></svg> |
| 91 |
</span> |
| 92 |
<?php endif; ?> |
| 93 |
</div> |
| 94 |
<span class="suredonation-campaign-donors__amount"><?php echo esc_html( $total ); ?></span> |
| 95 |
</li> |
| 96 |
<?php endforeach; ?> |
| 97 |
</ul> |
| 98 |
<?php endif; ?> |
| 99 |
</div> |
| 100 |
<?php |
| 101 |
$output = ob_get_clean(); |
| 102 |
|
| 103 |
return false !== $output ? $output : ''; |
| 104 |
} |
| 105 |
} |
| 106 |
|