/**
  * @param int $service_id
  *
  * @return TranslationProxy_Project|WP_Error
  */
 public static function select_service($service_id)
 {
     global $sitepress;
     $service_selected = false;
     $error = false;
     /** @var $service TranslationProxy_Service */
     $service = TranslationProxy_Api::proxy_request("/services/{$service_id}.json");
     if ($service) {
         self::deselect_active_service();
         //set language map
         $service->languages_map = self::languages_map($service);
         //set information about custom fields
         $service->custom_fields = self::get_custom_fields($service_id, true);
         $service->custom_fields_data = false;
         $sitepress->set_setting('translation_service', $service);
         $result = $service;
         $service_selected = true;
         //Force authentication if no user input is needed
         if (!TranslationProxy::service_requires_authentication($service)) {
             $networking = wpml_tm_load_tp_networking();
             $project_factory = new WPML_TP_Project_Factory();
             $auth = new WPML_TP_Service_Authentication($sitepress, $networking, $project_factory, new stdClass());
             $auth->run();
         }
     } else {
         $result = new WP_Error('0', 'No service selected', array('service_id' => $service_id));
     }
     //Do not store selected service if this operation failed;
     if ($error || !$service_selected) {
         $sitepress->set_setting('translation_service', false);
     }
     $sitepress->save_settings();
     return $result;
 }
 /**
  * @param int   $service_id
  * @param array $custom_fields
  * @param bool  $force
  *
  * @return TranslationProxy_Project|WP_Error
  */
 public static function authenticate_service($service_id, $custom_fields, $force = false)
 {
     global $sitepress;
     $service = self::get_current_service();
     if ($service_id != $service->id) {
         return new WP_Error('0', 'Services are not matching!');
     }
     if ($service) {
         $service->custom_fields_data = $custom_fields;
         $sitepress->set_setting('translation_service', $service);
         $project = self::create_project($service, $force);
         if ($project) {
             $translation_projects = self::get_translation_projects();
             $networking = wpml_tm_load_tp_networking();
             $extra_fields = $networking->get_extra_fields_remote($project);
             // Store project, to be used if service is restored
             $translation_project = array('id' => $project->id, 'access_key' => $project->access_key, 'ts_id' => $project->ts_id, 'ts_access_key' => $project->ts_access_key, 'extra_fields' => $extra_fields);
             $translation_projects[TranslationProxy_Project::generate_service_index($service)] = $translation_project;
             $sitepress->set_setting('icl_translation_projects', $translation_projects);
             $sitepress->save_settings();
         } else {
             $service->custom_fields_data = false;
             $sitepress->set_setting('translation_service', $service);
         }
         $result = $project;
     } else {
         $result = new WP_Error('0', 'No active service');
     }
     $sitepress->save_settings();
     return $result;
 }
 public function init()
 {
     $networking = wpml_tm_load_tp_networking();
     $this->wpml_icl_migration = new WPML_ICL_Migration($networking);
     add_action('wp_ajax_upgrade_icl_account', array($this, 'upgrade_icl_account'));
     add_action('wp_ajax_show_translator_list', array($this, 'show_translator_list'));
     add_action('wp_ajax_migrate_one_job', array($this, 'migrate_one_job'));
     add_action('wp_ajax_upgrade_icl_jobs', array($this, 'upgrade_icl_jobs'));
     add_action('wp_ajax_icl_job_count', array($this, 'icl_job_count'));
     add_action('admin_menu', array($this, 'add_admin_menu'));
 }
 public static function service_request($url, $params = array(), $method = 'GET', $has_return_value = true, $json_response = false, $has_api_response = false)
 {
     return wpml_tm_load_tp_networking()->send_request($url, $params, $method, $has_return_value, $json_response, $has_api_response);
 }
예제 #5
0
파일: ajax.php 프로젝트: agiper/wordpress
/**
 * Ajax action for authenticating and invalidating a translation service
 */
function wpml_tm_translation_service_authentication_ajax()
{
    if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'translation_service_authentication')) {
        die('Wrong Nonce');
    }
    /** @var SitePress $sitepress */
    global $sitepress;
    $networking = wpml_tm_load_tp_networking();
    $project_factory = new WPML_TP_Project_Factory();
    $auth_factory = new WPML_TP_Service_Authentication_Factory($sitepress, $networking, $project_factory);
    if (empty($_POST['invalidate']) && isset($_POST['service_id']) && isset($_POST['custom_fields'])) {
        $authentication_action = new WPML_TP_Service_Authentication_Ajax_Action($auth_factory, $_POST['custom_fields']);
    } elseif (!empty($_POST['invalidate'])) {
        $authentication_action = new WPML_TP_Service_Invalidation_Ajax_Action($auth_factory);
    }
    if (!isset($authentication_action)) {
        die('Invalid Request');
    }
    wp_send_json_success($authentication_action->run());
}
 public static function get_basket_extra_fields_array($force_refresh = false)
 {
     if ($force_refresh) {
         $networking = wpml_tm_load_tp_networking();
         $project = TranslationProxy::get_current_project();
         $extra_fields = $networking->get_extra_fields_remote($project);
     } else {
         $extra_fields = TranslationProxy::get_extra_fields_local();
     }
     return TranslationProxy::maybe_convert_extra_fields($extra_fields);
 }
 public function check_status($batch_id)
 {
     $tp_networking = wpml_tm_load_tp_networking();
     $params = array('batch_id' => $batch_id, 'project_id' => $this->id, 'accesskey' => $this->access_key);
     try {
         $tp_networking->send_request(OTG_TRANSLATION_PROXY_URL . "/batches/{batch_id}/check.json", $params, 'GET', false, true);
         return true;
     } catch (Exception $ex) {
         $this->add_error($ex->getMessage());
         return false;
     }
 }
 /**
  * @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, $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;
     $note = get_post_meta($post_id, '_icl_translator_note', true);
     if (!$note) {
         $note = null;
     }
     $err = false;
     $res = false;
     $source_language = $sitepress->get_language_for_element($post_id, $element_type);
     $target_language = is_array($target_languages) ? end($target_languages) : $target_languages;
     if (empty($target_language) || $target_language === $source_language) {
         return false;
     }
     $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)) {
         $tp_networking = wpml_tm_load_tp_networking();
         $project = $tp_networking->get_current_project();
         if ($iclTranslationManagement->is_external_type($element_type_prefix)) {
             $job_object = new WPML_External_Translation_Job($job_id);
         } else {
             $job_object = new WPML_Post_Translation_Job($job_id);
             $job_object->load_terms_from_post_into_job();
         }
         list($err, $project, $res) = $job_object->send_to_tp($project, $translator_id, $this->cms_id_helper, $this->tmg, $note);
         if ($err) {
             $this->enqueue_project_errors($project);
         }
     }
     return $err ? false : $res;
     //last $ret
 }
 public function check_status($batch_id)
 {
     $tp_networking = wpml_tm_load_tp_networking();
     $params = array('batch_id' => $batch_id, 'project_id' => $this->id, 'accesskey' => $this->access_key);
     $tp_networking->send_request(OTG_TRANSLATION_PROXY_URL . "/batches/{batch_id}/check.json", $params, 'GET', true);
 }