, nodeIndex: array} */ public static function plan($html) { // CRITICAL: Remove speculationrules script tags completely before DOM processing // These contain JSON that DOMDocument corrupts when parsing/saving // Since prefetch/prerender rules should never be translated anyway, removing is safe $html = preg_replace('/]*>[\s\S]*?<\/script>/i', '', $html); // Extract and preserve other script/style tags to prevent DOMDocument from corrupting them // CRITICAL: Only extract from , never from . Using
placeholders // in causes DOMDocument to implicitly close and move ALL subsequent // head elements (meta, link, style, script) into , breaking page rendering. $placeholders = []; $placeholder_counter = 0; $headEndPos = stripos($html, ''); if ($headEndPos !== false) { $headPart = substr($html, 0, $headEndPos + 7); $bodyPart = substr($html, $headEndPos + 7); $bodyPart = self::extractAndReplace($bodyPart, 'script', $placeholders, $placeholder_counter); $bodyPart = self::extractAndReplace($bodyPart, 'style', $placeholders, $placeholder_counter); $html = $headPart . $bodyPart; } else { $html = self::extractAndReplace($html, 'script', $placeholders, $placeholder_counter); $html = self::extractAndReplace($html, 'style', $placeholders, $placeholder_counter); } $doc = new \DOMDocument(); $internalErrors = libxml_use_internal_errors(true); $htmlToLoad = self::ensureUtf8($html); $flags = LIBXML_HTML_NODEFDTD; // Hint UTF-8 to libxml to avoid mojibake for emojis/special chars $doc->loadHTML('' . $htmlToLoad, $flags); libxml_clear_errors(); libxml_use_internal_errors($internalErrors); $segments = []; $nodeIndex = []; $counter = 0; $visitedTextNodes = new \SplObjectStorage(); $exclusions = ['script','style','code','pre','noscript','img','video','audio','source','track']; $translatableTags = ['title','p','h1','h2','h3','h4','h5','h6','li','td','th','caption','figcaption','span','label','a','button','input','textarea','select','div']; $attrNames = ['title','alt','placeholder','aria-label','value']; $xpath = new \DOMXPath($doc); // Exclude nodes by tag (but collect descendant text nodes for each translatable tag) foreach ($translatableTags as $tag) { $nodes = $xpath->query('//' . $tag); if (!$nodes) continue; foreach ($nodes as $node) { if (self::isExcluded($node, $exclusions)) { continue; } if ($node instanceof \DOMElement && $node->hasAttribute('data-ai-trans-skip')) { continue; } // Collect descendant text nodes (preserve inline markup and cover

, , etc.) $textNodes = self::collectTextNodes($node, $exclusions, $visitedTextNodes); if (!empty($textNodes)) { foreach ($textNodes as $tn) { $orig = (string) $tn->nodeValue; if (trim($orig) === '') { continue; } $id = 't' . (++$counter); // Preserve original leading/trailing whitespace to avoid losing spaces around inline elements $isMenu = self::isMenuContext($tn); $segments[] = ['id' => $id, 'text' => $orig, 'type' => ($isMenu ? 'menu' : 'node')]; $nodeIndex[$id] = $tn; // map to DOMText for precise merge } } // Attributes if ($node instanceof \DOMElement) { // Skip elements with data-ai-trans-skip attribute (matches JavaScript behavior) if ($node->hasAttribute('data-ai-trans-skip')) { continue; } // Include input[type=submit|button|reset] value if (strtolower($node->tagName) === 'input') { $type = strtolower($node->getAttribute('type')); if (in_array($type, ['submit','button','reset'], true)) { $val = trim($node->getAttribute('value')); if ($val !== '') { // Normalize: replace multiple spaces with single space (matches JavaScript behavior) $normalized = preg_replace('/\s+/u', ' ', $val); $id = 'a' . (++$counter); $segments[] = ['id' => $id, 'text' => $normalized, 'type' => 'attr', 'attr' => 'value']; $nodeIndex[$id] = $node; } } } foreach ($attrNames as $an) { if ($node->hasAttribute($an)) { $val = trim($node->getAttribute($an)); if ($val !== '') { // Normalize: replace multiple spaces with single space (matches JavaScript behavior) $normalized = preg_replace('/\s+/u', ' ', $val); $id = 'a' . (++$counter); $segments[] = ['id' => $id, 'text' => $normalized, 'type' => 'attr', 'attr' => $an]; $nodeIndex[$id] = $node; } } } } } } // Systematically collect UI attributes using the EXACT same logic as JavaScript // JavaScript: querySelectorAll("input,textarea,select,button,[title],[aria-label],.initial-greeting,.chatbot-bot-text") // This ensures we collect ALL UI elements that JavaScript also collects, preventing trial-and-error $uiElements = $xpath->query('//input | //textarea | //select | //button | //*[@title] | //*[@aria-label] | //*[contains(@class, "initial-greeting")] | //*[contains(@class, "chatbot-bot-text")]'); if ($uiElements) { $processedElements = new \SplObjectStorage(); foreach ($uiElements as $node) { if (!$node instanceof \DOMElement) { continue; } if (self::isExcluded($node, $exclusions)) { continue; } // Skip elements with data-ai-trans-skip attribute (matches JavaScript behavior) if ($node->hasAttribute('data-ai-trans-skip')) { continue; } // Skip if already processed in translatableTags loop (to avoid duplicates) if (isset($processedElements[$node])) { continue; } $processedElements[$node] = true; // Normalize function (matches JavaScript: trim and replace multiple spaces) $normalize = function($text) { return preg_replace('/\s+/u', ' ', trim($text)); }; // Collect placeholder (for input, textarea, select) $tagName = strtolower($node->tagName ?? ''); if (in_array($tagName, ['input', 'textarea', 'select'], true)) { if ($node->hasAttribute('placeholder')) { $val = $normalize($node->getAttribute('placeholder')); if ($val !== '' && mb_strlen($val) >= 2) { // Check if already collected $alreadyCollected = false; foreach ($nodeIndex as $mapped) { if ($mapped === $node) { foreach ($segments as $seg) { if (isset($seg['attr']) && $seg['attr'] === 'placeholder' && $nodeIndex[$seg['id']] === $node) { $alreadyCollected = true; break 2; } } } } if (!$alreadyCollected) { $id = 'a' . (++$counter); $segments[] = ['id' => $id, 'text' => $val, 'type' => 'attr', 'attr' => 'placeholder']; $nodeIndex[$id] = $node; } } } } // Collect title if ($node->hasAttribute('title')) { $val = $normalize($node->getAttribute('title')); if ($val !== '' && mb_strlen($val) >= 2) { $alreadyCollected = false; foreach ($nodeIndex as $mapped) { if ($mapped === $node) { foreach ($segments as $seg) { if (isset($seg['attr']) && $seg['attr'] === 'title' && $nodeIndex[$seg['id']] === $node) { $alreadyCollected = true; break 2; } } } } if (!$alreadyCollected) { $id = 'a' . (++$counter); $segments[] = ['id' => $id, 'text' => $val, 'type' => 'attr', 'attr' => 'title']; $nodeIndex[$id] = $node; } } } // Collect aria-label if ($node->hasAttribute('aria-label')) { $val = $normalize($node->getAttribute('aria-label')); if ($val !== '' && mb_strlen($val) >= 2) { $alreadyCollected = false; foreach ($nodeIndex as $mapped) { if ($mapped === $node) { foreach ($segments as $seg) { if (isset($seg['attr']) && $seg['attr'] === 'aria-label' && $nodeIndex[$seg['id']] === $node) { $alreadyCollected = true; break 2; } } } } if (!$alreadyCollected) { $id = 'a' . (++$counter); $segments[] = ['id' => $id, 'text' => $val, 'type' => 'attr', 'attr' => 'aria-label']; $nodeIndex[$id] = $node; } } } // Collect value for input[type=submit|button|reset] (matches JavaScript behavior) if ($tagName === 'input') { $type = strtolower($node->getAttribute('type') ?? ''); if (in_array($type, ['submit', 'button', 'reset'], true)) { if ($node->hasAttribute('value')) { $val = $normalize($node->getAttribute('value')); if ($val !== '' && mb_strlen($val) >= 2) { $alreadyCollected = false; foreach ($nodeIndex as $mapped) { if ($mapped === $node) { foreach ($segments as $seg) { if (isset($seg['attr']) && $seg['attr'] === 'value' && $nodeIndex[$seg['id']] === $node) { $alreadyCollected = true; break 2; } } } } if (!$alreadyCollected) { $id = 'a' . (++$counter); $segments[] = ['id' => $id, 'text' => $val, 'type' => 'attr', 'attr' => 'value']; $nodeIndex[$id] = $node; } } } } } // Collect textContent for .initial-greeting and .chatbot-bot-text (matches JavaScript behavior) $classes = $node->getAttribute('class') ?? ''; if (strpos($classes, 'initial-greeting') !== false || strpos($classes, 'chatbot-bot-text') !== false) { $text = trim($node->textContent ?? ''); if ($text !== '' && mb_strlen($text) >= 2) { $normalized = $normalize($text); // Collect text nodes from this element $textNodes = self::collectTextNodes($node, $exclusions, new \SplObjectStorage()); if (!empty($textNodes)) { $tn = $textNodes[0]; // Check if this text node is already in index $existingId = null; foreach ($nodeIndex as $id => $mapped) { if ($mapped === $tn) { $existingId = $id; break; } } if ($existingId !== null) { // Update existing segment with normalized text foreach ($segments as &$seg) { if ($seg['id'] === $existingId) { $seg['text'] = $normalized; break; } } unset($seg); } else { // Add new segment $id = 't' . (++$counter); $segments[] = ['id' => $id, 'text' => $normalized, 'type' => 'node']; $nodeIndex[$id] = $tn; } } else { // No text nodes found, use element itself $id = 't' . (++$counter); $segments[] = ['id' => $id, 'text' => $normalized, 'type' => 'node']; $nodeIndex[$id] = $node; } } } } } // Additionally: collect any remaining text nodes not under the above tags (global sweep) $allTextNodes = self::collectTextNodes($doc, $exclusions, $visitedTextNodes); if (!empty($allTextNodes)) { foreach ($allTextNodes as $tn) { $orig = (string) $tn->nodeValue; if (trim($orig) === '') continue; // Avoid duplicates: only include if not already in index $already = false; foreach ($nodeIndex as $mapped) { if ($mapped === $tn) { $already = true; break; } } if ($already) continue; $id = 't' . (++$counter); // Preserve whitespace $isMenu = self::isMenuContext($tn); $segments[] = ['id' => $id, 'text' => $orig, 'type' => ($isMenu ? 'menu' : 'node')]; $nodeIndex[$id] = $tn; } } // Collect alt attributes from elements // img is excluded from text node extraction (void element) but alt text IS translatable $imgAltExclusions = array_values(array_diff($exclusions, ['img'])); $imgElements = $xpath->query('//img[@alt]'); if ($imgElements) { foreach ($imgElements as $img) { if (!$img instanceof \DOMElement) continue; if (self::isExcluded($img, $imgAltExclusions)) continue; $alt = trim($img->getAttribute('alt')); if ($alt !== '' && mb_strlen($alt) >= 2) { $normalized = preg_replace('/\s+/u', ' ', $alt); $id = 'a' . (++$counter); $segments[] = ['id' => $id, 'text' => $normalized, 'type' => 'attr', 'attr' => 'alt']; $nodeIndex[$id] = $img; } } } // OG title/description/image:alt (but skip meta name="description" - handled by AI_SEO::inject()) // AI_SEO handles meta description separately to ensure admin setting is used $metaDesc = $xpath->query('//meta[translate(@property, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="og:title" or translate(@property, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="og:description" or translate(@property, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="og:image:alt"]'); if ($metaDesc) { foreach ($metaDesc as $m) { if ($m instanceof \DOMElement) { $content = trim((string) $m->getAttribute('content')); if ($content !== '') { $id = 'm' . (++$counter); $segments[] = ['id' => $id, 'text' => $content, 'type' => 'attr', 'attr' => 'content']; $nodeIndex[$id] = $m; } } } } return [ 'doc' => $doc, 'segments' => $segments, 'nodeIndex' => $nodeIndex, 'originalHTML' => $html, 'placeholders' => $placeholders, ]; } /** * Extract tags by type (script/style) and replace with placeholders. * More robust than regex as it handles attributes with special chars. * * @param string $html * @param string $tagName * @param array $placeholders * @param int $counter * @return string */ public static function extractAndReplace($html, $tagName, &$placeholders, &$counter) { $result = ''; $pos = 0; $tagLower = strtolower($tagName); $openTag = '<' . $tagName; $closeTag = ''; $closeLowerTag = ''; while (($start = stripos($html, $openTag, $pos)) !== false) { // Add content before this tag $result .= substr($html, $pos, $start - $pos); // Find the end of opening tag (look for >) $tagEnd = strpos($html, '>', $start); if ($tagEnd === false) { // No closing > found, treat rest as normal content $result .= substr($html, $start); break; } // Find closing tag (case-insensitive) $closePos = stripos($html, $closeTag, $tagEnd); if ($closePos === false) { // No closing tag found, include what we have $result .= substr($html, $start); break; } // Extract full tag including content $fullTag = substr($html, $start, $closePos - $start + strlen($closeTag)); // Create placeholder using a div with data attribute - divs are reliably preserved by DOMDocument // Script tags with type="text/plain" may be removed, and HTML comments can also be removed $placeholderId = strtoupper($tagName) . '_PLACEHOLDER_' . (++$counter); $placeholder = '

'; $placeholders[$placeholderId] = $fullTag; $result .= $placeholder; // Move position forward $pos = $closePos + strlen($closeTag); } // Add remaining content if ($pos < strlen($html)) { $result .= substr($html, $pos); } return $result; } /** * Restore a placeholder div with the original script/style tag. * Uses multiple patterns to handle DOMDocument output variations (attribute order, * xmlns, self-closing, whitespace) that can cause preloader/Elementor conflicts. * * @param string $html HTML containing placeholder divs * @param string $placeholderId Placeholder ID (e.g. SCRIPT_PLACEHOLDER_1) * @param string $original_tag Original script or style tag to restore * @return string HTML with placeholder replaced */ public static function restorePlaceholder($html, $placeholderId, $original_tag) { $escapedId = preg_quote($placeholderId, '/'); $patterns = [ // Primary: standard
, any attribute order '/]*\bdata-ai-placeholder\s*=\s*["\']' . $escapedId . '["\'][^>]*>\s*<\/div>/is', // Fallback: data-ai-placeholder first (common DOMDocument output) '/]*>\s*<\/div>/is', // Fallback: self-closing
(some parsers) '/]*\bdata-ai-placeholder\s*=\s*["\']' . $escapedId . '["\'][^>]*\/>/is', // Fallback: loose match for edge cases '/]*data-ai-placeholder=["\']' . $escapedId . '["\'][^>]*>[\s\S]*?<\/div>/is', ]; foreach ($patterns as $pattern) { if (preg_match($pattern, $html)) { return preg_replace($pattern, $original_tag, $html, 1); } } return $html; } /** * Merge translated segments into the DOM and return final HTML. * * @param array $plan * @param array $translations map id => translated string * @return string */ public static function merge(array $plan, array $translations) { $doc = $plan['doc']; $segments = $plan['segments']; $nodeIndex = $plan['nodeIndex']; foreach ($segments as $seg) { $id = $seg['id']; if (!isset($translations[$id])) { continue; } $translated = (string) $translations[$id]; $node = $nodeIndex[$id] ?? null; if (!$node) continue; if (($seg['type'] ?? '') === 'attr') { $attr = $seg['attr'] ?? null; if ($attr && $node instanceof \DOMElement) { $node->setAttribute($attr, $translated); } } else { // Replace at text-node granularity when available if ($node instanceof \DOMText) { // Preserve leading/trailing spaces that often represent word boundaries $orig = (string) $node->nodeValue; $lead = ''; $trail = ''; // Capture whitespace only (spaces, tabs, non-breaking spaces) at both ends if ($orig !== '') { if (preg_match('/^([\x{00A0}\s]+)/u', $orig, $m1)) { $lead = (string) $m1[1]; } if (preg_match('/([\x{00A0}\s]+)$/u', $orig, $m2)) { $trail = (string) $m2[1]; } } // Also ensure a space between adjacent inline anchors/text when original had none trimmed away // Only add an extra single space if both ends are non-empty words and no whitespace exists between $node->nodeValue = $lead . $translated . $trail; } else { self::replaceNodeText($node, $translated); } } } // Cleanup: remove duplicated words around anchors introduced by segmented translation self::fixAnchorAdjacencyDuplicates($doc); // Return the full HTML - DOMDocument has fixed any structural issues $result = $doc->saveHTML(); // Remove XML declaration added by loadHTML (causes quirks mode) $result = preg_replace('/<\?xml[^?]*\?>\s*/i', '', $result); // Preserve DOCTYPE from original HTML to avoid quirks mode $origHTML = isset($plan['originalHTML']) ? $plan['originalHTML'] : ''; if ($origHTML && preg_match('/^(]*>)/i', (string) $origHTML, $docMatch)) { // Ensure we don't duplicate if saveHTML already includes it if (stripos($result, ' $original_tag) { $result = self::restorePlaceholder($result, $placeholderId, $original_tag); } } return $result; } /** * Return true when $node or any of its ancestors should be excluded from translation. * Excludes nodes inside excluded tag names, WordPress admin bar, data-ai-trans-skip elements, * wp-admin/no-translate/notranslate classes, and ACF field wrappers. * * @param \DOMNode $node * @param string[] $exclusions Tag names that block translation (e.g. 'script', 'style'). * @return bool */ private static function isExcluded(\DOMNode $node, array $exclusions) { for ($n = $node; $n; $n = $n->parentNode) { if ($n instanceof \DOMElement) { $tag = strtolower($n->tagName); if (in_array($tag, $exclusions, true)) { return true; } // Skip WordPress admin bar (front-end toolbar) $id = strtolower((string) $n->getAttribute('id')); if ($id === 'wpadminbar' || $id === 'wp-toolbar') { return true; } if ($n->hasAttribute('data-ai-trans-skip')) { return true; } $classAttr = ' ' . strtolower((string) $n->getAttribute('class')) . ' '; if (strpos($classAttr, ' wp-admin ') !== false || strpos($classAttr, ' no-translate ') !== false || strpos($classAttr, ' notranslate ') !== false) { return true; } // Optional: keep WooCommerce product names untranslated (brand names/SKUs). // Reuses the notranslate mechanism for known product-name elements. if (self::keepProductNamesEnabled() && (strpos($classAttr, ' product_title ') !== false || strpos($classAttr, ' woocommerce-loop-product__title ') !== false || strpos($classAttr, ' product-name ') !== false || strpos($classAttr, ' wc-block-components-product-name ') !== false) ) { return true; } // Skip ACF fields that should not be translated individually (only as part of page content) if (strpos($classAttr, ' acf-field ') !== false || strpos($classAttr, ' acf- ') !== false) { return true; } } } return false; } /** * Whether the "Do not translate product names" WooCommerce setting is enabled. * Result is cached statically for the request. * * @return bool */ private static function keepProductNamesEnabled() { static $keep = null; if ($keep === null) { $settings = \AITranslate\AI_Translate_Core::settings(); $keep = !empty($settings['woocommerce_keep_product_names']); } return $keep; } /** * Collect all descendant DOMText nodes that are not inside excluded tags. * Uses a visited set to avoid duplicates when multiple tag selectors overlap. * * @param \DOMNode $root * @param array $exclusions * @param \SplObjectStorage $visited * @return \DOMText[] */ private static function collectTextNodes(\DOMNode $root, array $exclusions, \SplObjectStorage $visited) { $result = []; $stack = [$root]; while (!empty($stack)) { /** @var \DOMNode $n */ $n = array_pop($stack); if ($n instanceof \DOMElement) { $tag = strtolower($n->tagName); if (in_array($tag, $exclusions, true)) { continue; } if ($n->hasAttribute('data-ai-trans-skip')) { continue; } } foreach (iterator_to_array($n->childNodes ?? []) as $child) { if ($child->nodeType === XML_TEXT_NODE) { // Check if text node has any excluded ancestor if (!isset($visited[$child]) && !self::isExcluded($child, $exclusions)) { $visited[$child] = true; $result[] = $child; } } else { $stack[] = $child; } } } return $result; } /** * Replace all direct text-node children of $node with a single new text node. * Non-text child nodes (e.g. inline elements) are left in place. * * @param \DOMNode $node * @param string $text Replacement text. * @return void */ private static function replaceNodeText(\DOMNode $node, $text) { // Remove existing text nodes $toRemove = []; foreach ($node->childNodes as $child) { if ($child->nodeType === XML_TEXT_NODE) { $toRemove[] = $child; } } foreach ($toRemove as $c) { $node->removeChild($c); } $node->insertBefore($node->ownerDocument->createTextNode($text), $node->firstChild); } /** * Ensure HTML string is valid UTF-8. * * @param string $html * @return string */ public static function ensureUtf8($html) { if (!\mb_detect_encoding($html, 'UTF-8', true)) { $html = \mb_convert_encoding($html, 'UTF-8'); } return $html; } /** * Remove duplicated adjacent anchor text such as "... Kontakt Kontakt ...". * Heuristic: if previous/next text node repeats the anchor's text, drop the duplicate. * * @param \DOMDocument $doc * @return void */ private static function fixAnchorAdjacencyDuplicates(\DOMDocument $doc) { $xpath = new \DOMXPath($doc); $links = $xpath->query('//a'); if (!$links) return; foreach ($links as $a) { if (!$a instanceof \DOMElement) continue; $anchorText = trim((string) $a->textContent); if ($anchorText === '') continue; // Previous sibling duplicate ("... Kontakt" + Kontakt) $prev = $a->previousSibling; if ($prev && $prev->nodeType === XML_TEXT_NODE) { $prevText = (string) $prev->nodeValue; $prevTrimRight = rtrim($prevText); if ($prevTrimRight !== '' && \mb_strlen($prevTrimRight) >= \mb_strlen($anchorText)) { $tail = \mb_substr($prevTrimRight, -\mb_strlen($anchorText)); if ($tail === $anchorText) { $newPrev = \mb_substr($prevTrimRight, 0, \mb_strlen($prevTrimRight) - \mb_strlen($anchorText)); $newPrev = rtrim($newPrev); if ($newPrev !== '' && substr($newPrev, -1) !== ' ') { $newPrev .= ' '; } $prev->nodeValue = $newPrev; } } } // Next sibling duplicate (Kontakt + " Kontakt ...") $next = $a->nextSibling; if ($next && $next->nodeType === XML_TEXT_NODE) { $nextText = (string) $next->nodeValue; $nextTrimLeft = ltrim($nextText); if ($nextTrimLeft !== '' && \mb_strlen($nextTrimLeft) >= \mb_strlen($anchorText)) { $head = \mb_substr($nextTrimLeft, 0, \mb_strlen($anchorText)); if ($head === $anchorText) { $rest = \mb_substr($nextTrimLeft, \mb_strlen($anchorText)); $rest = ltrim($rest); if ($rest !== '' && $rest[0] !== ' ') { $rest = ' ' . $rest; } $next->nodeValue = $rest; } } } } } /** * Detect if a text node is likely inside a site navigation/menu context. * Heuristics: inside