public function create_translation($locale) { $client = new Lingotek_API(); if (false === ($translation = $client->get_translation($this->document_id, $locale, $this->source))) { return; } self::$creating_translation = true; $translation = json_decode($translation, true); // wp_insert_post expects array $args = $translation['args']; // update existing translation if ($tr_id = $this->pllm->get_term($this->source, $locale)) { $args['name'] = $translation['name']; wp_update_term($tr_id, $this->type, $args); $this->safe_translation_status_update($locale, 'current'); } else { $tr_lang = $this->pllm->get_language($locale); // translate parent $term = get_term($this->source, $this->type); $args['parent'] = $term->parent && ($tr_parent = $this->pllm->get_translation('term', $term->parent, $locale)) ? $tr_parent : 0; // attempt to get a unique slug in case it already exists in another language if (isset($args['slug']) && term_exists($args['slug'])) { $args['slug'] .= '-' . $tr_lang->slug; } $tr = wp_insert_term($translation['name'], $this->type, $args); if (!is_wp_error($tr)) { $this->pllm->set_term_language($tr['term_id'], $tr_lang); $this->safe_translation_status_update($locale, 'current', array($tr_lang->slug => $tr['term_id'])); wp_set_object_terms($tr['term_id'], $this->term_id, 'term_translations'); } } self::$creating_translation = false; }
public function create_translation($locale, $automatic = false) { // Removes content sanitization so YouTube videos, links, etc don't get removed when inserting translations remove_filter('content_save_pre', 'wp_filter_post_kses'); remove_filter('content_filtered_save_pre', 'wp_filter_post_kses'); $client = new Lingotek_API(); if (false === ($translation = $client->get_translation($this->document_id, $locale, $this->source))) { return; } self::$creating_translation = true; $prefs = Lingotek_Model::get_prefs(); // need an array by default $translation = json_decode($translation, true); // wp_insert_post expects array $tr_post = $translation['post']; $post = get_post($this->source); // source post $tr_post['post_status'] = $prefs['download_post_status'] === self::SAME_AS_SOURCE ? $post->post_status : $prefs['download_post_status']; // status // update existing translation if ($tr_id = $this->pllm->get_post($this->source, $locale)) { $tr_post['ID'] = $tr_id; // copy or ignore metas self::copy_or_ignore_metas($post->ID, $tr_id); // translate metas if (isset($translation['metas'])) { self::copy_translated_metas($translation['metas'], $tr_id); } wp_update_post($tr_post); $this->safe_translation_status_update($locale, 'current'); } else { if ($this->translations[$locale] == 'ready' || $automatic) { unset($post->post_name); // forces the creation of a new default slug if not translated by Lingotek $tr_post = array_merge((array) $post, $tr_post); // copy all untranslated fields from the original post $tr_post['ID'] = null; // will force the creation of a new post // translate parent $tr_post['post_parent'] = $post->post_parent && ($tr_parent = $this->pllm->get_translation('post', $post->post_parent, $locale)) ? $tr_parent : 0; if ('attachment' == $post->post_type) { $tr_id = wp_insert_attachment($tr_post); add_post_meta($tr_id, '_wp_attachment_metadata', get_post_meta($this->source, '_wp_attachment_metadata', true)); add_post_meta($tr_id, '_wp_attached_file', get_post_meta($this->source, '_wp_attached_file', true)); } else { $tr_id = wp_insert_post($tr_post); } if ($tr_id) { $tr_lang = $this->pllm->get_language($locale); $this->pllm->set_post_language($tr_id, $tr_lang); $this->safe_translation_status_update($locale, 'current', array($tr_lang->slug => $tr_id)); wp_set_object_terms($tr_id, $this->term_id, 'post_translations'); // assign terms and metas $GLOBALS['polylang']->sync->copy_post_metas($this->source, $tr_id, $tr_lang->slug); // copy or ignore metas self::copy_or_ignore_metas($post->ID, $tr_id); // translate metas if (isset($translation['metas'])) { self::copy_translated_metas($translation['metas'], $tr_id); } } } } self::$creating_translation = false; // Adds content sanitization back in after Lingotek saves the translation add_filter('content_save_pre', 'wp_filter_post_kses'); add_filter('content_filtered_save_pre', 'wp_filter_post_kses'); }
public function create_translation($locale) { $client = new Lingotek_API(); if (false === ($translation = $client->get_translation($this->document_id, $locale))) { return; } $strings = wp_list_pluck(PLL_Admin_Strings::get_strings(), 'name', 'string'); // get the strings name for the filter $translations = json_decode($translation, true); // wp_insert_post expects array $language = $this->pllm->get_language($locale); $mo = new PLL_MO(); $mo->import_from_db($language); foreach ($translations as $key => $translation) { $translation = apply_filters('pll_sanitize_string_translation', $translation, $strings[$key], $this->name); $mo->add_entry($mo->make_entry($key, $translation)); } $mo->export_to_db($language); $this->safe_translation_status_update($locale, 'current'); }