| 1 |
<?php |
| 2 |
/** |
| 3 |
* Donation Form Shortcode |
| 4 |
* |
| 5 |
* Renders a donation form via shortcode [suredonation_form id="123"] |
| 6 |
* |
| 7 |
* @package SureDonation |
| 8 |
* @since 0.0.1 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace SureDonation\Inc\Shortcodes; |
| 12 |
|
| 13 |
use SureDonation\Inc\Fields\Form_Renderer; |
| 14 |
use SureDonation\Inc\Helper; |
| 15 |
use SureDonation\Inc\Post_Types\Donation_Form as Donation_Form_CPT; |
| 16 |
use SureDonation\Inc\Traits\Get_Instance; |
| 17 |
|
| 18 |
// Exit if accessed directly. |
| 19 |
if ( ! defined( 'ABSPATH' ) ) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Donation_Form shortcode class. |
| 25 |
* |
| 26 |
* @since 0.0.1 |
| 27 |
*/ |
| 28 |
class Donation_Form { |
| 29 |
use Get_Instance; |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor. |
| 33 |
* |
| 34 |
* @since 0.0.1 |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
add_shortcode( 'suredonation_form', [ $this, 'render_shortcode' ] ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Render the donation form shortcode. |
| 42 |
* |
| 43 |
* @param array<string, mixed> $atts Shortcode attributes. |
| 44 |
* @return string Form HTML. |
| 45 |
* @since 0.0.1 |
| 46 |
*/ |
| 47 |
public function render_shortcode( $atts ) { |
| 48 |
$atts = shortcode_atts( |
| 49 |
[ |
| 50 |
'id' => 0, |
| 51 |
], |
| 52 |
$atts, |
| 53 |
'suredonation_form' |
| 54 |
); |
| 55 |
|
| 56 |
$form_id = absint( $atts['id'] ); |
| 57 |
|
| 58 |
if ( ! $form_id ) { |
| 59 |
if ( current_user_can( 'edit_posts' ) ) { |
| 60 |
return '<p class="suredonation-error">' . esc_html__( 'Please specify a form ID: [suredonation_form id="123"]', 'suredonation' ) . '</p>'; |
| 61 |
} |
| 62 |
return ''; |
| 63 |
} |
| 64 |
|
| 65 |
// Get the form post. |
| 66 |
$form = get_post( $form_id ); |
| 67 |
|
| 68 |
if ( ! $form || Donation_Form_CPT::POST_TYPE !== $form->post_type ) { |
| 69 |
if ( current_user_can( 'edit_posts' ) ) { |
| 70 |
return '<p class="suredonation-error">' . esc_html__( 'Invalid donation form ID.', 'suredonation' ) . '</p>'; |
| 71 |
} |
| 72 |
return ''; |
| 73 |
} |
| 74 |
|
| 75 |
if ( 'publish' !== $form->post_status && ! current_user_can( 'edit_posts' ) ) { |
| 76 |
return ''; |
| 77 |
} |
| 78 |
|
| 79 |
// Enqueue frontend assets. |
| 80 |
$this->enqueue_assets( $form_id, $form ); |
| 81 |
|
| 82 |
// Render the form. |
| 83 |
return $this->render_form( $form ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Render the form HTML. |
| 88 |
* |
| 89 |
* @param \WP_Post $form Form post object. |
| 90 |
* @return string Form HTML. |
| 91 |
* @since 0.0.1 |
| 92 |
*/ |
| 93 |
private function render_form( $form ) { |
| 94 |
$campaign_id = Donation_Form_CPT::get_form_campaign_id( $form->ID ); |
| 95 |
|
| 96 |
// Shared markup (also used by the donation-form block). |
| 97 |
return Form_Renderer::render( $form, $campaign_id ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Enqueue frontend assets. |
| 102 |
* |
| 103 |
* @param int $form_id Form post ID. |
| 104 |
* @param \WP_Post $form Form post object. |
| 105 |
* @return void |
| 106 |
* @since 0.0.1 |
| 107 |
*/ |
| 108 |
private function enqueue_assets( $form_id, $form ) { |
| 109 |
// Enqueue the shared compiled frontend stylesheet (same handle the block uses). |
| 110 |
wp_enqueue_style( 'suredonation-donation-form' ); |
| 111 |
|
| 112 |
// Enqueue frontend script (registered globally in Assets\Register). |
| 113 |
wp_enqueue_script( 'suredonation-form-frontend' ); |
| 114 |
|
| 115 |
// Localize script with payment and success message configuration. |
| 116 |
wp_localize_script( |
| 117 |
'suredonation-form-frontend', |
| 118 |
'suredonationPayment', |
| 119 |
Helper::get_form_payment_settings( $form_id ) |
| 120 |
); |
| 121 |
|
| 122 |
// Enqueue Stripe.js if Stripe is configured. |
| 123 |
$this->maybe_enqueue_stripe(); |
| 124 |
|
| 125 |
// Allow payment gateway extensions to enqueue their scripts (e.g., PayPal SDK). |
| 126 |
do_action( 'suredonation_enqueue_form_frontend_scripts', $form_id, $form->post_content ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Enqueue Stripe.js if Stripe is configured. |
| 131 |
* |
| 132 |
* @return void |
| 133 |
* @since 0.0.1 |
| 134 |
*/ |
| 135 |
private function maybe_enqueue_stripe() { |
| 136 |
if ( ! class_exists( 'SureDonation\Inc\Payments\Stripe\Stripe_Helper' ) ) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
$publishable_key = \SureDonation\Inc\Payments\Stripe\Stripe_Helper::get_stripe_publishable_key(); |
| 141 |
|
| 142 |
if ( ! empty( $publishable_key ) ) { |
| 143 |
wp_enqueue_script( |
| 144 |
'stripe-js', |
| 145 |
'https://js.stripe.com/v3/', |
| 146 |
[], |
| 147 |
null, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- External script. |
| 148 |
true |
| 149 |
); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|