PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 2.3.4
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v2.3.4
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.2 2.2.1 2.2.0 2.1.2 2.1.1 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 All 66 releases
better-payment / includes / Campaign / CampaignBlock.php

CampaignBlock.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 2.3.4, at includes/Campaign/CampaignBlock.php

110 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Better_Payment\Lite\Campaign;
4
5 use Better_Payment\Lite\Controller;
6 use Better_Payment\Lite\Campaign\Services\RendererService;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Registers the better-payment/campaign-display Gutenberg block.
14 * Server-side rendered — delegates to RendererService::render_campaign().
15 * Follows the same manual asset registration pattern as BlockManager.
16 */
17 class CampaignBlock extends Controller {
18
19 public function register() {
20 if ( ! function_exists( 'register_block_type' ) ) {
21 return;
22 }
23
24 $root = BETTER_PAYMENT_PATH;
25 $assets_dir = $root . '/assets/blocks/campaign-display/';
26
27 // Load dependency manifest generated by the blocks webpack build.
28 $asset_file = file_exists( $assets_dir . 'index.min.asset.php' )
29 ? require $assets_dir . 'index.min.asset.php'
30 : [ 'dependencies' => [], 'version' => BETTER_PAYMENT_VERSION ];
31
32 // Register editor script from compiled assets.
33 $script_src = file_exists( $assets_dir . 'index.min.js' )
34 ? BETTER_PAYMENT_ASSETS . '/blocks/campaign-display/index.min.js'
35 : null;
36
37 if ( $script_src ) {
38 wp_register_script(
39 'better-payment-campaign-display-editor',
40 $script_src,
41 $asset_file['dependencies'],
42 $asset_file['version'],
43 true
44 );
45 }
46
47 // Register frontend style from compiled assets.
48 $style_src = file_exists( $assets_dir . 'style.min.css' )
49 ? BETTER_PAYMENT_ASSETS . '/blocks/campaign-display/style.min.css'
50 : null;
51
52 if ( $style_src ) {
53 wp_register_style(
54 'better-payment-campaign-display-style',
55 $style_src,
56 [],
57 $asset_file['version']
58 );
59 }
60
61 // Use the assets/ copy of block.json, because src/ is excluded from the
62 // release zip and from `git archive` (.distignore / .gitattributes). Reading
63 // it only from src/ meant this block never registered in a distributed build.
64 // Same resolution order as BlockManager::register_single_block().
65 $block_json = $root . '/assets/blocks/blocks/campaign-display/';
66
67 // Fall back to src/ in a dev checkout where the blocks build has not run.
68 if ( ! file_exists( $block_json . 'block.json' ) ) {
69 $block_json = $root . '/src/blocks/campaign-display/';
70 }
71
72 if ( ! file_exists( $block_json . 'block.json' ) ) {
73 return;
74 }
75
76 register_block_type(
77 $block_json,
78 [
79 'render_callback' => [ $this, 'render' ],
80 'editor_script' => 'better-payment-campaign-display-editor',
81 'style' => 'better-payment-campaign-display-style',
82 ]
83 );
84 }
85
86 /**
87 * Server-side render callback.
88 *
89 * @param array $attributes Block attributes from block.json.
90 * @return string
91 */
92 public function render( array $attributes ): string {
93 $campaign_id = absint( $attributes['campaignId'] ?? 0 );
94 if ( ! $campaign_id ) {
95 return '<p class="bp-campaign-placeholder">' . esc_html__( 'Please select a campaign.', 'better-payment' ) . '</p>';
96 }
97
98 $post = get_post( $campaign_id );
99 if ( ! $post || $post->post_type !== 'bp_campaign' ) {
100 return '';
101 }
102
103 if ( ! wp_script_is( 'fundraising-campaign-script', 'enqueued' ) ) {
104 wp_enqueue_script( 'fundraising-campaign-script' );
105 }
106
107 return RendererService::render_campaign( $campaign_id );
108 }
109 }
110