/** * Returns the original EM_Location object from the provided EM_Location object * @param EM_Location|EM_Event $object * @return EM_Location|EM_Event */ public static function get_original($object) { global $em_wpml_original_cache, $pagenow; if (!empty($em_wpml_original_cache[$object->blog_id][$object->post_id])) { //we have done this before.... $original_post_id = $em_wpml_original_cache[$object->blog_id][$object->post_id]; //retrieve cached ID } else { //find the original post id via WPML $original_post_id = SitePress::get_original_element_id($object->post_id, 'post_' . $object->post_type); //check a few admin specific stuff if a standard check didn't work, in case we're in the admin area translating via WPML if (empty($original_post_id) && is_admin()) { if (!empty($_REQUEST['trid'])) { //we are adding a new translation belonging to a trid set $original_post_id = SitePress::get_original_element_id_by_trid($_REQUEST['trid']); } elseif (!empty($_REQUEST['icl_translation_of'])) { //a new translation has just been submitted $translation_of = $_REQUEST['icl_translation_of']; //could be a translation from another translation, e.g. try adding a translation from a second language $original_post_id = SitePress::get_original_element_id($translation_of, 'post_' . $object->post_type); } } } //save to the cache (whether already saved or not) $em_wpml_original_cache[$object->blog_id][$object->post_id] = $original_post_id; //if the post_ids don't match then the original translation is different to the one passed on, so switch the $object to that translation if ($original_post_id != $object->post_id) { //get the EM_Event or EM_Location object if ($object->post_type == EM_POST_TYPE_EVENT) { $object = em_get_event($original_post_id, 'post_id'); } elseif ($object->post_type == EM_POST_TYPE_LOCATION) { $object = em_get_location($original_post_id, 'post_id'); } } return $object; }
function __construct($taxonomy = '', $args = array()) { global $wpdb, $sitepress, $sitepress_settings; $default_language = $sitepress->get_default_language(); $_active_languages = $sitepress->get_active_languages(); if (empty($taxonomy)) { global $wp_taxonomies; foreach ($wp_taxonomies as $tax_key => $tax) { if ($sitepress->is_translated_taxonomy($tax_key)) { $this->taxonomy = $tax_key; break; } } } else { $this->taxonomy = $taxonomy; } $this->args = $args; $this->show_selector = isset($args['taxonomy_selector']) ? $args['taxonomy_selector'] : true; $this->show_tax_sync = isset($args['taxonomy_sync']) ? $args['taxonomy_sync'] : true; $this->taxonomy_obj = get_taxonomy($this->taxonomy); // filters $this->status = isset($this->args['status']) ? $this->args['status'] : WPML_TT_TAXONOMIES_NOT_TRANSLATED; if (isset($this->args['languages']) && $this->args['languages']) { foreach ($_active_languages as $language) { if (in_array($language['code'], $args['languages'])) { $selected_languages[$language['code']] = $language; } } } $this->selected_languages = !empty($selected_languages) ? $selected_languages : $_active_languages; if (defined('WPML_ST_FOLDER')) { // get labels translations if ($sitepress_settings['st']['strings_language'] != $default_language) { $singular_original = $wpdb->get_var($wpdb->prepare("SELECT s.value FROM {$wpdb->prefix}icl_strings s\r\n JOIN {$wpdb->prefix}icl_string_translations t ON t.string_id = s.id \r\n WHERE s.context='WordPress' AND t.value = %s AND s.name LIKE %s AND t.language=%s", $this->taxonomy_obj->labels->singular_name, 'taxonomy singular name: %', $sitepress->get_admin_language())); $general_original = $wpdb->get_var($wpdb->prepare("SELECT s.value FROM {$wpdb->prefix}icl_strings s\r\n JOIN {$wpdb->prefix}icl_string_translations t ON t.string_id = s.id \r\n WHERE s.context='WordPress' AND t.value = %s AND s.name LIKE %s AND t.language=%s", $this->taxonomy_obj->labels->name, 'taxonomy general name: %', $sitepress->get_admin_language())); } if (empty($singular_original)) { $singular_original = $this->taxonomy_obj->labels->singular_name; } if (empty($general_original)) { $general_original = $this->taxonomy_obj->labels->name; } $this->taxonomy_obj->labels_translations[$sitepress_settings['st']['strings_language']]['singular'] = $singular_original; $this->taxonomy_obj->labels_translations[$sitepress_settings['st']['strings_language']]['general'] = $general_original; $languages_pool = array_diff(array_merge(array_keys($this->selected_languages), array($default_language)), array($sitepress_settings['st']['strings_language'])); foreach ($languages_pool as $language) { $singular = $wpdb->get_var($wpdb->prepare("SELECT t.value FROM {$wpdb->prefix}icl_string_translations t\r\n JOIN {$wpdb->prefix}icl_strings s ON t.string_id = s.id \r\n WHERE s.context='WordPress' and s.name=%s AND t.language=%s", 'taxonomy singular name: ' . $singular_original, $language)); $general = $wpdb->get_var($wpdb->prepare("SELECT t.value FROM {$wpdb->prefix}icl_string_translations t\r\n JOIN {$wpdb->prefix}icl_strings s ON t.string_id = s.id \r\n WHERE s.context='WordPress' and s.name=%s AND t.language=%s", 'taxonomy general name: ' . $general_original, $language)); $this->taxonomy_obj->labels_translations[$language]['singular'] = $singular ? $singular : ''; $this->taxonomy_obj->labels_translations[$language]['general'] = $general ? $general : ''; } } // build list of exclusion based on filters foreach ($this->selected_languages as $language) { $lcode_alias = str_replace('-', '', $language['code']); $joins[] = " LEFT JOIN {$wpdb->prefix}icl_translations t{$lcode_alias} ON t{$lcode_alias}.trid = t.trid AND t{$lcode_alias}.language_code='{$language['code']}'"; $selects[] = "t{$lcode_alias}.element_id AS element_id_{$lcode_alias}"; } if (isset($joins) && isset($selects)) { $joins = join(' ', $joins); $selects = join(', ', $selects); if ($this->status == WPML_TT_TAXONOMIES_NOT_TRANSLATED) { $res = $wpdb->get_results($wpdb->prepare("\r\n\t\t SELECT t.element_id, {$selects}\r\n\t\t FROM {$wpdb->prefix}icl_translations t\r\n\t\t {$joins}\r\n\t\t WHERE t.element_type = %s AND t.language_code = %s\r\n\t\t ", 'tax_' . $this->taxonomy, $default_language)); foreach ($res as $row) { $translations = 0; foreach ($row as $r) { if ($r > 0) { $translations++; } } } } } if (!empty($excludes)) { $get_terms_args['exclude'] = $wpdb->get_col($wpdb->prepare("SELECT term_id FROM {$wpdb->term_taxonomy} WHERE taxonomy=%s AND term_taxonomy_id IN (" . join(',', $excludes) . ")", $this->taxonomy)); } // get_terms args $get_terms_args['hide_empty'] = false; $get_terms_args['orderby'] = 'name'; if (!empty($this->args['search'])) { $get_terms_args['search'] = $this->args['search']; $this->search = $args['search']; } if (!empty($this->args['child_of'])) { $get_terms_args['child_of'] = $this->args['child_of']; $this->child_of = $get_terms_args['child_of']; } else { $this->child_of = 0; } //No filtering necessary here. All language filtering is done be the subsequent functionality. remove_all_filters('terms_clauses'); remove_all_filters('list_terms_exclusions'); $_terms = get_terms($this->taxonomy, $get_terms_args); // on search - force include parents if (!empty($this->search)) { if ($_terms) { foreach ($_terms as $term) { $in_results = false; foreach ($_terms as $term2) { if ($term2->term_id == $term->parent) { $in_results = true; break; } } if (!$in_results) { while ($term->parent > 0) { $term = get_term($term->parent, $this->taxonomy); $_terms[] = $term; } } } } } $this->terms_count = count($_terms); $_terms = $this->order_terms_by_hierarchy($_terms); $this->current_page = isset($this->args['page']) ? $this->args['page'] : 1; $offset = ($this->current_page - 1) * WPML_TT_TERMS_PER_PAGE; $this->terms = array_slice($_terms, $offset, WPML_TT_TERMS_PER_PAGE); // prepend parents if needed if (isset($this->terms[0])) { while ($this->terms[0]->parent > 0 && $this->terms[0]->parent != $this->child_of) { foreach ($_terms as $term) { if ($term->term_id == $this->terms[0]->parent) { $guide_parent = $term; break; } } if (!empty($guide_parent)) { array_unshift($this->terms, $guide_parent); } } } unset($_terms); if (is_wp_error($this->terms)) { $this->error = sprintf(__('Unknown taxonomy: %s'), $this->taxonomy); return false; } if (empty($this->terms) || is_wp_error($this->terms)) { return; } // limit for pagination? // get term taxonomy ids foreach ($this->terms as $term) { $tt_ids[] = $term->term_taxonomy_id; } // get list of matching trids $trids = $wpdb->get_col($wpdb->prepare("SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_type = %s AND element_id IN (" . join(',', $tt_ids) . ")", 'tax_' . $this->taxonomy)); // get terms by trids $res = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}icl_translations WHERE element_type = %s AND trid IN (" . join(',', $trids) . ")", 'tax_' . $this->taxonomy)); $terms_by_trid = array(); foreach ($res as $row) { $terms_by_trid[$row->trid][$row->language_code] = array('term_taxonomy_id' => $row->element_id, 'source_lang' => $row->source_language_code); } $terms_in_old_format = $this->terms; $this->terms = array(); foreach ($terms_by_trid as $trid => $term_translation) { foreach ($term_translation as $lang => $term_trid_array) { foreach ($terms_in_old_format as $key => $term_object) { if (isset($term_object->term_taxonomy_id) && $term_object->term_taxonomy_id == $term_trid_array['term_taxonomy_id']) { if (!$term_trid_array['source_lang']) { $term_object->translation_of = $term_object->term_taxonomy_id; } else { $term_object->translation_of = SitePress::get_original_element_id($term_object->term_taxonomy_id, 'tax' . $taxonomy); } $this->terms[$trid][$lang] = $term_object; if ($term_object->translation_of == $term_object->term_taxonomy_id || !isset($this->terms[$trid]['source_lang'])) { $this->terms[$trid]['source_lang'] = $lang; } } } } } /* If we try to filter for untranslated terms, we will not show any row that has translations in every language. */ foreach ($this->terms as $key => $translated_terms) { $all_found = true; foreach ($this->selected_languages as $lang => $langauge) { if (!isset($translated_terms[$lang])) { $all_found = false; } } if (!$all_found && isset($_active_languages[$translated_terms['source_lang']])) { /* If we try to filter for untranslated terms, we will always show the original element and its column.*/ $this->selected_languages[$translated_terms['source_lang']] = $_active_languages[$translated_terms['source_lang']]; } elseif (isset($this->args['status']) && $this->args['status'] == 1) { unset($this->terms[$key]); } } }