'slug']); $locales = pll_languages_list(['fields' => 'locale']); $names = pll_languages_list(['fields' => 'name']); $languages = []; foreach ($codes as $index => $code) { $languages[] = [ 'code' => (string) $code, 'locale' => (string) ($locales[$index] ?? ''), 'name' => (string) ($names[$index] ?? $code), ]; } return $languages; } public function defaultLanguage(): ?string { $language = pll_default_language('slug'); return is_string($language) ? $language : null; } public function isTranslated(string $postType): bool { return pll_is_translated_post_type($postType); } public function postLanguage(int $postId): ?string { $language = pll_get_post_language($postId, 'slug'); return is_string($language) ? $language : null; } public function translations(int $postId): array { $translations = []; foreach (pll_get_post_translations($postId) as $language => $id) { $translations[(string) $language] = (int) $id; } return $translations; } public function query(array $args, string $language): WP_Query { $args['lang'] = ''; $args['tax_query'] = [['taxonomy' => 'language', 'field' => 'slug', 'terms' => $language]]; return new WP_Query($args); } public function link(int $sourceId, int $translationId, string $language): void { $sourceLanguage = $this->postLanguage($sourceId); if ($sourceLanguage === null) { throw new RuntimeException('The source post has no language.'); } pll_set_post_language($translationId, $language); $translations = $this->translations($sourceId); $translations[$sourceLanguage] = $sourceId; $translations[$language] = $translationId; pll_save_post_translations($translations); } }