/**
  * @param array $file
  *
  * @return bool|WP_Error
  */
 private function import_xliff($file)
 {
     global $current_user, $iclTranslationManagement;
     get_currentuserinfo();
     // We don't want any redirects happening when we save the translation
     add_filter('wp_redirect', array($this, '_stop_redirect'));
     $this->success = array();
     $contents = array();
     if (isset($file['tmp_name']) && $file['tmp_name']) {
         $fh = fopen($file['tmp_name'], 'r');
         $data = fread($fh, 4);
         fclose($fh);
         if ($data[0] == 'P' && $data[1] == 'K' && $data[2] == chr(03) && $data[3] == chr(04)) {
             if (class_exists('ZipArchive')) {
                 $z = new ZipArchive();
                 $zopen = $z->open($file['tmp_name'], 4);
                 if (true !== $zopen) {
                     return new WP_Error('incompatible_archive', __('Incompatible Archive.'));
                 }
                 for ($i = 0; $i < $z->numFiles; $i++) {
                     if (!($info = $z->statIndex($i))) {
                         return new WP_Error('stat_failed', __('Could not retrieve file from archive.'));
                     }
                     $content = $z->getFromIndex($i);
                     if (false === $content) {
                         return new WP_Error('extract_failed', __('Could not extract file from archive.'), $info['name']);
                     }
                     $contents[$info['name']] = $content;
                 }
             } else {
                 require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';
                 $archive = new PclZip($file['tmp_name']);
                 // Is the archive valid?
                 if (false == ($archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING))) {
                     return new WP_Error('incompatible_archive', __('Incompatible Archive.'), $archive->errorInfo(true));
                 }
                 if (0 == count($archive_files)) {
                     return new WP_Error('empty_archive', __('Empty archive.'));
                 }
                 foreach ($archive_files as $content) {
                     $contents[$content['filename']] = $content['content'];
                 }
             }
         } else {
             $fh = fopen($file['tmp_name'], 'r');
             $data = fread($fh, $file['size']);
             fclose($fh);
             $contents[$file['name']] = $data;
         }
         foreach ($contents as $name => $content) {
             if (!function_exists('simplexml_load_string')) {
                 return new WP_Error('xml_missing', __('The Simple XML library is missing.', 'wpml-translation-management'));
             }
             $new_error_handler = create_function('$errno, $errstr, $errfile, $errline', 'throw new ErrorException( $errstr, $errno, 1, $errfile, $errline );');
             set_error_handler($new_error_handler);
             try {
                 $xml = simplexml_load_string($content);
             } catch (Exception $e) {
                 $xml = false;
             }
             restore_error_handler();
             if (!$xml || !isset($xml->file)) {
                 return new WP_Error('not_xml_file', sprintf(__('"%s" is not a valid XLIFF file.', 'wpml-translation-management'), $name));
             }
             $file_attributes = $xml->file->attributes();
             if (!$file_attributes || !isset($file_attributes['original'])) {
                 return new WP_Error('not_xml_file', sprintf(__('"%s" is not a valid XLIFF file.', 'wpml-translation-management'), $name));
             }
             $original = (string) $file_attributes['original'];
             list($job_id, $md5) = explode('-', $original);
             $job = $iclTranslationManagement->get_translation_job((int) $job_id, false, false, 1);
             // don't include not-translatable and don't auto-assign
             $current_job_id = $iclTranslationManagement->get_translation_job_id($job->trid, $job->language_code);
             if (!$job || $current_job_id != $job_id || $md5 != md5($job_id . $job->original_doc_id)) {
                 return new WP_Error('xliff_doesnt_match', __('The uploaded xliff file doesn\'t belong to this system.', 'wpml-translation-management'));
             }
             if ($current_user->ID != $job->translator_id) {
                 return new WP_Error('not_your_job', sprintf(__('The translation job (%s) doesn\'t belong to you.', 'wpml-translation-management'), $job_id));
             }
             $data = array('job_id' => $job_id, 'fields' => array(), 'complete' => 1);
             foreach ($xml->file->body->children() as $node) {
                 $attr = $node->attributes();
                 $type = (string) $attr['id'];
                 $target = $this->get_xliff_node_target($node);
                 if (!$target) {
                     return new WP_Error('xliff_invalid', __('The uploaded xliff file does not seem to be properly formed.', 'wpml-translation-management'));
                 }
                 foreach ($job->elements as $element) {
                     if ($element->field_type == $type) {
                         $target = str_replace('<br class="xliff-newline" />', "\n", $target);
                         if ($element->field_format == 'csv_base64') {
                             $target = explode(',', $target);
                         }
                         $field = array();
                         $field['data'] = $target;
                         $field['finished'] = 1;
                         $field['tid'] = $element->tid;
                         $field['field_type'] = $element->field_type;
                         $field['format'] = $element->field_format;
                         $data['fields'][] = $field;
                         break;
                     }
                 }
             }
             wpml_tm_save_data($data);
             $this->success[] = sprintf(__('Translation of job %s has been uploaded and completed.', 'wpml-translation-management'), $job_id);
         }
         if (sizeof($this->success) > 0) {
             add_action('admin_notices', array($this, '_success'));
             return true;
         }
     }
     return false;
 }
