Beispiel #1
0
 private function init_job()
 {
     $this->job = false;
     $this->translator_has_job_language_pairs = false;
     $this->user_can_take_this_job = false;
     $this->job_id = WPML_Translation_Editor::get_job_id_from_request();
     if ($this->job_id) {
         WPML_Translation_Job_Terms::set_translated_term_values($this->job_id);
         $this->job = $this->sitepressTM->get_translation_job($this->job_id, false, true, 1);
         if ($this->job) {
             $this->user_can_take_this_job = $this->job->translator_id == 0 || $this->job->translator_id == $this->current_user->ID;
             $this->job_language_pairs = array('lang_from' => $this->job->source_language_code, 'lang_to' => $this->job->language_code);
             $this->translator_has_job_language_pairs = $this->sitepressTM->is_translator($this->current_user->ID, $this->job_language_pairs);
         }
     }
     $this->is_valid_job = !$this->job || (!$this->user_can_take_this_job || !$this->translator_has_job_language_pairs) && !current_user_can('manage_options');
 }
 function save_post_translation($translation_id, $translation)
 {
     global $wpdb, $sitepress_settings, $sitepress, $icl_adjust_id_url_filter_off;
     $icl_adjust_id_url_filter_off = true;
     $translation_info = $wpdb->get_row($wpdb->prepare("\n                SELECT * FROM {$wpdb->prefix}icl_translations tr\n                    JOIN {$wpdb->prefix}icl_translation_status ts ON ts.translation_id = tr.translation_id\n                WHERE tr.translation_id=%d", $translation_id));
     $lang_code = $translation_info->language_code;
     $trid = $translation_info->trid;
     $original_post_details = $wpdb->get_row("\n            SELECT p.post_author, p.post_type, p.post_status, p.comment_status, p.ping_status, p.post_parent, p.menu_order, p.post_date, t.language_code\n            FROM {$wpdb->prefix}icl_translations t \n            JOIN {$wpdb->posts} p ON t.element_id = p.ID AND CONCAT('post_',p.post_type) = t.element_type\n            WHERE trid='{$trid}' AND p.ID = '{$translation['original_id']}'\n        ");
     //is the original post a sticky post?
     $sticky_posts = get_option('sticky_posts');
     $is_original_sticky = $original_post_details->post_type == 'post' && in_array($translation['original_id'], $sticky_posts);
     $this->_content_fix_image_paths_in_body($translation);
     $this->_content_fix_relative_link_paths_in_body($translation);
     $this->_content_decode_shortcodes($translation);
     // handle the page parent and set it to the translated parent if we have one.
     if ($original_post_details->post_parent) {
         $post_parent_trid = $wpdb->get_var($wpdb->prepare("\tSELECT trid\n\t\t\t\t\t\tFROM {$wpdb->prefix}icl_translations\n\t\t\t\t\t\tWHERE element_type= %s AND element_id = %d ", 'post_' . $original_post_details->post_type, $original_post_details->post_parent));
         if ($post_parent_trid) {
             $parent_id = $wpdb->get_var($wpdb->prepare("SELECT element_id\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t FROM {$wpdb->prefix}icl_translations\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t WHERE element_type = %s\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t  AND trid = %d\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t  AND language_code = %s ", 'post_' . $original_post_details->post_type, $post_parent_trid, $lang_code));
         }
     }
     // determine post id based on trid
     $post_id = $translation_info->element_id;
     if ($post_id) {
         // see if the post really exists - make sure it wasn't deleted while the plugin was
         if (!$wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE ID = %d ", $post_id))) {
             $is_update = false;
             $q = "DELETE FROM {$wpdb->prefix}icl_translations WHERE element_type=%s AND element_id=%d";
             $q_prepared = $wpdb->prepare($q, array('post_' . $original_post_details->post_type, $post_id));
             $wpdb->query($q_prepared);
         } else {
             $is_update = true;
             $postarr['ID'] = $_POST['post_ID'] = $post_id;
         }
     } else {
         $is_update = false;
     }
     $postarr['post_title'] = $translation['title'];
     if ($sitepress_settings['translated_document_page_url'] == 'translate' && isset($translation['URL'])) {
         $postarr['post_name'] = $translation['URL'];
     }
     $postarr['post_content'] = $translation['body'];
     if (isset($translation['excerpt']) && $translation['excerpt'] != "") {
         $postarr['post_excerpt'] = $translation['excerpt'];
     }
     if (isset($translated_taxonomies) && is_array($translated_taxonomies)) {
         foreach ($translated_taxonomies as $taxonomy => $values) {
             $postarr['tax_input'][$taxonomy] = join(',', (array) $values);
         }
     }
     $postarr['post_author'] = $original_post_details->post_author;
     $postarr['post_type'] = $original_post_details->post_type;
     if ($sitepress_settings['sync_comment_status']) {
         $postarr['comment_status'] = $original_post_details->comment_status;
     }
     if ($sitepress_settings['sync_ping_status']) {
         $postarr['ping_status'] = $original_post_details->ping_status;
     }
     if ($sitepress_settings['sync_page_ordering']) {
         $postarr['menu_order'] = $original_post_details->menu_order;
     }
     if ($sitepress_settings['sync_private_flag'] && $original_post_details->post_status == 'private') {
         $postarr['post_status'] = 'private';
     }
     if (!$is_update) {
         $postarr['post_status'] = !$sitepress_settings['translated_document_status'] ? 'draft' : $original_post_details->post_status;
     } else {
         // set post_status to the current post status.
         $postarr['post_status'] = $wpdb->get_var($wpdb->prepare("SELECT post_status\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t FROM {$wpdb->prefix}posts\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t WHERE ID = %d ", $post_id));
     }
     if ($sitepress_settings['sync_post_date']) {
         $postarr['post_date'] = $original_post_details->post_date;
     }
     if (isset($parent_id) && $sitepress_settings['sync_page_parent']) {
         $_POST['post_parent'] = $postarr['post_parent'] = $parent_id;
         $_POST['parent_id'] = $postarr['parent_id'] = $parent_id;
     }
     if ($is_update) {
         $postarr['post_name'] = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM {$wpdb->posts} WHERE ID=%d", $post_id));
     }
     $_POST['trid'] = $trid;
     $_POST['lang'] = $lang_code;
     $_POST['skip_sitepress_actions'] = true;
     global $wp_rewrite;
     if (!isset($wp_rewrite)) {
         $wp_rewrite = new WP_Rewrite();
     }
     kses_remove_filters();
     $postarr = apply_filters('icl_pre_save_pro_translation', $postarr);
     $new_post_id = wp_insert_post($postarr);
     do_action('icl_pro_translation_saved', $new_post_id);
     // set stickiness
     if ($is_original_sticky && $sitepress_settings['sync_sticky_flag']) {
         stick_post($new_post_id);
     } else {
         if ($original_post_details->post_type == 'post' && $is_update) {
             unstick_post($new_post_id);
             //just in case - if this is an update and the original post stckiness has changed since the post was sent to translation
         }
     }
     foreach ((array) $sitepress_settings['translation-management']['custom_fields_translation'] as $cf => $op) {
         if ($op == 1) {
             $sitepress->_sync_custom_field($translation['original_id'], $new_post_id, $cf);
         } elseif ($op == 2 && isset($translation['field-' . $cf])) {
             $field_translation = $translation['field-' . $cf];
             $field_type = $translation['field-' . $cf . '-type'];
             if ($field_type == 'custom_field') {
                 $field_translation = str_replace('&#0A;', "\n", $field_translation);
                 // always decode html entities  eg decode & to &
                 $field_translation = html_entity_decode($field_translation);
                 update_post_meta($new_post_id, $cf, $field_translation);
             }
         }
     }
     // set specific custom fields
     $copied_custom_fields = array('_top_nav_excluded', '_cms_nav_minihome');
     foreach ($copied_custom_fields as $ccf) {
         $val = get_post_meta($translation['original_id'], $ccf, true);
         update_post_meta($new_post_id, $ccf, $val);
     }
     // sync _wp_page_template
     if ($sitepress_settings['sync_page_template']) {
         $_wp_page_template = get_post_meta($translation['original_id'], '_wp_page_template', true);
         update_post_meta($new_post_id, '_wp_page_template', $_wp_page_template);
     }
     // sync post format
     if ($sitepress_settings['sync_post_format']) {
         $_wp_post_format = get_post_format($translation['original_id']);
         set_post_format($new_post_id, $_wp_post_format);
     }
     if (!$new_post_id) {
         return false;
     }
     if (!$is_update) {
         $wpdb->update($wpdb->prefix . 'icl_translations', array('element_id' => $new_post_id), array('translation_id' => $translation_id));
     }
     update_post_meta($new_post_id, '_icl_translation', 1);
     TranslationManagement::set_page_url($new_post_id);
     global $iclTranslationManagement;
     $ts = array('status' => ICL_TM_COMPLETE, 'needs_update' => 0, 'translation_id' => $translation_id);
     $translator_id = $wpdb->get_var($wpdb->prepare("SELECT translator_id FROM {$wpdb->prefix}icl_translation_status WHERE translation_id=%d", $translation_id));
     if (!$translator_id) {
         $lang_status = TranslationProxy_Translator::get_language_pairs();
         foreach ($lang_status as $languages_pair) {
             if ($languages_pair['from'] == $original_post_details->language_code && $languages_pair['to'] == $lang_code && isset($languages_pair['translators'][0]['id'])) {
                 $ts['translator_id'] = $languages_pair['translators'][0]['id'];
                 break;
             }
         }
     }
     // update translation status
     $iclTranslationManagement->update_translation_status($ts);
     // add new translation job
     //$translation_package = $iclTranslationManagement->create_translation_package(get_post($translation['original_id']));
     //$job_id = $iclTranslationManagement->add_translation_job($translation_info->rid, $translation_info->translator_id, $translation_package);
     $job_id = $iclTranslationManagement->get_translation_job_id($trid, $lang_code);
     // save the translation
     $iclTranslationManagement->mark_job_done($job_id);
     $parts = explode('_', $translation['original_id']);
     if ($parts[0] != 'external') {
         $iclTranslationManagement->save_job_fields_from_post($job_id, get_post($new_post_id));
         $this->_content_fix_links_to_translated_content($new_post_id, $lang_code, "post_{$original_post_details->post_type}");
         // Now try to fix links in other translated content that may link to this post.
         $sql = "SELECT\n                        tr.element_id\n                    FROM\n                        {$wpdb->prefix}icl_translations tr\n                    JOIN\n                        {$wpdb->prefix}icl_translation_status ts\n                    ON\n                        tr.translation_id = ts.translation_id\n                    WHERE\n                        ts.links_fixed = 0 AND tr.element_type = %s AND tr.language_code = %s AND tr.element_id IS NOT NULL";
         $sql_prepared = $wpdb->prepare($sql, array('post_' . $original_post_details->post_type, $lang_code));
         $needs_fixing = $wpdb->get_results($sql_prepared);
         foreach ($needs_fixing as $id) {
             if ($id->element_id != $new_post_id) {
                 // fix all except the new_post_id. We have already done this.
                 $this->_content_fix_links_to_translated_content($id->element_id, $lang_code, "post_{$original_post_details->post_type}");
             }
         }
         // if this is a parent page then make sure it's children point to this.
         $this->fix_translated_children($translation['original_id'], $new_post_id, $lang_code);
     }
     WPML_Translation_Job_Terms::save_terms_from_job($job_id, $lang_code);
     do_action('icl_pro_translation_completed', $new_post_id);
     return true;
 }
 function save_job_fields_from_post($job_id, $post)
 {
     global $wpdb;
     $data['complete'] = 1;
     $data['job_id'] = $job_id;
     $job = $this->get_translation_job($job_id, 1);
     if (class_exists('WPML_Translation_Job_Terms')) {
         $term_names = WPML_Translation_Job_Terms::get_term_field_array_for_post($post->ID);
     } else {
         $term_names = array();
     }
     if (is_object($job) && is_array($job->elements)) {
         foreach ($job->elements as $element) {
             $field_data = '';
             switch ($element->field_type) {
                 case 'title':
                     $field_data = $this->encode_field_data($post->post_title, $element->field_format);
                     break;
                 case 'body':
                     $field_data = $this->encode_field_data($post->post_content, $element->field_format);
                     break;
                 case 'excerpt':
                     $field_data = $this->encode_field_data($post->post_excerpt, $element->field_format);
                     break;
                 case 'URL':
                     $field_data = $this->encode_field_data($post->post_name, $element->field_format);
                     break;
                 default:
                     if (false !== strpos($element->field_type, 'field-') && !empty($this->settings['custom_fields_translation'])) {
                         $cf_name = preg_replace('#^field-#', '', $element->field_type);
                         if (isset($this->settings['custom_fields_translation'][$cf_name])) {
                             if ($this->settings['custom_fields_translation'][$cf_name] == 1) {
                                 //copy
                                 //TODO: [WPML 3.3] Check when this code is run, it seems obsolete
                                 $field_data = get_post_meta($job->original_doc_id, $cf_name, 1);
                                 if (is_scalar($field_data)) {
                                     $field_data = $this->encode_field_data($field_data, $element->field_format);
                                 } else {
                                     $field_data = '';
                                 }
                             } elseif ($this->settings['custom_fields_translation'][$cf_name] == 2) {
                                 // translate
                                 $field_data = get_post_meta($post->ID, $cf_name, 1);
                                 if (is_scalar($field_data)) {
                                     $field_data = $this->encode_field_data($field_data, $element->field_format);
                                 } else {
                                     $field_data = '';
                                 }
                             }
                         }
                     } else {
                         if (isset($term_names[$element->field_type])) {
                             $field_data = $this->encode_field_data($term_names[$element->field_type], $element->field_format);
                         }
                     }
             }
             $wpdb->update($wpdb->prefix . 'icl_translate', array('field_data_translated' => $field_data, 'field_finished' => 1), array('tid' => $element->tid));
         }
         $this->mark_job_done($job_id);
     }
 }