(function (){
var WIDGET_ID='cf-turnstile-woo-checkout';
function cfturnstileWooWidget(){
return document.getElementById(WIDGET_ID);
}
function cfturnstileWooOnReady(fn){
if(document.readyState==='loading'){
document.addEventListener('DOMContentLoaded', fn);
}else{
fn();
}}
function cfturnstileWooClicked(e, selector){
return !!(e.target&&e.target.closest&&e.target.closest(selector) );
}
function cfturnstileWooOpts(target){
return(typeof window.cfturnstileOpts==='function') ? window.cfturnstileOpts(target):{};}
function turnstileWooCheckoutReset(){
if(typeof turnstile==='undefined'){
return;
}
var el=cfturnstileWooWidget();
if(!el){
return;
}
if(!el.firstElementChild){
try { turnstile.render(el, cfturnstileWooOpts(el) ); } catch(e){}
return;
}
try {
turnstile.reset(el);
return;
} catch(e){}
try { turnstile.remove(el); } catch(e){}
try { turnstile.render(el, cfturnstileWooOpts(el) ); } catch(e){}}
/* "Show login" toggle: rebuild that widget once the panel is open. Bound natively on document
so it survives fragment refreshes and works whether or not jQuery has loaded. */
document.addEventListener('click', function(e){
if(!cfturnstileWooClicked(e, '.showlogin, .show-login, .e-show-login, .woocommerce-form-login-toggle a') ){
return;
}
setTimeout(function (){
if(typeof turnstile==='undefined'){
return;
}
try { turnstile.remove('.sct-woocommerce-login'); } catch(err){}
try { turnstile.render('.sct-woocommerce-login', cfturnstileWooOpts('.sct-woocommerce-login') ); } catch(err){}}, 250);
});
function cfturnstileWooRun(){
var $=window.jQuery;
$(document).ready(function (){
var attempted=false;
function hasCheckoutError(){
return !!document.querySelector('.woocommerce-error');
}
$(document.body).on('submit', 'form.checkout', function (){
attempted=true;
});
if(hasCheckoutError()){
setTimeout(turnstileWooCheckoutReset, 50);
}
$(document.body).on('update_checkout updated_checkout applied_coupon_in_checkout removed_coupon_in_checkout', function (){
var el=cfturnstileWooWidget();
if(!el||!el.firstElementChild){
setTimeout(turnstileWooCheckoutReset, 300);
return;
}
if(attempted&&hasCheckoutError()){
setTimeout(turnstileWooCheckoutReset, 300);
attempted=false;
}});
$(document.body).on('checkout_error', function (){
setTimeout(turnstileWooCheckoutReset, 500);
attempted=false;
});
});
}
function cfturnstileWooBlockRun(){
if(!document.querySelector('.wp-block-woocommerce-checkout, .wc-block-checkout')||typeof wp==='undefined'||!wp.data){
return;
}
function setExtensionData(token){
var dispatch=wp.data.dispatch('wc/store/checkout');
if(!dispatch){
return;
}
if(typeof dispatch.setExtensionData==='function'){
dispatch.setExtensionData('simple-cloudflare-turnstile', { token: token });
}else if(typeof dispatch.__internalSetExtensionData==='function'){
dispatch.__internalSetExtensionData('simple-cloudflare-turnstile', { token: token });
}}
function renderBlockWidget(){
if(typeof turnstile==='undefined'){
return;
}
var el=cfturnstileWooWidget();
if(!el){
return;
}
if(el.getAttribute('data-sct-init')==='true'&&el.firstElementChild){
try {
turnstile.reset(el);
setExtensionData('');
return;
} catch(e){}}
try { turnstile.remove(el); } catch(e){}
try {
turnstile.render(el, {
sitekey: el.dataset.sitekey,
appearance: el.dataset.appearance||'always',
callback: setExtensionData,
'expired-callback': function (){ setExtensionData(''); }});
el.setAttribute('data-sct-init', 'true');
} catch(e){}}
var clickTimer=null;
document.addEventListener('click', function(e){
if(!cfturnstileWooClicked(e, '.wc-block-components-checkout-place-order-button') ){
return;
}
clearTimeout(clickTimer);
clickTimer=setTimeout(renderBlockWidget, 2000);
});
wp.data.subscribe(function (){
var el=cfturnstileWooWidget();
if(el&&(!el.firstElementChild||el.getAttribute('data-sct-init')!=='true') ){
renderBlockWidget();
}}, 'wc/store/cart');
renderBlockWidget();
}
cfturnstileWooOnReady(cfturnstileWooBlockRun);
if(typeof window.jQuery!=='undefined'){
cfturnstileWooRun();
}else{
var tries=0;
var wait=setInterval(function (){
if(typeof window.jQuery!=='undefined'){
clearInterval(wait);
cfturnstileWooRun();
}else if(++tries > 1200){
clearInterval(wait);
}}, 50);
}})();