| @@ -9,8 +9,10 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | namespace SureDonation\Inc\Shortcodes; |
| 12 | 12 | |
| 13 | +use SureDonation\Inc\Fields\Form_Renderer; | |
| 14 | +use SureDonation\Inc\Fields\Form_Styling; | |
| 13 | 15 | use SureDonation\Inc\Helper; |
| 14 | 16 | use SureDonation\Inc\Post_Types\Donation_Form as Donation_Form_CPT; |
| 15 | 17 | use SureDonation\Inc\Traits\Get_Instance; |
| 16 | 18 | |
| @@ -75,9 +77,9 @@ | ||
| 75 | 77 | return ''; |
| 76 | 78 | } |
| 77 | 79 | |
| 78 | 80 | // Enqueue frontend assets. |
| 79 | - $this->enqueue_assets(); | |
| 81 | + $this->enqueue_assets( $form_id, $form ); | |
| 80 | 82 | |
| 81 | 83 | // Render the form. |
| 82 | 84 | return $this->render_form( $form ); |
| 83 | 85 | } |
| @@ -89,93 +91,56 @@ | ||
| 89 | 91 | * @return string Form HTML. |
| 90 | 92 | * @since 0.0.1 |
| 91 | 93 | */ |
| 92 | 94 | private function render_form( $form ) { |
| 93 | - $form_id = $form->ID; | |
| 94 | - $campaign_id = Donation_Form_CPT::get_form_campaign_id( $form_id ); | |
| 95 | + $campaign_id = Donation_Form_CPT::get_form_campaign_id( $form->ID ); | |
| 95 | 96 | |
| 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(); | |
| 97 | + // Shared markup (also used by the donation-form block). | |
| 98 | + return Form_Renderer::render( $form, $campaign_id ); | |
| 136 | 99 | } |
| 137 | 100 | |
| 138 | 101 | /** |
| 139 | 102 | * Enqueue frontend assets. |
| 140 | 103 | * |
| 104 | + * @param int $form_id Form post ID. | |
| 105 | + * @param \WP_Post $form Form post object. | |
| 141 | 106 | * @return void |
| 142 | 107 | * @since 0.0.1 |
| 143 | 108 | */ |
| 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 | - ); | |
| 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 | + } | |
| 152 | 118 | |
| 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 | - ); | |
| 119 | + // Enqueue frontend script (registered globally in Assets\Register). | |
| 120 | + wp_enqueue_script( 'suredonation-form-frontend' ); | |
| 161 | 121 | |
| 162 | 122 | // Localize script with payment and success message configuration. |
| 163 | 123 | wp_localize_script( |
| 164 | - 'suredonation-form', | |
| 124 | + 'suredonation-form-frontend', | |
| 165 | 125 | '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 | - ] | |
| 126 | + Helper::get_form_payment_settings( $form_id ) | |
| 174 | 127 | ); |
| 175 | 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 | + | |
| 176 | 137 | // Enqueue Stripe.js if Stripe is configured. |
| 177 | 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 ); | |
| 178 | 143 | } |
| 179 | 144 | |
| 180 | 145 | /** |
| 181 | 146 | * Enqueue Stripe.js if Stripe is configured. |
| @@ -187,11 +152,11 @@ | ||
| 187 | 152 | if ( ! class_exists( 'SureDonation\Inc\Payments\Stripe\Stripe_Helper' ) ) { |
| 188 | 153 | return; |
| 189 | 154 | } |
| 190 | 155 | |
| 191 | - $publishable_key = \SureDonation\Inc\Payments\Stripe\Stripe_Helper::get_stripe_publishable_key(); | |
| 192 | - | |
| 193 | - if ( ! empty( $publishable_key ) ) { | |
| 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() ) { | |
| 194 | 159 | wp_enqueue_script( |
| 195 | 160 | 'stripe-js', |
| 196 | 161 | 'https://js.stripe.com/v3/', |
| 197 | 162 | [], |