/**
  * @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;
 }
 /**
  *
  * Cancel translation for given cms_id
  *
  * @param $rid
  * @param $cms_id
  *
  * @return bool
  */
 function cancel_translation($rid, $cms_id)
 {
     /**
      * @var WPML_String_Translation $WPML_String_Translation
      * @var TranslationManagement $iclTranslationManagement
      */
     global $sitepress, $wpdb, $WPML_String_Translation, $iclTranslationManagement;
     $res = false;
     if (empty($cms_id)) {
         // it's a string
         if (isset($WPML_String_Translation)) {
             $res = $WPML_String_Translation->cancel_remote_translation($rid);
         }
     } else {
         $cms_id_parts = $this->cms_id_helper->parse_cms_id($cms_id);
         $post_type = $cms_id_parts[0];
         $_element_id = $cms_id_parts[1];
         $_target_lang = $cms_id_parts[3];
         $job_id = isset($cms_id_parts[4]) ? $cms_id_parts[4] : false;
         $element_type_prefix = 'post';
         if ($job_id) {
             $element_type_prefix = $iclTranslationManagement->get_element_type_prefix_from_job_id($job_id);
         }
         $element_type = $element_type_prefix . '_' . $post_type;
         if ($_element_id && $post_type && $_target_lang) {
             $trid = $sitepress->get_element_trid($_element_id, $element_type);
         } else {
             $trid = null;
         }
         if ($trid) {
             $translation_id_query = "SELECT i.translation_id\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFROM {$wpdb->prefix}icl_translations i\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tJOIN {$wpdb->prefix}icl_translation_status s\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tON i.translation_id = s.translation_id\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE i.trid=%d\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tAND i.language_code=%s\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tAND s.status IN (%d, %d)\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tLIMIT 1";
             $translation_id_args = array($trid, $_target_lang, ICL_TM_IN_PROGRESS, ICL_TM_WAITING_FOR_TRANSLATOR);
             $translation_id_prepare = $wpdb->prepare($translation_id_query, $translation_id_args);
             $translation_id = $wpdb->get_var($translation_id_prepare);
             if ($translation_id) {
                 global $iclTranslationManagement;
                 $iclTranslationManagement->cancel_translation_request($translation_id);
                 $res = true;
             }
         }
     }
     return $res;
 }