コード例 #2
0
 /**
  * @param array $file
  *
  * @return bool|WP_Error
  */
 private function import_xliff($file)
 {
     global $current_user;
     // We don't want any redirects happening when we save the translation
     add_filter('wp_redirect', array($this, '_stop_redirect'));
     $this->success = array();
     $contents = array();
     if (isset($file['tmp_name']) && $file['tmp_name']) {
         $fh = fopen($file['tmp_name'], 'r');
         $data = fread($fh, 4);
         fclose($fh);
         if ($data[0] == 'P' && $data[1] == 'K' && $data[2] == chr(03) && $data[3] == chr(04)) {
             if (class_exists('ZipArchive')) {
                 $z = new ZipArchive();
                 $zopen = $z->open($file['tmp_name'], 4);
                 if (true !== $zopen) {
                     return new WP_Error('incompatible_archive', __('Incompatible Archive.'));
                 }
                 for ($i = 0; $i < $z->numFiles; $i++) {
                     if (!($info = $z->statIndex($i))) {
                         return new WP_Error('stat_failed', __('Could not retrieve file from archive.'));
                     }
                     $content = $z->getFromIndex($i);
                     if (false === $content) {
                         return new WP_Error('extract_failed', __('Could not extract file from archive.'), $info['name']);
                     }
                     $contents[$info['name']] = $content;
                 }
             } else {
                 require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';
                 $archive = new PclZip($file['tmp_name']);
                 // Is the archive valid?
                 if (false == ($archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING))) {
                     return new WP_Error('incompatible_archive', __('Incompatible Archive.'), $archive->errorInfo(true));
                 }
                 if (0 == count($archive_files)) {
                     return new WP_Error('empty_archive', __('Empty archive.'));
                 }
                 foreach ($archive_files as $content) {
                     $contents[$content['filename']] = $content['content'];
                 }
             }
         } else {
             $fh = fopen($file['tmp_name'], 'r');
             $data = fread($fh, $file['size']);
             fclose($fh);
             $contents[$file['name']] = $data;
         }
         foreach ($contents as $name => $content) {
             if ($this->validate_file_name($name)) {
                 list($job, $job_data) = $this->validate_file($name, $content, $current_user);
                 if (null !== $this->error) {
                     return $job_data;
                 }
                 wpml_tm_save_data($job_data);
                 $this->success[] = sprintf(__('Translation of job %s has been uploaded and completed.', 'wpml-translation-management'), $job->job_id);
             }
         }
         if (count($this->success)) {
             add_action('admin_notices', array($this, '_success'));
             return true;
         }
     }
     return false;
 }
