/**
  * @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 $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);
     }
 }