コード例 #1
0
 function save_translation($data)
 {
     global $wpdb, $sitepress, $sitepress_settings, $ICL_Pro_Translation;
     $new_post_id = false;
     $is_incomplete = false;
     foreach ($data['fields'] as $field) {
         $this->_save_translation_field($field['tid'], $field);
         if (!isset($field['finished']) || !$field['finished']) {
             $is_incomplete = true;
         }
     }
     //check if translation job still exists
     $job_id = $wpdb->get_var($wpdb->prepare("SELECT count(1) FROM {$wpdb->prefix}icl_translate_job WHERE job_id=%d", $data['job_id']));
     if ($job_id == 0) {
         wp_redirect(admin_url(sprintf('admin.php?page=%s', WPML_TM_FOLDER . '/menu/translations-queue.php', 'job-cancelled')));
         exit;
     }
     if (!empty($data['complete']) && !$is_incomplete) {
         $wpdb->update($wpdb->prefix . 'icl_translate_job', array('translated' => 1), array('job_id' => $data['job_id']));
         $rid = $wpdb->get_var($wpdb->prepare("SELECT rid FROM {$wpdb->prefix}icl_translate_job WHERE job_id=%d", $data['job_id']));
         $translation_id = $wpdb->get_var($wpdb->prepare("SELECT translation_id FROM {$wpdb->prefix}icl_translation_status WHERE rid=%d", $rid));
         $wpdb->update($wpdb->prefix . 'icl_translation_status', array('status' => ICL_TM_COMPLETE, 'needs_update' => 0), array('rid' => $rid));
         list($element_id, $trid) = $wpdb->get_row($wpdb->prepare("SELECT element_id, trid FROM {$wpdb->prefix}icl_translations WHERE translation_id=%d", $translation_id), ARRAY_N);
         $job = $this->get_translation_job($data['job_id'], true);
         $parts = explode('_', $job->original_doc_id);
         if ($parts[0] == 'external') {
             // Translations are saved in the string table for 'external' types
             $id = array_pop($parts);
             unset($parts[0]);
             $type = implode('_', $parts);
             foreach ($job->elements as $field) {
                 if ($field->field_translate) {
                     if (function_exists('icl_st_is_registered_string')) {
                         $string_id = icl_st_is_registered_string($type, $id . '_' . $field->field_type);
                         if (!$string_id) {
                             icl_register_string($type, $id . '_' . $field->field_type, self::decode_field_data($field->field_data, $field->field_format));
                             $string_id = icl_st_is_registered_string($type, $id . '_' . $field->field_type);
                         }
                         if ($string_id) {
                             icl_add_string_translation($string_id, $job->language_code, self::decode_field_data($field->field_data_translated, $field->field_format), ICL_STRING_TRANSLATION_COMPLETE);
                         }
                     }
                 }
             }
         } else {
             if (!is_null($element_id)) {
                 $postarr['ID'] = $_POST['post_ID'] = $element_id;
             }
             foreach ($job->elements as $field) {
                 switch ($field->field_type) {
                     case 'title':
                         $postarr['post_title'] = self::decode_field_data($field->field_data_translated, $field->field_format);
                         break;
                     case 'body':
                         $postarr['post_content'] = self::decode_field_data($field->field_data_translated, $field->field_format);
                         break;
                     case 'excerpt':
                         $postarr['post_excerpt'] = self::decode_field_data($field->field_data_translated, $field->field_format);
                         break;
                     case 'URL':
                         $postarr['post_name'] = self::decode_field_data($field->field_data_translated, $field->field_format);
                         break;
                     case 'tags':
                         $tags = self::decode_field_data($field->field_data_translated, $field->field_format);
                         $original_tags = self::decode_field_data($field->field_data, $field->field_format);
                         // create tags that don't exist
                         foreach ($tags as $k => $t) {
                             $thetag = $sitepress->get_term_by_name_and_lang($t, 'post_tag', $job->language_code);
                             $tags[$k] = $t;
                             // Save $t as we may have added @.lang to it
                             if (empty($thetag)) {
                                 $the_original_tag = $sitepress->get_term_by_name_and_lang($original_tags[$k], 'post_tag', $job->source_language_code);
                                 $tmp = self::icl_insert_term($t, 'post_tag', array(), $job->language_code);
                                 if (!is_wp_error($tmp) && isset($tmp['term_taxonomy_id'])) {
                                     $sitepress->set_term_translation($the_original_tag, $tmp['term_taxonomy_id'], 'post_tag', $job->language_code, $job->source_language_code);
                                 }
                             }
                         }
                         $postarr['tags_input'] = join(',', $tags);
                         $postarr['tax_input']['post_tag'] = $tags;
                         break;
                     case 'categories':
                         $cats = self::decode_field_data($field->field_data_translated, $field->field_format);
                         $original_cats = self::decode_field_data($field->field_data, $field->field_format);
                         $missing_parents = array();
                         $cat_ids = array();
                         foreach ($cats as $k => $c) {
                             $parent_missing = false;
                             $thecat = $sitepress->get_term_by_name_and_lang($c, 'category', $job->language_code);
                             $cat_id = 0;
                             if (empty($thecat)) {
                                 $the_original_cat = $sitepress->get_term_by_name_and_lang($original_cats[$k], 'category', $job->source_language_code);
                                 if ($the_original_cat) {
                                     $original_parent_id = $wpdb->get_var("SELECT parent FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id=" . $the_original_cat->term_taxonomy_id);
                                 } else {
                                     $original_parent_id = false;
                                 }
                                 if ($original_parent_id) {
                                     $translated_parent_id = icl_object_id($original_parent_id, 'category', false, $job->language_code);
                                     if (!$translated_parent_id) {
                                         // The parent is missing. Possibly because we haven't created a translation of it yet
                                         $parent_missing = true;
                                     }
                                 } else {
                                     $translated_parent_id = 0;
                                 }
                                 $tmp = self::icl_insert_term($c, 'category', array('parent' => $translated_parent_id), $job->language_code);
                                 if (!is_wp_error($tmp) && isset($tmp['term_taxonomy_id'])) {
                                     $sitepress->set_term_translation($the_original_cat, $tmp['term_taxonomy_id'], 'category', $job->language_code, $job->source_language_code);
                                     $cat_id = $tmp['term_id'];
                                 }
                             } else {
                                 $cat_id = $thecat->term_id;
                             }
                             if ($cat_id) {
                                 $cat_ids[] = $cat_id;
                             }
                             if ($parent_missing && isset($original_parent_id)) {
                                 $missing_parents[$cat_id] = $original_parent_id;
                             }
                         }
                         // Double check missing parents as they might be available now.
                         foreach ($missing_parents as $cat_id => $original_parent_id) {
                             $translated_parent_id = icl_object_id($original_parent_id, 'category', false, $job->language_code);
                             $cat_trid = $sitepress->get_element_trid($cat_id, 'tax_category');
                             $buf_post = isset($_POST) ? $_POST : array();
                             $_POST['icl_trid'] = $cat_trid;
                             $_POST['icl_translation_of'] = $wpdb->get_var($wpdb->prepare("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE element_type='tax_category' AND trid=%d AND source_language_code IS NULL", $cat_trid));
                             $_POST['icl_tax_category_language'] = $job->language_code;
                             wp_update_term($cat_id, 'category', array('parent' => $translated_parent_id));
                             $_POST = $buf_post;
                             $cat_tax_id = $wpdb->get_var($wpdb->prepare("SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id=%d AND taxonomy='category'", $cat_id));
                             $wpdb->update($wpdb->prefix . 'icl_translations', array('language_code' => $job->language_code, 'trid' => $cat_trid, 'source_language_code' => $job->source_language_code), array('element_type' => 'tax_category', 'element_id' => $cat_tax_id));
                             $sitepress->update_terms_relationship_cache(array($translated_parent_id, $cat_id), 'category');
                         }
                         $postarr['post_category'] = $cat_ids;
                         if ($cat_ids) {
                             $sitepress->update_terms_relationship_cache($missing_parents, 'category');
                             $sitepress->update_terms_relationship_cache($cat_ids, 'category');
                         }
                         break;
                     default:
                         if (in_array($field->field_type, $sitepress->get_translatable_taxonomies(false, $job->original_post_type))) {
                             $taxs = self::decode_field_data($field->field_data_translated, $field->field_format);
                             $missing_parents = array();
                             $original_taxs = self::decode_field_data($field->field_data, $field->field_format);
                             $taxonomy = $field->field_type;
                             $taxonomy_obj = get_taxonomy($taxonomy);
                             // array = hierarchical, string = non-hierarchical.
                             if ($taxonomy_obj->hierarchical) {
                                 $missing_parents = array();
                             }
                             $alltaxs = $tax_ids = array();
                             foreach ($taxs as $k => $c) {
                                 $the_original_tax_parent = false;
                                 $parent_missing = false;
                                 if ($taxonomy_obj->hierarchical) {
                                     $parent_missing = false;
                                 }
                                 $thetax = $sitepress->get_term_by_name_and_lang($c, $taxonomy, $job->language_code);
                                 $taxs[$k] = $c;
                                 // Save $c as we may have added @.lang to it
                                 if (empty($thetax)) {
                                     $the_original_tax = $sitepress->get_term_by_name_and_lang($original_taxs[$k], $taxonomy, $job->source_language_code);
                                     if ($taxonomy_obj->hierarchical && $the_original_tax) {
                                         $the_original_tax_parent = $wpdb->get_var("SELECT parent FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id=" . $the_original_tax->term_taxonomy_id);
                                     }
                                     if ($the_original_tax_parent) {
                                         $translated_parent_id = icl_object_id($the_original_tax_parent, $taxonomy, false, $job->language_code);
                                         if (!$translated_parent_id) {
                                             // The parent is missing. Possibly because we haven't created a translation of it yet
                                             $parent_missing = true;
                                         }
                                     } else {
                                         $translated_parent_id = 0;
                                     }
                                     $tmp = self::icl_insert_term($c, $taxonomy, array('parent' => $translated_parent_id), $job->language_code);
                                     if (isset($tmp['term_taxonomy_id'])) {
                                         $sitepress->set_term_translation($the_original_tax, $tmp['term_taxonomy_id'], $taxonomy, $job->language_code, $job->source_language_code);
                                     }
                                     $tax_id = $tmp['term_id'];
                                 } else {
                                     $tax_id = $thetax->term_id;
                                 }
                                 if ($taxonomy_obj->hierarchical && $parent_missing) {
                                     $missing_parents[$tax_id] = $the_original_tax_parent;
                                 }
                                 $tax_ids[] = $tax_id;
                                 $alltaxs[] = $c;
                             }
                             // Double check missing parents as they might be available now.
                             foreach ($missing_parents as $tax_id => $the_original_tax_parent) {
                                 $translated_parent_id = icl_object_id($the_original_tax_parent, $taxonomy, false, $job->language_code);
                                 $tax_trid = $sitepress->get_element_trid($tax_id, 'tax_' . $taxonomy);
                                 $buf_post = isset($_POST) ? $_POST : array();
                                 $_POST['icl_trid'] = $tax_trid;
                                 $_POST['icl_translation_of'] = $wpdb->get_var($wpdb->prepare("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE element_type=%s AND trid=%d AND source_language_code IS NULL", 'tax_' . $taxonomy, $tax_trid));
                                 $_POST['icl_tax_' . $taxonomy . '_language'] = $job->language_code;
                                 wp_update_term($tax_id, $taxonomy, array('parent' => $translated_parent_id));
                                 $_POST = $buf_post;
                                 $tax_tax_id = $wpdb->get_var($wpdb->prepare("SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id=%d AND taxonomy=%s", $tax_id, $taxonomy));
                                 $wpdb->update($wpdb->prefix . 'icl_translations', array('language_code' => $job->language_code, 'trid' => $tax_trid, 'source_language_code' => $job->source_language_code), array('element_type' => 'tax_' . $taxonomy, 'element_id' => $tax_tax_id));
                                 $sitepress->update_terms_relationship_cache(array($translated_parent_id, $tax_id), $taxonomy);
                             }
                             if ($taxonomy_obj->hierarchical) {
                                 $postarr['tax_input'][$taxonomy] = $tax_ids;
                             } else {
                                 $postarr['tax_input'][$taxonomy] = $taxs;
                             }
                         }
                 }
             }
             $original_post = get_post($job->original_doc_id);
             $postarr['post_author'] = $original_post->post_author;
             $postarr['post_type'] = $original_post->post_type;
             if ($sitepress_settings['sync_comment_status']) {
                 $postarr['comment_status'] = $original_post->comment_status;
             }
             if ($sitepress_settings['sync_ping_status']) {
                 $postarr['ping_status'] = $original_post->ping_status;
             }
             if ($sitepress_settings['sync_page_ordering']) {
                 $postarr['menu_order'] = $original_post->menu_order;
             }
             if ($sitepress_settings['sync_private_flag'] && $original_post->post_status == 'private') {
                 $postarr['post_status'] = 'private';
             }
             if ($sitepress_settings['sync_post_date']) {
                 $postarr['post_date'] = $original_post->post_date;
             }
             //set as draft or the same status as original post
             $postarr['post_status'] = !$sitepress_settings['translated_document_status'] ? 'draft' : $original_post->post_status;
             if ($original_post->post_parent) {
                 $post_parent_trid = $wpdb->get_var("SELECT trid FROM {$wpdb->prefix}icl_translations\n                        WHERE element_type='post_{$original_post->post_type}' AND element_id='{$original_post->post_parent}'");
                 if ($post_parent_trid) {
                     $parent_id = $wpdb->get_var("SELECT element_id FROM {$wpdb->prefix}icl_translations\n                            WHERE element_type='post_{$original_post->post_type}' AND trid='{$post_parent_trid}' AND language_code='{$job->language_code}'");
                 }
             }
             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;
             }
             $_POST['trid'] = $trid;
             $_POST['lang'] = $job->language_code;
             $_POST['skip_sitepress_actions'] = true;
             $postarr = apply_filters('icl_pre_save_pro_translation', $postarr);
             if (isset($element_id)) {
                 // it's an update so dont change the url
                 $postarr['post_name'] = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM {$wpdb->posts} WHERE ID=%d", $element_id));
             }
             if (isset($element_id)) {
                 // it's an update so dont change post date
                 $existing_post = get_post($element_id);
                 $postarr['post_date'] = $existing_post->post_date;
                 $postarr['post_date_gmt'] = $existing_post->post_date_gmt;
             }
             $new_post_id = $this->icl_insert_post($postarr, $job->language_code);
             icl_cache_clear($postarr['post_type'] . 's_per_language');
             // clear post counter per language in cache
             // set taxonomies for users with limited caps
             if (!current_user_can('manage-categories') && !empty($postarr['tax_input'])) {
                 foreach ($postarr['tax_input'] as $taxonomy => $terms) {
                     wp_set_post_terms($new_post_id, $terms, $taxonomy, FALSE);
                     // true to append to existing tags | false to replace existing tags
                 }
             }
             do_action('icl_pro_translation_saved', $new_post_id, $data['fields']);
             // Allow identical slugs
             $post_name = sanitize_title($postarr['post_title']);
             $post_name_rewritten = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM {$wpdb->posts} WHERE ID=%d", $new_post_id));
             $post_name_base = $post_name;
             if ($post_name != $post_name_rewritten) {
                 $incr = 1;
                 do {
                     $exists = $wpdb->get_var($wpdb->prepare("\n                            SELECT p.ID FROM {$wpdb->posts} p\n                                JOIN {$wpdb->prefix}icl_translations t ON t.element_id = p.ID AND t.element_type=%s\n                            WHERE p.ID <> %d AND t.language_code = %s AND p.post_name=%s\n                        ", 'post_' . $postarr['post_type'], $new_post_id, $job->language_code, $post_name));
                     if ($exists) {
                         $incr++;
                     } else {
                         break;
                     }
                     $post_name = $post_name_base . '-' . $incr;
                 } while ($exists);
                 $wpdb->update($wpdb->posts, array('post_name' => $post_name), array('ID' => $new_post_id));
             }
             $ICL_Pro_Translation->_content_fix_links_to_translated_content($new_post_id, $job->language_code);
             // update body translation with the links fixed
             $new_post_content = $wpdb->get_var($wpdb->prepare("SELECT post_content FROM {$wpdb->posts} WHERE ID=%d", $new_post_id));
             foreach ($job->elements as $jel) {
                 if ($jel->field_type == 'body') {
                     $fields_data_translated = $this->encode_field_data($new_post_content, $jel->field_format);
                     break;
                 }
             }
             $wpdb->update($wpdb->prefix . 'icl_translate', array('field_data_translated' => $fields_data_translated), array('job_id' => $data['job_id'], 'field_type' => 'body'));
             // set stickiness
             //is the original post a sticky post?
             remove_filter('option_sticky_posts', array($sitepress, 'option_sticky_posts'));
             // remove filter used to get language relevant stickies. get them all
             $sticky_posts = get_option('sticky_posts');
             $is_original_sticky = $original_post->post_type == 'post' && in_array($original_post->ID, $sticky_posts);
             if ($is_original_sticky && $sitepress_settings['sync_sticky_flag']) {
                 stick_post($new_post_id);
             } else {
                 if ($original_post->post_type == 'post' && !is_null($element_id)) {
                     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
                 }
             }
             //sync plugins texts
             foreach ((array) $this->settings['custom_fields_translation'] as $cf => $op) {
                 if ($op == 1) {
                     update_post_meta($new_post_id, $cf, get_post_meta($original_post->ID, $cf, true));
                 }
             }
             // set specific custom fields
             $copied_custom_fields = array('_top_nav_excluded', '_cms_nav_minihome');
             foreach ($copied_custom_fields as $ccf) {
                 $val = get_post_meta($original_post->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($original_post->ID, '_wp_page_template', true);
                 if (!empty($_wp_page_template)) {
                     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($original_post->ID);
                 set_post_format($new_post_id, $_wp_post_format);
             }
             // set the translated custom fields if we have any.
             foreach ((array) $this->settings['custom_fields_translation'] as $field_name => $val) {
                 if ($val == 2) {
                     // should be translated
                     // find it in the translation
                     foreach ($job->elements as $name => $eldata) {
                         if ($eldata->field_data == $field_name) {
                             if (preg_match("/field-(.*?)-name/", $eldata->field_type, $match)) {
                                 $field_id = $match[1];
                                 foreach ($job->elements as $k => $v) {
                                     if ($v->field_type == 'field-' . $field_id) {
                                         $field_translation = self::decode_field_data($v->field_data_translated, $v->field_format);
                                     }
                                     if ($v->field_type == 'field-' . $field_id . '-type') {
                                         $field_type = $v->field_data;
                                     }
                                 }
                                 if (isset($field_type) && $field_type == 'custom_field') {
                                     $field_translation = str_replace('&#0A;', "\n", $field_translation);
                                     // always decode html entities  eg decode &amp; to &
                                     $field_translation = html_entity_decode($field_translation);
                                     update_post_meta($new_post_id, $field_name, $field_translation);
                                 }
                             }
                         }
                     }
                 }
             }
             $link = get_edit_post_link($new_post_id);
             if ($link == '') {
                 // the current user can't edit so just include permalink
                 $link = get_permalink($new_post_id);
             }
             if (is_null($element_id)) {
                 $wpdb->update($wpdb->prefix . 'icl_translations', array('element_id' => $new_post_id), array('translation_id' => $translation_id));
                 $user_message = __('Translation added: ', 'sitepress') . '<a href="' . $link . '">' . $postarr['post_title'] . '</a>.';
             } else {
                 $user_message = __('Translation updated: ', 'sitepress') . '<a href="' . $link . '">' . $postarr['post_title'] . '</a>.';
             }
             // synchronize the page parent for translations
             if ($trid && $sitepress_settings['sync_page_parent']) {
                 $translations = $sitepress->get_element_translations($trid, 'post_' . $postarr['post_type']);
                 foreach ($translations as $target_lang => $target_details) {
                     if ($target_lang != $job->language_code) {
                         if ($target_details->element_id) {
                             $sitepress->fix_translated_parent($new_post_id, $target_details->element_id, $target_lang);
                         }
                     }
                 }
             }
         }
         if (isset($user_message)) {
             $this->messages[] = array('type' => 'updated', 'text' => $user_message);
         }
         if ($this->settings['notification']['completed'] != ICL_TM_NOTIFICATION_NONE) {
             require_once ICL_PLUGIN_PATH . '/inc/translation-management/tm-notification.class.php';
             if ($data['job_id']) {
                 $tn_notification = new TM_Notification();
                 $tn_notification->work_complete($data['job_id'], !is_null($element_id));
             }
         }
         self::set_page_url($new_post_id);
         // redirect to jobs list
         wp_redirect(admin_url(sprintf('admin.php?page=%s&%s=%d', WPML_TM_FOLDER . '/menu/translations-queue.php', is_null($element_id) ? 'added' : 'updated', is_null($element_id) ? $new_post_id : $element_id)));
     } else {
         $this->messages[] = array('type' => 'updated', 'text' => __('Translation (incomplete) saved.', 'sitepress'));
     }
     do_action('icl_pro_translation_completed', $new_post_id);
 }
コード例 #2
0
 function save_translation($data)
 {
     global $wpdb, $sitepress, $sitepress_settings, $ICL_Pro_Translation;
     $new_post_id = false;
     $is_incomplete = false;
     foreach ($data['fields'] as $field) {
         $this->_save_translation_field($field['tid'], $field);
         if (!isset($field['finished']) || !$field['finished']) {
             $is_incomplete = true;
         }
     }
     //check if translation job still exists
     $job_count = $wpdb->get_var($wpdb->prepare("SELECT count(1) FROM {$wpdb->prefix}icl_translate_job WHERE job_id=%d", $data['job_id']));
     if ($job_count == 0) {
         wp_redirect(admin_url(sprintf('admin.php?page=%s', WPML_TM_FOLDER . '/menu/translations-queue.php', 'job-cancelled')));
         exit;
     }
     if (!empty($data['complete']) && !$is_incomplete) {
         $wpdb->update($wpdb->prefix . 'icl_translate_job', array('translated' => 1), array('job_id' => $data['job_id']));
         $rid = $wpdb->get_var($wpdb->prepare("SELECT rid FROM {$wpdb->prefix}icl_translate_job WHERE job_id=%d", $data['job_id']));
         $translation_id = $wpdb->get_var($wpdb->prepare("SELECT translation_id FROM {$wpdb->prefix}icl_translation_status WHERE rid=%d", $rid));
         $wpdb->update($wpdb->prefix . 'icl_translation_status', array('status' => ICL_TM_COMPLETE, 'needs_update' => 0), array('rid' => $rid));
         list($element_id, $trid) = $wpdb->get_row($wpdb->prepare("SELECT element_id, trid FROM {$wpdb->prefix}icl_translations WHERE translation_id=%d", $translation_id), ARRAY_N);
         $job = $this->get_translation_job($data['job_id'], true);
         $parts = explode('_', $job->original_doc_id);
         if ($parts[0] == 'external') {
             // Translations are saved in the string table for 'external' types
             $id = array_pop($parts);
             unset($parts[0]);
             $type = implode('_', $parts);
             $type = apply_filters('WPML_get_package_type', $type, $job->original_doc_id);
             foreach ($job->elements as $field) {
                 if ($field->field_translate) {
                     if (function_exists('icl_st_is_registered_string')) {
                         $string_id = icl_st_is_registered_string($type, $id . '_' . $field->field_type);
                         if (!$string_id) {
                             icl_register_string($type, $id . '_' . $field->field_type, self::decode_field_data($field->field_data, $field->field_format));
                             $string_id = icl_st_is_registered_string($type, $id . '_' . $field->field_type);
                         }
                         if ($string_id) {
                             icl_add_string_translation($string_id, $job->language_code, self::decode_field_data($field->field_data_translated, $field->field_format), ICL_STRING_TRANSLATION_COMPLETE);
                         }
                     }
                 }
             }
         } else {
             if (!is_null($element_id)) {
                 $postarr['ID'] = $_POST['post_ID'] = $element_id;
             }
             foreach ($job->elements as $field) {
                 switch ($field->field_type) {
                     case 'title':
                         $postarr['post_title'] = self::decode_field_data($field->field_data_translated, $field->field_format);
                         break;
                     case 'body':
                         $postarr['post_content'] = self::decode_field_data($field->field_data_translated, $field->field_format);
                         break;
                     case 'excerpt':
                         $postarr['post_excerpt'] = self::decode_field_data($field->field_data_translated, $field->field_format);
                         break;
                     case 'URL':
                         $postarr['post_name'] = self::decode_field_data($field->field_data_translated, $field->field_format);
                         break;
                     default:
                         break;
                 }
             }
             $original_post = get_post($job->original_doc_id);
             $postarr['post_author'] = $original_post->post_author;
             $postarr['post_type'] = $original_post->post_type;
             if ($sitepress_settings['sync_comment_status']) {
                 $postarr['comment_status'] = $original_post->comment_status;
             }
             if ($sitepress_settings['sync_ping_status']) {
                 $postarr['ping_status'] = $original_post->ping_status;
             }
             if ($sitepress_settings['sync_page_ordering']) {
                 $postarr['menu_order'] = $original_post->menu_order;
             }
             if ($sitepress_settings['sync_private_flag'] && $original_post->post_status == 'private') {
                 $postarr['post_status'] = 'private';
             }
             if ($sitepress_settings['sync_post_date']) {
                 $postarr['post_date'] = $original_post->post_date;
             }
             //set as draft or the same status as original post
             $postarr['post_status'] = !$sitepress_settings['translated_document_status'] ? 'draft' : $original_post->post_status;
             if ($original_post->post_parent) {
                 $post_parent_trid = $wpdb->get_var("SELECT trid FROM {$wpdb->prefix}icl_translations\n\t\t\t\t\t\tWHERE element_type='post_{$original_post->post_type}' AND element_id='{$original_post->post_parent}'");
                 if ($post_parent_trid) {
                     $parent_id = $wpdb->get_var("SELECT element_id FROM {$wpdb->prefix}icl_translations\n\t\t\t\t\t\t\tWHERE element_type='post_{$original_post->post_type}' AND trid='{$post_parent_trid}' AND language_code='{$job->language_code}'");
                 }
             }
             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;
             }
             $_POST['trid'] = $trid;
             $_POST['lang'] = $job->language_code;
             $_POST['skip_sitepress_actions'] = true;
             $postarr = apply_filters('icl_pre_save_pro_translation', $postarr);
             if (isset($element_id)) {
                 // it's an update so dont change the url
                 $postarr['post_name'] = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM {$wpdb->posts} WHERE ID=%d", $element_id));
             }
             if (isset($element_id)) {
                 // it's an update so dont change post date
                 $existing_post = get_post($element_id);
                 $postarr['post_date'] = $existing_post->post_date;
                 $postarr['post_date_gmt'] = $existing_post->post_date_gmt;
             }
             $new_post_id = $this->icl_insert_post($postarr, $job->language_code);
             icl_cache_clear($postarr['post_type'] . 's_per_language');
             // clear post counter per language in cache
             do_action('icl_pro_translation_saved', $new_post_id, $data['fields']);
             // Allow identical slugs
             $post_name = sanitize_title($postarr['post_title']);
             // for Translated documents options:Page URL = Translate
             if (isset($data['fields']['URL']['data']) && $data['fields']['URL']['data']) {
                 $post_name = $data['fields']['URL']['data'];
             }
             $post_name_rewritten = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM {$wpdb->posts} WHERE ID=%d", $new_post_id));
             $post_name_base = $post_name;
             if ($post_name != $post_name_rewritten || $postarr['post_type'] == 'post' || $postarr['post_type'] == 'page') {
                 $incr = 1;
                 do {
                     $exists = $wpdb->get_var($wpdb->prepare("\n\t\t\t\t\t\t\tSELECT p.ID FROM {$wpdb->posts} p\n\t\t\t\t\t\t\t\tJOIN {$wpdb->prefix}icl_translations t ON t.element_id = p.ID\n\t\t\t\t\t\t\tWHERE p.ID <> %d AND t.language_code = %s AND p.post_name=%s\n\t\t\t\t\t\t", $new_post_id, $job->language_code, $post_name));
                     if ($exists) {
                         $incr++;
                     } else {
                         break;
                     }
                     $post_name = $post_name_base . '-' . $incr;
                 } while ($exists);
                 $wpdb->update($wpdb->posts, array('post_name' => $post_name), array('ID' => $new_post_id));
             }
             $ICL_Pro_Translation->_content_fix_links_to_translated_content($new_post_id, $job->language_code);
             // update body translation with the links fixed
             $new_post_content = $wpdb->get_var($wpdb->prepare("SELECT post_content FROM {$wpdb->posts} WHERE ID=%d", $new_post_id));
             foreach ($job->elements as $jel) {
                 if ($jel->field_type == 'body') {
                     $fields_data_translated = $this->encode_field_data($new_post_content, $jel->field_format);
                     break;
                 }
             }
             $wpdb->update($wpdb->prefix . 'icl_translate', array('field_data_translated' => $fields_data_translated), array('job_id' => $data['job_id'], 'field_type' => 'body'));
             // set stickiness
             //is the original post a sticky post?
             remove_filter('option_sticky_posts', array($sitepress, 'option_sticky_posts'));
             // remove filter used to get language relevant stickies. get them all
             $sticky_posts = get_option('sticky_posts');
             $is_original_sticky = $original_post->post_type == 'post' && in_array($original_post->ID, $sticky_posts);
             if ($is_original_sticky && $sitepress_settings['sync_sticky_flag']) {
                 stick_post($new_post_id);
             } else {
                 if ($original_post->post_type == 'post' && !is_null($element_id)) {
                     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
                 }
             }
             //sync plugins texts
             foreach ((array) $this->settings['custom_fields_translation'] as $cf => $op) {
                 if ($op == 1) {
                     update_post_meta($new_post_id, $cf, get_post_meta($original_post->ID, $cf, true));
                 }
             }
             // set specific custom fields
             $copied_custom_fields = array('_top_nav_excluded', '_cms_nav_minihome');
             foreach ($copied_custom_fields as $ccf) {
                 $val = get_post_meta($original_post->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($original_post->ID, '_wp_page_template', true);
                 if (!empty($_wp_page_template)) {
                     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($original_post->ID);
                 set_post_format($new_post_id, $_wp_post_format);
             }
             // set the translated custom fields if we have any.
             foreach ((array) $this->settings['custom_fields_translation'] as $field_name => $val) {
                 if ($val == 2) {
                     // should be translated
                     // find it in the translation
                     foreach ($job->elements as $name => $eldata) {
                         if ($eldata->field_data == $field_name) {
                             if (preg_match("/field-(.*?)-name/", $eldata->field_type, $match)) {
                                 $field_id = $match[1];
                                 foreach ($job->elements as $k => $v) {
                                     if ($v->field_type == 'field-' . $field_id) {
                                         $field_translation = self::decode_field_data($v->field_data_translated, $v->field_format);
                                     }
                                     if ($v->field_type == 'field-' . $field_id . '-type') {
                                         $field_type = $v->field_data;
                                     }
                                 }
                                 if (isset($field_type) && $field_type == 'custom_field') {
                                     $field_translation = str_replace('&#0A;', "\n", $field_translation);
                                     // always decode html entities  eg decode &amp; to &
                                     $field_translation = html_entity_decode($field_translation);
                                     update_post_meta($new_post_id, $field_name, $field_translation);
                                 }
                             }
                         }
                     }
                 }
             }
             $link = get_edit_post_link($new_post_id);
             if ($link == '') {
                 // the current user can't edit so just include permalink
                 $link = get_permalink($new_post_id);
             }
             if (is_null($element_id)) {
                 $wpdb->update($wpdb->prefix . 'icl_translations', array('element_id' => $new_post_id), array('translation_id' => $translation_id));
                 $user_message = __('Translation added: ', 'sitepress') . '<a href="' . $link . '">' . $postarr['post_title'] . '</a>.';
             } else {
                 $user_message = __('Translation updated: ', 'sitepress') . '<a href="' . $link . '">' . $postarr['post_title'] . '</a>.';
             }
             // synchronize the page parent for translations
             if ($trid && $sitepress_settings['sync_page_parent']) {
                 $translations = $sitepress->get_element_translations($trid, 'post_' . $postarr['post_type']);
                 foreach ($translations as $target_lang => $target_details) {
                     if ($target_lang != $job->language_code) {
                         if ($target_details->element_id) {
                             $sitepress->fix_translated_parent($new_post_id, $target_details->element_id, $target_lang);
                         }
                     }
                 }
             }
         }
         if (isset($user_message)) {
             $this->messages[] = array('type' => 'updated', 'text' => $user_message);
         }
         if ($this->settings['notification']['completed'] != ICL_TM_NOTIFICATION_NONE) {
             require_once ICL_PLUGIN_PATH . '/inc/translation-management/tm-notification.class.php';
             if ($data['job_id']) {
                 $tn_notification = new TM_Notification();
                 $tn_notification->work_complete($data['job_id'], !is_null($element_id));
             }
         }
         self::set_page_url($new_post_id);
         // redirect to jobs list
         wp_redirect(admin_url(sprintf('admin.php?page=%s&%s=%d', WPML_TM_FOLDER . '/menu/translations-queue.php', is_null($element_id) ? 'added' : 'updated', is_null($element_id) ? $new_post_id : $element_id)));
     } else {
         $this->messages[] = array('type' => 'updated', 'text' => __('Translation (incomplete) saved.', 'sitepress'));
     }
     /*
      * After all previous functionality the terms form the job are assigned to the new post just created or updated.
      * $overwrite is true by default for now.
      */
     $overwrite = true;
     WPML_Terms_Translations::save_all_terms_from_job($data['job_id'], $new_post_id, $overwrite);
     do_action('icl_pro_translation_completed', $new_post_id);
 }
 function save_translation($data)
 {
     global $wpdb, $sitepress, $sitepress_settings, $ICL_Pro_Translation;
     $is_incomplete = false;
     foreach ($data['fields'] as $field) {
         $this->_save_translation_field($field['tid'], $field);
         if (!isset($field['finished']) || !$field['finished']) {
             $is_incomplete = true;
         }
     }
     if ($data['complete'] && !$is_incomplete) {
         $wpdb->update($wpdb->prefix . 'icl_translate_job', array('translated' => 1), array('job_id' => $data['job_id']));
         $rid = $wpdb->get_var($wpdb->prepare("SELECT rid FROM {$wpdb->prefix}icl_translate_job WHERE job_id=%d", $data['job_id']));
         $translation_id = $wpdb->get_var($wpdb->prepare("SELECT translation_id FROM {$wpdb->prefix}icl_translation_status WHERE rid=%d", $rid));
         $wpdb->update($wpdb->prefix . 'icl_translation_status', array('status' => ICL_TM_COMPLETE, 'needs_update' => 0), array('rid' => $rid));
         list($element_id, $trid) = $wpdb->get_row($wpdb->prepare("SELECT element_id, trid FROM {$wpdb->prefix}icl_translations WHERE translation_id=%d", $translation_id), ARRAY_N);
         $job = $this->get_translation_job($data['job_id'], true);
         if (!is_null($element_id)) {
             $postarr['ID'] = $_POST['post_ID'] = $element_id;
         }
         foreach ($job->elements as $field) {
             switch ($field->field_type) {
                 case 'title':
                     $postarr['post_title'] = $this->decode_field_data($field->field_data_translated, $field->field_format);
                     break;
                 case 'body':
                     $postarr['post_content'] = $this->decode_field_data($field->field_data_translated, $field->field_format);
                     break;
                 case 'excerpt':
                     $postarr['post_excerpt'] = $this->decode_field_data($field->field_data_translated, $field->field_format);
                     break;
                 case 'tags':
                     $tags = $this->decode_field_data($field->field_data_translated, $field->field_format);
                     $original_tags = $this->decode_field_data($field->field_data, $field->field_format);
                     // create tags that don't exist
                     foreach ($tags as $k => $t) {
                         $thetag = get_term_by('name', $t, 'post_tag');
                         if (empty($thetag)) {
                             $the_original_tag = get_term_by('name', $original_tags[$k], 'post_tag');
                             $tmp = wp_insert_term($t, 'post_tag');
                             if (isset($tmp['term_taxonomy_id'])) {
                                 $tag_trid = $sitepress->get_element_trid($the_original_tag->term_taxonomy_id, 'tax_post_tag');
                                 $wpdb->update($wpdb->prefix . 'icl_translations', array('language_code' => $job->language_code, 'trid' => $tag_trid, 'source_language_code' => $job->source_language_code), array('element_type' => 'tax_post_tag', 'element_id' => $tmp['term_taxonomy_id']));
                             }
                         }
                     }
                     $postarr['tags_input'] = join(',', $this->decode_field_data($field->field_data_translated, $field->field_format));
                     $postarr['tax_input']['post_tag'] = $this->decode_field_data($field->field_data_translated, $field->field_format);
                     break;
                 case 'categories':
                     $cats = $this->decode_field_data($field->field_data_translated, $field->field_format);
                     $original_cats = $this->decode_field_data($field->field_data, $field->field_format);
                     foreach ($cats as $k => $c) {
                         $thecat = get_term_by('name', $c, 'category');
                         if (empty($thecat)) {
                             $the_original_cat = get_term_by('name', $original_cats[$k], 'category');
                             $the_original_cat_parent = $wpdb->get_var("SELECT parent FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id=" . $the_original_cat->term_taxonomy_id);
                             if ($the_original_cat_parent) {
                                 $op_tr = icl_object_id($the_original_cat_parent, 'category', false, $job->language_code);
                             } else {
                                 $op_tr = 0;
                             }
                             $tmp = wp_insert_term($c, 'category', array('parent' => $op_tr));
                             if (isset($tmp['term_taxonomy_id'])) {
                                 $cat_trid = $sitepress->get_element_trid($the_original_cat->term_taxonomy_id, 'tax_category');
                                 $wpdb->update($wpdb->prefix . 'icl_translations', array('language_code' => $job->language_code, 'trid' => $cat_trid, 'source_language_code' => $job->source_language_code), array('element_type' => 'tax_category', 'element_id' => $tmp['term_taxonomy_id']));
                             }
                             $cat_id = $tmp['term_id'];
                         } else {
                             $cat_id = $thecat->term_id;
                         }
                         $cat_ids[] = $cat_id;
                     }
                     $postarr['post_category'] = $cat_ids;
                     break;
                 default:
                     if (in_array($field->field_type, $sitepress->get_translatable_taxonomies(false, $job->original_post_type))) {
                         $taxs = $this->decode_field_data($field->field_data_translated, $field->field_format);
                         $original_taxs = $this->decode_field_data($field->field_data, $field->field_format);
                         $taxonomy = $field->field_type;
                         $alltaxs = array();
                         foreach ($taxs as $k => $c) {
                             $thetax = get_term_by('name', $c, $taxonomy);
                             if (empty($thetax)) {
                                 $the_original_tax = get_term_by('name', $original_taxs[$k], $taxonomy);
                                 $the_original_tax_parent = $wpdb->get_var("SELECT parent FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id=" . $the_original_tax->term_taxonomy_id);
                                 if ($the_original_tax_parent) {
                                     $op_tr = icl_object_id($the_original_tax_parent, $taxonomy, false, $job->language_code);
                                 } else {
                                     $op_tr = 0;
                                 }
                                 $tmp = wp_insert_term($c, $taxonomy, array('parent' => $op_tr));
                                 if (isset($tmp['term_taxonomy_id'])) {
                                     $tax_trid = $sitepress->get_element_trid($the_original_tax->term_taxonomy_id, 'tax_' . $taxonomy);
                                     $wpdb->update($wpdb->prefix . 'icl_translations', array('language_code' => $job->language_code, 'trid' => $tax_trid, 'source_language_code' => $job->source_language_code), array('element_type' => 'tax_' . $taxonomy, 'element_id' => $tmp['term_taxonomy_id']));
                                 }
                                 $tax_id = $tmp['term_id'];
                             } else {
                                 $tax_id = $thetax->term_id;
                             }
                             $tax_ids[] = $tax_id;
                             $alltaxs[] = $c;
                         }
                         $postarr['tax_input'][$taxonomy] = join(',', $alltaxs);
                     }
             }
         }
         $original_post = get_post($job->original_doc_id);
         $postarr['post_author'] = $original_post->post_author;
         $postarr['post_type'] = $original_post->post_type;
         if ($sitepress_settings['sync_comment_status']) {
             $postarr['comment_status'] = $original_post->comment_status;
         }
         if ($sitepress_settings['sync_ping_status']) {
             $postarr['ping_status'] = $original_post->ping_status;
         }
         if ($sitepress_settings['sync_page_ordering']) {
             $postarr['menu_order'] = $original_post->menu_order;
         }
         if ($sitepress_settings['sync_private_flag'] && $original_post->post_status == 'private') {
             $postarr['post_status'] = 'private';
         }
         if (is_null($element_id)) {
             $postarr['post_status'] = !$sitepress_settings['translated_document_status'] ? 'draft' : $original_post->post_status;
         } else {
             // set post_status to the current post status.
             $postarr['post_status'] = $wpdb->get_var("SELECT post_status FROM {$wpdb->prefix}posts WHERE ID = " . $element_id);
         }
         if ($original_post->post_parent) {
             $post_parent_trid = $wpdb->get_var("SELECT trid FROM {$wpdb->prefix}icl_translations \r\n                    WHERE element_type='post_{$original_post->post_type}' AND element_id='{$original_post->post_parent}'");
             if ($post_parent_trid) {
                 $parent_id = $wpdb->get_var("SELECT element_id FROM {$wpdb->prefix}icl_translations \r\n                        WHERE element_type='post_{$original_post->post_type}' AND trid='{$post_parent_trid}' AND language_code='{$job->language_code}'");
             }
         }
         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;
         }
         $_POST['trid'] = $trid;
         $_POST['lang'] = $job->language_code;
         $_POST['skip_sitepress_actions'] = true;
         $new_post_id = wp_insert_post($postarr);
         $ICL_Pro_Translation->_content_fix_links_to_translated_content($new_post_id, $job->language_code);
         // update body translation with the links fixed
         $new_post_content = $wpdb->get_var($wpdb->prepare("SELECT post_content FROM {$wpdb->posts} WHERE ID=%d", $new_post_id));
         foreach ($job->elements as $jel) {
             if ($jel->field_type == 'body') {
                 $fields_data_translated = $this->encode_field_data($new_post_content, $jel->field_format);
                 break;
             }
         }
         $wpdb->update($wpdb->prefix . 'icl_translate', array('field_data_translated' => $fields_data_translated), array('job_id' => $data['job_id'], 'field_type' => 'body'));
         // set stickiness
         //is the original post a sticky post?
         remove_filter('option_sticky_posts', array($sitepress, 'option_sticky_posts'));
         // remove filter used to get language relevant stickies. get them all
         $sticky_posts = get_option('sticky_posts');
         $is_original_sticky = $original_post_details->post_type == 'post' && in_array($original_post->ID, $sticky_posts);
         if ($is_original_sticky && $sitepress_settings['sync_sticky_flag']) {
             stick_post($new_post_id);
         } else {
             if ($original_post->post_type == 'post' && !is_null($element_id)) {
                 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
             }
         }
         //sync plugins texts
         foreach ((array) $this->settings['custom_fields_translation'] as $cf => $op) {
             if ($op == 1) {
                 update_post_meta($new_post_id, $cf, get_post_meta($original_post->ID, $cf, true));
             }
         }
         // set specific custom fields
         $copied_custom_fields = array('_top_nav_excluded', '_cms_nav_minihome');
         foreach ($copied_custom_fields as $ccf) {
             $val = get_post_meta($original_post->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($original_post->ID, '_wp_page_template', true);
             if (!empty($_wp_page_template)) {
                 update_post_meta($new_post_id, '_wp_page_template', $_wp_page_template);
             }
         }
         // set the translated custom fields if we have any.
         foreach ((array) $this->settings['custom_fields_translation'] as $field_name => $val) {
             if ($val == 2) {
                 // should be translated
                 // find it in the translation
                 foreach ($job->elements as $name => $eldata) {
                     if ($eldata->field_data == $field_name) {
                         if (preg_match("/field-(.*?)-name/", $eldata->field_type, $match)) {
                             $field_id = $match[1];
                             foreach ($job->elements as $k => $v) {
                                 if ($v->field_type == 'field-' . $field_id) {
                                     $field_translation = $this->decode_field_data($v->field_data_translated, $v->field_format);
                                 }
                                 if ($v->field_type == 'field-' . $field_id . '-type') {
                                     $field_type = $v->field_data;
                                 }
                             }
                             if ($field_type == 'custom_field') {
                                 $field_translation = str_replace('&#0A;', "\n", $field_translation);
                                 // always decode html entities  eg decode &amp; to &
                                 $field_translation = html_entity_decode($field_translation);
                                 update_post_meta($new_post_id, $field_name, $field_translation);
                             }
                         }
                     }
                 }
             }
         }
         if (is_null($element_id)) {
             $wpdb->update($wpdb->prefix . 'icl_translations', array('element_id' => $new_post_id), array('translation_id' => $translation_id));
             $user_message = __('Translation added: ', 'sitepress') . '<a href="' . get_edit_post_link($new_post_id) . '">' . $postarr['post_title'] . '</a>.';
         } else {
             $user_message = __('Translation updated: ', 'sitepress') . '<a href="' . get_edit_post_link($new_post_id) . '">' . $postarr['post_title'] . '</a>.';
         }
         $this->messages[] = array('type' => 'updated', 'text' => $user_message);
         if ($this->settings['notification']['completed'] != ICL_TM_NOTIFICATION_NONE) {
             require_once ICL_PLUGIN_PATH . '/inc/translation-management/tm-notification.class.php';
             if ($data['job_id']) {
                 $tn_notification = new TM_Notification();
                 $tn_notification->work_complete($data['job_id'], !is_null($element_id));
             }
         }
     }
 }