Plugin Directory

Changeset 3462482


Ignore:
Timestamp:
02/16/2026 12:06:35 PM (5 days ago)
Author:
gkanters
Message:

Harden language switcher behavior across themes by preventing hash-anchor hijacks, replacing fixed mobile init timeout with retry binding, removing fragile style-attribute CSS selector, and using translated title cleanup plus root-safe redirect.

Location:
ai-translate/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • ai-translate/trunk/ai-translate.php

    r3462432 r3462482  
    654654                \AITranslate\AI_Lang::set_cookie((string) $defaultLang);
    655655                \AITranslate\AI_Lang::set_current((string) $defaultLang);
    656                 // Add JavaScript redirect to / after page renders
     656                // Add JavaScript redirect to site root after page renders
    657657                add_action('wp_footer', function() {
    658                     echo '<script>setTimeout(function(){window.location.href="/";},100);</script>';
     658                    echo '<script>window.location.replace(' . wp_json_encode(home_url('/')) . ');</script>';
    659659                }, 999);
    660660                // Continue to render page - Rule 3 below will be skipped
     
    29312931                $menu_item->ID,
    29322932                array(
    2933                     'menu-item-title' => 'Language Switcher'
     2933                            'menu-item-title' => __('Language Switcher', 'ai-translate')
    29342934                )
    29352935            );
  • ai-translate/trunk/assets/language-switcher.css

    r3436692 r3462482  
    126126
    127127/* Force show submenu when JavaScript sets ai-force-show class */
    128 .menu-item-language-switcher .sub-menu.ai-force-show,
    129 .menu-item-language-switcher .sub-menu[style*="display: block"] {
     128.menu-item-language-switcher .sub-menu.ai-force-show {
    130129    display: block !important;
    131130    opacity: 1 !important;
  • ai-translate/trunk/assets/language-switcher.js

    r3436692 r3462482  
    218218 */
    219219function initMobileMenuSupport() {
    220     // Wait for mobile menu to be ready
    221     setTimeout(function() {
     220    let attempts = 0;
     221    const maxAttempts = 10;
     222
     223    function bindMobileSwitchers() {
    222224        // Find language switcher menu items
    223225        const languageItems = document.querySelectorAll('.menu-item-language-switcher');
     226        let boundCount = 0;
    224227
    225228        languageItems.forEach(function(item) {
     
    229232            // Only handle dropdown items (with submenu), inline items work without JS
    230233            if (link && submenu) {
     234                if (link.getAttribute('data-ai-mobile-bound') === '1') {
     235                    return;
     236                }
     237                link.setAttribute('data-ai-mobile-bound', '1');
     238                boundCount++;
     239
    231240                // Add click handler for mobile
    232241                link.addEventListener('click', function(event) {
     242                    // Prevent hash-jumps and theme anchor handlers from hijacking this toggle.
     243                    event.preventDefault();
     244
    233245                    // Only handle click on mobile/tablet (screen width <= 768px)
    234246                    if (window.innerWidth <= 768) {
    235                         event.preventDefault();
    236247                        event.stopPropagation();
    237248
     
    270281        });
    271282
    272 
    273     }, 1000); // Shorter timeout for testing
     283        // Retry shortly for themes that render/replace menus after initial load.
     284        if (boundCount === 0 && attempts < maxAttempts) {
     285            attempts++;
     286            setTimeout(bindMobileSwitchers, 300);
     287        }
     288    }
     289
     290    bindMobileSwitchers();
    274291}
  • ai-translate/trunk/assets/switcher.js

    r3438193 r3462482  
    6464      // Handle dropdown toggle button clicks
    6565      b.addEventListener("click", function (e) {
     66        e.preventDefault();
    6667        e.stopPropagation();
    6768        var open = !w.classList.contains("ai-trans-open");
Note: See TracChangeset for help on using the changeset viewer.