1/* Woo Checkout */ 2jQuery( document ).ready(function() { 3 4 var cfturnstileWooCheckoutAttempted = false; 5 6 // Track actual submission attempts so we don't reset on every checkout refresh. 7 jQuery( document.body ).on( 'submit', 'form.checkout', function() { 8 cfturnstileWooCheckoutAttempted = true; 9 }); 10 11 // If the page loads with an existing checkout error force a reset so the next submission has a fresh token. 12 if ( jQuery('.woocommerce-error, .woocommerce-NoticeGroup-checkout .woocommerce-error').length ) { 13 setTimeout( turnstileWooCheckoutReset, 50 ); 14 } 15 16 jQuery( document.body ).on( 'update_checkout updated_checkout applied_coupon_in_checkout removed_coupon_in_checkout', function() { 17 // Re-render if the widget container was replaced/emptied or is missing entirely. 18 var $widget = jQuery('#cf-turnstile-woo-checkout'); 19 if ( !$widget.length || $widget.is(':empty') ) { 20 // Use longer delay to ensure DOM replacement is complete. 21 setTimeout( turnstileWooCheckoutReset, 300 ); 22 return; 23 } 24 25 // After a failed submit, Woo will typically refresh the checkout fragments. 26 // Reset here (only after an attempted submit) to avoid the stale/used token issue. 27 if ( cfturnstileWooCheckoutAttempted && jQuery('.woocommerce-error, .woocommerce-NoticeGroup-checkout .woocommerce-error').length ) { 28 setTimeout( turnstileWooCheckoutReset, 300 ); 29 cfturnstileWooCheckoutAttempted = false; 30 } 31 }); 32 33 // Woo triggers this when the AJAX checkout submission returns an error. 34 jQuery( document.body ).on( 'checkout_error', function() { 35 setTimeout( turnstileWooCheckoutReset, 500 ); 36 cfturnstileWooCheckoutAttempted = false; 37 }); 38}); 39function turnstileWooCheckoutReset() { 40 if ( typeof turnstile === 'undefined' ) { 41 return; 42 } 43 44 var el = document.getElementById('cf-turnstile-woo-checkout'); 45 if ( !el ) { 46 return; 47 } 48 49 // If Woo replaced the container and it's now empty, render a fresh widget. 50 if ( !el.innerHTML || el.innerHTML.trim() === '' ) { 51 try { 52 turnstile.render(el); 53 } catch (e) { 54 try { turnstile.render('#cf-turnstile-woo-checkout'); } catch (e2) {} 55 } 56 return; 57 } 58 59 // Preferred: reset the existing widget (clears the used/expired token). 60 try { 61 if ( typeof turnstile.reset === 'function' ) { 62 turnstile.reset(el); 63 return; 64 } 65 } catch (e) {} 66 67 // Fallback: remove + render. 68 try { 69 turnstile.remove(el); 70 } catch (e) { 71 try { turnstile.remove('#cf-turnstile-woo-checkout'); } catch (e2) {} 72 } 73 try { 74 turnstile.render(el); 75 } catch (e) { 76 try { turnstile.render('#cf-turnstile-woo-checkout'); } catch (e2) {} 77 } 78} 79/* On click ".checkout .showlogin" link re-render */ 80jQuery('.showlogin').on('click', function() { 81 turnstile.remove('.sct-woocommerce-login'); 82 turnstile.render('.sct-woocommerce-login'); 83}); 84 85/* Woo Checkout Block */ 86document.addEventListener('DOMContentLoaded', function() { 87 if (typeof wp !== 'undefined' && wp.data && typeof turnstile !== 'undefined') { 88 89 function setTurnstileExtensionData(token) { 90 var dispatch = wp.data.dispatch('wc/store/checkout'); 91 if (typeof dispatch.setExtensionData === 'function') { 92 dispatch.setExtensionData('simple-cloudflare-turnstile', { token: token }); 93 } else if (typeof dispatch.__internalSetExtensionData === 'function') { 94 dispatch.__internalSetExtensionData('simple-cloudflare-turnstile', { token: token }); 95 } 96 } 97 98 function cfturnstileWooBlockCheckoutRender() { 99 100 var turnstileItem = document.getElementById('cf-turnstile-woo-checkout'); 101 if (!turnstileItem) return; 102 103 // If already initialized, try reset to preserve state but clear token 104 if (turnstileItem.getAttribute('data-sct-init') === 'true' && turnstileItem.hasChildNodes()) { 105 try { 106 turnstile.reset(turnstileItem); 107 setTurnstileExtensionData(''); 108 return; 109 } catch (e) {} 110 } 111 112 // Ensure any existing widget (auto-rendered or otherwise) is removed 113 try { turnstile.remove(turnstileItem); } catch (e) {} 114 115 try { 116 turnstile.render(turnstileItem, { 117 sitekey: turnstileItem.dataset.sitekey, 118 callback: setTurnstileExtensionData, 119 'expired-callback': function() { setTurnstileExtensionData(''); } 120 }); 121 turnstileItem.setAttribute('data-sct-init', 'true'); 122 } catch (e) {} 123 } 124 125 // Re-render Turnstile after the Place order button is clicked 126 var cfturnstileWooBlockClickTimer = null; 127 jQuery(document.body).on('click', '.wc-block-components-checkout-place-order-button', function() { 128 if (cfturnstileWooBlockClickTimer) { 129 clearTimeout(cfturnstileWooBlockClickTimer); 130 } 131 cfturnstileWooBlockClickTimer = setTimeout(function() { 132 cfturnstileWooBlockCheckoutRender(); 133 }, 2000); 134 }); 135 136 // Render Turnstile when the checkout data is updated and the widget is not present or not initialized 137 var unsubscribe = wp.data.subscribe(function() { 138 const turnstileItem = document.getElementById('cf-turnstile-woo-checkout'); 139 if (turnstileItem && (turnstileItem.innerHTML.trim() === '' || turnstileItem.getAttribute('data-sct-init') !== 'true')) { 140 cfturnstileWooBlockCheckoutRender(); 141 } 142 }, 'wc/store/cart'); 143 } 144});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.