| @@ -56,16 +56,8 @@ | ||
| 56 | 56 | */ |
| 57 | 57 | protected $stripe_connected; |
| 58 | 58 | |
| 59 | 59 | /** |
| 60 | - * Whether the resolved Stripe account is known to be unable to charge cards. | |
| 61 | - * | |
| 62 | - * @var bool | |
| 63 | - * @since 1.5.1 | |
| 64 | - */ | |
| 65 | - protected $stripe_card_blocked = false; | |
| 66 | - | |
| 67 | - /** | |
| 68 | 60 | * Payment mode (live or test). |
| 69 | 61 | * |
| 70 | 62 | * @var string |
| 71 | 63 | * @since 0.0.1 |
| @@ -152,53 +144,8 @@ | ||
| 152 | 144 | */ |
| 153 | 145 | protected $aria_described_by = ''; |
| 154 | 146 | |
| 155 | 147 | /** |
| 156 | - * The payment type as configured in the editor, before the default-choice and | |
| 157 | - * Pro-availability rewrites below. | |
| 158 | - * | |
| 159 | - * $payment_type holds the *active* type being rendered; this holds the admin's | |
| 160 | - * intent, so 'both' can be distinguished from a plain single-mode block. | |
| 161 | - * | |
| 162 | - * @var string | |
| 163 | - * @since 1.5.1 | |
| 164 | - */ | |
| 165 | - protected $original_payment_type = 'one-time'; | |
| 166 | - | |
| 167 | - /** | |
| 168 | - * Which choice is pre-selected in 'both' mode ('one-time' or 'subscription'). | |
| 169 | - * | |
| 170 | - * @var string | |
| 171 | - * @since 1.5.1 | |
| 172 | - */ | |
| 173 | - protected $default_payment_choice = 'one-time'; | |
| 174 | - | |
| 175 | - /** | |
| 176 | - * Admin-configured label for the "make it recurring" opt-in checkbox, shown only | |
| 177 | - * in 'both' mode. | |
| 178 | - * | |
| 179 | - * @var string | |
| 180 | - * @since 1.5.1 | |
| 181 | - */ | |
| 182 | - protected $recurring_toggle_label = ''; | |
| 183 | - | |
| 184 | - /** | |
| 185 | - * One-time amount configuration. Used only in 'both' mode. | |
| 186 | - * | |
| 187 | - * @var array<string, mixed> | |
| 188 | - * @since 1.5.1 | |
| 189 | - */ | |
| 190 | - protected $one_time_config = []; | |
| 191 | - | |
| 192 | - /** | |
| 193 | - * Subscription amount configuration. Used only in 'both' mode. | |
| 194 | - * | |
| 195 | - * @var array<string, mixed> | |
| 196 | - * @since 1.5.1 | |
| 197 | - */ | |
| 198 | - protected $subscription_config = []; | |
| 199 | - | |
| 200 | - /** | |
| 201 | 148 | * Constructor for the Payment Markup class. |
| 202 | 149 | * |
| 203 | 150 | * @param array<mixed> $attributes Block attributes. |
| 204 | 151 | * @since 0.0.1 |
| @@ -224,34 +171,14 @@ | ||
| 224 | 171 | if ( empty( $this->currency ) || 'USD' === $this->currency ) { |
| 225 | 172 | $this->currency = Payment_Helper::get_currency(); |
| 226 | 173 | } |
| 227 | 174 | |
| 228 | - // Get the publishable key for this form's selected account (mode-aware). | |
| 229 | - $stripe_account_id = Stripe_Helper::resolve_account_for_form( $this->form_id ); | |
| 230 | - $this->stripe_publishable_key = Stripe_Helper::get_stripe_publishable_key( '', $stripe_account_id ); | |
| 175 | + // Get appropriate Stripe publishable key based on mode. | |
| 176 | + $this->stripe_publishable_key = Stripe_Helper::get_stripe_publishable_key(); | |
| 231 | 177 | |
| 232 | - // Stripe told us at connect (or on account.updated) that this account | |
| 233 | - // cannot charge cards in this mode. Rendering the card box anyway is what | |
| 234 | - // gave donors an empty "Credit / Debit Card" accordion and lost every | |
| 235 | - // attempt in silence. Only a positively-known-bad state counts — an | |
| 236 | - // account we have never checked stays enabled, because a stale flag | |
| 237 | - // hiding a working gateway is a worse failure than the one being fixed. | |
| 238 | - $this->stripe_card_blocked = Stripe_Helper::is_card_capability_blocked( $stripe_account_id ); | |
| 239 | - | |
| 240 | - // Fall back to one-time if a recurring path is configured but Pro is not | |
| 241 | - // active or too old. 'both' collapses too, which is what hides the donor's | |
| 242 | - // recurring choice: the chooser only renders while the type is still 'both'. | |
| 243 | - // | |
| 244 | - // The collapse lives in Payment_Helper::effective_payment_type(), shared | |
| 245 | - // with validate_payment_type(). Both have to reach the same answer: if the | |
| 246 | - // validator disagrees with what this markup rendered, it rejects the very | |
| 247 | - // request its own form invited. | |
| 248 | - // | |
| 249 | - // $configured_type stays the RAW attribute — the 'both' branch further down | |
| 250 | - // keys on it deliberately, because a collapsed form still has 'both' stored | |
| 251 | - // and validate_payment_amount() reads that stored config. | |
| 178 | + // Fall back to one-time if subscription is configured but pro is not active. | |
| 252 | 179 | $configured_type = $attributes['paymentType'] ?? 'one-time'; |
| 253 | - $this->payment_type = Payment_Helper::effective_payment_type( $configured_type ); | |
| 180 | + $this->payment_type = 'subscription' === $configured_type && ! defined( 'SUREDONATION_PRO_VER' ) ? 'one-time' : $configured_type; | |
| 254 | 181 | $this->subscription_plan = $attributes['subscriptionPlan'] ?? []; |
| 255 | 182 | $this->amount_type = $attributes['amountType'] ?? 'fixed'; |
| 256 | 183 | $this->fixed_amount = $attributes['fixedAmount'] ?? 10; |
| 257 | 184 | $this->minimum_amount = $attributes['minimumAmount'] ?? 0; |
| @@ -262,61 +189,8 @@ | ||
| 262 | 189 | |
| 263 | 190 | // Set variable amount field mapping. |
| 264 | 191 | $this->variable_amount_field = $attributes['variableAmountField'] ?? ''; |
| 265 | 192 | |
| 266 | - // Dual-mode ('both') configuration. $payment_type is rewritten below to the | |
| 267 | - // active choice, so keep the admin's intent separately. | |
| 268 | - $this->original_payment_type = $this->payment_type; | |
| 269 | - | |
| 270 | - $this->default_payment_choice = $attributes['defaultPaymentChoice'] ?? 'one-time'; | |
| 271 | - if ( ! in_array( $this->default_payment_choice, [ 'one-time', 'subscription' ], true ) ) { | |
| 272 | - $this->default_payment_choice = 'one-time'; | |
| 273 | - } | |
| 274 | - | |
| 275 | - $this->recurring_toggle_label = $attributes['recurringToggleLabel'] ?? __( 'Make this a recurring donation', 'suredonation' ); | |
| 276 | - | |
| 277 | - $this->one_time_config = [ | |
| 278 | - 'amount_type' => $attributes['oneTimeAmountType'] ?? 'fixed', | |
| 279 | - 'fixed_amount' => isset( $attributes['oneTimeFixedAmount'] ) ? (float) $attributes['oneTimeFixedAmount'] : 10.0, | |
| 280 | - 'minimum_amount' => isset( $attributes['oneTimeMinimumAmount'] ) ? (float) $attributes['oneTimeMinimumAmount'] : 0.0, | |
| 281 | - 'variable_field' => $attributes['oneTimeVariableAmountField'] ?? '', | |
| 282 | - ]; | |
| 283 | - | |
| 284 | - $this->subscription_config = [ | |
| 285 | - 'amount_type' => $attributes['subscriptionAmountType'] ?? 'fixed', | |
| 286 | - 'fixed_amount' => isset( $attributes['subscriptionFixedAmount'] ) ? (float) $attributes['subscriptionFixedAmount'] : 10.0, | |
| 287 | - 'minimum_amount' => isset( $attributes['subscriptionMinimumAmount'] ) ? (float) $attributes['subscriptionMinimumAmount'] : 0.0, | |
| 288 | - 'variable_field' => $attributes['subscriptionVariableAmountField'] ?? '', | |
| 289 | - ]; | |
| 290 | - | |
| 291 | - // In 'both' mode the initially visible amount — and the mode Stripe Elements | |
| 292 | - // initialises in — is the default choice. Rewrite the scalar properties so all | |
| 293 | - // the rendering below keeps working unchanged; the chooser's JS re-syncs them | |
| 294 | - // client-side when the donor switches. | |
| 295 | - // | |
| 296 | - // Keyed on the CONFIGURED type, not the (possibly collapsed) active type: when | |
| 297 | - // the recurring add-on is inactive a 'both' form collapses to one-time and the | |
| 298 | - // chooser never renders, but validate_payment_amount() still reads the stored | |
| 299 | - // 'both' config and checks the submitted amount against the one_time sub-config. | |
| 300 | - // Seed the active scalars from that sub-config rather than the shared | |
| 301 | - // attributes, so the price the donor sees matches the price validation enforces | |
| 302 | - // — otherwise a form whose shared and one-time amounts diverge renders one | |
| 303 | - // price and then rejects it with no admin signal. | |
| 304 | - if ( 'both' === $configured_type ) { | |
| 305 | - // The chooser now defaults to one-time and opts into recurring via a | |
| 306 | - // checkbox, so a 'both' block always initialises in one-time regardless of | |
| 307 | - // the (legacy) defaultPaymentChoice — the JS reveals the subscription panel | |
| 308 | - // and re-syncs these scalars when the box is ticked. This is also what | |
| 309 | - // validate_payment_amount() demands when Pro is inactive and the form has | |
| 310 | - // collapsed to one-time, so the two cases converge. | |
| 311 | - $this->default_payment_choice = 'one-time'; | |
| 312 | - $this->payment_type = 'one-time'; | |
| 313 | - $this->amount_type = $this->one_time_config['amount_type']; | |
| 314 | - $this->fixed_amount = $this->one_time_config['fixed_amount']; | |
| 315 | - $this->minimum_amount = $this->one_time_config['minimum_amount']; | |
| 316 | - $this->variable_amount_field = $this->one_time_config['variable_field']; | |
| 317 | - } | |
| 318 | - | |
| 319 | 193 | // Parse payment methods from block attributes (with backward compat fallback). |
| 320 | 194 | $block_payment_methods = $attributes['paymentMethods'] ?? null; |
| 321 | 195 | if ( ! is_array( $block_payment_methods ) || empty( $block_payment_methods ) ) { |
| 322 | 196 | $gateway = $attributes['gateway'] ?? 'stripe'; |
| @@ -325,9 +199,9 @@ | ||
| 325 | 199 | |
| 326 | 200 | // Filter to only globally-available gateways. |
| 327 | 201 | $available = []; |
| 328 | 202 | foreach ( $block_payment_methods as $method ) { |
| 329 | - if ( 'stripe' === $method && $this->stripe_connected && ! empty( $this->stripe_publishable_key ) && ! $this->stripe_card_blocked ) { | |
| 203 | + if ( 'stripe' === $method && $this->stripe_connected && ! empty( $this->stripe_publishable_key ) ) { | |
| 330 | 204 | $available[] = 'stripe'; |
| 331 | 205 | } elseif ( 'offline' === $method && Offline_Helper::is_offline_enabled() ) { |
| 332 | 206 | $available[] = 'offline'; |
| 333 | 207 | } |
| @@ -344,21 +218,10 @@ | ||
| 344 | 218 | * @since 1.0.0 |
| 345 | 219 | */ |
| 346 | 220 | $available = apply_filters( 'suredonation_available_payment_methods', $available, $block_payment_methods, $attributes ); |
| 347 | 221 | |
| 348 | - // The fallback exists so a form still advertises something when nothing | |
| 349 | - // resolved as available, but it must not resurrect a gateway we | |
| 350 | - // positively know cannot charge: `payment_methods` is what | |
| 351 | - // `data-payment-methods` advertises to the frontend script, and listing | |
| 352 | - // a gateway whose container was never rendered makes the script select | |
| 353 | - // it, try to mount into nothing, and never initialise the gateway that | |
| 354 | - // does work — an empty box again, by a different route. | |
| 355 | - $fallback = $this->stripe_card_blocked | |
| 356 | - ? array_values( array_diff( $block_payment_methods, [ 'stripe' ] ) ) | |
| 357 | - : $block_payment_methods; | |
| 222 | + $this->payment_methods = ! empty( $available ) ? $available : $block_payment_methods; | |
| 358 | 223 | |
| 359 | - $this->payment_methods = ! empty( $available ) ? $available : $fallback; | |
| 360 | - | |
| 361 | 224 | // BACKWARD COMPATIBILITY: Migrate customer fields from subscriptionPlan. |
| 362 | 225 | if ( empty( $this->customer_name_field ) && ! empty( $this->subscription_plan['customer_name'] ) ) { |
| 363 | 226 | $this->customer_name_field = $this->subscription_plan['customer_name']; |
| 364 | 227 | } |
| @@ -374,25 +237,24 @@ | ||
| 374 | 237 | * @return string |
| 375 | 238 | * @since 0.0.1 |
| 376 | 239 | */ |
| 377 | 240 | public function markup() { |
| 378 | - $has_stripe = in_array( 'stripe', $this->payment_methods, true ); | |
| 241 | + $has_stripe = in_array( 'stripe', $this->payment_methods, true ); | |
| 242 | + $has_offline = in_array( 'offline', $this->payment_methods, true ); | |
| 379 | 243 | |
| 380 | - // Determine which gateways are actually connected/available for this form. | |
| 381 | - // get_registered_payment_methods() returns only enabled methods (Stripe when | |
| 382 | - // connected, Offline when enabled, plus any added by extensions such as | |
| 383 | - // PayPal), so it is the single gateway-agnostic source of truth. | |
| 384 | - $methods = $this->get_registered_payment_methods(); | |
| 244 | + // If only Stripe is selected, it must be connected. | |
| 245 | + if ( $has_stripe && ! $has_offline && ( ! $this->stripe_connected || empty( $this->stripe_publishable_key ) ) ) { | |
| 246 | + return ''; | |
| 247 | + } | |
| 385 | 248 | |
| 386 | - // No payment gateway is connected/available. Show a clear message instead of | |
| 387 | - // returning an empty string, which previously left donors with a silently | |
| 388 | - // broken form and no way to donate. See issue #219. | |
| 389 | - if ( empty( $methods ) ) { | |
| 390 | - return $this->render_gateway_unavailable_notice(); | |
| 249 | + // If no gateway is available at all, hide the block. | |
| 250 | + if ( empty( $this->payment_methods ) ) { | |
| 251 | + return ''; | |
| 391 | 252 | } |
| 392 | 253 | |
| 393 | 254 | // Validate payment field requirements. |
| 394 | - if ( ! $this->validate_payment_requirements() ) { | |
| 255 | + $is_valid = $this->validate_payment_requirements(); | |
| 256 | + if ( ! $is_valid ) { | |
| 395 | 257 | return ''; |
| 396 | 258 | } |
| 397 | 259 | |
| 398 | 260 | $field_classes = $this->get_field_classes(); |
| @@ -405,14 +267,8 @@ | ||
| 405 | 267 | data-form-id="<?php echo esc_attr( $this->form_id ); ?>" |
| 406 | 268 | class="<?php echo esc_attr( $field_classes ); ?>" |
| 407 | 269 | data-payment-methods="<?php echo esc_attr( $payment_methods_csv ); ?>" |
| 408 | 270 | data-gateway="<?php echo esc_attr( $default_gateway ); ?>" |
| 409 | - <?php | |
| 410 | - // data-stripe-key and data-payment-mode are render-time values and only | |
| 411 | - // a fallback: the form script fetches the current gateway configuration | |
| 412 | - // at runtime (Payment_Helper::get_frontend_gateway_config()) because a | |
| 413 | - // page cache can serve this markup long after a test/live switch. | |
| 414 | - ?> | |
| 415 | 271 | <?php if ( $has_stripe && ! empty( $this->stripe_publishable_key ) ) { ?> |
| 416 | 272 | data-stripe-key="<?php echo esc_attr( $this->stripe_publishable_key ); ?>" |
| 417 | 273 | <?php } ?> |
| 418 | 274 | data-currency="<?php echo esc_attr( strtolower( $this->currency ) ); ?>" |
| @@ -425,28 +281,13 @@ | ||
| 425 | 281 | data-customer-email-field="<?php echo esc_attr( $this->customer_email_field ); ?>" |
| 426 | 282 | data-nonce="<?php echo esc_attr( wp_create_nonce( 'suredonation_donation_form' ) ); ?>" |
| 427 | 283 | <?php if ( 'variable' === $this->amount_type ) { ?> |
| 428 | 284 | data-variable-amount-field="<?php echo esc_attr( $this->variable_amount_field ); ?>" |
| 429 | - <?php } elseif ( '' !== $this->get_inactive_variable_field() ) { ?> | |
| 430 | - <?php // In 'both' mode, populate the top-level variable-field attribute from whichever choice is the variable one so the amount is resolvable in the initial (one-time) markup; the runtime switch then rewrites it per choice from the per-choice data-* attributes. ?> | |
| 431 | - data-variable-amount-field="<?php echo esc_attr( $this->get_inactive_variable_field() ); ?>" | |
| 432 | 285 | <?php } ?> |
| 433 | 286 | <?php if ( $this->minimum_amount > 0 ) { ?> |
| 434 | 287 | data-minimum-amount="<?php echo esc_attr( (string) $this->minimum_amount ); ?>" |
| 435 | 288 | <?php } ?> |
| 436 | - <?php if ( 'both' === $this->original_payment_type ) { ?> | |
| 437 | - data-original-payment-type="both" | |
| 438 | - data-default-payment-choice="<?php echo esc_attr( $this->default_payment_choice ); ?>" | |
| 439 | - data-one-time-amount-type="<?php echo esc_attr( Helper::get_string_value( $this->one_time_config['amount_type'] ) ); ?>" | |
| 440 | - data-one-time-fixed-amount="<?php echo esc_attr( Helper::get_string_value( $this->one_time_config['fixed_amount'] ) ); ?>" | |
| 441 | - data-one-time-minimum-amount="<?php echo esc_attr( Helper::get_string_value( $this->one_time_config['minimum_amount'] ) ); ?>" | |
| 442 | - data-one-time-variable-amount-field="<?php echo esc_attr( Helper::get_string_value( $this->one_time_config['variable_field'] ) ); ?>" | |
| 443 | - data-subscription-amount-type="<?php echo esc_attr( Helper::get_string_value( $this->subscription_config['amount_type'] ) ); ?>" | |
| 444 | - data-subscription-fixed-amount="<?php echo esc_attr( Helper::get_string_value( $this->subscription_config['fixed_amount'] ) ); ?>" | |
| 445 | - data-subscription-minimum-amount="<?php echo esc_attr( Helper::get_string_value( $this->subscription_config['minimum_amount'] ) ); ?>" | |
| 446 | - data-subscription-variable-amount-field="<?php echo esc_attr( Helper::get_string_value( $this->subscription_config['variable_field'] ) ); ?>" | |
| 447 | - <?php } ?> | |
| 448 | - <?php if ( $this->has_subscription_path() && ! empty( $this->subscription_plan ) ) { ?> | |
| 289 | + <?php if ( 'subscription' === $this->payment_type && ! empty( $this->subscription_plan ) ) { ?> | |
| 449 | 290 | data-subscription-plan-name="<?php echo esc_attr( isset( $this->subscription_plan['name'] ) ? Helper::get_string_value( $this->subscription_plan['name'] ) : __( 'Subscription Plan', 'suredonation' ) ); ?>" |
| 450 | 291 | data-subscription-interval="<?php echo esc_attr( isset( $this->subscription_plan['interval'] ) ? Helper::get_string_value( $this->subscription_plan['interval'] ) : 'month' ); ?>" |
| 451 | 292 | data-subscription-billing-cycles="<?php echo esc_attr( isset( $this->subscription_plan['billingCycles'] ) ? Helper::get_string_value( $this->subscription_plan['billingCycles'] ) : '0' ); ?>" |
| 452 | 293 | <?php } ?> |
| @@ -456,16 +297,9 @@ | ||
| 456 | 297 | <div class="sd-payment-field-wrapper"> |
| 457 | 298 | <?php |
| 458 | 299 | // Amount display — uses wp_kses with data attributes allowed |
| 459 | 300 | // because wp_kses_post strips data-message-format, data-currency-symbol etc. |
| 460 | - if ( 'both' === $this->original_payment_type ) { | |
| 461 | - // Donor picks the type; both amount panels are rendered and the | |
| 462 | - // chooser's JS toggles which one is visible. | |
| 463 | - echo wp_kses( $this->render_payment_type_chooser(), Helper::get_allowed_form_html() ); | |
| 464 | - echo wp_kses( $this->render_dual_amount_displays(), Helper::get_allowed_form_html() ); | |
| 465 | - } else { | |
| 466 | - echo wp_kses( $this->render_amount_display(), Helper::get_allowed_form_html() ); | |
| 467 | - } | |
| 301 | + echo wp_kses( $this->render_amount_display(), Helper::get_allowed_form_html() ); | |
| 468 | 302 | |
| 469 | 303 | // Test mode notice. |
| 470 | 304 | if ( 'test' === $this->payment_mode && $has_stripe ) { |
| 471 | 305 | echo wp_kses_post( $this->get_test_mode_notice() ); |
| @@ -470,22 +304,15 @@ | ||
| 470 | 304 | if ( 'test' === $this->payment_mode && $has_stripe ) { |
| 471 | 305 | echo wp_kses_post( $this->get_test_mode_notice() ); |
| 472 | 306 | } |
| 473 | 307 | |
| 474 | - // Admin-only: the form renders (Offline is enabled) but no real | |
| 475 | - // gateway is connected. | |
| 476 | - echo wp_kses_post( $this->get_gateway_setup_notice() ); | |
| 477 | - | |
| 478 | - // Admin-only: the form renders on its other gateways, but the | |
| 479 | - // card form was dropped because Stripe cannot charge. | |
| 480 | - echo wp_kses_post( $this->get_capability_notice() ); | |
| 481 | - | |
| 482 | - // Payment methods accordion ( $methods computed above ). | |
| 308 | + // Payment methods accordion. | |
| 309 | + $methods = $this->get_registered_payment_methods(); | |
| 483 | 310 | echo $this->render_payment_methods_accordion( $methods ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Method builds markup with esc_attr/esc_html on all dynamic values and wp_kses_post on content. |
| 484 | 311 | ?> |
| 485 | 312 | |
| 486 | - <!-- Payment error display (hidden via CSS; shown by JS on error). --> | |
| 487 | - <div class="sd-payment-error"></div> | |
| 313 | + <!-- Payment error display --> | |
| 314 | + <div class="sd-payment-error" style="display: none;"></div> | |
| 488 | 315 | </div> |
| 489 | 316 | </div> |
| 490 | 317 | <?php |
| 491 | 318 | $output = ob_get_clean(); |
| @@ -502,20 +329,15 @@ | ||
| 502 | 329 | $available_methods = [ |
| 503 | 330 | 'stripe' => [ |
| 504 | 331 | 'id' => 'stripe', |
| 505 | 332 | 'label' => __( 'Credit / Debit Card', 'suredonation' ), |
| 506 | - // The capability check belongs here rather than in the | |
| 507 | - // `suredonation_available_payment_methods` loop above: that loop | |
| 508 | - // falls back to the raw block selection when it ends up empty, so | |
| 509 | - // dropping Stripe there would put it straight back on a form that | |
| 510 | - // offers nothing else. | |
| 511 | - 'enabled' => $this->stripe_connected && ! empty( $this->stripe_publishable_key ) && ! $this->stripe_card_blocked, | |
| 333 | + 'enabled' => $this->stripe_connected && ! empty( $this->stripe_publishable_key ), | |
| 512 | 334 | 'container_class' => 'sd-payment-element', |
| 513 | 335 | 'container_id' => 'sd-payment-element-' . $this->block_id, |
| 514 | 336 | ], |
| 515 | 337 | 'offline' => [ |
| 516 | 338 | 'id' => 'offline', |
| 517 | - 'label' => __( 'Offline Donation', 'suredonation' ), | |
| 339 | + 'label' => __( 'Offline Payment', 'suredonation' ), | |
| 518 | 340 | 'enabled' => Offline_Helper::is_offline_enabled(), |
| 519 | 341 | 'container_class' => 'sd-offline-instructions', |
| 520 | 342 | 'content' => Offline_Helper::get_offline_instructions( $this->get_campaign_name() ), |
| 521 | 343 | ], |
| @@ -585,20 +407,15 @@ | ||
| 585 | 407 | <div |
| 586 | 408 | class="sd-accordion-item <?php echo esc_attr( $is_first ? 'sd-payment-active' : '' ); ?>" |
| 587 | 409 | data-method="<?php echo esc_attr( $method['id'] ); ?>" |
| 588 | 410 | > |
| 589 | - <?php if ( $is_single_method ) { ?> | |
| 590 | - <?php // Single method: the header is a static label, not an accordion toggle, so omit the interactive button semantics (keyboard/screen-reader users shouldn't hit an inert "button"). ?> | |
| 591 | - <div class="sd-accordion-header"> | |
| 592 | - <?php } else { ?> | |
| 593 | - <div | |
| 594 | - class="sd-accordion-header" | |
| 595 | - role="button" | |
| 596 | - tabindex="0" | |
| 597 | - aria-expanded="<?php echo esc_attr( $is_first ? 'true' : 'false' ); ?>" | |
| 598 | - aria-controls="sd-accordion-content-<?php echo esc_attr( $method['id'] ); ?>-<?php echo esc_attr( $this->block_id ); ?>" | |
| 599 | - > | |
| 600 | - <?php } ?> | |
| 411 | + <div | |
| 412 | + class="sd-accordion-header" | |
| 413 | + role="button" | |
| 414 | + tabindex="0" | |
| 415 | + aria-expanded="<?php echo esc_attr( $is_first ? 'true' : 'false' ); ?>" | |
| 416 | + aria-controls="sd-accordion-content-<?php echo esc_attr( $method['id'] ); ?>-<?php echo esc_attr( $this->block_id ); ?>" | |
| 417 | + > | |
| 601 | 418 | <div class="sd-payment-input-wrapper"> |
| 602 | 419 | <input |
| 603 | 420 | type="radio" |
| 604 | 421 | name="sd-gateway-choice-<?php echo esc_attr( $this->block_id ); ?>" |
| @@ -614,9 +431,9 @@ | ||
| 614 | 431 | </div> |
| 615 | 432 | </div> |
| 616 | 433 | <div |
| 617 | 434 | id="sd-accordion-content-<?php echo esc_attr( $method['id'] ); ?>-<?php echo esc_attr( $this->block_id ); ?>" |
| 618 | - class="sd-accordion-content<?php echo Offline_Helper::is_blank_instructions( $method['content'] ?? '' ) ? ' sd-accordion-content-empty' : ''; ?>" | |
| 435 | + class="sd-accordion-content" | |
| 619 | 436 | role="region" |
| 620 | 437 | > |
| 621 | 438 | <div |
| 622 | 439 | class="sd-payment-method-content" |
| @@ -646,165 +463,23 @@ | ||
| 646 | 463 | return is_string( $template ) ? $template : ''; |
| 647 | 464 | } |
| 648 | 465 | |
| 649 | 466 | /** |
| 650 | - * Whether this block can create a subscription — either it is subscription-only, | |
| 651 | - * or it is 'both' and the donor may choose recurring. | |
| 652 | - * | |
| 653 | - * @return bool | |
| 654 | - * @since 1.5.1 | |
| 655 | - */ | |
| 656 | - private function has_subscription_path() { | |
| 657 | - return 'subscription' === $this->payment_type || 'both' === $this->original_payment_type; | |
| 658 | - } | |
| 659 | - | |
| 660 | - /** | |
| 661 | - * Variable-amount field slug of the choice that is *not* active on page load. | |
| 662 | - * | |
| 663 | - * Returns '' outside 'both' mode, or when the inactive choice is not variable. | |
| 664 | - * | |
| 665 | - * @return string | |
| 666 | - * @since 1.5.1 | |
| 667 | - */ | |
| 668 | - private function get_inactive_variable_field() { | |
| 669 | - if ( 'both' !== $this->original_payment_type ) { | |
| 670 | - return ''; | |
| 671 | - } | |
| 672 | - | |
| 673 | - $inactive = 'subscription' === $this->default_payment_choice | |
| 674 | - ? $this->one_time_config | |
| 675 | - : $this->subscription_config; | |
| 676 | - | |
| 677 | - return 'variable' === $inactive['amount_type'] | |
| 678 | - ? Helper::get_string_value( $inactive['variable_field'] ) | |
| 679 | - : ''; | |
| 680 | - } | |
| 681 | - | |
| 682 | - /** | |
| 683 | - * Render the "make it recurring" toggle, shown only in 'both' mode. | |
| 684 | - * | |
| 685 | - * The block always initialises as one-time; ticking this checkbox opts the donor | |
| 686 | - * into the recurring choice (the JS reveals the subscription amount panel and | |
| 687 | - * re-points the block at that config). A single opt-in checkbox is friendlier than | |
| 688 | - * a two-button chooser for a form that is one-time by default. | |
| 689 | - * | |
| 690 | - * @return string Chooser markup. | |
| 691 | - * @since 1.5.1 | |
| 692 | - */ | |
| 693 | - private function render_payment_type_chooser() { | |
| 694 | - $checkbox_id = 'sd-payment-recurring-' . $this->block_id; | |
| 695 | - $one_time_panel = 'sd-payment-amount-' . $this->block_id . '-one-time'; | |
| 696 | - $subscription_panel = 'sd-payment-amount-' . $this->block_id . '-subscription'; | |
| 697 | - | |
| 698 | - /** | |
| 699 | - * Filter the label on the "make it recurring" opt-in checkbox. Defaults to the | |
| 700 | - * admin-configured `recurringToggleLabel` block attribute. | |
| 701 | - * | |
| 702 | - * @since 1.5.1 | |
| 703 | - * @param string $label Checkbox label. | |
| 704 | - * @param string $block_id The payment block id. | |
| 705 | - */ | |
| 706 | - $label = apply_filters( | |
| 707 | - 'suredonation_payment_recurring_toggle_label', | |
| 708 | - $this->recurring_toggle_label, | |
| 709 | - $this->block_id | |
| 710 | - ); | |
| 711 | - | |
| 712 | - // Mirror the checkbox-field markup (sd-checkbox-wrap / sd-checkbox-label / | |
| 713 | - // sd-input-checkbox / sd-checkbox-text) so this opt-in inherits the exact same | |
| 714 | - // styling as every other SureDonation checkbox instead of a bespoke look. | |
| 715 | - ob_start(); | |
| 716 | - ?> | |
| 717 | - <div class="sd-payment-recurring-toggle"> | |
| 718 | - <div class="sd-block-wrap sd-checkbox-wrap"> | |
| 719 | - <label class="sd-checkbox-label" for="<?php echo esc_attr( $checkbox_id ); ?>"> | |
| 720 | - <input | |
| 721 | - class="sd-input-checkbox sd-payment-recurring-checkbox" | |
| 722 | - type="checkbox" | |
| 723 | - id="<?php echo esc_attr( $checkbox_id ); ?>" | |
| 724 | - value="1" | |
| 725 | - <?php // Without this, a soft reload restores the tick while the script re-runs from scratch. The handler reconciles the mismatch too; this stops it arising. ?> | |
| 726 | - autocomplete="off" | |
| 727 | - aria-controls="<?php echo esc_attr( $one_time_panel . ' ' . $subscription_panel ); ?>" | |
| 728 | - /> | |
| 729 | - <span class="sd-checkbox-text"><?php echo esc_html( Helper::get_string_value( $label ) ); ?></span> | |
| 730 | - </label> | |
| 731 | - </div> | |
| 732 | - </div> | |
| 733 | - <?php | |
| 734 | - $markup = ob_get_clean(); | |
| 735 | - return is_string( $markup ) ? $markup : ''; | |
| 736 | - } | |
| 737 | - | |
| 738 | - /** | |
| 739 | - * Render both amount panels for 'both' mode, one per choice. Only the default | |
| 740 | - * choice's panel is visible; the chooser's JS toggles them. | |
| 741 | - * | |
| 742 | - * @return string Amount panels markup. | |
| 743 | - * @since 1.5.1 | |
| 744 | - */ | |
| 745 | - private function render_dual_amount_displays() { | |
| 746 | - $panels = [ | |
| 747 | - 'one-time' => $this->one_time_config, | |
| 748 | - 'subscription' => $this->subscription_config, | |
| 749 | - ]; | |
| 750 | - | |
| 751 | - ob_start(); | |
| 752 | - foreach ( $panels as $type => $config ) { | |
| 753 | - $panel_id = 'sd-payment-amount-' . $this->block_id . '-' . $type; | |
| 754 | - ?> | |
| 755 | - <div | |
| 756 | - id="<?php echo esc_attr( $panel_id ); ?>" | |
| 757 | - class="sd-payment-amount-block sd-payment-amount-block--<?php echo esc_attr( $type ); ?>" | |
| 758 | - data-payment-type="<?php echo esc_attr( $type ); ?>" | |
| 759 | - <?php echo $type === $this->default_payment_choice ? '' : 'hidden'; ?> | |
| 760 | - > | |
| 761 | - <?php | |
| 762 | - echo wp_kses( | |
| 763 | - $this->render_amount_display( | |
| 764 | - $type, | |
| 765 | - Helper::get_string_value( $config['amount_type'] ), | |
| 766 | - floatval( Helper::get_string_value( $config['fixed_amount'] ) ), | |
| 767 | - floatval( Helper::get_string_value( $config['minimum_amount'] ) ) | |
| 768 | - ), | |
| 769 | - Helper::get_allowed_form_html() | |
| 770 | - ); | |
| 771 | - ?> | |
| 772 | - </div> | |
| 773 | - <?php | |
| 774 | - } | |
| 775 | - $markup = ob_get_clean(); | |
| 776 | - return is_string( $markup ) ? $markup : ''; | |
| 777 | - } | |
| 778 | - | |
| 779 | - /** | |
| 780 | 467 | * Render amount display section. |
| 781 | 468 | * |
| 782 | - * Defaults to this block's active configuration. 'both' mode passes each choice's | |
| 783 | - * configuration explicitly so the same renderer produces both panels. | |
| 784 | - * | |
| 785 | - * @param string|null $payment_type Payment type to render for, or null for the active one. | |
| 786 | - * @param string|null $amount_type 'fixed' or 'variable', or null for the active one. | |
| 787 | - * @param float|null $fixed_amount Fixed amount, or null for the active one. | |
| 788 | - * @param float|null $minimum_amount Minimum amount, or null for the active one. | |
| 789 | 469 | * @return string Amount display HTML. |
| 790 | 470 | * @since 0.0.1 |
| 791 | 471 | */ |
| 792 | - private function render_amount_display( $payment_type = null, $amount_type = null, $fixed_amount = null, $minimum_amount = null ) { | |
| 793 | - $payment_type = null === $payment_type ? $this->payment_type : $payment_type; | |
| 794 | - $amount_type = null === $amount_type ? $this->amount_type : $amount_type; | |
| 795 | - $fixed_amount = null === $fixed_amount ? $this->fixed_amount : $fixed_amount; | |
| 796 | - $minimum_amount = null === $minimum_amount ? $this->minimum_amount : $minimum_amount; | |
| 797 | - | |
| 472 | + private function render_amount_display() { | |
| 798 | 473 | ob_start(); |
| 799 | 474 | |
| 800 | - if ( 'fixed' === $amount_type ) { | |
| 475 | + if ( 'fixed' === $this->amount_type ) { | |
| 801 | 476 | ?> |
| 802 | 477 | <!-- Fixed Payment Amount Display. --> |
| 803 | 478 | <div class="sd-payment-amount sd-label"> |
| 804 | 479 | <span class="sd-payment-value"> |
| 805 | 480 | <?php |
| 806 | - if ( 'subscription' === $payment_type && ! empty( $this->subscription_plan ) ) { | |
| 481 | + if ( 'subscription' === $this->payment_type && ! empty( $this->subscription_plan ) ) { | |
| 807 | 482 | $interval = isset( $this->subscription_plan['interval'] ) ? Helper::get_string_value( $this->subscription_plan['interval'] ) : 'month'; |
| 808 | 483 | $billing_cycles = isset( $this->subscription_plan['billingCycles'] ) ? Helper::get_string_value( $this->subscription_plan['billingCycles'] ) : '0'; |
| 809 | 484 | $interval_label = $this->get_interval_label( $interval ); |
| 810 | 485 | |
| @@ -813,9 +488,9 @@ | ||
| 813 | 488 | echo esc_html( |
| 814 | 489 | sprintf( |
| 815 | 490 | /* translators: 1: Amount with currency, 2: Interval (day/week/month/quarter/year) */ |
| 816 | 491 | __( '%1$s per %2$s (until cancelled)', 'suredonation' ), |
| 817 | - $this->format_currency( $fixed_amount, $this->currency ), | |
| 492 | + $this->format_currency( $this->fixed_amount, $this->currency ), | |
| 818 | 493 | $interval_label |
| 819 | 494 | ) |
| 820 | 495 | ); |
| 821 | 496 | } elseif ( (int) $billing_cycles > 0 ) { |
| @@ -822,9 +497,9 @@ | ||
| 822 | 497 | echo esc_html( |
| 823 | 498 | sprintf( |
| 824 | 499 | /* translators: 1: Amount with currency, 2: Interval (day/week/month/quarter/year), 3: Number of billing cycles */ |
| 825 | 500 | __( '%1$s per %2$s (%3$s payments)', 'suredonation' ), |
| 826 | - $this->format_currency( $fixed_amount, $this->currency ), | |
| 501 | + $this->format_currency( $this->fixed_amount, $this->currency ), | |
| 827 | 502 | $interval_label, |
| 828 | 503 | $billing_cycles |
| 829 | 504 | ) |
| 830 | 505 | ); |
| @@ -832,15 +507,15 @@ | ||
| 832 | 507 | echo esc_html( |
| 833 | 508 | sprintf( |
| 834 | 509 | /* translators: 1: Amount with currency, 2: Interval (day/week/month/quarter/year) */ |
| 835 | 510 | __( '%1$s per %2$s', 'suredonation' ), |
| 836 | - $this->format_currency( $fixed_amount, $this->currency ), | |
| 511 | + $this->format_currency( $this->fixed_amount, $this->currency ), | |
| 837 | 512 | $interval_label |
| 838 | 513 | ) |
| 839 | 514 | ); |
| 840 | 515 | } |
| 841 | 516 | } else { |
| 842 | - echo esc_html( $this->format_currency( $fixed_amount, $this->currency ) ); | |
| 517 | + echo esc_html( $this->format_currency( $this->fixed_amount, $this->currency ) ); | |
| 843 | 518 | } |
| 844 | 519 | ?> |
| 845 | 520 | </span> |
| 846 | 521 | </div> |
| @@ -852,9 +527,9 @@ | ||
| 852 | 527 | <div class="sd-payment-amount-wrapper"> |
| 853 | 528 | <?php |
| 854 | 529 | // Generate message format for variable amounts. |
| 855 | 530 | $message_format = '{amount}'; |
| 856 | - if ( 'subscription' === $payment_type && ! empty( $this->subscription_plan ) ) { | |
| 531 | + if ( 'subscription' === $this->payment_type && ! empty( $this->subscription_plan ) ) { | |
| 857 | 532 | $interval = isset( $this->subscription_plan['interval'] ) ? Helper::get_string_value( $this->subscription_plan['interval'] ) : 'month'; |
| 858 | 533 | $billing_cycles = isset( $this->subscription_plan['billingCycles'] ) ? Helper::get_string_value( $this->subscription_plan['billingCycles'] ) : '0'; |
| 859 | 534 | $interval_label = $this->get_interval_label( $interval ); |
| 860 | 535 | |
| @@ -874,9 +549,9 @@ | ||
| 874 | 549 | <span class="sd-payment-value" data-currency="<?php echo esc_attr( strtolower( $this->currency ) ); ?>" data-currency-symbol="<?php echo esc_attr( Payment_Helper::get_currency_symbol( $this->currency ) ); ?>" data-message-format="<?php echo esc_attr( $message_format ); ?>"> |
| 875 | 550 | <?php esc_html_e( 'Complete the form to view the amount.', 'suredonation' ); ?> |
| 876 | 551 | </span> |
| 877 | 552 | </div> |
| 878 | - <?php if ( $minimum_amount > 0 ) { ?> | |
| 553 | + <?php if ( $this->minimum_amount > 0 ) { ?> | |
| 879 | 554 | <span class="sd-help"> |
| 880 | 555 | <?php |
| 881 | 556 | echo esc_html( |
| 882 | 557 | sprintf( |
| @@ -881,9 +556,9 @@ | ||
| 881 | 556 | echo esc_html( |
| 882 | 557 | sprintf( |
| 883 | 558 | /* translators: %s: Minimum amount with currency */ |
| 884 | 559 | __( 'Minimum amount: %s', 'suredonation' ), |
| 885 | - $this->format_currency( $minimum_amount, $this->currency ) | |
| 560 | + $this->format_currency( $this->minimum_amount, $this->currency ) | |
| 886 | 561 | ) |
| 887 | 562 | ); |
| 888 | 563 | ?> |
| 889 | 564 | </span> |
| @@ -906,13 +581,10 @@ | ||
| 906 | 581 | if ( empty( $this->customer_email_field ) ) { |
| 907 | 582 | return false; |
| 908 | 583 | } |
| 909 | 584 | |
| 910 | - // Check subscription-specific requirements. Keyed on whether a subscription | |
| 911 | - // is reachable at all, not on the active type: in 'both' mode the donor can | |
| 912 | - // switch to recurring even when one-time is the default, so the name field | |
| 913 | - // and plan must be configured either way. | |
| 914 | - if ( $this->has_subscription_path() ) { | |
| 585 | + // Check subscription-specific requirements. | |
| 586 | + if ( 'subscription' === $this->payment_type ) { | |
| 915 | 587 | if ( empty( $this->customer_name_field ) ) { |
| 916 | 588 | return false; |
| 917 | 589 | } |
| 918 | 590 | |
| @@ -932,11 +604,17 @@ | ||
| 932 | 604 | * @return string |
| 933 | 605 | * @since 0.0.1 |
| 934 | 606 | */ |
| 935 | 607 | private function format_currency( $amount, $currency ) { |
| 936 | - // Delegate to the single source of truth so decimal handling and the | |
| 937 | - // currency sign position stay consistent with every other surface. | |
| 938 | - return Payment_Helper::format_amount( $amount, $currency ); | |
| 608 | + $symbol = Payment_Helper::get_currency_symbol( $currency ); | |
| 609 | + | |
| 610 | + // Format based on currency. | |
| 611 | + if ( in_array( $currency, [ 'JPY', 'KRW' ], true ) ) { | |
| 612 | + // No decimal places for these currencies. | |
| 613 | + return $symbol . number_format( $amount, 0 ); | |
| 614 | + } | |
| 615 | + | |
| 616 | + return $symbol . number_format( $amount, 2 ); | |
| 939 | 617 | } |
| 940 | 618 | |
| 941 | 619 | /** |
| 942 | 620 | * Get the human-readable label for a payment interval slug. |
| @@ -969,16 +647,11 @@ | ||
| 969 | 647 | if ( ! current_user_can( 'manage_options' ) ) { |
| 970 | 648 | return ''; |
| 971 | 649 | } |
| 972 | 650 | |
| 973 | - $this->prevent_page_cache(); | |
| 651 | + // Build dynamic link to payment settings. | |
| 652 | + $settings_url = admin_url( 'admin.php?page=suredonation_settings&tab=payments' ); | |
| 974 | 653 | |
| 975 | - // Build dynamic link to payment settings (shared, hash-routed URL). | |
| 976 | - // Tagged so the settings screen can record that this notice is what brought | |
| 977 | - // the admin over. No subpage: the payments screen falls back to the Stripe | |
| 978 | - // panel, which is where the arrival is read. | |
| 979 | - $settings_url = Payment_Helper::get_settings_url( '', [ 'sd_notice' => 'frontend_test_mode' ] ); | |
| 980 | - | |
| 981 | 654 | ob_start(); |
| 982 | 655 | ?> |
| 983 | 656 | <div class="sd-test-mode-notice"> |
| 984 | 657 | <strong><?php esc_html_e( 'Test mode is enabled:', 'suredonation' ); ?></strong> |
| @@ -985,250 +658,8 @@ | ||
| 985 | 658 | <a href="<?php echo esc_url( $settings_url ); ?>" target="_blank" rel="noopener noreferrer"> |
| 986 | 659 | <?php esc_html_e( 'Click here to enable live mode and accept payment', 'suredonation' ); ?> |
| 987 | 660 | </a> |
| 988 | 661 | </div> |
| 989 | - <?php | |
| 990 | - $output = ob_get_clean(); | |
| 991 | - return false !== $output ? $output : ''; | |
| 992 | - } | |
| 993 | - | |
| 994 | - /** | |
| 995 | - * Render the notice shown when no payment gateway is connected/available. | |
| 996 | - * | |
| 997 | - * Replaces the previous behavior of rendering nothing, which left donors with | |
| 998 | - * a silently broken form. The `data-payment-available="0"` marker lets the | |
| 999 | - * frontend script skip gateway init and hide the otherwise-inert submit | |
| 1000 | - * button. See issue #219. | |
| 1001 | - * | |
| 1002 | - * @return string Notice markup. | |
| 1003 | - * @since 1.1.1 | |
| 1004 | - */ | |
| 1005 | - private function render_gateway_unavailable_notice() { | |
| 1006 | - $field_classes = $this->get_field_classes( [ 'sd-payment-unavailable' ] ); | |
| 1007 | - | |
| 1008 | - // Both audiences are told the gateways are not configured, but only donors | |
| 1009 | - // are told to contact the administrator — an admin *is* that person and | |
| 1010 | - // gets the actionable button below, so the sentence would send them in a | |
| 1011 | - // circle. Mirrors the capability + settings-URL pattern used by | |
| 1012 | - // get_test_mode_notice(). | |
| 1013 | - $is_admin = current_user_can( 'manage_options' ); | |
| 1014 | - $configure_url = ''; | |
| 1015 | - $configure_text = ''; | |
| 1016 | - | |
| 1017 | - // The two audiences get different copy here, so either variant being | |
| 1018 | - // cached and replayed to the other is wrong. | |
| 1019 | - if ( $is_admin ) { | |
| 1020 | - $this->prevent_page_cache(); | |
| 1021 | - } | |
| 1022 | - | |
| 1023 | - // "Not configured" is wrong when Stripe is connected and simply cannot | |
| 1024 | - // charge — the admin would go looking for a setup step they already | |
| 1025 | - // completed, and reconnecting does not fix an account-side restriction. | |
| 1026 | - // Say what is actually true instead. Donors are never told which | |
| 1027 | - // gateway or why: it is the site's problem, not theirs. | |
| 1028 | - $blocked_only = $this->stripe_card_blocked; | |
| 1029 | - | |
| 1030 | - if ( $blocked_only ) { | |
| 1031 | - $notice_text = $is_admin | |
| 1032 | - ? __( 'Stripe is connected but cannot accept card payments for this account.', 'suredonation' ) | |
| 1033 | - : __( 'Card payments are currently unavailable. Please contact the site administrator.', 'suredonation' ); | |
| 1034 | - } else { | |
| 1035 | - $notice_text = $is_admin | |
| 1036 | - ? __( 'Payment gateways are not configured.', 'suredonation' ) | |
| 1037 | - : __( 'Payment gateways are not configured. Please contact the site administrator.', 'suredonation' ); | |
| 1038 | - } | |
| 1039 | - | |
| 1040 | - if ( $is_admin && $blocked_only ) { | |
| 1041 | - // The fix is in the gateway's own settings, where the capability | |
| 1042 | - // warning and the Stripe dashboard link live. | |
| 1043 | - $configure_url = Payment_Helper::get_settings_url( 'stripe', [ 'sd_notice' => 'stripe_capability' ] ); | |
| 1044 | - $configure_text = __( 'Review Stripe account status', 'suredonation' ); | |
| 1045 | - } elseif ( $is_admin ) { | |
| 1046 | - // Route the admin to the right place. If a gateway is already usable | |
| 1047 | - // on the site, the block simply hasn't selected it — send them to | |
| 1048 | - // this form's editor to fix the payment block. Otherwise no gateway | |
| 1049 | - // is set up at all — send them to global payment settings. | |
| 1050 | - $edit_link = $this->form_id > 0 ? get_edit_post_link( (int) $this->form_id ) : ''; | |
| 1051 | - | |
| 1052 | - if ( Payment_Helper::has_usable_gateway() && $edit_link ) { | |
| 1053 | - // Deliberately untagged: this branch goes to the block editor, not | |
| 1054 | - // the settings screen, so there is no arrival for it to record. | |
| 1055 | - // Tracking it would need a hook in the editor, which is not worth | |
| 1056 | - // a tracker on a public page — the reason this whole mechanism | |
| 1057 | - // records arrivals rather than clicks. | |
| 1058 | - $configure_url = $edit_link; | |
| 1059 | - $configure_text = __( 'Edit this form’s payment settings', 'suredonation' ); | |
| 1060 | - } else { | |
| 1061 | - // No gateway connected — send the admin straight to the gateway | |
| 1062 | - // connect screen (Stripe) rather than the currency/mode page. | |
| 1063 | - $configure_url = Payment_Helper::get_settings_url( 'stripe', [ 'sd_notice' => 'frontend_gateway_unavailable' ] ); | |
| 1064 | - $configure_text = __( 'Configure payment gateway', 'suredonation' ); | |
| 1065 | - } | |
| 1066 | - } | |
| 1067 | - | |
| 1068 | - ob_start(); | |
| 1069 | - ?> | |
| 1070 | - <div | |
| 1071 | - data-block-id="<?php echo esc_attr( $this->block_id ); ?>" | |
| 1072 | - data-form-id="<?php echo esc_attr( $this->form_id ); ?>" | |
| 1073 | - data-payment-available="0" | |
| 1074 | - class="<?php echo esc_attr( $field_classes ); ?>" | |
| 1075 | - > | |
| 1076 | - <?php echo wp_kses_post( $this->label_markup ); ?> | |
| 1077 | - <div class="sd-payment-field-wrapper"> | |
| 1078 | - <div class="sd-payment-notice" role="status"> | |
| 1079 | - <p><?php echo esc_html( $notice_text ); ?></p> | |
| 1080 | - <?php echo wp_kses_post( $is_admin ? $this->render_configure_link( $configure_url, $configure_text ) : '' ); ?> | |
| 1081 | - </div> | |
| 1082 | - </div> | |
| 1083 | - </div> | |
| 1084 | - <?php | |
| 1085 | - $output = ob_get_clean(); | |
| 1086 | - return false !== $output ? $output : ''; | |
| 1087 | - } | |
| 1088 | - | |
| 1089 | - /** | |
| 1090 | - * Render the admin-only notice shown when the payment field renders but no | |
| 1091 | - * real payment gateway is connected. | |
| 1092 | - * | |
| 1093 | - * Enabling Offline Donation makes the field render normally, so the | |
| 1094 | - * donor-facing "gateways are not configured" notice never fires — an admin | |
| 1095 | - * viewing the live form gets no signal that neither Stripe nor PayPal was | |
| 1096 | - * ever connected. Offline is a manual method, not a gateway, so the prompt | |
| 1097 | - * is still warranted. Donors see nothing: the form works for them. | |
| 1098 | - * | |
| 1099 | - * @return string Notice markup, or empty string when a gateway is connected | |
| 1100 | - * or the viewer is not an administrator. | |
| 1101 | - * @since 1.5.1 | |
| 1102 | - */ | |
| 1103 | - private function get_gateway_setup_notice() { | |
| 1104 | - if ( ! current_user_can( 'manage_options' ) || Payment_Helper::is_any_gateway_connected() ) { | |
| 1105 | - return ''; | |
| 1106 | - } | |
| 1107 | - | |
| 1108 | - $this->prevent_page_cache(); | |
| 1109 | - | |
| 1110 | - ob_start(); | |
| 1111 | - ?> | |
| 1112 | - <div class="sd-payment-notice sd-payment-notice--admin" role="status"> | |
| 1113 | - <p><?php esc_html_e( 'No payment gateway is connected, so donors can only give using the offline method.', 'suredonation' ); ?></p> | |
| 1114 | - <?php | |
| 1115 | - echo wp_kses_post( | |
| 1116 | - $this->render_configure_link( | |
| 1117 | - Payment_Helper::get_settings_url( 'stripe', [ 'sd_notice' => 'frontend_gateway_setup' ] ), | |
| 1118 | - __( 'Configure payment gateway', 'suredonation' ) | |
| 1119 | - ) | |
| 1120 | - ); | |
| 1121 | - ?> | |
| 1122 | - </div> | |
| 1123 | - <?php | |
| 1124 | - $output = ob_get_clean(); | |
| 1125 | - return false !== $output ? $output : ''; | |
| 1126 | - } | |
| 1127 | - | |
| 1128 | - /** | |
| 1129 | - * Render the admin-only notice shown when the card form was dropped because | |
| 1130 | - * the connected Stripe account cannot charge cards. | |
| 1131 | - * | |
| 1132 | - * Only fires when the field still renders, which means another gateway | |
| 1133 | - * covered for it. That is the quiet case: donors can still give, so the | |
| 1134 | - * donor-facing notice never runs, and an admin looking at the live form sees | |
| 1135 | - * a working PayPal button with no hint that the card option they configured | |
| 1136 | - * has silently gone. Leaving that unsaid is the same silence this whole | |
| 1137 | - * feature exists to end, just relocated — the site would keep taking a | |
| 1138 | - * fraction of the donations it expects until someone happened to notice. | |
| 1139 | - * | |
| 1140 | - * Donors see nothing: the form works for them, and the account state is the | |
| 1141 | - * site's business. | |
| 1142 | - * | |
| 1143 | - * @return string Notice markup, or empty string when the card form was not | |
| 1144 | - * dropped or the viewer is not an administrator. | |
| 1145 | - * @since 1.5.1 | |
| 1146 | - */ | |
| 1147 | - private function get_capability_notice() { | |
| 1148 | - if ( ! $this->stripe_card_blocked || ! current_user_can( 'manage_options' ) ) { | |
| 1149 | - return ''; | |
| 1150 | - } | |
| 1151 | - | |
| 1152 | - $this->prevent_page_cache(); | |
| 1153 | - | |
| 1154 | - ob_start(); | |
| 1155 | - ?> | |
| 1156 | - <div class="sd-payment-notice sd-payment-notice--admin" role="status"> | |
| 1157 | - <p><?php esc_html_e( 'Stripe is connected but cannot accept card payments for this account, so the card option is hidden on this form.', 'suredonation' ); ?></p> | |
| 1158 | - <?php | |
| 1159 | - echo wp_kses_post( | |
| 1160 | - $this->render_configure_link( | |
| 1161 | - // The marker rides the query string so the settings screen | |
| 1162 | - // can record that this notice is what brought the admin | |
| 1163 | - // there. Tracking the arrival rather than the click keeps | |
| 1164 | - // every byte of analytics off the public page the notice | |
| 1165 | - // renders on. | |
| 1166 | - Payment_Helper::get_settings_url( 'stripe', [ 'sd_notice' => 'stripe_capability' ] ), | |
| 1167 | - __( 'Review Stripe account status', 'suredonation' ) | |
| 1168 | - ) | |
| 1169 | - ); | |
| 1170 | - ?> | |
| 1171 | - </div> | |
| 1172 | - <?php | |
| 1173 | - $output = ob_get_clean(); | |
| 1174 | - return false !== $output ? $output : ''; | |
| 1175 | - } | |
| 1176 | - | |
| 1177 | - /** | |
| 1178 | - * Keep the current response out of any full-page cache. | |
| 1179 | - * | |
| 1180 | - * Several notices here are chosen by a `current_user_can()` check while | |
| 1181 | - * rendering a public donation page, so the HTML an admin receives differs | |
| 1182 | - * from a donor's. A full-page cache in front of the site does not know that: | |
| 1183 | - * if it stores an admin's copy, every later visitor is served admin-only | |
| 1184 | - * text — the account's Stripe status, and a deep link into wp-admin — and if | |
| 1185 | - * it stores a donor's, an admin stops being warned at all. | |
| 1186 | - * | |
| 1187 | - * Most caches already skip logged-in users, so this is belt to that braces; | |
| 1188 | - * it is cheap, and the failure it prevents is silent. `DONOTCACHEPAGE` is the | |
| 1189 | - * convention every major WordPress cache honours, and it is still read after | |
| 1190 | - * a block renders. `nocache_headers()` covers proxies, guarded because | |
| 1191 | - * headers are usually long gone by the time a block runs. | |
| 1192 | - * | |
| 1193 | - * @return void | |
| 1194 | - * @since 1.5.1 | |
| 1195 | - */ | |
| 1196 | - private function prevent_page_cache() { | |
| 1197 | - if ( ! defined( 'DONOTCACHEPAGE' ) ) { | |
| 1198 | - // Intentionally unprefixed: this is the name the caching plugins | |
| 1199 | - // read, so a prefixed one would do nothing at all. | |
| 1200 | - define( 'DONOTCACHEPAGE', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound | |
| 1201 | - } | |
| 1202 | - | |
| 1203 | - if ( ! headers_sent() ) { | |
| 1204 | - nocache_headers(); | |
| 1205 | - } | |
| 1206 | - } | |
| 1207 | - | |
| 1208 | - /** | |
| 1209 | - * Render the actionable settings link shared by the payment notices. | |
| 1210 | - * | |
| 1211 | - * @param string $url Target URL. | |
| 1212 | - * @param string $text Link text. | |
| 1213 | - * @return string Link markup, or empty string when there is nothing to link to. | |
| 1214 | - * @since 1.5.1 | |
| 1215 | - */ | |
| 1216 | - private function render_configure_link( $url, $text ) { | |
| 1217 | - if ( '' === $url || '' === $text ) { | |
| 1218 | - return ''; | |
| 1219 | - } | |
| 1220 | - | |
| 1221 | - ob_start(); | |
| 1222 | - ?> | |
| 1223 | - <a | |
| 1224 | - class="sd-payment-notice__configure" | |
| 1225 | - href="<?php echo esc_url( $url ); ?>" | |
| 1226 | - target="_blank" | |
| 1227 | - rel="noopener noreferrer" | |
| 1228 | - > | |
| 1229 | - <?php echo esc_html( $text ); ?> | |
| 1230 | - </a> | |
| 1231 | 662 | <?php |
| 1232 | 663 | $output = ob_get_clean(); |
| 1233 | 664 | return false !== $output ? $output : ''; |
| 1234 | 665 | } |