public function ajax_get_current_status() { $lgtm =& $GLOBALS['wp_lingotek']->model; $pllm = $GLOBALS['polylang']->model; $languages = pll_languages_list(array('fields' => 'locale')); $object_ids = $_POST['check_ids']; if ($object_ids === null) { return; } $terms = isset($_POST['terms_translations']); //The main array consists of //ids and nonces. Each id has a source language, languages with statuses, and a workbench link $content_metadata = array(); foreach ($object_ids as $object_id) { $id = $object_id; $type = $terms ? 'term' : 'post'; if (isset($_POST['taxonomy'])) { $taxonomy = $_POST['taxonomy']; if (strpos($_POST['taxonomy'], '&')) { $taxonomy = strstr($_POST['taxonomy'], '&', true); } } else { $taxonomy = get_post_type($id); } $content_metadata[$id] = array('existing_trans' => false, 'source' => false, 'doc_id' => null, 'source_id' => null, 'source_status' => null); $document = $lgtm->get_group($type, $object_id); if ($document && !isset($document->source) && count($document->desc_array) >= 3) { $content_metadata[$id]['existing_trans'] = true; } if ($document && isset($document->source) && isset($document->document_id) && isset($document->status) && isset($document->translations)) { if ($document->source !== (int) $object_id) { $document = $lgtm->get_group($type, $document->source); } $source_id = $document->source !== null ? $document->source : $object_id; $source_language = $terms ? pll_get_term_language($document->source, 'locale') : pll_get_post_language($document->source, 'locale'); $existing_translations = $pllm->get_translations($type, $source_id); if (count($existing_translations) > 1) { $content_metadata[$id]['existing_trans'] = true; } $content_metadata[$id]['source'] = $source_language; $content_metadata[$id]['doc_id'] = $document->document_id; $content_metadata[$id]['source_id'] = $document->source; $content_metadata[$id]['source_status'] = $document->status; $target_status = $document->status == 'edited' || $document->status == null ? 'edited' : 'current'; $content_metadata[$id][$source_language]['status'] = $document->source == $object_id ? $document->status : $target_status; if (is_array($document->translations)) { foreach ($document->translations as $locale => $translation_status) { $content_metadata[$id][$locale]['status'] = $translation_status; $workbench_link = Lingotek_Actions::workbench_link($document->document_id, $locale); $content_metadata[$id][$locale]['workbench_link'] = $workbench_link; } } //fills in missing languages, makes life easier for the updater foreach ($languages as $language) { foreach ($content_metadata as $group => $status) { $language_obj = $pllm->get_language($source_language); $target_lang_obj = $pllm->get_language($language); $profile = Lingotek_Model::get_profile($taxonomy, $language_obj, $group); if ($profile['profile'] != 'disabled' && $status['source'] != false) { if (!isset($status[$language])) { $content_metadata[$group][$language]['status'] = "none"; if ($document->is_disabled_target($pllm->get_language($source_language), $pllm->get_language($language)) || isset($document->desc_array[$target_lang_obj->slug]) && !isset($document->source)) { $content_metadata[$group][$language]['status'] = 'disabled'; } } } } } } $language = $type == 'post' ? pll_get_post_language($id) : pll_get_term_language($id); $language = $pllm->get_language($language); if ($language) { $profile = Lingotek_Model::get_profile($taxonomy, $language, $id); if ($profile['profile'] == 'disabled' && $content_metadata[$id]['source'] == false) { $content_metadata[$id]['source'] = 'disabled'; } } } //get the nonces associated with the different actions $content_metadata['request_nonce'] = $this->lingotek_get_matching_nonce('lingotek-request'); $content_metadata['download_nonce'] = $this->lingotek_get_matching_nonce('lingotek-download'); $content_metadata['upload_nonce'] = $this->lingotek_get_matching_nonce('lingotek-upload'); $content_metadata['status_nonce'] = $this->lingotek_get_matching_nonce('lingotek-status'); wp_send_json($content_metadata); }
/** * Fix "get_term_link" for this taxonomy. */ public function term_link_filter($termlink, $term, $taxonomy) { // Check if the post type is handle. if (isset($this->taxonomies[$taxonomy])) { if (!is_object($term)) { if (is_int($term)) { $term = get_term($term, $taxonomy); } else { $term = get_term_by('slug', $term, $taxonomy); } } if (!is_object($term)) { $term = new WP_Error('invalid_term', __('Empty Term')); } if (is_wp_error($term)) { return $term; } // Get the term language. $lang = pll_get_term_language($term->term_id, 'slug'); if (!$lang) { // If term has no language assigned, use default language as fallback. $lang = pll_default_language(); } // Check if the language is handle. if (isset($this->taxonomies[$taxonomy]->translated_slugs[$lang])) { $taxonomy = $term->taxonomy; $termlink = $this->taxonomies[$taxonomy]->translated_struct[$lang]; $slug = $term->slug; $t = get_taxonomy($taxonomy); if (empty($termlink)) { if ('category' == $taxonomy) { $termlink = '?cat=' . $term->term_id; } elseif ($t->query_var) { $termlink = "?{$t->query_var}={$slug}"; } else { $termlink = "?taxonomy={$taxonomy}&term={$slug}"; } $termlink = home_url($termlink); } else { if ($t->rewrite['hierarchical']) { $hierarchical_slugs = array(); $ancestors = get_ancestors($term->term_id, $taxonomy); foreach ((array) $ancestors as $ancestor) { $ancestor_term = get_term($ancestor, $taxonomy); $hierarchical_slugs[] = $ancestor_term->slug; } $hierarchical_slugs = array_reverse($hierarchical_slugs); $hierarchical_slugs[] = $slug; $termlink = str_replace("%{$taxonomy}%", implode('/', $hierarchical_slugs), $termlink); } else { $termlink = str_replace("%{$taxonomy}%", $slug, $termlink); } $termlink = home_url(user_trailingslashit($termlink, 'category')); } // Back Compat filters. if ('post_tag' == $taxonomy) { $termlink = apply_filters('tag_link', $termlink, $term->term_id); } elseif ('category' == $taxonomy) { $termlink = apply_filters('category_link', $termlink, $term->term_id); } } } return $termlink; }
/** * Retrive list of translated objects given a post or term ID or object. * * @param string $type Post type or taxonomy. * @param int|WP_Post $id Optional. Post or Term ID or object. Default current post. * @param bool $add_self Optional. If no translations, return self. Default is "false". * @return array $translations List of translated posts or terms. * * @package WordPress\Skeleton\Polylang */ function pll_get_translations($type, $id, $add_self = false) { global $polylang; if (!is_object($polylang)) { return false; } $translations = $polylang->model->get_translations($type, $id); if (empty($translations) && $add_self) { if ($type == 'post' || $polylang->is_translated_post_type($type)) { $translations = [pll_get_post_language($id) => $id]; } elseif ($type == 'term' || $polylang->is_translated_taxonomy($type)) { $translations = [pll_get_term_language($id) => $id]; } } if (is_array($translations) && !empty($translations)) { foreach ($translations as $language => $object_id) { $translations[$language] = is_tax($type) ? get_term($object_id, $type) : get_post($object_id); } } return $translations; }
/** * Get current language information from Multilingual plugins * * @since 2.6.6 * * @return array */ public static function get_current_language() { /** * @var $sitepress SitePress object * @var $polylang object */ /* * @todo wpml-comp Remove global object usage */ global $sitepress, $polylang; $lang_data = false; $translator = false; $current_language = false; // Multilingual support if (did_action('wpml_loaded') && apply_filters('wpml_setting', true, 'auto_adjust_ids')) { // WPML support $translator = 'WPML'; // Get the global current language (if set) $wpml_language = apply_filters('wpml_current_language', null); $current_language = $wpml_language != 'all' ? $wpml_language : ''; } elseif ((function_exists('PLL') || is_object($polylang)) && function_exists('pll_current_language')) { // Polylang support $translator = 'PLL'; // Get the global current language (if set) $current_language = pll_current_language('slug'); } /** * Admin functions that overwrite the current language * * @since 2.6.6 */ if (is_admin() && !empty($translator)) { if ($translator == 'PLL') { /** * Polylang support * Get the current user's perferred language. * This is a user meta setting that will overwrite the language returned from pll_current_language() * @see polylang/admin/admin-base.php -> init_user() */ $current_language = get_user_meta(get_current_user_id(), 'pll_filter_content', true); } // Get current language based on the object language if available if (function_exists('get_current_screen')) { $current_screen = get_current_screen(); /** * Overwrite the current language if needed for post types */ if (isset($current_screen->base) && ($current_screen->base == 'post' || $current_screen->base == 'edit')) { if (!empty($_GET['post'])) { /** * WPML support * In WPML the current language is always set to default on an edit screen * We need to overwrite this when the current object is not-translatable to enable relationships with different languages */ if ($translator == 'WPML' && !apply_filters('wpml_is_translated_post_type', false, get_post_type($_GET['post']))) { // Overwrite the current language to nothing if this is a NOT-translatable post_type $current_language = ''; } /** * Polylang support (1.5.4+) * In polylang the preferred language could be anything. * We only want the related objects if they are not translatable OR the same language as the current object */ if ($translator == 'PLL' && function_exists('pll_get_post_language') && pll_is_translated_post_type(get_post_type($_GET['post']))) { // Overwrite the current language if this is a translateable post_type $current_language = pll_get_post_language((int) $_GET['post']); } } /** * Polylang support (1.0.1+) * In polylang the preferred language could be anything. * When we're adding a new object and language is set we only want the related objects if they are not translatable OR the same language */ if ($translator == 'PLL' && !empty($_GET['new_lang']) && !empty($_GET['post_type']) && pll_is_translated_post_type(sanitize_text_field($_GET['post_type']))) { $current_language = $_GET['new_lang']; } /** * Overwrite the current language if needed for taxonomies */ } elseif (isset($current_screen->base) && ($current_screen->base == 'term' || $current_screen->base == 'edit-tags')) { // @todo MAYBE: Similar function like get_post_type for taxonomies so we don't need to check for $_GET['taxonomy'] if (!empty($_GET['taxonomy'])) { /* * @todo wpml-comp API call for taxonomy needed! * Suggested API call: * add_filter( 'wpml_is_translated_taxonomy', $_GET['taxonomy'], 10, 2 ); */ /** * WPML support * In WPML the current language is always set to default on an edit screen * We need to overwrite this when the current object is not-translatable to enable relationships with different languages */ if ($translator == 'WPML' && method_exists($sitepress, 'is_translated_taxonomy') && !$sitepress->is_translated_taxonomy($_GET['taxonomy'])) { // Overwrite the current language to nothing if this is a NOT-translatable taxonomy $current_language = ''; } /** * Polylang support (1.5.4+) * In polylang the preferred language could be anything. * We only want the related objects if they are not translatable OR the same language as the current object */ if ($translator == 'PLL' && !empty($_GET['tag_ID']) && function_exists('pll_get_term_language') && pll_is_translated_taxonomy(sanitize_text_field($_GET['taxonomy']))) { // Overwrite the current language if this is a translatable taxonomy $current_language = pll_get_term_language((int) $_GET['tag_ID']); } } /** * Polylang support (1.0.1+) * In polylang the preferred language could be anything. * When we're adding a new object and language is set we only want the related objects if they are not translatable OR the same language */ if ($translator == 'PLL' && !empty($_GET['new_lang']) && !empty($_GET['taxonomy']) && pll_is_translated_taxonomy(sanitize_text_field($_GET['taxonomy']))) { $current_language = $_GET['new_lang']; } } } } $current_language = pods_sanitize(sanitize_text_field($current_language)); if (!empty($current_language)) { // We need to return language data $lang_data = array('language' => $current_language, 't_id' => 0, 'tt_id' => 0, 'term' => null); /** * Polylang support * Get the language taxonomy object for the current language */ if ($translator == 'PLL') { $current_language_t = false; // Get the language term object if (function_exists('PLL') && isset(PLL()->model) && method_exists(PLL()->model, 'get_language')) { // Polylang 1.8 and newer $current_language_t = PLL()->model->get_language($current_language); } elseif (is_object($polylang) && isset($polylang->model) && method_exists($polylang->model, 'get_language')) { // Polylang 1.2 - 1.7.x $current_language_t = $polylang->model->get_language($current_language); } elseif (is_object($polylang) && method_exists($polylang, 'get_language')) { // Polylang 1.1.x and older $current_language_t = $polylang->get_language($current_language); } // If the language object exists, add it! if ($current_language_t && !empty($current_language_t->term_id)) { $lang_data['t_id'] = (int) $current_language_t->term_id; $lang_data['tt_id'] = (int) $current_language_t->term_taxonomy_id; $lang_data['term'] = $current_language_t; } } } /** * Override language data used by Pods. * * @since 2.6.6 * * @param array|false $lang_data { * Language data * * @type string $language Language slug * @type int $t_id Language term_id * @type int $tt_id Language term_taxonomy_id * @type WP_Term $term Language term object * } * @param string|boolean $translator Language plugin used */ $lang_data = apply_filters('pods_get_current_language', $lang_data, $translator); return $lang_data; }