PluginProbe
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management / 1.6.1
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management v1.6.1
1.6.1 1.6.0 1.5.1 1.5.0 1.4.0 1.3.0 trunk 0.0.1 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0
suredonation / inc / blocks / campaign-stats / block.php

block.php in SureDonation – Donation Forms, Fundraising Campaigns & Donor Management 1.6.1, at inc/blocks/campaign-stats/block.php

102 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PHP render for the Campaign Statistic block.
4 *
5 * @package SureDonation
6 * @since 1.0.0
7 */
8
9 namespace SureDonation\Inc\Blocks\Campaign_Stats;
10
11 use SureDonation\Inc\Blocks\Base;
12 use SureDonation\Inc\Campaigns\Campaign_Page;
13 use SureDonation\Inc\Campaigns\Campaign_Stats as Stats;
14 use SureDonation\Inc\Payments\Payment_Helper;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit; // Exit if accessed directly.
18 }
19
20 /**
21 * Campaign Statistic 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 $statistic = isset( $attributes['statistic'] ) ? (string) $attributes['statistic'] : 'average-donation';
45 $stats = Stats::get_cached_stats( $campaign_id );
46
47 $config = self::get_statistic_config( $statistic, $stats );
48
49 $wrapper = get_block_wrapper_attributes( [ 'class' => 'suredonation-campaign-stat suredonation-campaign-stat--' . sanitize_html_class( $statistic ) ] );
50
51 ob_start();
52 ?>
53 <div <?php echo $wrapper; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- get_block_wrapper_attributes() returns escaped markup. ?>>
54 <span class="suredonation-campaign-stat__value"><?php echo esc_html( $config['value'] ); ?></span>
55 <span class="suredonation-campaign-stat__label"><?php echo esc_html( $config['label'] ); ?></span>
56 </div>
57 <?php
58 $output = ob_get_clean();
59
60 return false !== $output ? $output : '';
61 }
62
63 /**
64 * Resolve the display value and label for a given statistic.
65 *
66 * @param string $statistic Statistic key.
67 * @param array<string, mixed> $stats Campaign stats.
68 * @return array{value:string,label:string}
69 * @since 1.0.0
70 */
71 private static function get_statistic_config( $statistic, $stats ) {
72 switch ( $statistic ) {
73 case 'total-raised':
74 return [
75 'value' => Payment_Helper::format_amount( $stats['total_raised'] ),
76 'label' => __( 'Total Raised', 'suredonation' ),
77 ];
78 case 'top-donation':
79 return [
80 'value' => Payment_Helper::format_amount( $stats['largest_donation'] ),
81 'label' => __( 'Top Donation', 'suredonation' ),
82 ];
83 case 'donor-count':
84 return [
85 'value' => number_format_i18n( $stats['donor_count'] ),
86 'label' => __( 'Donors', 'suredonation' ),
87 ];
88 case 'donation-count':
89 return [
90 'value' => number_format_i18n( $stats['donation_count'] ),
91 'label' => __( 'Donations', 'suredonation' ),
92 ];
93 case 'average-donation':
94 default:
95 return [
96 'value' => Payment_Helper::format_amount( $stats['average_donation'] ),
97 'label' => __( 'Average Donation', 'suredonation' ),
98 ];
99 }
100 }
101 }
102