/** * ========================================================= * PERFORMANCE PATCH SAFE * - defer script non critici * - fix accessibilità bottoni Owl Carousel * - alleggerisce alcune pagine senza toccare la logica esistente * ========================================================= */ /** * 1) DEFER per script non critici */ add_filter('script_loader_tag', function ($tag, $handle, $src) { $defer_handles = [ 'utility-megamenu', 'home-slider', 'mintur-last-news-mobile-slider', 'search-js', 'mintur-open-data', 'mdt-opendata-accordion', ]; if (in_array($handle, $defer_handles, true)) { return ''; } return $tag; }, 10, 3); /** * 2) FIX ACCESSIBILITÀ OWL CAROUSEL * Aggiunge aria-label ai bottoni prev/next ed elimina role="presentation" * Non rompe gli slider esistenti */ add_action('wp_enqueue_scripts', function () { // registro uno script "vuoto" solo per poter aggiungere JS inline wp_register_script('mintur-a11y-owl-fix', false, [], null, true); wp_enqueue_script('mintur-a11y-owl-fix'); $js = <<<'JS' document.addEventListener('DOMContentLoaded', function () { function fixOwlButtons(root) { root = root || document; root.querySelectorAll('.owl-prev').forEach(function(btn){ btn.removeAttribute('role'); if (!btn.getAttribute('aria-label')) { btn.setAttribute('aria-label', 'Elemento precedente'); } if (!btn.getAttribute('type')) { btn.setAttribute('type', 'button'); } }); root.querySelectorAll('.owl-next').forEach(function(btn){ btn.removeAttribute('role'); if (!btn.getAttribute('aria-label')) { btn.setAttribute('aria-label', 'Elemento successivo'); } if (!btn.getAttribute('type')) { btn.setAttribute('type', 'button'); } }); } fixOwlButtons(document); var observer = new MutationObserver(function() { fixOwlButtons(document); }); observer.observe(document.body, { childList: true, subtree: true }); }); JS; wp_add_inline_script('mintur-a11y-owl-fix', $js); }, 99); /** * 3) ALLEGGERIMENTO SAFE: * se alcuni handle sono già stati registrati/caricati globalmente, * li rimuove dove non servono * * NB: non tocca open-data (già gestita bene) */ add_action('wp_enqueue_scripts', function () { // Slider homepage: fuori dalla home non servono if (!is_front_page()) { wp_dequeue_script('home-slider'); wp_dequeue_script('mintur-last-news-mobile-slider'); } // Ricerca: fuori da /ricerca/ non servono if (!is_page('ricerca')) { wp_dequeue_script('search-js'); wp_dequeue_style('search-style'); } }, 100);