| 1 |
<?php |
| 2 |
/** |
| 3 |
* PHP render for the Campaign Goal block. |
| 4 |
* |
| 5 |
* @package SureDonation |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SureDonation\Inc\Blocks\Campaign_Goal; |
| 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 Goal 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 |
$stats = Campaign_Stats::get_cached_stats( $campaign_id ); |
| 45 |
$is_count = 'donation_count' === $stats['goal_type']; |
| 46 |
|
| 47 |
$raised_value = $is_count |
| 48 |
? number_format_i18n( $stats['donation_count'] ) |
| 49 |
: Payment_Helper::format_amount( $stats['total_raised'] ); |
| 50 |
|
| 51 |
$goal_value = $is_count |
| 52 |
? number_format_i18n( $stats['goal_amount'] ) |
| 53 |
: Payment_Helper::format_amount( $stats['goal_amount'] ); |
| 54 |
|
| 55 |
$percentage = (float) $stats['progress_percentage']; |
| 56 |
$show_progress = ! isset( $attributes['showProgressBar'] ) || $attributes['showProgressBar']; |
| 57 |
|
| 58 |
$wrapper = get_block_wrapper_attributes( [ 'class' => 'suredonation-campaign-goal' ] ); |
| 59 |
|
| 60 |
ob_start(); |
| 61 |
?> |
| 62 |
<div <?php echo $wrapper; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- get_block_wrapper_attributes() returns escaped markup. ?>> |
| 63 |
<div class="suredonation-campaign-goal__amounts"> |
| 64 |
<span class="suredonation-campaign-goal__raised"><?php echo esc_html( $raised_value ); ?></span> |
| 65 |
<span class="suredonation-campaign-goal__goal"> |
| 66 |
<?php |
| 67 |
/* translators: %s: formatted goal amount or count. */ |
| 68 |
printf( esc_html__( 'raised of %s goal', 'suredonation' ), esc_html( $goal_value ) ); |
| 69 |
?> |
| 70 |
</span> |
| 71 |
</div> |
| 72 |
<?php if ( $show_progress ) : ?> |
| 73 |
<div class="suredonation-campaign-goal__bar" role="progressbar" aria-valuenow="<?php echo esc_attr( (string) round( $percentage ) ); ?>" aria-valuemin="0" aria-valuemax="100"> |
| 74 |
<span class="suredonation-campaign-goal__bar-fill" style="width:<?php echo esc_attr( (string) min( 100, $percentage ) ); ?>%"></span> |
| 75 |
</div> |
| 76 |
<?php endif; ?> |
| 77 |
<div class="suredonation-campaign-goal__meta"> |
| 78 |
<span><?php echo esc_html( number_format_i18n( $stats['donor_count'] ) ); ?> <?php echo esc_html( _n( 'donor', 'donors', (int) $stats['donor_count'], 'suredonation' ) ); ?></span> |
| 79 |
<span><?php echo esc_html( (string) round( $percentage ) ); ?>%</span> |
| 80 |
</div> |
| 81 |
</div> |
| 82 |
<?php |
| 83 |
$output = ob_get_clean(); |
| 84 |
|
| 85 |
return false !== $output ? $output : ''; |
| 86 |
} |
| 87 |
} |
| 88 |
|