/ on a regular post). // If the strict filter yields no unique hit, retry without the CPT constraint — // we still require exactly one valid published, public, non-attachment post. if (count($valid) !== 1 && $expected_post_type !== null) { $relaxed = self::filter_candidate_posts($candidate_ids, null); if (count($relaxed) === 1) { $valid = $relaxed; } } if (count($valid) !== 1) { return null; } $post = reset($valid); $target_url = self::build_canonical_url($post, $lang); if ($target_url === null || $target_url === '') { return null; } // Self-redirect / loop guard: compare decoded forms so percent-encoded // non-ASCII (e.g. %C4%A7 vs ħ) does not look like a different URL and // trigger an infinite 301 to the same resource. $norm = static function ($value) { $value = (string) $value; if (strpos($value, '%') !== false) { $decoded = rawurldecode($value); if (mb_check_encoding($decoded, 'UTF-8')) { return $decoded; } } return $value; }; $current_url = function_exists('home_url') ? home_url($path) : $path; if (rtrim($norm($target_url), '/') === rtrim($norm((string) $current_url), '/')) { return null; } // Same-host guard. $home_host = function_exists('home_url') ? wp_parse_url(home_url('/'), PHP_URL_HOST) : null; $tgt_host = wp_parse_url($target_url, PHP_URL_HOST); if ($home_host !== null && $home_host !== $tgt_host) { return null; } if ($was_auto_discovered) { AI_Slugs::record_history($post->ID, $lang, $basename); } return $target_url; } /** * Filter candidate post IDs down to published, public, non-attachment posts. * When $expected_post_type is provided, also requires a matching post_type. * * @param array $candidate_ids * @param string|null $expected_post_type * @return array Post objects keyed by ID. */ private static function filter_candidate_posts(array $candidate_ids, $expected_post_type) { $valid = []; foreach ($candidate_ids as $pid) { $post = get_post((int) $pid); if (!$post || $post->post_status !== 'publish') continue; if ($post->post_type === 'attachment') continue; if (!self::is_public_post_type((string) $post->post_type)) continue; if ($expected_post_type !== null && $post->post_type !== $expected_post_type) continue; $valid[(int) $post->ID] = $post; } return $valid; } /** * Build the canonical translated URL for a post in the target language. * Will not trigger a new translation: requires an existing translated slug * (or homepage / default-language behaviour). * * @param object $post WordPress post object. * @param string $lang Target language code. * @return string|null Absolute URL or null when no safe URL can be built. */ private static function build_canonical_url($post, $lang) { $default = AI_Lang::default(); $is_default = ($default !== null && strtolower($lang) === strtolower((string) $default)); $front_id = (int) get_option('page_on_front'); if ($front_id > 0 && (int) $post->ID === $front_id) { $path = $is_default ? '/' : '/' . $lang . '/'; return home_url($path); } if ($is_default) { // Default language uses the source slug; build via permalink to honour CPT prefix. $perm = get_permalink((int) $post->ID); return is_string($perm) ? $perm : null; } // Non-default language: require an existing translated slug; do NOT call API. $translated = AI_Slugs::get_or_generate((int) $post->ID, $lang, false); if (!is_string($translated) || $translated === '') { return null; } $cpt_prefix = function_exists('ai_translate_cpt_path_prefix') ? ai_translate_cpt_path_prefix((string) $post->post_type) : ''; $path = '/' . $lang . '/' . $cpt_prefix . trim($translated, '/') . '/'; return home_url($path); } /** * Is this language code one we should consider for recovery (default, * enabled, or detectable)? * * @param string $lang * @return bool */ private static function is_recognised_language($lang) { $low = strtolower((string) $lang); $default = AI_Lang::default(); if ($default !== null && $low === strtolower((string) $default)) return true; $enabled = (array) AI_Lang::enabled(); $detectable = (array) AI_Lang::detectable(); if (in_array($low, array_map('strtolower', $enabled), true)) return true; if (in_array($low, array_map('strtolower', $detectable), true)) return true; return false; } /** * Whether the given post type is registered as public. * * @param string $post_type * @return bool */ private static function is_public_post_type($post_type) { if ($post_type === '') return false; $obj = function_exists('get_post_type_object') ? get_post_type_object($post_type) : null; if (!$obj) return false; return !empty($obj->public); } }