PluginProbe
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management / 1.3.0
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management v1.3.0
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 / blocks / donation-form / block.php

block.php in SureDonation – Donation Forms, Fundraising Campaigns & Donor Management 1.3.0, at inc/blocks/donation-form/block.php

168 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PHP render for Donation Form Block.
4 *
5 * @package SureDonation
6 * @since 0.0.1
7 */
8
9 namespace SureDonation\Inc\Blocks\Donation_Form;
10
11 use SureDonation\Inc\Blocks\Base;
12 use SureDonation\Inc\Campaigns\Campaign_Page;
13 use SureDonation\Inc\Fields\Form_Renderer;
14 use SureDonation\Inc\Helper;
15 use SureDonation\Inc\Post_Types\Donation_Form;
16
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit; // Exit if accessed directly.
19 }
20
21 /**
22 * Donation Form Block.
23 *
24 * @since 0.0.1
25 */
26 class Block extends Base {
27 /**
28 * Render the block
29 *
30 * @param array<string, mixed> $attributes Block attributes.
31 * @param string $content Block content.
32 * @return string
33 * @since 0.0.1
34 */
35 public function render( $attributes, $content = '' ) {
36 unset( $content ); // Unused parameter.
37
38 if ( empty( $attributes ) ) {
39 return '';
40 }
41
42 // A custom form is required.
43 $form_id = isset( $attributes['formId'] ) ? absint( Helper::get_string_value( $attributes['formId'] ) ) : 0;
44
45 if ( ! $form_id ) {
46 // Show message only to users who can edit posts.
47 if ( current_user_can( 'edit_posts' ) ) {
48 return '<div class="suredonation-notice">' . esc_html__( 'Please select a donation form in the block settings.', 'suredonation' ) . '</div>';
49 }
50 return '';
51 }
52
53 return $this->render_custom_form( $form_id, $attributes );
54 }
55
56 /**
57 * Render a custom donation form.
58 *
59 * @param int $form_id Form post ID.
60 * @param array<string, mixed> $attributes Block attributes.
61 * @return string
62 * @since 0.0.1
63 */
64 private function render_custom_form( $form_id, $attributes ) {
65 // Get the form post.
66 $form = get_post( $form_id );
67 if ( ! $form instanceof \WP_Post || Donation_Form::POST_TYPE !== $form->post_type ) {
68 return '<div class="suredonation-notice">' . esc_html__( 'Invalid form selected.', 'suredonation' ) . '</div>';
69 }
70
71 // Check if form is published.
72 if ( 'publish' !== $form->post_status ) {
73 return '<div class="suredonation-notice">' . esc_html__( 'This form is not available.', 'suredonation' ) . '</div>';
74 }
75
76 // Get the campaign ID from block attributes or form meta.
77 $campaign_id = isset( $attributes['campaignId'] ) ? absint( Helper::get_string_value( $attributes['campaignId'] ) ) : 0;
78 if ( ! $campaign_id ) {
79 $campaign_id = Donation_Form::get_form_campaign_id( $form_id );
80 }
81
82 // Validate campaign if set.
83 if ( $campaign_id ) {
84 $campaign = get_post( $campaign_id );
85 if ( ! $campaign instanceof \WP_Post || SUREDONATION_POST_TYPE !== $campaign->post_type ) {
86 return '<div class="suredonation-notice">' . esc_html__( 'Invalid campaign selected.', 'suredonation' ) . '</div>';
87 }
88
89 // Check campaign status.
90 $campaign_status = Helper::get_campaign_meta_value( $campaign_id, 'campaign_status', 'active' );
91 if ( 'paused' === $campaign_status || 'completed' === $campaign_status ) {
92 return '<div class="suredonation-notice">' . esc_html__( 'This campaign is not currently accepting donations.', 'suredonation' ) . '</div>';
93 }
94 }
95
96 // Enqueue frontend styles for custom form blocks.
97 // Note: Frontend JS is handled by the payment block (form-frontend.js).
98 wp_enqueue_style( 'suredonation-donation-form' );
99
100 // Enqueue form frontend script.
101 wp_enqueue_script( 'suredonation-form-frontend' );
102
103 /**
104 * Fires when a donation form is rendered on the frontend.
105 *
106 * Allows payment gateway extensions to enqueue their scripts (e.g., PayPal SDK).
107 *
108 * @param int $form_id The donation form post ID.
109 * @param string $form_content The form post content (blocks).
110 * @since 1.0.0
111 */
112 do_action( 'suredonation_enqueue_form_frontend_scripts', $form_id, $form->post_content );
113
114 // Pass form settings to frontend (wp_localize_script handles script-context escaping).
115 wp_localize_script(
116 'suredonation-form-frontend',
117 'suredonationPayment',
118 Helper::get_form_payment_settings( $form_id )
119 );
120
121 // Expose the resolved validation messages so client-side validation
122 // mirrors the server's configured messages.
123 wp_localize_script(
124 'suredonation-form-frontend',
125 'suredonationValidationMessages',
126 \SureDonation\Inc\Field_Validation::get_resolved_validation_messages()
127 );
128
129 // Shared markup (also used by the [suredonation_form] shortcode).
130 $form_html = Form_Renderer::render( $form, $campaign_id );
131
132 return $this->maybe_anchor_form( $form_html );
133 }
134
135 /**
136 * Wrap the first donation form rendered on the page in the campaign form
137 * anchor.
138 *
139 * The Campaign Donate Button links to `#suredonation-donation-form`. Owning
140 * that anchor here (rather than relying on a wrapping group set up by the
141 * page seeder) keeps the scroll target alive wherever the form block is
142 * placed — including after a user removes and manually re-adds it. Only the
143 * first instance per request is anchored so the id stays unique on the page.
144 *
145 * "First" means first in PHP render order for the request, not first in the
146 * visible page. On the campaign page that is the only/intended form. The
147 * known trade-offs of that scope: a form rendered earlier in the request
148 * (e.g. a sidebar/widget form) would claim the anchor instead, and
149 * ServerSideRender emits the anchored wrapper into the block's editor
150 * preview too. Both are acceptable for the campaign-page use case.
151 *
152 * @param string $form_html Rendered form markup.
153 * @return string
154 * @since 1.1.0
155 */
156 private function maybe_anchor_form( $form_html ) {
157 static $anchored = false;
158
159 if ( $anchored || '' === $form_html ) {
160 return $form_html;
161 }
162
163 $anchored = true;
164
165 return '<div id="' . esc_attr( Campaign_Page::FORM_ANCHOR ) . '" class="suredonation-donation-form-anchor">' . $form_html . '</div>';
166 }
167 }
168