Plugin Directory

Changeset 3464235


Ignore:
Timestamp:
02/18/2026 10:55:20 AM (3 days ago)
Author:
gkanters
Message:

Remove unwanted caching artefacts

Location:
ai-translate/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ai-translate/trunk/README.md

    r3462546 r3464235  
    191191- Fix issue with language switcher in some specific themes.
    192192- All enabled and detectable languages added to Wordpress sitemap (/wp-sitemap.xml)
     193- Adviced models on top per API provider.
    193194- Code base refactoring. Improve performance and security, thighening code.
    194195- Better detect usable models from OpenAI. 
  • ai-translate/trunk/includes/class-ai-ob.php

    r3463619 r3464235  
    5252        } elseif (!$should_show_admin_bar && $has_admin_bar) {
    5353            // Regular user should NOT see admin bar but it's in cached content
    54             // Remove admin bar completely
    55             $html = preg_replace(
    56                 '/<div[^>]*id=["\']wpadminbar["\'][^>]*>.*?<\/div>/s',
    57                 '',
    58                 $html
    59             );
     54            // Remove admin bar robustly via DOM (regex breaks on nested toolbar markup).
     55            $doc = new \DOMDocument();
     56            $internalErrors = libxml_use_internal_errors(true);
     57            $htmlToLoad = AI_DOM::ensureUtf8((string) $html);
     58            $doc->loadHTML('<?xml encoding="utf-8" ?>' . $htmlToLoad, LIBXML_HTML_NODEFDTD);
     59            libxml_clear_errors();
     60            libxml_use_internal_errors($internalErrors);
     61
     62            $xpath = new \DOMXPath($doc);
     63            $adminNodes = $xpath->query('//*[@id="wpadminbar" or @id="wp-toolbar"]');
     64            if ($adminNodes !== false) {
     65                foreach ($adminNodes as $adminNode) {
     66                    if ($adminNode->parentNode) {
     67                        $adminNode->parentNode->removeChild($adminNode);
     68                    }
     69                }
     70            }
     71
     72            // Remove inline adminbar style blocks from old cached variants.
     73            $styleNodes = $xpath->query('//style');
     74            if ($styleNodes !== false) {
     75                foreach ($styleNodes as $styleNode) {
     76                    $styleText = strtolower((string) $styleNode->textContent);
     77                    if (strpos($styleText, '#wpadminbar') !== false ||
     78                        strpos($styleText, 'body{margin-top:32px') !== false ||
     79                        strpos($styleText, 'body {margin-top:32px') !== false) {
     80                        if ($styleNode->parentNode) {
     81                            $styleNode->parentNode->removeChild($styleNode);
     82                        }
     83                    }
     84                }
     85            }
     86
     87            // Also remove body/html "admin-bar" class to avoid layout offsets.
     88            $bodyNodes = $xpath->query('//body');
     89            if ($bodyNodes !== false) {
     90                foreach ($bodyNodes as $bodyNode) {
     91                    if ($bodyNode instanceof \DOMElement) {
     92                        $classAttr = trim((string) $bodyNode->getAttribute('class'));
     93                        if ($classAttr !== '') {
     94                            $classes = preg_split('/\s+/', $classAttr);
     95                            if (is_array($classes)) {
     96                                $classes = array_values(array_filter($classes, function ($className) {
     97                                    return $className !== 'admin-bar';
     98                                }));
     99                                if (!empty($classes)) {
     100                                    $bodyNode->setAttribute('class', implode(' ', $classes));
     101                                } else {
     102                                    $bodyNode->removeAttribute('class');
     103                                }
     104                            }
     105                        }
     106                    }
     107                }
     108            }
     109            $htmlNodes = $xpath->query('//html');
     110            if ($htmlNodes !== false) {
     111                foreach ($htmlNodes as $htmlNode) {
     112                    if ($htmlNode instanceof \DOMElement) {
     113                        $classAttr = trim((string) $htmlNode->getAttribute('class'));
     114                        if ($classAttr !== '') {
     115                            $classes = preg_split('/\s+/', $classAttr);
     116                            if (is_array($classes)) {
     117                                $classes = array_values(array_filter($classes, function ($className) {
     118                                    return $className !== 'admin-bar';
     119                                }));
     120                                if (!empty($classes)) {
     121                                    $htmlNode->setAttribute('class', implode(' ', $classes));
     122                                } else {
     123                                    $htmlNode->removeAttribute('class');
     124                                }
     125                            }
     126                        }
     127                    }
     128                }
     129            }
     130
     131            $html = $doc->saveHTML();
     132            $html = preg_replace('/<\?xml[^?]*\?>\s*/i', '', $html);
    60133        }
    61134
Note: See TracChangeset for help on using the changeset viewer.