PluginProbe
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management / 0.0.1
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management v0.0.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 0.0.1, at inc/shortcodes/donation-form.php

204 lines 5.2 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\Helper;
14 use SureDonation\Inc\Post_Types\Donation_Form as Donation_Form_CPT;
15 use SureDonation\Inc\Traits\Get_Instance;
16
17 // Exit if accessed directly.
18 if ( ! defined( 'ABSPATH' ) ) {
19 exit;
20 }
21
22 /**
23 * Donation_Form shortcode class.
24 *
25 * @since 0.0.1
26 */
27 class Donation_Form {
28 use Get_Instance;
29
30 /**
31 * Constructor.
32 *
33 * @since 0.0.1
34 */
35 public function __construct() {
36 add_shortcode( 'suredonation_form', [ $this, 'render_shortcode' ] );
37 }
38
39 /**
40 * Render the donation form shortcode.
41 *
42 * @param array<string, mixed> $atts Shortcode attributes.
43 * @return string Form HTML.
44 * @since 0.0.1
45 */
46 public function render_shortcode( $atts ) {
47 $atts = shortcode_atts(
48 [
49 'id' => 0,
50 ],
51 $atts,
52 'suredonation_form'
53 );
54
55 $form_id = absint( $atts['id'] );
56
57 if ( ! $form_id ) {
58 if ( current_user_can( 'edit_posts' ) ) {
59 return '<p class="suredonation-error">' . esc_html__( 'Please specify a form ID: [suredonation_form id="123"]', 'suredonation' ) . '</p>';
60 }
61 return '';
62 }
63
64 // Get the form post.
65 $form = get_post( $form_id );
66
67 if ( ! $form || Donation_Form_CPT::POST_TYPE !== $form->post_type ) {
68 if ( current_user_can( 'edit_posts' ) ) {
69 return '<p class="suredonation-error">' . esc_html__( 'Invalid donation form ID.', 'suredonation' ) . '</p>';
70 }
71 return '';
72 }
73
74 if ( 'publish' !== $form->post_status && ! current_user_can( 'edit_posts' ) ) {
75 return '';
76 }
77
78 // Enqueue frontend assets.
79 $this->enqueue_assets();
80
81 // Render the form.
82 return $this->render_form( $form );
83 }
84
85 /**
86 * Render the form HTML.
87 *
88 * @param \WP_Post $form Form post object.
89 * @return string Form HTML.
90 * @since 0.0.1
91 */
92 private function render_form( $form ) {
93 $form_id = $form->ID;
94 $campaign_id = Donation_Form_CPT::get_form_campaign_id( $form_id );
95
96 // Parse the form content (blocks).
97 $blocks = parse_blocks( $form->post_content );
98
99 // Render blocks.
100 $blocks_html = '';
101 foreach ( $blocks as $block ) {
102 if ( ! empty( $block['blockName'] ) ) {
103 // Add form ID to block attributes.
104 $block['attrs']['formId'] = $form_id;
105 $blocks_html .= render_block( $block );
106 }
107 }
108
109 // Generate unique form ID for the container.
110 $unique_form_id = 'suredonation-form-' . $form_id . '-' . wp_rand();
111
112 // Nonce action based on campaign.
113 $nonce_action = $campaign_id ? 'suredonation_donation_' . $campaign_id : 'suredonation_donation_standalone';
114
115 ob_start();
116 ?>
117 <div id="<?php echo esc_attr( $unique_form_id ); ?>" class="sd-form-container" data-form-id="<?php echo esc_attr( (string) $form_id ); ?>" data-campaign-id="<?php echo esc_attr( (string) $campaign_id ); ?>">
118 <form class="sd-form" method="post">
119 <?php wp_nonce_field( $nonce_action, 'suredonation_nonce' ); ?>
120 <input type="hidden" name="form_id" value="<?php echo esc_attr( (string) $form_id ); ?>" />
121 <input type="hidden" name="campaign_id" value="<?php echo esc_attr( (string) $campaign_id ); ?>" />
122 <input type="hidden" name="action" value="suredonation_submit_donation" />
123
124 <?php
125 echo wp_kses( $blocks_html, Helper::get_allowed_form_html() );
126 ?>
127 </form>
128
129 <!-- Success message container (hidden by default, shown after successful donation) -->
130 <div class="sd-success-box" style="display: none;">
131 <div class="sd-success-box-description"></div>
132 </div>
133 </div>
134 <?php
135 return (string) ob_get_clean();
136 }
137
138 /**
139 * Enqueue frontend assets.
140 *
141 * @return void
142 * @since 0.0.1
143 */
144 private function enqueue_assets() {
145 // Enqueue frontend styles.
146 wp_enqueue_style(
147 'suredonation-form',
148 SUREDONATION_URL . 'assets/css/form.css',
149 [],
150 SUREDONATION_VER
151 );
152
153 // Enqueue frontend scripts.
154 wp_enqueue_script(
155 'suredonation-form',
156 SUREDONATION_URL . 'assets/build/form-frontend.js',
157 [],
158 SUREDONATION_VER,
159 true
160 );
161
162 // Localize script with payment and success message configuration.
163 wp_localize_script(
164 'suredonation-form',
165 'suredonationPayment',
166 [
167 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
168 'nonce' => wp_create_nonce( 'suredonation_payment_nonce' ),
169 'confirmationType' => 'message', // Default to showing message. Could be 'redirect' in future.
170 'redirectUrl' => '',
171 'successTitle' => __( 'Thank You!', 'suredonation' ),
172 'successMessage' => __( 'Your donation has been processed successfully. A confirmation email will be sent to you shortly.', 'suredonation' ),
173 ]
174 );
175
176 // Enqueue Stripe.js if Stripe is configured.
177 $this->maybe_enqueue_stripe();
178 }
179
180 /**
181 * Enqueue Stripe.js if Stripe is configured.
182 *
183 * @return void
184 * @since 0.0.1
185 */
186 private function maybe_enqueue_stripe() {
187 if ( ! class_exists( 'SureDonation\Inc\Payments\Stripe\Stripe_Helper' ) ) {
188 return;
189 }
190
191 $publishable_key = \SureDonation\Inc\Payments\Stripe\Stripe_Helper::get_stripe_publishable_key();
192
193 if ( ! empty( $publishable_key ) ) {
194 wp_enqueue_script(
195 'stripe-js',
196 'https://js.stripe.com/v3/',
197 [],
198 null, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- External script.
199 true
200 );
201 }
202 }
203 }
204