In-request translation cache keyed by "{lang}|{md5(text)}". */ private static $metaTranslationCache = []; /** * Inject hreflang/canonical and translate simple meta labels. * * @param string $html * @param string $lang * @return string */ public static function inject($html, $lang) { if (is_admin()) { return $html; } $default = AI_Lang::default(); if ($lang === null) { return $html; } // Parse HTML into DOM $doc = new \DOMDocument(); $internalErrors = libxml_use_internal_errors(true); $htmlToLoad = self::ensureUtf8($html); $flags = LIBXML_HTML_NODEFDTD; $doc->loadHTML('' . $htmlToLoad, $flags); libxml_clear_errors(); libxml_use_internal_errors($internalErrors); $xpath = new \DOMXPath($doc); // Apply all SEO modifications to the DOM self::inject_dom($doc, $xpath, $lang); $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 if (preg_match('/^(]*>)/i', (string) $html, $docMatch)) { // Ensure we don't duplicate if saveHTML already includes it if (stripos($result, ' framing to avoid theme structure conflicts // Extract only the from DOMDocument result and merge with original HTML body if (preg_match('/]*>([\s\S]*?)<\/head>/i', (string) $result, $headNew) && preg_match('/]*>([\s\S]*?)<\/head>/i', (string) $html, $headOrig) && preg_match('/]*>([\s\S]*?)<\/body>/i', (string) $html, $bodyOrig)) { $newHead = (string) $headNew[1]; $origBodyTag = (string) $bodyOrig[0]; // Build result: DOCTYPE + updated head + original body $doctype = ''; if (preg_match('/^(]*>)/i', (string) $html, $doctypeMatch)) { $doctype = $doctypeMatch[1] . "\n"; } $htmlTag = ']*)>/i', (string) $html, $htmlMatch)) { $htmlTag = ''; } $result = $doctype . $htmlTag . "\n" . $newHead . "\n" . $origBodyTag . ""; } return $result; } /** * Apply all SEO modifications to an already-parsed DOMDocument. * Modifies the document in place. Used by combined DOM pass in AI_OB. * * @param \DOMDocument $doc Parsed HTML document. * @param \DOMXPath $xpath XPath instance for the document. * @param string $lang Target language code. * @return void */ public static function inject_dom(\DOMDocument $doc, \DOMXPath $xpath, string $lang): void { $default = AI_Lang::default(); if ($default === null) { return; } $head = $doc->getElementsByTagName('head')->item(0); if (!$head) { $head = $doc->createElement('head'); $htmlEl = $doc->getElementsByTagName('html')->item(0); if ($htmlEl) { $htmlEl->insertBefore($head, $htmlEl->firstChild); } else { $doc->insertBefore($head, $doc->firstChild); } } $isTranslatedLang = strtolower((string)$lang) !== strtolower((string)$default); if ($isTranslatedLang) { self::batchTranslateMeta(self::collectMetaTexts($doc, $xpath), $default, $lang); } // Translate content for non-default languages try { if ($isTranslatedLang) { $titleNode = $doc->getElementsByTagName('title')->item(0); if ($titleNode) { $origTitle = trim((string) $titleNode->textContent); if ($origTitle !== '') { $newTitle = self::maybeTranslateMeta($origTitle, $default, $lang); if (is_string($newTitle) && $newTitle !== '') { while ($titleNode->firstChild) { $titleNode->removeChild($titleNode->firstChild); } $titleNode->appendChild($doc->createTextNode($newTitle)); } } } } } catch (\Throwable $e) { // Fail-safe: keep original title on error } // 1) Meta Description $metaDescNodes = $xpath->query('//head/meta[translate(@name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="description"]'); $existingMetaDesc = null; if ($metaDescNodes && $metaDescNodes->length > 0) { $item = $metaDescNodes->item(0); if ($item instanceof \DOMElement) { $existingMetaDesc = $item; } } $desc = self::computeMetaDescription(); $shouldReplace = false; if ($existingMetaDesc === null) { $shouldReplace = true; } else { $existingContent = trim((string) $existingMetaDesc->getAttribute('content')); if ($isTranslatedLang) { $shouldReplace = true; } elseif (is_front_page() || is_home()) { $homepage = self::get_homepage_meta_description(); if ($homepage !== '') { if ($existingContent === '' || $existingContent !== $homepage) { $shouldReplace = true; } } elseif ($existingContent === '') { $shouldReplace = true; } } elseif ($existingContent === '') { $shouldReplace = true; } } if ($shouldReplace && $desc !== '') { $finalDesc = self::maybeTranslateMeta($desc, $default, $lang); $finalDesc = self::truncateUtf8($finalDesc, 200, '...'); if ($finalDesc !== '') { if ($existingMetaDesc !== null) { $existingMetaDesc->setAttribute('content', esc_attr($finalDesc)); } else { $meta = $doc->createElement('meta'); $meta->setAttribute('name', 'description'); $meta->setAttribute('content', esc_attr($finalDesc)); $head->appendChild($meta); $head->appendChild($doc->createTextNode("\n")); } } } else { if ($isTranslatedLang && $existingMetaDesc !== null && $desc === '') { $existingContent = trim((string) $existingMetaDesc->getAttribute('content')); if ($existingContent !== '') { $finalDesc = self::maybeTranslateMeta($existingContent, $default, $lang); $finalDesc = self::truncateUtf8($finalDesc, 200, '...'); if ($finalDesc !== '') { $existingMetaDesc->setAttribute('content', esc_attr($finalDesc)); } } } } // 2) Open Graph $ogTitleMissing = self::isOgMissing($xpath, 'og:title'); $ogDescMissing = self::isOgMissing($xpath, 'og:description'); $ogImageMissing = self::isOgMissing($xpath, 'og:image'); $ogUrlMissing = self::isOgMissing($xpath, 'og:url'); $ogLocale = ''; $langNorm = strtolower(trim((string) $lang)); if ($langNorm !== '') { if (preg_match('/^([a-z]{2})[-_](.+)$/i', $langNorm, $m)) { $langPart = strtolower($m[1]); $regionPart = strtoupper(preg_replace('/[^a-z]/i', '', $m[2])); if ($regionPart !== '') { $ogLocale = $langPart . '_' . $regionPart; } } if ($ogLocale === '') { $ogLocaleMap = [ 'en' => 'en_US', 'nl' => 'nl_NL', 'de' => 'de_DE', 'fr' => 'fr_FR', 'es' => 'es_ES', 'it' => 'it_IT', 'pt' => 'pt_PT', 'ru' => 'ru_RU', 'ka' => 'ka_GE', 'fi' => 'fi_FI', 'sv' => 'sv_SE', 'no' => 'no_NO', 'da' => 'da_DK', 'pl' => 'pl_PL', 'cs' => 'cs_CZ', 'sk' => 'sk_SK', 'hu' => 'hu_HU', 'ro' => 'ro_RO', 'bg' => 'bg_BG', 'hr' => 'hr_HR', 'sl' => 'sl_SI', 'el' => 'el_GR', 'tr' => 'tr_TR', 'ar' => 'ar_SA', 'he' => 'he_IL', 'hi' => 'hi_IN', 'ja' => 'ja_JP', 'ko' => 'ko_KR', 'zh' => 'zh_CN', 'th' => 'th_TH', 'vi' => 'vi_VN', 'id' => 'id_ID', 'uk' => 'uk_UA', 'kk' => 'kk_KZ', 'et' => 'et_EE', 'ga' => 'ga_IE', 'lv' => 'lv_LV', 'lt' => 'lt_LT', 'mt' => 'mt_MT', ]; $ogLocale = $ogLocaleMap[$langNorm] ?? $langNorm; } } if ($ogLocale !== '') { $existingOgLocale = $xpath->query('//meta[translate(@property, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="og:locale"]'); if ($existingOgLocale && $existingOgLocale->length > 0) { for ($i = $existingOgLocale->length - 1; $i >= 0; $i--) { $ogLocaleElem = $existingOgLocale->item($i); if ($ogLocaleElem instanceof \DOMElement) { $parent = $ogLocaleElem->parentNode; if ($parent) { $parent->removeChild($ogLocaleElem); } } } } $meta = $doc->createElement('meta'); $meta->setAttribute('property', 'og:locale'); $meta->setAttribute('content', esc_attr($ogLocale)); $head->appendChild($meta); $head->appendChild($doc->createTextNode("\n")); } $shouldReplaceOgDesc = $isTranslatedLang; if (!$isTranslatedLang && (is_front_page() || is_home())) { $homepage = self::get_homepage_meta_description(); if ($homepage !== '') { $shouldReplaceOgDesc = true; } } if ($ogDescMissing) { $shouldReplaceOgDesc = true; } $shouldReplaceOgImage = (is_front_page() || is_home()); if ($ogTitleMissing || $shouldReplaceOgDesc || $ogImageMissing || $ogUrlMissing || $shouldReplaceOgImage) { $ogTitle = ''; if ($ogTitleMissing) { $titleNode = $doc->getElementsByTagName('title')->item(0); if ($titleNode) { $ogTitle = trim((string) $titleNode->textContent); } if ($ogTitle === '') { if (is_singular()) { $postId = get_queried_object_id(); if ($postId) { $ogTitle = (string) get_the_title($postId); } } elseif (is_front_page() || is_home()) { $ogTitle = (string) get_bloginfo('description'); } elseif (is_archive()) { $ogTitle = (string) get_the_archive_title(); } } if ($ogTitle !== '' && $isTranslatedLang) { $ogTitle = self::maybeTranslateMeta($ogTitle, $default, $lang); } } $ogDesc = ''; if ($shouldReplaceOgDesc) { $ogDesc = self::computeMetaDescription(); if ($ogDesc !== '') { $ogDesc = self::maybeTranslateMeta($ogDesc, $default, $lang); $ogDesc = self::truncateUtf8($ogDesc, 200, '...'); } } $ogImage = ''; if ($ogImageMissing || $shouldReplaceOgImage) { if ($shouldReplaceOgImage) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- hook prefixed with plugin slug equivalent (ai_translate) $logoId = apply_filters('ai_translate_homepage_logo_id', null); if ($logoId && $logoId > 0) { $url = wp_get_attachment_image_url($logoId, 'large'); if (is_string($url) && $url !== '') { $ogImage = (string) $url; } } } if ($ogImage === '' && is_singular()) { $postId = get_queried_object_id(); if ($postId && has_post_thumbnail($postId)) { $imgId = get_post_thumbnail_id($postId); $url = wp_get_attachment_image_url($imgId, 'large'); if (is_string($url) && $url !== '') { $ogImage = (string) $url; } } } if ($ogImage === '') { $logoId = get_theme_mod('custom_logo'); if ($logoId) { $url = wp_get_attachment_image_url($logoId, 'large'); if (is_string($url) && $url !== '') { $ogImage = (string) $url; } } } } $ogUrl = ''; if ($ogUrlMissing) { $currentAbs = self::currentPageUrlAbsolute(); $path = AI_URL::rewrite_single_href($currentAbs, $lang, $default); if (is_string($path) && $path !== '') { if (preg_match('#^https?://#i', $path)) { $ogUrl = $path; } else { $ogUrl = home_url($path); } } if ($ogUrl === '') { $ogUrl = $currentAbs; } } if ($ogTitleMissing && $ogTitle !== '') { $meta = $doc->createElement('meta'); $meta->setAttribute('property', 'og:title'); $meta->setAttribute('content', esc_attr(trim($ogTitle))); $head->appendChild($meta); $head->appendChild($doc->createTextNode("\n")); } if ($shouldReplaceOgDesc && $ogDesc !== '') { $existingOgDesc = $xpath->query('//meta[translate(@property, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="og:description"]'); if ($existingOgDesc && $existingOgDesc->length > 0) { $ogDescElem = $existingOgDesc->item(0); if ($ogDescElem instanceof \DOMElement) { $ogDescElem->setAttribute('content', esc_attr(trim($ogDesc))); } } else { $meta = $doc->createElement('meta'); $meta->setAttribute('property', 'og:description'); $meta->setAttribute('content', esc_attr(trim($ogDesc))); $head->appendChild($meta); $head->appendChild($doc->createTextNode("\n")); } } elseif ($isTranslatedLang && $shouldReplaceOgDesc && $ogDesc === '') { $existingOgDesc = $xpath->query('//meta[translate(@property, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="og:description"]'); if ($existingOgDesc && $existingOgDesc->length > 0) { $ogDescElem = $existingOgDesc->item(0); if ($ogDescElem instanceof \DOMElement) { $existingContent = trim((string) $ogDescElem->getAttribute('content')); if ($existingContent !== '') { $translatedOgDesc = self::maybeTranslateMeta($existingContent, $default, $lang); $translatedOgDesc = self::truncateUtf8($translatedOgDesc, 200, '...'); if ($translatedOgDesc !== '') { $ogDescElem->setAttribute('content', esc_attr(trim($translatedOgDesc))); } } } } } if (($ogImageMissing || $shouldReplaceOgImage) && $ogImage !== '') { $existingOgImage = $xpath->query('//meta[translate(@property, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="og:image"]'); if ($existingOgImage && $existingOgImage->length > 0) { $ogImageElem = $existingOgImage->item(0); if ($ogImageElem instanceof \DOMElement) { $ogImageElem->setAttribute('content', esc_url($ogImage)); } } else { $meta = $doc->createElement('meta'); $meta->setAttribute('property', 'og:image'); $meta->setAttribute('content', esc_url($ogImage)); $head->appendChild($meta); $head->appendChild($doc->createTextNode("\n")); } } if ($isTranslatedLang) { $existingOgImageAlt = $xpath->query('//meta[translate(@property, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="og:image:alt"]'); if ($existingOgImageAlt && $existingOgImageAlt->length > 0) { $ogImageAltElem = $existingOgImageAlt->item(0); if ($ogImageAltElem instanceof \DOMElement) { $existingAlt = trim((string) $ogImageAltElem->getAttribute('content')); if ($existingAlt !== '') { $translatedAlt = self::maybeTranslateMeta($existingAlt, $default, $lang); if (is_string($translatedAlt) && $translatedAlt !== '') { $ogImageAltElem->setAttribute('content', esc_attr(trim($translatedAlt))); } } } } } if ($ogUrlMissing && $ogUrl !== '') { $meta = $doc->createElement('meta'); $meta->setAttribute('property', 'og:url'); $meta->setAttribute('content', esc_url($ogUrl)); $head->appendChild($meta); $head->appendChild($doc->createTextNode("\n")); } } // 3) hreflang alternates self::injectHreflang($doc, $xpath, $head, $lang, $default); } /** * Generate hreflang tags as plain text strings. * * @param string $lang * @param string $default * @return string */ private static function generateHreflangTags($lang, $default) { $enabled = AI_Translate_Core::enabled_languages(); $detectable = AI_Translate_Core::detectable_languages(); $langs = array_values(array_unique(array_merge($enabled, $detectable))); if (is_string($default) && $default !== '' && !in_array($default, $langs, true)) { $langs[] = $default; } $currentAbs = self::currentPageUrlAbsolute(); $defaultUrl = ''; $post_id = 0; if (is_singular()) { $post_id = get_queried_object_id(); } $tags = []; foreach ($langs as $lc) { $lc = sanitize_key((string) $lc); $href = self::buildHreflangUrl($currentAbs, $lc, $default, $post_id); if ($href === '') { continue; } if ($lc === $default) { $defaultUrl = $href; } $tags[] = '<link rel="alternate" hreflang="' . esc_attr($lc) . '" href="' . esc_url($href) . '" />'; } if (is_string($default) && $default !== '') { if ($defaultUrl === '') { $defaultUrl = self::buildHreflangUrl($currentAbs, $default, $default, $post_id); } if ($defaultUrl !== '') { $tags[] = '<link rel="alternate" hreflang="x-default" href="' . esc_url($defaultUrl) . '" />'; } } return implode("\n", $tags); } /** * Delegate to centralized active domain resolver. * * @return string */ private static function get_active_domain() { return AI_Translate_Core::get_active_domain(); } /** * Delegate to centralized homepage meta description. * * @return string */ private static function get_homepage_meta_description() { return AI_Translate_Core::get_homepage_meta_description(); } /** * Delegate to centralized website context. * * @return string */ private static function get_website_context() { return AI_Translate_Core::get_website_context(); } /** * Compute a meta description candidate in the site's default language. * * @return string */ private static function computeMetaDescription() { // Homepage setting first if (is_front_page() || is_home()) { $homepage = self::get_homepage_meta_description(); if ($homepage !== '') { return $homepage; } } if (is_singular() && !(is_front_page() || is_home())) { $postId = get_queried_object_id(); if ($postId) { $excerpt = (string) get_post_field('post_excerpt', $postId, 'raw'); if (trim($excerpt) !== '') { return $excerpt; } $content = (string) get_post_field('post_content', $postId, 'raw'); $stripped = wp_strip_all_tags(strip_shortcodes($content)); return (string) wp_trim_words($stripped, 55, ''); } } $blogdesc = (string) get_option('blogdescription', ''); return $blogdesc; } /** * Translate a small meta string when needed using the batch API. * * @param string $text * @param string $default * @param string $lang * @return string */ private static function maybeTranslateMeta($text, $default, $lang) { if (!is_string($text) || $text === '') { return ''; } if (!$default || !$lang || strtolower((string)$lang) === strtolower((string)$default)) { return $text; } if (preg_match('/^[a-z0-9\-]+$/i', trim($text)) && strpos($text, ' ') === false && strpos($text, '-') !== false) { return $text; } $cacheKey = $lang . '|' . md5($text); if (isset(self::$metaTranslationCache[$cacheKey])) { return self::$metaTranslationCache[$cacheKey]; } $plan = ['segments' => [ ['id' => 'm', 'text' => (string)$text, 'type' => 'node'] ]]; $ctx = [ 'website_context' => self::get_website_context(), ]; $res = AI_Batch::translate_plan($plan, $default, $lang, $ctx); $segs = isset($res['segments']) && is_array($res['segments']) ? $res['segments'] : []; $translated = isset($segs['m']) ? (string) $segs['m'] : $text; self::$metaTranslationCache[$cacheKey] = $translated; return $translated; } /** * Pre-translate multiple meta texts in a single API call. * Results are stored in $metaTranslationCache so subsequent * maybeTranslateMeta() calls return instantly from cache. * * @param string[] $texts Raw texts to translate. * @param string $default Source language. * @param string $lang Target language. */ /** * Turn meta texts into translate_plan() segments: skips empty strings, slug-like tokens and * texts already in the per-request cache, and de-duplicates. * * @param string[] $texts * @param string $lang * @param string $idPrefix Prefix for the segment ids. * @return array<int, array{id:string,text:string,type:string}> */ private static function buildMetaSegments(array $texts, $lang, string $idPrefix): array { $segments = []; $seen = []; $idx = 0; foreach ($texts as $text) { if (!is_string($text) || trim($text) === '') { continue; } if (preg_match('/^[a-z0-9\-]+$/i', trim($text)) && strpos($text, ' ') === false && strpos($text, '-') !== false) { continue; } $dedupKey = md5($text); if (isset($seen[$dedupKey]) || isset(self::$metaTranslationCache[$lang . '|' . $dedupKey])) { continue; } $seen[$dedupKey] = true; $segments[] = ['id' => $idPrefix . $idx, 'text' => $text, 'type' => 'node']; $idx++; } return $segments; } /** * Register texts the page translation already produced in the target language, so the meta * pass does not send them to the API again. The head is translated together with the page * (title, meta description), and inject_dom() then collects those translated strings as if they * were source text: one extra sequential API round trip per page, for a result that is the same * text. Short or single-word strings are left out: they may be source text that does need * translating (for example a breadcrumb "Home" next to a menu item that kept the English word). * * @param array<string,mixed> $translations Segment id => translated text. * @param string $lang Target language. */ public static function mark_translated(array $translations, $lang): void { if (!is_string($lang) || $lang === '') { return; } foreach ($translations as $value) { if (!is_string($value)) { continue; } $trimmed = trim($value); if (mb_strlen($trimmed) < 12 || !preg_match('/\s/', $trimmed)) { continue; } self::$metaTranslationCache[$lang . '|' . md5($trimmed)] = $trimmed; if ($trimmed !== $value) { self::$metaTranslationCache[$lang . '|' . md5($value)] = $value; } } } /** * Segments for the meta texts that inject_dom(), translateTwitterCards() and translateJsonLd() * translate later, each in its own sequential API round trip. Adding these to the page's own * translate_plan() call translates them in the same parallel stage; the later calls then find * every text in the segment cache and make no API request. * * @param string $html Page HTML in the default language. * @param string $lang Target language. * @return array<int, array{id:string,text:string,type:string}> */ public static function prefetch_segments($html, $lang): array { $default = AI_Lang::default(); if ($default === null || !is_string($html) || $html === '' || strtolower((string) $lang) === strtolower((string) $default)) { return []; } $texts = []; // <title>, meta description, og:description, og:image:alt, post title (same source as inject_dom) try { $doc = new \DOMDocument(); $internalErrors = libxml_use_internal_errors(true); $doc->loadHTML('<?xml encoding="utf-8" ?>' . self::ensureUtf8($html), LIBXML_HTML_NODEFDTD); libxml_clear_errors(); libxml_use_internal_errors($internalErrors); $texts = self::collectMetaTexts($doc, new \DOMXPath($doc)); } catch (\Throwable $e) { $texts = []; } // Twitter Card tags (same pattern as translateTwitterCards) $twitterNames = ['twitter:text:title', 'twitter:title', 'twitter:description', 'twitter:image:alt']; $names = implode('|', array_map(function ($n) { return preg_quote($n, '#'); }, $twitterNames)); if (preg_match_all('#<meta\s+name="(?:' . $names . ')"\s+content="([^"]+)"[^>]*>#i', $html, $twitterMatches) && !empty($twitterMatches[1])) { foreach ($twitterMatches[1] as $raw) { $decoded = html_entity_decode($raw, ENT_QUOTES, 'UTF-8'); if (trim($decoded) !== '') { $texts[] = $decoded; } } } // JSON-LD (same collector as translateJsonLd) if (preg_match_all('#<script\b[^>]*type=["\']application/ld\+json["\'][^>]*>(.*?)</script>#is', $html, $ldBlocks) && !empty($ldBlocks[1])) { foreach ($ldBlocks[1] as $raw) { $json = json_decode($raw, true); if (is_array($json)) { self::collectJsonLdTexts($json, $texts); } } } return self::buildMetaSegments($texts, $lang, 'pf'); } private static function batchTranslateMeta(array $texts, $default, $lang): void { if (empty($texts) || !is_string($default) || !is_string($lang) || $default === '' || $lang === '' || strtolower($lang) === strtolower($default)) { return; } $segments = self::buildMetaSegments($texts, $lang, 's'); if (empty($segments)) { return; } $plan = ['segments' => $segments]; $ctx = ['website_context' => self::get_website_context()]; $res = AI_Batch::translate_plan($plan, $default, $lang, $ctx); $segs = isset($res['segments']) && is_array($res['segments']) ? $res['segments'] : []; foreach ($segments as $seg) { $id = $seg['id']; $origText = $seg['text']; $translated = isset($segs[$id]) ? trim((string) $segs[$id]) : ''; if ($translated !== '') { self::$metaTranslationCache[$lang . '|' . md5($origText)] = $translated; } } } /** * Gather all meta/SEO texts from the DOM that may need translation. * Used to pre-populate the translation cache in a single batch call. * * @param \DOMDocument $doc * @param \DOMXPath $xpath * @return string[] */ private static function collectMetaTexts(\DOMDocument $doc, \DOMXPath $xpath): array { $texts = []; $titleNode = $doc->getElementsByTagName('title')->item(0); if ($titleNode) { $t = trim((string) $titleNode->textContent); if ($t !== '') { $texts[] = $t; } } $desc = self::computeMetaDescription(); if ($desc !== '') { $texts[] = $desc; } $metaDescNodes = $xpath->query('//head/meta[translate(@name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="description"]'); if ($metaDescNodes && $metaDescNodes->length > 0) { $item = $metaDescNodes->item(0); if ($item instanceof \DOMElement) { $c = trim((string) $item->getAttribute('content')); if ($c !== '') { $texts[] = $c; } } } $ogDescNodes = $xpath->query('//meta[translate(@property, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="og:description"]'); if ($ogDescNodes && $ogDescNodes->length > 0) { $item = $ogDescNodes->item(0); if ($item instanceof \DOMElement) { $c = trim((string) $item->getAttribute('content')); if ($c !== '') { $texts[] = $c; } } } $ogAltNodes = $xpath->query('//meta[translate(@property, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="og:image:alt"]'); if ($ogAltNodes && $ogAltNodes->length > 0) { $item = $ogAltNodes->item(0); if ($item instanceof \DOMElement) { $c = trim((string) $item->getAttribute('content')); if ($c !== '') { $texts[] = $c; } } } if (function_exists('is_singular') && is_singular()) { $postId = get_queried_object_id(); if ($postId) { $postTitle = (string) get_the_title($postId); if ($postTitle !== '') { $texts[] = $postTitle; } } } return $texts; } /** * Inject hreflang alternate links into <head>. * * @param \DOMDocument $doc * @param \DOMXPath $xpath * @param \DOMElement $head * @param string $current * @param string $default * @return void */ private static function injectHreflang($doc, $xpath, $head, $current, $default) { // Remove all existing hreflang alternate links to ensure clean injection // This prevents conflicts with incorrectly generated hreflang tags from other sources $links = $xpath->query('//head/link[@rel="alternate" and @hreflang]'); if ($links) { foreach ($links as $lnk) { if ($lnk instanceof \DOMElement && $lnk->parentNode) { $lnk->parentNode->removeChild($lnk); } } } $enabled = AI_Translate_Core::enabled_languages(); $detectable = AI_Translate_Core::detectable_languages(); $langs = array_values(array_unique(array_merge($enabled, $detectable))); if (is_string($default) && $default !== '' && !in_array($default, $langs, true)) { $langs[] = $default; } $currentAbs = self::currentPageUrlAbsolute(); $defaultUrl = ''; // Determine post_id for singular pages to enable slug translation $post_id = 0; if (is_singular()) { $post_id = get_queried_object_id(); } foreach ($langs as $lc) { $lc = sanitize_key((string) $lc); // Build URL with translated slug if available $href = self::buildHreflangUrl($currentAbs, $lc, $default, $post_id); if ($href === '') { continue; } if ($lc === $default) { $defaultUrl = $href; } $el = $doc->createElement('link'); $el->setAttribute('rel', 'alternate'); $el->setAttribute('hreflang', $lc); $el->setAttribute('href', esc_url($href)); $head->appendChild($doc->createTextNode("\n")); $head->appendChild($el); } // x-default equals default language URL if (is_string($default) && $default !== '' ) { if ($defaultUrl === '') { $defaultUrl = self::buildHreflangUrl($currentAbs, $default, $default, $post_id); } if ($defaultUrl !== '') { $el = $doc->createElement('link'); $el->setAttribute('rel', 'alternate'); $el->setAttribute('hreflang', 'x-default'); $el->setAttribute('href', esc_url($defaultUrl)); $head->appendChild($doc->createTextNode("\n")); $head->appendChild($el); $head->appendChild($doc->createTextNode("\n")); } } } /** * Build hreflang URL with translated slug for a given language. * * @param string $currentUrl * @param string $targetLang * @param string $default * @param int $post_id * @return string */ private static function buildHreflangUrl($currentUrl, $targetLang, $default, $post_id) { // For homepage if (is_front_page() || is_home()) { if (strtolower($targetLang) === strtolower($default)) { return home_url('/'); } return home_url('/' . $targetLang . '/'); } // For singular posts/pages with slug translation if ($post_id > 0 && function_exists('ai_translate_build_translated_path')) { $path = ai_translate_build_translated_path((int) $post_id, $targetLang, $default, '/'); if ($path !== null && $path !== '') { return home_url($path); } } // Fallback: use basic rewrite (language prefix only) $path = AI_URL::rewrite_single_href($currentUrl, $targetLang, $default); if (!is_string($path) || $path === '') { return ''; } return home_url($path); } /** * Return the translated URL for a post and language (same URL as hreflang uses). * Use this for warm cache, crawlers, or any code that needs the canonical translated URL. * * @param int $post_id Post ID (0 = homepage). * @param string $lang Language code. * @return string Full URL or empty string on failure. */ public static function get_translated_url($post_id, $lang) { $lang = sanitize_key((string) $lang); $default = AI_Lang::default(); if ($default === null || $default === '') { return ''; } $front_page_id = (int) get_option('page_on_front'); if ($front_page_id > 0 && (int) $post_id === $front_page_id) { if (strtolower($lang) === strtolower($default)) { return home_url('/'); } return home_url('/' . $lang . '/'); } if ((int) $post_id === 0) { if (strtolower($lang) === strtolower($default)) { return home_url('/'); } return home_url('/' . $lang . '/'); } // 404 page: use a non-existent path to trigger a 404 response if ((int) $post_id === -1) { return home_url('/' . $lang . '/ait-404-cache-warm/'); } if (function_exists('ai_translate_build_translated_path')) { $path = ai_translate_build_translated_path((int) $post_id, $lang, $default, '/'); if ($path !== null && $path !== '') { return home_url($path); } } $currentUrl = get_permalink((int) $post_id); if (!$currentUrl) { return ''; } $path = AI_URL::rewrite_single_href($currentUrl, $lang, $default); if (!is_string($path) || $path === '') { return ''; } return home_url($path); } /** * Determine if a given Open Graph property is missing. * * @param \DOMXPath $xpath * @param string $prop * @return bool */ private static function isOgMissing($xpath, $prop) { // Search entire document, not just <head>. DOMDocument may move OG tags from // Jetpack/Yoast/RankMath to <body> when placeholder <div>s break <head> structure. $q = sprintf('//meta[translate(@property, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="%s"]', strtolower($prop)); $nodes = $xpath->query($q); return !($nodes && $nodes->length > 0); } /** * Truncate a UTF-8 string at character boundary. * * @param string $text * @param int $max * @param string $suffix * @return string */ private static function truncateUtf8($text, $max, $suffix) { $text = (string) $text; if ($text === '') return ''; if (mb_strlen($text) <= $max) return $text; $tr = mb_substr($text, 0, $max); $lastSpace = mb_strrpos($tr, ' '); if ($lastSpace !== false) { $tr = mb_substr($tr, 0, $lastSpace); } else { $tr = mb_substr($tr, 0, max(0, $max - mb_strlen($suffix))); } return rtrim($tr) . $suffix; } /** * Delegate to centralized UTF-8 helper. * * @param string $html * @return string */ private static function ensureUtf8($html) { return AI_DOM::ensureUtf8($html); } /** * Compute absolute URL for the current page. * * @return string */ private static function currentPageUrlAbsolute() { if (is_singular()) { $postId = get_queried_object_id(); if ($postId) { $p = get_permalink($postId); if (is_string($p) && $p !== '') return (string) $p; } } if (is_front_page() || is_home()) { return home_url('/'); } $req_uri = ai_translate_request_uri('/'); $path = (string) wp_parse_url($req_uri, PHP_URL_PATH); if ($path === '') { $path = '/'; } return home_url($path); } /** * Translate Twitter Card meta tags in final HTML. * Runs on the fully assembled HTML (after DOMDocument + placeholder restore) * because DOMDocument may move Jetpack meta tags out of <head> when * preceding script/style placeholders break the head structure. * * @param string $html Full HTML output. * @param string $lang Target language code. * @return string HTML with translated Twitter Card meta tags. */ public static function translateTwitterCards($html, $lang) { $default = AI_Lang::default(); if ($default === null || strtolower((string) $lang) === strtolower((string) $default)) { return $html; } $twitterNames = ['twitter:text:title', 'twitter:title', 'twitter:description', 'twitter:image:alt']; $names = implode('|', array_map(function ($n) { return preg_quote($n, '#'); }, $twitterNames)); $pattern = '#(<meta\s+name="(?:' . $names . ')"\s+content=")([^"]+)("[^>]*>)#i'; if (preg_match_all($pattern, $html, $allMatches) && !empty($allMatches[2])) { $texts = []; foreach ($allMatches[2] as $raw) { $decoded = html_entity_decode($raw, ENT_QUOTES, 'UTF-8'); if (trim($decoded) !== '') { $texts[] = $decoded; } } self::batchTranslateMeta($texts, $default, $lang); } return preg_replace_callback($pattern, function ($m) use ($default, $lang) { $original = html_entity_decode($m[2], ENT_QUOTES, 'UTF-8'); if (trim($original) === '') { return $m[0]; } $translated = self::maybeTranslateMeta($original, $default, $lang); if (!is_string($translated) || $translated === '' || $translated === $original) { return $m[0]; } return $m[1] . esc_attr($translated) . $m[3]; }, $html); } /** * Translate JSON-LD BreadcrumbList name fields in final HTML. * Called after script placeholders have been restored. * * @param string $html Full HTML output. * @param string $lang Target language code. * @return string HTML with translated JSON-LD. */ public static function translateJsonLd($html, $lang) { $default = AI_Lang::default(); if ($default === null || strtolower((string) $lang) === strtolower((string) $default)) { return $html; } if (preg_match_all('#<script\b[^>]*type=["\']application/ld\+json["\'][^>]*>(.*?)</script>#is', $html, $allBlocks) && !empty($allBlocks[1])) { $texts = []; foreach ($allBlocks[1] as $raw) { $json = json_decode($raw, true); if (is_array($json)) { self::collectJsonLdTexts($json, $texts); } } if (!empty($texts)) { self::batchTranslateMeta($texts, $default, $lang); } } return preg_replace_callback( '#(<script\b[^>]*type=["\']application/ld\+json["\'][^>]*>)(.*?)(</script>)#is', function ($m) use ($default, $lang) { $json = json_decode($m[2], true); if (!is_array($json)) { return $m[0]; } $changed = self::translateJsonLdNode($json, $default, $lang); if (!$changed) { return $m[0]; } $encoded = wp_json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); if (!is_string($encoded) || $encoded === '') { return $m[0]; } return $m[1] . $encoded . $m[3]; }, $html ); } /** * Recursively translate name/description fields in JSON-LD BreadcrumbList nodes. * * @param array &$node JSON-LD node (modified in place). * @param string $default Default language. * @param string $lang Target language. * @return bool Whether any field was translated. */ /** * Recursively collect translatable text values from a JSON-LD structure. * * @param array $node JSON-LD node. * @param string[] &$texts Collected texts (appended in place). */ private static function collectJsonLdTexts(array $node, array &$texts): void { $type = $node['@type'] ?? ''; if ($type === 'BreadcrumbList' && isset($node['itemListElement']) && is_array($node['itemListElement'])) { foreach ($node['itemListElement'] as $item) { if (isset($item['name']) && is_string($item['name']) && trim($item['name']) !== '') { $texts[] = $item['name']; } } } // WooCommerce Product schema: only name/description are user-facing prose; // offers, sku, brand and ratings must stay untouched. if ($type === 'Product') { foreach (['name', 'description'] as $field) { if (isset($node[$field]) && is_string($node[$field]) && trim($node[$field]) !== '') { $texts[] = $node[$field]; } } } if (isset($node['@graph']) && is_array($node['@graph'])) { foreach ($node['@graph'] as $graphNode) { if (is_array($graphNode)) { self::collectJsonLdTexts($graphNode, $texts); } } } } private static function translateJsonLdNode(array &$node, $default, $lang) { $type = $node['@type'] ?? ''; $changed = false; if ($type === 'BreadcrumbList' && isset($node['itemListElement']) && is_array($node['itemListElement'])) { foreach ($node['itemListElement'] as &$item) { if (isset($item['name']) && is_string($item['name']) && trim($item['name']) !== '') { $translated = self::maybeTranslateMeta($item['name'], $default, $lang); if (is_string($translated) && $translated !== '' && $translated !== $item['name']) { $item['name'] = $translated; $changed = true; } } } unset($item); } // WooCommerce Product schema: translate name/description only (see collector). if ($type === 'Product') { foreach (['name', 'description'] as $field) { if (isset($node[$field]) && is_string($node[$field]) && trim($node[$field]) !== '') { $translated = self::maybeTranslateMeta($node[$field], $default, $lang); if (is_string($translated) && $translated !== '' && $translated !== $node[$field]) { $node[$field] = $translated; $changed = true; } } } } // Handle @graph arrays (used by Yoast and others) if (isset($node['@graph']) && is_array($node['@graph'])) { foreach ($node['@graph'] as &$graphNode) { if (is_array($graphNode)) { if (self::translateJsonLdNode($graphNode, $default, $lang)) { $changed = true; } } } unset($graphNode); } return $changed; } }