= max_ms ) {' . "\n" . ' clearInterval(timer);' . "\n" . ' // Switch to slow polling (1s) so we still init even without user interaction later.' . "\n" . ' var slow = setInterval(function(){ if ( is_ready() && run_once() ) clearInterval(slow); }, 1000);' . "\n" . ' try{console.warn("WPBC: _wpbc not detected within " + max_ms + "ms; using slow polling.");}catch(_){}' . "\n" . ' }' . "\n" . ' }, step);' . "\n" . // Listen for your custom ready events (emit from core once _wpbc is ready). ' var evs = ' . wp_json_encode( array_values( $custom_events ) ) . ';' . "\n" . ' evs.forEach(function(name){' . "\n" . ' document.addEventListener(name, function onready(){ if (is_ready() && run_once()) document.removeEventListener(name, onready); });' . "\n" . ' });' . "\n" . // Standard lifecycle events (covers DOMContentLoaded + load). ' if (document.readyState === "loading") {' . "\n" . ' document.addEventListener("DOMContentLoaded", function(){ if (is_ready()) run_once(); }, { once:true });' . "\n" . ' }' . "\n" . ' window.addEventListener("load", function(){ if (is_ready()) run_once(); }, { once:true });' . "\n" . ' window.addEventListener("pageshow", function(){ if (is_ready()) run_once(); }, { once:true });' . "\n" . ' document.addEventListener("visibilitychange", function(){ if (!document.hidden && is_ready()) run_once(); });' . "\n" . // User interaction hooks — some cache plugins load delayed JS only after interaction. ' var ui = ' . wp_json_encode( array_values( $user_events ) ) . ';' . "\n" . ' var ui_bailed = false;' . "\n" . ' function on_ui(){ if (ui_bailed) return; if (is_ready() && run_once()){ ui_bailed = true; ui.forEach(function(t){ document.removeEventListener(t, on_ui, true); }); } }' . "\n" . ' ui.forEach(function(t){ document.addEventListener(t, on_ui, true); });' . "\n" . '})();' . "\n" . '})();'; wp_add_inline_script( 'wpbc_all', $boot_js ); } add_action( 'wpbc_enqueue_js_files', 'wpbc_localize_js_vars', 51 );