PluginProbe
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management / 1.6.1
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management v1.6.1
1.6.1 1.6.0 1.5.1 1.5.0 1.4.0 1.3.0 trunk 0.0.1 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0
suredonation / inc / shortcodes / donation-form.php

donation-form.php in SureDonation – Donation Forms, Fundraising Campaigns & Donor Management 1.6.1, at inc/shortcodes/donation-form.php

169 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Fields\Form_Styling;
15 use SureDonation\Inc\Helper;
16 use SureDonation\Inc\Post_Types\Donation_Form as Donation_Form_CPT;
17 use SureDonation\Inc\Traits\Get_Instance;
18
19 // Exit if accessed directly.
20 if ( ! defined( 'ABSPATH' ) ) {
21 exit;
22 }
23
24 /**
25 * Donation_Form shortcode class.
26 *
27 * @since 0.0.1
28 */
29 class Donation_Form {
30 use Get_Instance;
31
32 /**
33 * Constructor.
34 *
35 * @since 0.0.1
36 */
37 public function __construct() {
38 add_shortcode( 'suredonation_form', [ $this, 'render_shortcode' ] );
39 }
40
41 /**
42 * Render the donation form shortcode.
43 *
44 * @param array<string, mixed> $atts Shortcode attributes.
45 * @return string Form HTML.
46 * @since 0.0.1
47 */
48 public function render_shortcode( $atts ) {
49 $atts = shortcode_atts(
50 [
51 'id' => 0,
52 ],
53 $atts,
54 'suredonation_form'
55 );
56
57 $form_id = absint( $atts['id'] );
58
59 if ( ! $form_id ) {
60 if ( current_user_can( 'edit_posts' ) ) {
61 return '<p class="suredonation-error">' . esc_html__( 'Please specify a form ID: [suredonation_form id="123"]', 'suredonation' ) . '</p>';
62 }
63 return '';
64 }
65
66 // Get the form post.
67 $form = get_post( $form_id );
68
69 if ( ! $form || Donation_Form_CPT::POST_TYPE !== $form->post_type ) {
70 if ( current_user_can( 'edit_posts' ) ) {
71 return '<p class="suredonation-error">' . esc_html__( 'Invalid donation form ID.', 'suredonation' ) . '</p>';
72 }
73 return '';
74 }
75
76 if ( 'publish' !== $form->post_status && ! current_user_can( 'edit_posts' ) ) {
77 return '';
78 }
79
80 // Enqueue frontend assets.
81 $this->enqueue_assets( $form_id, $form );
82
83 // Render the form.
84 return $this->render_form( $form );
85 }
86
87 /**
88 * Render the form HTML.
89 *
90 * @param \WP_Post $form Form post object.
91 * @return string Form HTML.
92 * @since 0.0.1
93 */
94 private function render_form( $form ) {
95 $campaign_id = Donation_Form_CPT::get_form_campaign_id( $form->ID );
96
97 // Shared markup (also used by the donation-form block).
98 return Form_Renderer::render( $form, $campaign_id );
99 }
100
101 /**
102 * Enqueue frontend assets.
103 *
104 * @param int $form_id Form post ID.
105 * @param \WP_Post $form Form post object.
106 * @return void
107 * @since 0.0.1
108 */
109 private function enqueue_assets( $form_id, $form ) {
110 // Enqueue the shared compiled frontend stylesheet (same handle the block
111 // uses). Skipped when the form has default styling disabled — the site's
112 // own CSS then controls the appearance. Scripts are always loaded so
113 // payment and validation keep working. The enqueue is additive across
114 // forms on a page, so a styled form still loads the stylesheet.
115 if ( ! Form_Styling::is_default_styling_disabled( $form_id ) ) {
116 wp_enqueue_style( 'suredonation-donation-form' );
117 }
118
119 // Enqueue frontend script (registered globally in Assets\Register).
120 wp_enqueue_script( 'suredonation-form-frontend' );
121
122 // Localize script with payment and success message configuration.
123 wp_localize_script(
124 'suredonation-form-frontend',
125 'suredonationPayment',
126 Helper::get_form_payment_settings( $form_id )
127 );
128
129 // Expose the resolved validation messages so client-side validation
130 // mirrors the server's configured messages (matches the block render).
131 wp_localize_script(
132 'suredonation-form-frontend',
133 'suredonationValidationMessages',
134 \SureDonation\Inc\Field_Validation::get_resolved_validation_messages()
135 );
136
137 // Enqueue Stripe.js if Stripe is configured.
138 $this->maybe_enqueue_stripe();
139
140 // Allow payment gateway extensions to enqueue their scripts and localize
141 // their frontend configuration (see the action's docblock in block.php).
142 do_action( 'suredonation_enqueue_form_frontend_scripts', $form_id, $form->post_content );
143 }
144
145 /**
146 * Enqueue Stripe.js if Stripe is configured.
147 *
148 * @return void
149 * @since 0.0.1
150 */
151 private function maybe_enqueue_stripe() {
152 if ( ! class_exists( 'SureDonation\Inc\Payments\Stripe\Stripe_Helper' ) ) {
153 return;
154 }
155
156 // Load Stripe.js when any connected account exists; the per-form
157 // publishable key is passed to the frontend via the payment block markup.
158 if ( \SureDonation\Inc\Payments\Stripe\Stripe_Helper::is_stripe_connected() ) {
159 wp_enqueue_script(
160 'stripe-js',
161 'https://js.stripe.com/v3/',
162 [],
163 null, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- External script.
164 true
165 );
166 }
167 }
168 }
169