jQuery(document).ready(function($) {
function forceRenderFlatsomeTurnstile() {
// Look for any uninitialized Turnstile containers inside active popups
$('.mfp-content .cf-turnstile, #login-form-popup .cf-turnstile').each(function() {
var $widget = $(this);
// Render only if Cloudflare's API is ready and the box hasn't been mounted yet
if (typeof turnstile !== 'undefined' && !$widget.hasClass('ts-mounted')) {
var sitekey = $widget.attr('data-sitekey') || $widget.data('sitekey');
if (sitekey) {
turnstile.render(this, {
sitekey: sitekey,
theme: 'light',
size: 'normal'
});
$widget.addClass('ts-mounted');
}
}
});
}
// Monitor the site body for Flatsome modal injection actions
var flatsomeObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes.length) {
// If Flatsome elements open or appear, attempt initialization
if ($('.mfp-wrap').length || $('.lightbox-content').length) {
setTimeout(forceRenderFlatsomeTurnstile, 300); // 300ms cushion for slide-in animation
}
}
});
});
flatsomeObserver.observe(document.body, { childList: true, subtree: true });
// Backup trigger in case user toggles open/close rapidly
$(document).on('opened.magnificPopup', function() {
setTimeout(forceRenderFlatsomeTurnstile, 350);
});
});