| 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 |
* [bp_campaign id="42"] shortcode — renders a campaign on the frontend. |
| 14 |
* Rendering is delegated to RendererService, which supports both the |
| 15 |
* column-based layout schema and the legacy flat-array format. |
| 16 |
*/ |
| 17 |
class Shortcode extends Controller { |
| 18 |
|
| 19 |
public function register() { |
| 20 |
add_shortcode( 'bp_campaign', [ $this, 'render' ] ); |
| 21 |
} |
| 22 |
|
| 23 |
public function render( $atts ): string { |
| 24 |
$atts = shortcode_atts( [ 'id' => 0 ], $atts, 'bp_campaign' ); |
| 25 |
|
| 26 |
$campaign_id = absint( $atts['id'] ); |
| 27 |
if ( ! $campaign_id ) { |
| 28 |
return ''; |
| 29 |
} |
| 30 |
|
| 31 |
$post = get_post( $campaign_id ); |
| 32 |
if ( ! $post || $post->post_type !== 'bp_campaign' || $post->post_status !== 'publish' ) { |
| 33 |
return ''; |
| 34 |
} |
| 35 |
|
| 36 |
$this->enqueue_frontend_styles(); |
| 37 |
$this->enqueue_frontend_scripts(); |
| 38 |
|
| 39 |
return RendererService::render_campaign( $campaign_id ); |
| 40 |
} |
| 41 |
|
| 42 |
private function enqueue_frontend_scripts(): void { |
| 43 |
if ( wp_script_is( 'fundraising-campaign-script', 'enqueued' ) ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
wp_enqueue_script( 'fundraising-campaign-script' ); |
| 47 |
} |
| 48 |
|
| 49 |
private function enqueue_frontend_styles(): void { |
| 50 |
$handle = 'better-payment-campaign-display-style'; |
| 51 |
$css_file = BETTER_PAYMENT_PATH . '/assets/blocks/campaign-display/style.min.css'; |
| 52 |
|
| 53 |
if ( ! wp_style_is( $handle, 'registered' ) && file_exists( $css_file ) ) { |
| 54 |
wp_register_style( |
| 55 |
$handle, |
| 56 |
BETTER_PAYMENT_ASSETS . '/blocks/campaign-display/style.min.css', |
| 57 |
[], |
| 58 |
filemtime( $css_file ) |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
if ( ! wp_style_is( $handle, 'enqueued' ) ) { |
| 63 |
wp_enqueue_style( $handle ); |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|