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

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

167 lines 6.2 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\Helper;
13 use SureDonation\Inc\Post_Types\Donation_Form;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly.
17 }
18
19 /**
20 * Donation Form Block.
21 *
22 * @since 0.0.1
23 */
24 class Block extends Base {
25 /**
26 * Render the block
27 *
28 * @param array<string, mixed> $attributes Block attributes.
29 * @param string $content Block content.
30 * @return string
31 * @since 0.0.1
32 */
33 public function render( $attributes, $content = '' ) {
34 unset( $content ); // Unused parameter.
35
36 if ( empty( $attributes ) ) {
37 return '';
38 }
39
40 // A custom form is required.
41 $form_id = isset( $attributes['formId'] ) ? absint( Helper::get_string_value( $attributes['formId'] ) ) : 0;
42
43 if ( ! $form_id ) {
44 // Show message only to users who can edit posts.
45 if ( current_user_can( 'edit_posts' ) ) {
46 return '<div class="suredonation-notice">' . esc_html__( 'Please select a donation form in the block settings.', 'suredonation' ) . '</div>';
47 }
48 return '';
49 }
50
51 return $this->render_custom_form( $form_id, $attributes );
52 }
53
54 /**
55 * Render a custom donation form.
56 *
57 * @param int $form_id Form post ID.
58 * @param array<string, mixed> $attributes Block attributes.
59 * @return string
60 * @since 0.0.1
61 */
62 private function render_custom_form( $form_id, $attributes ) {
63 // Get the form post.
64 $form = get_post( $form_id );
65 if ( ! $form instanceof \WP_Post || Donation_Form::POST_TYPE !== $form->post_type ) {
66 return '<div class="suredonation-notice">' . esc_html__( 'Invalid form selected.', 'suredonation' ) . '</div>';
67 }
68
69 // Check if form is published.
70 if ( 'publish' !== $form->post_status ) {
71 return '<div class="suredonation-notice">' . esc_html__( 'This form is not available.', 'suredonation' ) . '</div>';
72 }
73
74 // Get the campaign ID from block attributes or form meta.
75 $campaign_id = isset( $attributes['campaignId'] ) ? absint( Helper::get_string_value( $attributes['campaignId'] ) ) : 0;
76 if ( ! $campaign_id ) {
77 $campaign_id = Donation_Form::get_form_campaign_id( $form_id );
78 }
79
80 // Validate campaign if set.
81 if ( $campaign_id ) {
82 $campaign = get_post( $campaign_id );
83 if ( ! $campaign instanceof \WP_Post || SUREDONATION_POST_TYPE !== $campaign->post_type ) {
84 return '<div class="suredonation-notice">' . esc_html__( 'Invalid campaign selected.', 'suredonation' ) . '</div>';
85 }
86
87 // Check campaign status.
88 $campaign_status = Helper::get_campaign_meta_value( $campaign_id, 'campaign_status', 'active' );
89 if ( 'paused' === $campaign_status || 'completed' === $campaign_status ) {
90 return '<div class="suredonation-notice">' . esc_html__( 'This campaign is not currently accepting donations.', 'suredonation' ) . '</div>';
91 }
92 }
93
94 // Enqueue frontend styles for custom form blocks.
95 // Note: Frontend JS is handled by the payment block (form-frontend.js).
96 wp_enqueue_style( 'suredonation-donation-form' );
97
98 // Get form settings from post meta.
99 $confirmation_type = get_post_meta( $form_id, '_suredonation_form_confirmationType', true );
100 $success_message = get_post_meta( $form_id, '_suredonation_form_successMessage', true );
101 $redirect_url = get_post_meta( $form_id, '_suredonation_form_redirectUrl', true );
102
103 // Set defaults.
104 $confirmation_type = ! empty( $confirmation_type ) ? $confirmation_type : 'message';
105 $success_message = ! empty( $success_message ) ? $success_message : esc_html__( 'Thank you for your donation!', 'suredonation' );
106
107 // Pass form settings to frontend via inline script.
108 // Using wp_add_inline_script to ensure settings are available.
109 $form_settings = wp_json_encode(
110 [
111 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
112 'nonce' => wp_create_nonce( 'suredonation_payment_nonce' ),
113 'confirmationType' => $confirmation_type,
114 'successTitle' => esc_html__( 'Thank You!', 'suredonation' ),
115 'successMessage' => wp_kses_post( Helper::get_string_value( $success_message ) ),
116 'redirectUrl' => ! empty( $redirect_url ) ? esc_url( Helper::get_string_value( $redirect_url ) ) : '',
117 ]
118 );
119 wp_add_inline_script( 'suredonation-form-frontend', "window.suredonationPayment = {$form_settings};", 'before' );
120
121 // Get the form content (blocks).
122 $form_content = $form->post_content;
123
124 // Generate unique form ID.
125 $unique_form_id = 'suredonation-form-' . $form_id . '-' . wp_rand();
126
127 // Get primary color from block attributes.
128 $primary_color = isset( $attributes['primaryColor'] ) && is_string( $attributes['primaryColor'] )
129 ? $attributes['primaryColor']
130 : '#10b981';
131
132 // Parse blocks from form content.
133 $blocks = parse_blocks( $form_content );
134
135 // Render form wrapper with blocks.
136 ob_start();
137 ?>
138 <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 ); ?>" style="--sd-primary-color: <?php echo esc_attr( $primary_color ); ?>">
139 <form class="sd-form" method="post">
140 <?php
141 $nonce_action = $campaign_id ? 'suredonation_donation_' . $campaign_id : 'suredonation_donation_standalone';
142 wp_nonce_field( $nonce_action, 'suredonation_nonce' );
143 ?>
144 <input type="hidden" name="form_id" value="<?php echo esc_attr( (string) $form_id ); ?>">
145 <input type="hidden" name="campaign_id" value="<?php echo esc_attr( (string) $campaign_id ); ?>">
146 <input type="hidden" name="action" value="suredonation_submit_donation">
147
148 <?php
149 // Render each block.
150 foreach ( $blocks as $block ) {
151 if ( ! empty( $block['blockName'] ) ) {
152 echo wp_kses( render_block( $block ), self::get_allowed_form_html() );
153 }
154 }
155 ?>
156 </form>
157 <!-- Success Message Container -->
158 <div class="sd-single-form sd-success-box">
159 <div aria-live="polite" aria-atomic="true" role="alert" id="sd-success-message-<?php echo esc_attr( (string) $form_id ); ?>" class="sd-success-box-description"></div>
160 </div>
161 </div>
162 <?php
163 $output = ob_get_clean();
164 return false !== $output ? $output : '';
165 }
166 }
167