/** * @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; }
/** * Handle job update notifications from TP * * @param array $args * @param bool $bypass_auth if true forces ignoring the signature check when used together with polling * * @throws InvalidArgumentException * @return int|string */ function update_status($args, $bypass_auth = false) { if (!isset($args[0], $args[1], $args[2], $args[3])) { throw new InvalidArgumentException('This method requires an array of 4 input parameters!'); } $this->last_translation_proxy_job_id = $args[0]; $this->last_cms_id = $args[1]; $this->last_status = $args[2]; $this->last_signature = $args[3]; if (!$bypass_auth && !$this->authenticate_request()) { return 'Wrong signature'; } $job_data = array(); $job_data['id'] = $this->last_translation_proxy_job_id; $job_data['cms_id'] = $this->last_cms_id; $job_data['job_state'] = $this->last_status; $job_data['source_language'] = false; $job_data['target_language'] = false; switch ($this->last_status) { case 'translation_ready': $ret = $this->pro_translation->download_and_process_translation($this->last_translation_proxy_job_id, $this->last_cms_id); break; case 'cancelled': $ret = $this->pro_translation->cancel_translation($this->last_translation_proxy_job_id, $this->last_cms_id); break; default: return "Not supported status: {$this->last_status}"; } $this->last_job_data = $job_data; if ($this->pro_translation->errors) { $result = implode('', $this->pro_translation->errors); } elseif ((bool) $ret === true) { $result = self::CMS_SUCCESS; } else { $result = self::CMS_FAILED; } return $result; }
/** * * Handle job update notifications from TP * * @param array $args * @param bool $bypass_auth if true forces ignoring the signature check when used together with polling * * @return int|string */ function update_status($args, $bypass_auth = false) { if (!(isset($args[0]) && isset($args[1]) && isset($args[2]) && isset($args[3]))) { throw new InvalidArgumentException('This method requires an array of 4 input parameters!'); } $translation_proxy_job_id = $args[0]; $cms_id = $args[1]; $status = $args[2]; $signature = $args[3]; if (!$bypass_auth && !$this->authenticate_request($translation_proxy_job_id, $cms_id, $status, $signature)) { return "Wrong signature"; } switch ($status) { case "translation_ready": $ret = $this->pro_translation->download_and_process_translation($translation_proxy_job_id, $cms_id); break; case "cancelled": $ret = $this->pro_translation->cancel_translation($translation_proxy_job_id, $cms_id); break; default: return "Not supported status: {$status}"; } return $this->pro_translation->errors ? join('', $this->pro_translation->errors) : ((bool) $ret === true ? self::CMS_SUCCESS : self::CMS_FAILED); }