CampaignCommentsShortcode.php
3 days ago
CampaignDonationsShortcode.php
10 months ago
CampaignDonorsShortcode.php
10 months ago
CampaignFormShortcode.php
10 months ago
CampaignGoalShortcode.php
10 months ago
CampaignGridShortcode.php
10 months ago
CampaignShortcode.php
10 months ago
CampaignStatsShortcode.php
10 months ago
ShortcodeRenderController.php
10 months ago
CampaignStatsShortcode.php
79 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Campaigns\Shortcodes; |
| 4 | |
| 5 | use Give\Framework\Support\Facades\Scripts\ScriptAsset; |
| 6 | use Give\Helpers\Language; |
| 7 | |
| 8 | /** |
| 9 | * @since 4.7.0 |
| 10 | */ |
| 11 | class CampaignStatsShortcode |
| 12 | { |
| 13 | /** |
| 14 | * @since 4.7.0 updated to use ShortcodeRenderController |
| 15 | * @since 4.7.0 |
| 16 | * |
| 17 | * @param array<string, mixed> $atts |
| 18 | * |
| 19 | * @return string |
| 20 | */ |
| 21 | public function renderShortcode($atts): string |
| 22 | { |
| 23 | $this->loadAssets(); |
| 24 | $attributes = $this->parseAttributes($atts); |
| 25 | |
| 26 | $renderFile = GIVE_PLUGIN_DIR . 'src/Campaigns/Blocks/CampaignStats/render.php'; |
| 27 | |
| 28 | return ShortcodeRenderController::renderWithBlockContext( |
| 29 | $renderFile, |
| 30 | 'givewp/campaign-stats-block', |
| 31 | $attributes |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @since 4.7.0 |
| 37 | */ |
| 38 | public function loadAssets(): void |
| 39 | { |
| 40 | $handleName = 'givewp-campaign-stats-block-app'; |
| 41 | $asset = ScriptAsset::get(GIVE_PLUGIN_DIR . 'build/campaignStatsBlockApp.asset.php'); |
| 42 | |
| 43 | wp_enqueue_script( |
| 44 | $handleName, |
| 45 | GIVE_PLUGIN_URL . 'build/campaignStatsBlockApp.js', |
| 46 | $asset['dependencies'], |
| 47 | $asset['version'], |
| 48 | true |
| 49 | ); |
| 50 | |
| 51 | Language::setScriptTranslations($handleName); |
| 52 | |
| 53 | wp_enqueue_style( |
| 54 | $handleName, |
| 55 | GIVE_PLUGIN_URL . 'build/campaignStatsBlockApp.css', |
| 56 | [], |
| 57 | $asset['version'] |
| 58 | ); |
| 59 | |
| 60 | wp_enqueue_style('givewp-design-system-foundation'); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @since 4.7.0 |
| 65 | */ |
| 66 | private function parseAttributes($atts): array |
| 67 | { |
| 68 | $atts = shortcode_atts([ |
| 69 | 'campaign_id' => 0, |
| 70 | 'statistic' => 'top-donation', |
| 71 | ], $atts, 'givewp_campaign_stats'); |
| 72 | |
| 73 | return [ |
| 74 | 'campaignId' => (int) $atts['campaign_id'], |
| 75 | 'statistic' => $atts['statistic'], |
| 76 | ]; |
| 77 | } |
| 78 | } |
| 79 |