/**
  * @param array $jobs
  *
  * @return array jobs array, from which all pending jobs correctly tracked in the wpml database were removed
  */
 private function filter_known_pending($jobs)
 {
     foreach ($jobs as &$job) {
         if ($job->cms_id && in_array($job->job_state, array('waiting_translation', 'delivered')) && $this->cms_id_helper->get_translation_id($job->cms_id)) {
             $job = null;
         }
     }
     return array_values(array_filter($jobs));
 }
 /**
  * @param array $jobs
  *
  * @return array jobs array, from which all pending jobs correctly tracked in the wpml database were removed
  */
 private function filter_known_pending($jobs)
 {
     foreach ($jobs as &$job) {
         if ($job->cms_id && in_array($job->job_state, array('waiting_translation', 'delivered')) && $this->cms_id_helper->get_translation_id($job->cms_id)) {
             $job = null;
         } elseif ($job->job_state === 'delivered' && !$job->cms_id && apply_filters('wpml_st_job_state_pending', false, $job)) {
             $job->job_state = 'translation_ready';
         }
     }
     return array_values(array_filter($jobs));
 }
 /**
  *
  * Downloads translation from TP and updates its document
  *
  * @param $translation_proxy_job_id
  * @param $cms_id
  *
  * @return bool|string
  *
  */
 function download_and_process_translation($translation_proxy_job_id, $cms_id)
 {
     try {
         global $wpdb;
         if (empty($cms_id)) {
             // it's a string
             //TODO: [WPML 3.3] this should be handled as any other element type in 3.3
             $target = $wpdb->get_var($wpdb->prepare("SELECT target FROM {$wpdb->prefix}icl_core_status WHERE rid=%d", $translation_proxy_job_id));
             return $this->process_translated_string($translation_proxy_job_id, $target);
         } else {
             $translation_id = $this->cms_id_helper->get_translation_id($cms_id);
             if (!empty($translation_id)) {
                 if ($this->add_translated_document($translation_id, $translation_proxy_job_id) === true) {
                     $this->throw_exception_for_mysql_errors();
                     return true;
                 } else {
                     $this->throw_exception_for_mysql_errors();
                     return false;
                 }
                 //in other case do not process that request
             } else {
                 return false;
             }
         }
     } catch (Exception $e) {
         return $e->getMessage();
     }
 }
 /**
  * @param TranslationProxy_Project $project
  *
  * @return array
  */
 function poll_for_translations($project)
 {
     /** @var WPML_String_Translation $WPML_String_Translation */
     global $sitepress, $WPML_String_Translation;
     $pending_jobs = $project->pending_jobs();
     $cancelled_jobs = $project->cancelled_jobs();
     $results = array_fill_keys(array('completed', 'cancelled', 'errors'), 0);
     $posts_need_sync = array();
     if ($pending_jobs) {
         foreach ($pending_jobs as $job) {
             $ret = $this->pro_translation->download_and_process_translation($job->id, $job->cms_id);
             if ($ret) {
                 $results['completed']++;
                 if ($job->cms_id) {
                     list(, $id_to_sync) = $this->cms_id_helper->parse_cms_id($job->cms_id);
                 } else {
                     $id_to_sync = $job->id;
                 }
                 $posts_need_sync[] = $id_to_sync;
             }
         }
     }
     if (!empty($cancelled_jobs)) {
         foreach ($cancelled_jobs as $job) {
             $ret = false;
             if ($job->cms_id != "") {
                 //we have a cms id for post translations
                 $ret = $this->pro_translation->cancel_translation($job->id, $job->cms_id);
                 $ret = $ret ? 1 : 0;
             } else {
                 //we only have an empty string here for string translations
                 if (isset($WPML_String_Translation)) {
                     $ret = isset($job->id) ? $WPML_String_Translation->cancel_remote_translation($job->id) : false;
                 }
             }
             if ($ret) {
                 $results['cancelled'] += $ret;
             }
         }
     }
     $sitepress->set_setting('last_picked_up', strtotime(current_time('mysql')));
     $sitepress->save_settings();
     $this->pro_translation->enqueue_project_errors($project);
     do_action('wpml_new_duplicated_terms', $posts_need_sync, false);
     return $results;
 }
 /**
  * @param $data
  *
  * @return array
  */
 public function poll_job($data)
 {
     $job = !empty($data['job_polled']) ? $data['job_polled'] : false;
     $results = array('completed' => empty($data['completed_jobs']) ? 0 : $data['completed_jobs'], 'cancelled' => empty($data['cancelled_jobs']) ? 0 : $data['cancelled_jobs'], 'errors' => empty($data['error_jobs']) ? array() : $data['error_jobs']);
     if ($job && in_array($job['job_state'], array('cancelled', 'translation_ready', 'delivered', 'waiting_translation'))) {
         $is_missing_in_db = !empty($job['cms_id']) && !$this->cms_id_helper->get_translation_id($job['cms_id']);
         $job_state = in_array($job['job_state'], array('delivered', 'waiting_translation'), true) && $is_missing_in_db ? 'translation_ready' : $job['job_state'];
         if ($job_state === 'translation_ready' || !$is_missing_in_db && $job_state === 'cancelled') {
             $res = $this->pro_translation->xmlrpc_updated_job_status_with_log(array($job['id'], $job['cms_id'], $job_state), true);
         } else {
             $res = 0;
         }
         if ($res === 1) {
             if ($job['job_state'] === 'translation_ready') {
                 $results['completed']++;
             } elseif ($job['job_state'] === 'cancelled') {
                 $results['cancelled']++;
             }
         } else {
             $results['errors'] = (array) $results['errors'];
             $results['errors'][] = $res;
         }
     }
     $errors = "";
     $status_cancelled = "";
     if (!empty($results['errors'])) {
         $status = __('Error', 'sitepress');
         $errors = join('<br />', array_filter((array) $results['errors']));
         $job_error = true;
     } else {
         $status = __('OK', 'sitepress');
         $job_error = false;
     }
     if ($results['completed'] == 1) {
         $status_completed = __('1 translation has been fetched from the translation service.', 'sitepress');
     } elseif ($results['completed'] > 1) {
         $status_completed = sprintf(__('%d translations have been fetched from the translation service.', 'sitepress'), $results['completed']);
     } else {
         $status_completed = '';
     }
     if ($results['cancelled']) {
         $status_cancelled = sprintf(__('%d translations have been marked as cancelled.', 'sitepress'), $results['cancelled']);
     }
     return array('job_error' => $job_error, 'status' => $status, 'errors' => $errors, 'completed' => $status_completed, 'cancelled' => $status_cancelled);
 }
 /**
  *
  * Downloads translation from TP and updates its document
  *
  * @param $translation_proxy_job_id
  * @param $cms_id
  *
  * @return bool|string
  *
  */
 function download_and_process_translation($translation_proxy_job_id, $cms_id)
 {
     global $wpdb;
     if (empty($cms_id)) {
         // it's a string
         //TODO: [WPML 3.3] this should be handled as any other element type in 3.3
         $target = $wpdb->get_var($wpdb->prepare("SELECT target FROM {$wpdb->prefix}icl_core_status WHERE rid=%d", $translation_proxy_job_id));
         return $this->process_translated_string($translation_proxy_job_id, $target);
     } else {
         $translation_id = $this->cms_id_helper->get_translation_id($cms_id, TranslationProxy::get_current_service());
         return !empty($translation_id) && $this->add_translated_document($translation_id, $translation_proxy_job_id);
     }
 }
 /**
  * @param TranslationProxy_Project $project
  * @param bool $batch_mode
  * @param int $translator_id
  * @param WPML_TM_CMS_ID $cms_id_helper
  * @param TranslationManagement $tm_instance
  * @param null|string $note
  *
  * @return array
  */
 function send_to_tp($project, $batch_mode, $translator_id, &$cms_id_helper, &$tm_instance, $note = null)
 {
     global $wpdb;
     $this->maybe_load_basic_data();
     $file = $this->to_xliff_file();
     $title = $this->get_title();
     $cms_id = $cms_id_helper->cms_id_from_job_id($this->get_id());
     $url = $this->get_url(true);
     $word_count = $this->estimate_word_count();
     $note = isset($note) ? $note : '';
     $is_update = intval($this->get_resultant_element_id());
     $source_language = $this->get_source_language_code();
     $target_language = $this->get_language_code();
     try {
         if ($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;
     }
     $translation_id = $this->get_translation_id();
     if ($res) {
         $tm_instance->update_translation_status(array('translation_id' => $translation_id, 'translator_id' => $translator_id, 'status' => ICL_TM_IN_PROGRESS, 'needs_update' => 0));
     } else {
         $previous_state = $wpdb->get_var($wpdb->prepare("\tSELECT _prevstate\n\t\t\t\t\t\t\t\t\tFROM {$wpdb->prefix}icl_translation_status\n\t\t\t\t\t\t\t\t\tWHERE translation_id=%d\n\t\t\t\t\t\t\t\t\tLIMIT 1", $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_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_id);
             $wpdb->update($wpdb->prefix . 'icl_translation_status', $data, $data_where);
         }
         $err = true;
     }
     return array(isset($err) ? $err : false, $project, $res);
 }