コード例 #3
0
 /**
  * @param int $translation_id
  * @param int $translation_proxy_job_id
  *
  * @return bool
  */
 function add_translated_document($translation_id, $translation_proxy_job_id)
 {
     global $wpdb, $sitepress;
     $project = TranslationProxy::get_current_project();
     $translation_info = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}icl_translations WHERE translation_id=%d", $translation_id));
     $translation = $project->fetch_translation($translation_proxy_job_id);
     if (!$translation) {
         $this->errors = array_merge($this->errors, $project->errors);
     } else {
         $translation = apply_filters('icl_data_from_pro_translation', $translation);
     }
     $ret = true;
     if (!empty($translation) && strpos($translation, 'xliff') !== false) {
         try {
             /** @var $job_xliff_translation WP_Error|array */
             $job_xliff_translation = $this->xliff_reader_factory->general_xliff_import()->import($translation, $translation_id);
             if (is_wp_error($job_xliff_translation)) {
                 $this->add_error($job_xliff_translation->get_error_message());
                 return false;
             }
             wpml_tm_save_data($job_xliff_translation);
             $translations = $sitepress->get_element_translations($translation_info->trid, $translation_info->element_type, false, true, true);
             if (isset($translations[$translation_info->language_code])) {
                 $translation = $translations[$translation_info->language_code];
                 if (isset($translation->element_id) && $translation->element_id) {
                     $translation_post_type_prepared = $wpdb->prepare("SELECT post_type FROM {$wpdb->posts} WHERE ID=%d", array($translation->element_id));
                     $translation_post_type = $wpdb->get_var($translation_post_type_prepared);
                 } else {
                     $translation_post_type = implode('_', array_slice(explode('_', $translation_info->element_type), 1));
                 }
                 if ($translation_post_type == 'page') {
                     $url = get_option('home') . '?page_id=' . $translation->element_id;
                 } else {
                     $url = get_option('home') . '?p=' . $translation->element_id;
                 }
                 $project->update_job($translation_proxy_job_id, $url);
             } else {
                 $project->update_job($translation_proxy_job_id);
             }
         } catch (Exception $e) {
             $ret = false;
         }
     }
     return $ret;
 }
コード例 #4
0
 /**
  * @param array $file
  *
  * @return bool|WP_Error
  */
 private function import_xliff($file)
 {
     global $current_user;
     get_currentuserinfo();
     // We don't want any redirects happening when we save the translation
     add_filter('wp_redirect', array($this, '_stop_redirect'));
     $this->success = array();
     $contents = array();
     if (isset($file['tmp_name']) && $file['tmp_name']) {
         $fh = fopen($file['tmp_name'], 'r');
         $data = fread($fh, 4);
         fclose($fh);
         if ($data[0] == 'P' && $data[1] == 'K' && $data[2] == chr(03) && $data[3] == chr(04)) {
             if (class_exists('ZipArchive')) {
                 $z = new ZipArchive();
                 $zopen = $z->open($file['tmp_name'], 4);
                 if (true !== $zopen) {
                     return new WP_Error('incompatible_archive', __('Incompatible Archive.'));
                 }
                 for ($i = 0; $i < $z->numFiles; $i++) {
                     if (!($info = $z->statIndex($i))) {
                         return new WP_Error('stat_failed', __('Could not retrieve file from archive.'));
                     }
                     $content = $z->getFromIndex($i);
                     if (false === $content) {
                         return new WP_Error('extract_failed', __('Could not extract file from archive.'), $info['name']);
                     }
                     $contents[$info['name']] = $content;
                 }
             } else {
                 require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';
                 $archive = new PclZip($file['tmp_name']);
                 // Is the archive valid?
                 if (false == ($archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING))) {
                     return new WP_Error('incompatible_archive', __('Incompatible Archive.'), $archive->errorInfo(true));
                 }
                 if (0 == count($archive_files)) {
                     return new WP_Error('empty_archive', __('Empty archive.'));
                 }
                 foreach ($archive_files as $content) {
                     $contents[$content['filename']] = $content['content'];
                 }
             }
         } else {
             $fh = fopen($file['tmp_name'], 'r');
             $data = fread($fh, $file['size']);
             fclose($fh);
             $contents[$file['name']] = $data;
         }
         foreach ($contents as $name => $content) {
             $new_error_handler = create_function('$errno, $errstr, $errfile, $errline', 'throw new ErrorException( $errstr, $errno, 1, $errfile, $errline );');
             set_error_handler($new_error_handler);
             try {
                 $xml = simplexml_load_string($content);
             } catch (Exception $e) {
                 $xml = false;
             }
             restore_error_handler();
             if (!$xml || !isset($xml->file)) {
                 return new WP_Error('not_xml_file', sprintf(__('"%s" is not a valid XLIFF file.', 'wpml-translation-management'), $name));
             }
             $job = $this->get_job_for_xliff($xml);
             if (is_wp_error($job)) {
                 return $job;
             }
             if ($current_user->ID != $job->translator_id) {
                 return new WP_Error('not_your_job', sprintf(__('The translation job (%s) doesn\'t belong to you.', 'wpml-translation-management'), $job->job_id));
             }
             wpml_tm_save_data($this->generate_job_data($xml, $job));
             $this->success[] = sprintf(__('Translation of job %s has been uploaded and completed.', 'wpml-translation-management'), $job->job_id);
         }
         if (sizeof($this->success) > 0) {
             add_action('admin_notices', array($this, '_success'));
             return true;
         }
     }
     return false;
 }