function send_strings_to_translation_service($string_ids, $target_language, $translator_id) { /** @var WPML_String_Translation $WPML_String_Translation */ global $WPML_String_Translation; if (sizeof($string_ids) > 0) { $project = $this->basket->get_project(); $strings = array(); $word_count = 0; $source_language = $this->basket->get_source_language(); foreach ($string_ids as $string_id) { $string_data_query = "SELECT id, context, name, value FROM {$this->wpdb->prefix}icl_strings WHERE id=%d"; $string_data_prepare = $this->wpdb->prepare($string_data_query, $string_id); $string_data = $this->wpdb->get_row($string_data_prepare); $word_count += $WPML_String_Translation->estimate_word_count($string_data->value, $source_language); $strings[] = $string_data; } $xliff = new WPML_TM_Xliff_Writer($this->job_factory); $res = $project->send_to_translation_batch_mode($xliff->get_strings_xliff_file($strings, $source_language, $target_language), 'String Translations', '', '', $source_language, $target_language, $word_count); if ($res) { foreach ($strings as $string_data) { $translation_service = TranslationProxy_Service::get_translator_data_from_wpml($translator_id); $added = icl_add_string_translation($string_data->id, $target_language, null, ICL_TM_WAITING_FOR_TRANSLATOR, $translation_service['translator_id'], $translation_service['translation_service'], TranslationProxy_Batch::update_translation_batch($this->basket->get_name())); if ($added) { $data = array('rid' => $res, 'string_translation_id' => $added, 'timestamp' => date('Y-m-d H:i:s'), 'md5' => md5($string_data->value)); $this->wpdb->insert($this->wpdb->prefix . 'icl_string_status', $data); //insert rid } } $this->wpdb->insert($this->wpdb->prefix . 'icl_core_status', array('rid' => $res, 'module' => '', 'origin' => $source_language, 'target' => $target_language, 'status' => ICL_TM_WAITING_FOR_TRANSLATOR)); if ($project->errors && count($project->errors)) { $res['errors'] = $project->errors; } return $res; } } return 0; }
function send_jobs($data) { global $wpdb, $sitepress; if (!isset($data['tr_action']) && isset($data['translate_to'])) { //adapt new format $data['tr_action'] = $data['translate_to']; unset($data['translate_to']); } if (isset($data['iclpost'])) { //adapt new format $data['posts_to_translate'] = $data['iclpost']; unset($data['iclpost']); } if (isset($data['post'])) { //adapt new format $data['posts_to_translate'] = $data['post']; unset($data['post']); } $batch_name = isset($data['batch_name']) ? $data['batch_name'] : false; $translate_from = TranslationProxy_Basket::get_source_language(); $data_default = array('translate_from' => $translate_from); extract($data_default); extract($data, EXTR_OVERWRITE); // no language selected ? if (!isset($tr_action) || empty($tr_action)) { $this->dashboard_select = $data; // prepopulate dashboard return false; } // no post selected ? if (!isset($posts_to_translate) || empty($posts_to_translate)) { $this->dashboard_select = $data; // pre-populate dashboard return false; } $selected_posts = $posts_to_translate; $selected_translators = isset($translators) ? $translators : array(); $selected_languages = $tr_action; $job_ids = array(); $element_type_prefix = 'post'; if (isset($data['element_type_prefix'])) { $element_type_prefix = $data['element_type_prefix']; } foreach ($selected_posts as $post_id) { $post = $this->get_post($post_id, $element_type_prefix); if (!$post) { continue; } $element_type = $element_type_prefix . '_' . $post->post_type; $post_trid = $sitepress->get_element_trid($post_id, $element_type); $post_translations = $sitepress->get_element_translations($post_trid, $element_type); $md5 = $this->post_md5($post); $translation_package = $this->create_translation_package($post); foreach ($selected_languages as $lang => $action) { // making this a duplicate? if ($action == 2) { // don't send documents that are in progress $current_translation_status = $this->get_element_translation($post_id, $lang, $element_type); if ($current_translation_status && $current_translation_status->status == ICL_TM_IN_PROGRESS) { continue; } $job_ids[] = $this->make_duplicate($post_id, $lang); } elseif ($action == 1) { if (empty($post_translations[$lang])) { $translation_id = $sitepress->set_element_language_details(null, $element_type, $post_trid, $lang, $translate_from); } else { $translation_id = $post_translations[$lang]->translation_id; } // don't send documents that are in progress // don't send documents that are already translated and don't need update $current_translation_status = $this->get_element_translation($post_id, $lang, $element_type); if ($current_translation_status && $current_translation_status->status == ICL_TM_IN_PROGRESS) { continue; } $_status = ICL_TM_WAITING_FOR_TRANSLATOR; if (isset($selected_translators[$lang])) { $translator = $selected_translators[$lang]; } else { $translator = get_current_user_id(); // returns current user id or 0 if user not logged in } $translation_data = TranslationProxy_Service::get_translator_data_from_wpml($translator); $translation_service = $translation_data['translation_service']; $translator_id = $translation_data['translator_id']; // set as default translator if ($translator_id > 0) { $this->set_default_translator($translator_id, $translate_from, $lang, $translation_service); } // add translation_status record $data = array('translation_id' => $translation_id, 'status' => $_status, 'translator_id' => $translator_id, 'needs_update' => 0, 'md5' => $md5, 'translation_service' => $translation_service, 'translation_package' => serialize($translation_package), 'batch_id' => TranslationProxy_Batch::update_translation_batch($batch_name)); $_prevstate = $wpdb->get_row($wpdb->prepare("\n\t\t\t\t\t\tSELECT status, translator_id, needs_update, md5, translation_service, translation_package, timestamp, links_fixed\n\t\t\t\t\t\tFROM {$wpdb->prefix}icl_translation_status\n\t\t\t\t\t\tWHERE translation_id = %d\n\t\t\t\t\t", $translation_id), ARRAY_A); if ($_prevstate) { $data['_prevstate'] = serialize($_prevstate); } $update_translation_status = $this->update_translation_status($data); $rid = $update_translation_status[0]; //__ adds or updates row in icl_translation_status, $job_id = $this->add_translation_job($rid, $translator_id, $translation_package); $job_ids[] = $job_id; if ($translation_service !== 'local') { /** @global WPML_Pro_Translation $ICL_Pro_Translation */ global $ICL_Pro_Translation; $sent = $ICL_Pro_Translation->send_post($post, array($lang), $translator_id, $job_id); if (!$sent) { $job_id = array_pop($job_ids); $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}icl_translate_job WHERE job_id=%d", $job_id)); $wpdb->query($wpdb->prepare("UPDATE {$wpdb->prefix}icl_translate_job SET revision = NULL WHERE rid=%d ORDER BY job_id DESC LIMIT 1", $rid)); $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}icl_translate WHERE job_id=%d", $job_id)); } } } // if / else is making a duplicate } } icl_cache_clear(); do_action('wpml_tm_empty_mail_queue'); return $job_ids; }
function send_strings_to_translation_service($string_ids, $target_language, $basket_name, $translator_id) { global $wpdb; // get all the untranslated strings $untranslated = array(); foreach ($string_ids as $st_id) { $untranslated[] = $st_id; } if (sizeof($untranslated) > 0) { $project = TranslationProxy::get_current_project(); $strings = array(); $word_count = 0; $source_language = $this->get_strings_language(); foreach ($untranslated as $string_id) { $string_data_query = "SELECT id, context, name, value FROM {$wpdb->prefix}icl_strings WHERE id=%d"; $string_data_prepare = $wpdb->prepare($string_data_query, $string_id); $string_data = $wpdb->get_row($string_data_prepare); $word_count += $this->estimate_word_count($string_data->value, $source_language); $strings[] = $string_data; } $xliff = new WPML_TM_xliff(); $file = $xliff->get_strings_xliff_file($strings, $source_language, $target_language); $title = "String Translations"; $cms_id = ''; $url = ''; $timestamp = date('Y-m-d H:i:s'); if (TranslationProxy::is_batch_mode()) { $res = $project->send_to_translation_batch_mode($file, $title, $cms_id, $url, $source_language, $target_language, $word_count); } else { $res = $project->send_to_translation($file, $title, $cms_id, $url, $source_language, $target_language, $word_count); } if ($res) { foreach ($strings as $string_data) { $batch_id = TranslationProxy_Batch::update_translation_batch($basket_name); $translation_service = TranslationProxy_Service::get_translator_data_from_wpml($translator_id); $added = icl_add_string_translation($string_data->id, $target_language, null, ICL_TM_WAITING_FOR_TRANSLATOR, $translation_service['translator_id'], $translation_service['translation_service'], $batch_id); if ($added) { $data = array('rid' => $res, 'string_translation_id' => $added, 'timestamp' => $timestamp, 'md5' => md5($string_data->value)); $wpdb->insert($wpdb->prefix . 'icl_string_status', $data); //insert rid } else { $this->add_message(sprintf(__('Unable to add "%s" string in tables', 'sitepress'), $string_data->name), 'error'); return 0; } } $wpdb->insert($wpdb->prefix . 'icl_core_status', array('rid' => $res, 'module' => '', 'origin' => $source_language, 'target' => $target_language, 'status' => CMS_REQUEST_WAITING_FOR_PROJECT_CREATION)); if ($project->errors && count($project->errors)) { $res['errors'] = $project->errors; } return $res; } } return 0; }