private function init_job() { $this->job = false; $this->translator_has_job_language_pairs = false; $this->user_can_take_this_job = false; $this->job_id = WPML_Translation_Editor::get_job_id_from_request(); if ($this->job_id) { WPML_Translation_Job_Terms::set_translated_term_values($this->job_id); $this->job = $this->sitepressTM->get_translation_job($this->job_id, false, true, 1); if ($this->job) { $this->user_can_take_this_job = $this->job->translator_id == 0 || $this->job->translator_id == $this->current_user->ID; $this->job_language_pairs = array('lang_from' => $this->job->source_language_code, 'lang_to' => $this->job->language_code); $this->translator_has_job_language_pairs = $this->sitepressTM->is_translator($this->current_user->ID, $this->job_language_pairs); } } $this->is_valid_job = !$this->job || (!$this->user_can_take_this_job || !$this->translator_has_job_language_pairs) && !current_user_can('manage_options'); }
/** * @param WP_Post|WPML_Package $post * @param $target_languages * @param int $translator_id * @param $job_id * * @return bool|int */ function send_post($post, $target_languages, $translator_id, $job_id) { global $sitepress, $sitepress_settings, $wpdb, $iclTranslationManagement; $this->maybe_init_translation_management($iclTranslationManagement); if (is_numeric($post)) { $post = get_post($post); } if (!$post) { return false; } $post_id = $post->ID; $post_type = $post->post_type; $element_type_prefix = $iclTranslationManagement->get_element_type_prefix_from_job_id($job_id); $element_type = $element_type_prefix . '_' . $post_type; $err = false; $res = false; $source_language = $sitepress->get_language_for_element($post_id, $element_type); if (empty($target_languages)) { return false; } // Make sure the previous request is complete. // Only send if it needs update foreach ($target_languages as $target_language) { if ($target_language == $source_language) { continue; } $translation = $this->tmg->get_element_translation($post_id, $target_language, $element_type); if (!$translation) { // translated the first time $err = true; } if (!$err && ($translation->needs_update || $translation->status == ICL_TM_NOT_TRANSLATED || $translation->status == ICL_TM_WAITING_FOR_TRANSLATOR)) { $project = TranslationProxy::get_current_project(); if ($post_type == 'page') { $post_url = site_url() . '?page_id=' . $post_id; } else { $post_url = site_url() . '?p=' . $post_id; } if ($iclTranslationManagement->is_external_type($element_type_prefix)) { $post_url = apply_filters('wpml_external_item_url', $post_url, $post_id); } if ($sitepress->get_setting('tm_block_retranslating_terms')) { WPML_Translation_Job_Terms::set_translated_term_values($job_id, true); } $xliff = new WPML_TM_xliff(); $file = $xliff->get_job_xliff_file($job_id); $title = $post->post_title; $cms_id = $this->build_cms_id($post_id, $post_type, $source_language, $target_language, $job_id); $url = $post_url; $word_count = self::estimate_total_word_count($post, $source_language); $note = isset($note) ? $note : ''; $is_update = intval(!empty($translation->element_id)); try { if (TranslationProxy::is_batch_mode()) { $res = $project->send_to_translation_batch_mode($file, $title, $cms_id, $url, $source_language, $target_language, $word_count, $translator_id, $note, $is_update); } else { $res = $project->send_to_translation($file, $title, $cms_id, $url, $source_language, $target_language, $word_count, $translator_id, $note, $is_update); } } catch (Exception $err) { // The translation entry will be removed $res = 0; } if ($res) { $this->tmg->update_translation_status(array('translation_id' => $translation->translation_id, 'status' => ICL_TM_IN_PROGRESS, 'needs_update' => 0)); } else { $this->enqueue_project_errors($project); $previous_state = $wpdb->get_var($wpdb->prepare("SELECT _prevstate FROM {$wpdb->prefix}icl_translation_status WHERE translation_id=%d", $translation->translation_id)); if (!empty($previous_state)) { $previous_state = unserialize($previous_state); $data = array('status' => $previous_state['status'], 'translator_id' => $previous_state['translator_id'], 'needs_update' => $previous_state['needs_update'], 'md5' => $previous_state['md5'], 'translation_service' => $previous_state['translation_service'], 'translation_package' => $previous_state['translation_package'], 'timestamp' => $previous_state['timestamp'], 'links_fixed' => $previous_state['links_fixed']); $data_where = array('translation_id' => $translation->translation_id); $wpdb->update($wpdb->prefix . 'icl_translation_status', $data, $data_where); } else { $data = array('status' => ICL_TM_NOT_TRANSLATED, 'needs_update' => 0); $data_where = array('translation_id' => $translation->translation_id); $wpdb->update($wpdb->prefix . 'icl_translation_status', $data, $data_where); } $err = true; } } // if needs translation } // foreach target lang return $err ? false : $res; //last $ret }