| 1 |
<?php |
| 2 |
/** |
| 3 |
* WooCommerce Payments (WooPayments) compatibility support. |
| 4 |
* |
| 5 |
* Prevents infinite checkout update loop caused by dynamic script |
| 6 |
* injection in the payment fragment on every update_order_review. |
| 7 |
* |
| 8 |
* @package RadiusTheme\SB |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace RadiusTheme\SB\Controllers\PluginsSupport; |
| 12 |
|
| 13 |
use RadiusTheme\SB\Helpers\BuilderFns; |
| 14 |
use RadiusTheme\SB\Traits\SingletonTrait; |
| 15 |
|
| 16 |
// Do not allow directly accessing this file. |
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit( 'This script cannot be accessed directly.' ); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* WooCommerce Payments compatibility class. |
| 23 |
* |
| 24 |
* WooPayments appends dynamic script tags (cart totals, currency, |
| 25 |
* nonces) to the .woocommerce-checkout-payment fragment on every |
| 26 |
* update_order_review AJAX response. Because these values change |
| 27 |
* each request, WooCommerce always sees the fragment as "new" and |
| 28 |
* replaces the DOM, causing Stripe to re-mount and fire another |
| 29 |
* update — creating an infinite loop. |
| 30 |
* |
| 31 |
* This class strips those dynamic scripts from the fragment and |
| 32 |
* moves them to a separate fragment key so the payment HTML stays |
| 33 |
* stable between requests. |
| 34 |
*/ |
| 35 |
class WooPaymentsSupport { |
| 36 |
|
| 37 |
/** |
| 38 |
* SingletonTrait. |
| 39 |
*/ |
| 40 |
use SingletonTrait; |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructor. |
| 44 |
*/ |
| 45 |
private function __construct() { |
| 46 |
add_filter( |
| 47 |
'woocommerce_update_order_review_fragments', |
| 48 |
[ $this, 'stabilize_payment_fragment' ], |
| 49 |
999 |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Stabilize the payment method fragment. |
| 55 |
* |
| 56 |
* Extracts inline script tags from the payment fragment so its |
| 57 |
* HTML stays identical between AJAX requests. The scripts are |
| 58 |
* moved to a separate fragment that JS can execute after DOM |
| 59 |
* replacement without triggering another update cycle. |
| 60 |
* |
| 61 |
* @param array $fragments Checkout AJAX fragments. |
| 62 |
* |
| 63 |
* @return array Modified fragments. |
| 64 |
*/ |
| 65 |
public function stabilize_payment_fragment( $fragments ) { |
| 66 |
$key = '.woocommerce-checkout-payment'; |
| 67 |
|
| 68 |
if ( empty( $fragments[ $key ] ) ) { |
| 69 |
return $fragments; |
| 70 |
} |
| 71 |
|
| 72 |
$html = $fragments[ $key ]; |
| 73 |
|
| 74 |
// Extract all <script> tags from the payment fragment. |
| 75 |
$scripts = ''; |
| 76 |
|
| 77 |
$html = preg_replace_callback( |
| 78 |
'#<script\b[^>]*>.*?</script>#is', |
| 79 |
static function ( $matches ) use ( &$scripts ) { |
| 80 |
$scripts .= $matches[0]; |
| 81 |
return ''; |
| 82 |
}, |
| 83 |
$html |
| 84 |
); |
| 85 |
|
| 86 |
// Set the cleaned (stable) HTML back as the fragment. |
| 87 |
$fragments[ $key ] = $html; |
| 88 |
|
| 89 |
// Pass extracted scripts in a separate fragment for JS execution. |
| 90 |
if ( ! empty( $scripts ) ) { |
| 91 |
$fragments['.rtsb-checkout-payment-scripts'] = '<div class="rtsb-checkout-payment-scripts" style="display:none;">' . $scripts . '</div>'; |
| 92 |
} |
| 93 |
|
| 94 |
return $fragments; |
| 95 |
} |
| 96 |
} |
| 97 |
|