[], 'version' => BETTER_PAYMENT_VERSION ]; // Register editor script from compiled assets. $script_src = file_exists( $assets_dir . 'index.min.js' ) ? BETTER_PAYMENT_ASSETS . '/blocks/campaign-display/index.min.js' : null; if ( $script_src ) { wp_register_script( 'better-payment-campaign-display-editor', $script_src, $asset_file['dependencies'], $asset_file['version'], true ); } // Register frontend style from compiled assets. $style_src = file_exists( $assets_dir . 'style.min.css' ) ? BETTER_PAYMENT_ASSETS . '/blocks/campaign-display/style.min.css' : null; if ( $style_src ) { wp_register_style( 'better-payment-campaign-display-style', $style_src, [], $asset_file['version'] ); } // Use the assets/ copy of block.json, because src/ is excluded from the // release zip and from `git archive` (.distignore / .gitattributes). Reading // it only from src/ meant this block never registered in a distributed build. // Same resolution order as BlockManager::register_single_block(). $block_json = $root . '/assets/blocks/blocks/campaign-display/'; // Fall back to src/ in a dev checkout where the blocks build has not run. if ( ! file_exists( $block_json . 'block.json' ) ) { $block_json = $root . '/src/blocks/campaign-display/'; } if ( ! file_exists( $block_json . 'block.json' ) ) { return; } register_block_type( $block_json, [ 'render_callback' => [ $this, 'render' ], 'editor_script' => 'better-payment-campaign-display-editor', 'style' => 'better-payment-campaign-display-style', ] ); } /** * Server-side render callback. * * @param array $attributes Block attributes from block.json. * @return string */ public function render( array $attributes ): string { $campaign_id = absint( $attributes['campaignId'] ?? 0 ); if ( ! $campaign_id ) { return '
' . esc_html__( 'Please select a campaign.', 'better-payment' ) . '
'; } $post = get_post( $campaign_id ); if ( ! $post || $post->post_type !== 'bp_campaign' ) { return ''; } if ( ! wp_script_is( 'fundraising-campaign-script', 'enqueued' ) ) { wp_enqueue_script( 'fundraising-campaign-script' ); } return RendererService::render_campaign( $campaign_id ); } }