private function migrate_strings()
 {
     global $wpdb, $sitepress;
     $languages = $sitepress->get_active_languages();
     $count = 0;
     $strings = $wpdb->get_results("SELECT id, context, value, name, language FROM {$wpdb->prefix}icl_strings WHERE context LIKE 'gravity_form-%'");
     foreach ($strings as $string) {
         if (!$this->endswith($string->name, '-value')) {
             $translations = $wpdb->get_results("SELECT language, status FROM {$wpdb->prefix}icl_string_translations WHERE string_id={$string->id}");
             foreach ($languages as $language) {
                 $found = false;
                 foreach ($translations as $translation) {
                     if ($translation->language == $language['code'] && $translation->status == 10) {
                         $found = true;
                         break;
                     }
                 }
                 if (!$found) {
                     $possible_translation = $this->find_translation($string->value, substr($string->context, strlen('gravity_form-')), $language['code']);
                     if ($possible_translation && $possible_translation != $string->value) {
                         $count++;
                         icl_add_string_translation($string->id, $language['code'], $possible_translation, 10);
                     }
                 }
             }
         }
     }
     return $count;
 }
 public static function translation_send_strings_local($string_ids, $target, $translator = null, $basket_name = null)
 {
     global $wpdb;
     static $site_translators;
     if (!isset($site_translators)) {
         $site_translators = TranslationManagement::get_blog_translators();
     }
     $mkey = $wpdb->prefix . 'strings_notification';
     $lkey = $wpdb->prefix . 'language_pairs';
     $slang = TranslationProxy_Basket::get_source_language();
     foreach ($string_ids as $string_id) {
         $batch_id = TranslationProxy_Batch::update_translation_batch($basket_name);
         $added = icl_add_string_translation($string_id, $target, null, ICL_TM_WAITING_FOR_TRANSLATOR, $translator, 'local', $batch_id);
         if ($added) {
             foreach ($site_translators as $key => $st) {
                 $ulangs = isset($st->{$lkey}) ? $st->{$lkey} : false;
                 if (!empty($ulangs) && !empty($ulangs[$slang][$target])) {
                     $enot = isset($st->{$mkey}) ? $st->{$mkey} : false;
                     if (empty($enot[$slang][$target])) {
                         self::translator_notification($st, $slang, $target);
                         $enot[$slang][$target] = 1;
                         $site_translators[$key]->{$mkey} = $enot;
                         update_option($wpdb->prefix . 'icl_translators_cached', $site_translators);
                     }
                 }
             }
         }
     }
     return 1;
 }
 /**
  * Ajax handler for saving label translations from the WPML Taxonomy Translations menu.
  */
 public function save_label_translations()
 {
     if (!wpml_is_action_authenticated('wpml_tt_save_labels_translation')) {
         wp_send_json_error('Wrong Nonce');
     }
     $general = isset($_POST['plural']) ? sanitize_text_field($_POST['plural']) : false;
     $singular = isset($_POST['singular']) ? sanitize_text_field($_POST['singular']) : false;
     $taxonomy = isset($_POST['taxonomy']) ? sanitize_text_field($_POST['taxonomy']) : false;
     $language = isset($_POST['taxonomy_language_code']) ? sanitize_text_field($_POST['taxonomy_language_code']) : false;
     if ($singular && $general && $taxonomy && $language) {
         $tax_label_data = $this->get_label_translations(false, $taxonomy);
         if (isset($tax_label_data['id_singular']) && $tax_label_data['id_singular'] && isset($tax_label_data['id_general']) && $tax_label_data['id_general']) {
             $original_id_singular = $tax_label_data['id_singular'];
             $original_id_plural = $tax_label_data['id_general'];
             icl_add_string_translation($original_id_singular, $language, $singular, ICL_TM_COMPLETE);
             $singular_result = (string) icl_get_string_by_id($original_id_singular, $language);
             icl_add_string_translation($original_id_plural, $language, $general, ICL_TM_COMPLETE);
             $plural_result = (string) icl_get_string_by_id($original_id_plural, $language);
             if ($singular_result && $plural_result) {
                 $result = array('singular' => $singular_result, 'general' => $plural_result, 'lang' => $language);
                 wp_send_json_success($result);
             }
         }
     }
     wp_send_json_error();
 }
 function test_get_translated_tax_slug()
 {
     $category_base = !empty($this->wc_permalinks['category_base']) ? trim($this->wc_permalinks['category_base'], '/') : 'product-category';
     $name = $this->url_translation->url_string_name('product_cat');
     do_action('wpml_register_single_string', $this->url_translation->url_strings_context(), $name, $category_base);
     $string_id = icl_get_string_id($category_base, $this->url_translation->url_strings_context(), $name);
     icl_add_string_translation($string_id, 'es', 'categoria-producto', ICL_TM_COMPLETE);
     $translated_tax = $this->url_translation->get_translated_tax_slug('product_cat', 'es');
     $this->assertTrue((bool) has_filter('wpml_translate_single_string'));
     $this->assertEquals('categoria-producto', $translated_tax['translated_slug']);
 }
 function test_translate_attributes_label_in_wp_taxonomies()
 {
     global $wp_taxonomies, $sitepress, $WPML_String_Translation;
     $WPML_String_Translation->init_active_languages();
     $label = 'Test attr';
     $name = wc_attribute_taxonomy_name($label);
     $taxonomy_data = array('hierarchical' => true, 'update_count_callback' => '_update_post_term_count', 'labels' => array('name' => $label, 'singular_name' => $label, 'search_items' => sprintf(__('Search %s', 'woocommerce'), $label), 'all_items' => sprintf(__('All %s', 'woocommerce'), $label), 'parent_item' => sprintf(__('Parent %s', 'woocommerce'), $label), 'parent_item_colon' => sprintf(__('Parent %s:', 'woocommerce'), $label), 'edit_item' => sprintf(__('Edit %s', 'woocommerce'), $label), 'update_item' => sprintf(__('Update %s', 'woocommerce'), $label), 'add_new_item' => sprintf(__('Add New %s', 'woocommerce'), $label), 'new_item_name' => sprintf(__('New %s', 'woocommerce'), $label)), 'show_ui' => true, 'show_in_menu' => false, 'show_in_nav_menus' => false, 'meta_box_cb' => false, 'query_var' => 1, 'rewrite' => false, 'sort' => false, 'public' => 1, 'show_in_nav_menus' => 1, 'capabilities' => array('manage_terms' => 'manage_product_terms', 'edit_terms' => 'edit_product_terms', 'delete_terms' => 'delete_product_terms', 'assign_terms' => 'assign_product_terms'));
     $taxonomy_data['rewrite'] = array('slug' => empty($permalinks['attribute_base']) ? '' : trailingslashit($permalinks['attribute_base']) . sanitize_title($label), 'with_front' => false, 'hierarchical' => true);
     do_action('wpml_register_single_string', 'WordPress', 'taxonomy singular name: ' . $label, $label);
     $string_id = icl_get_string_id($label, 'WordPress', 'taxonomy singular name: ' . $label);
     icl_add_string_translation($string_id, 'es', 'Test attr es', ICL_TM_COMPLETE);
     $WPML_String_Translation->clear_string_filter('es');
     $sitepress->switch_lang('es');
     register_taxonomy($name, apply_filters("woocommerce_taxonomy_objects_{$name}", array('product')), apply_filters("woocommerce_taxonomy_args_{$name}", $taxonomy_data));
     $this->assertTrue((bool) has_filter('wpml_translate_single_string'));
     $this->assertEquals('Test attr es', $wp_taxonomies[$name]->labels->name);
 }
 /**
  *
  * @param string $option_name
  * @param string $language
  * @param string $new_value
  * @param int|bool $status
  * @param int $translator_id
  * @param int $rec_level
  *
  * @return boolean|mixed
  */
 public function update_translation($option_name, $language, $new_value = null, $status = false, $translator_id = null, $rec_level = 0)
 {
     $new_value = (array) $new_value;
     $updated = array();
     foreach ($new_value as $index => $value) {
         if (is_array($value)) {
             $name = "[" . $option_name . "][" . $index . "]";
             $result = $this->update_translation($name, $language, $value, $status, $translator_id, $rec_level + 1);
             $updated[] = array_sum(explode(",", $result));
         } else {
             if (is_string($index)) {
                 $name = ($rec_level == 0 ? "[" . $option_name . "]" : $option_name) . $index;
             } else {
                 $name = $option_name;
             }
             $original_string_id = $this->wpdb->get_var($this->wpdb->prepare("\tSELECT id\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFROM {$this->wpdb->prefix}icl_strings\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE name = %s\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tAND language != %s", $name, $language));
             if ($original_string_id) {
                 $updated[] = icl_add_string_translation($original_string_id, $language, $value, $status, $translator_id);
             }
         }
     }
     return array_sum($updated) > 0 ? join(",", $updated) : false;
 }
 function send_strings_to_translation_service($string_ids, $target_language, $translator_id)
 {
     /** @var WPML_String_Translation $WPML_String_Translation */
     global $WPML_String_Translation;
     if (sizeof($string_ids) > 0) {
         $project = $this->basket->get_project();
         $strings = array();
         $word_count = 0;
         $source_language = $this->basket->get_source_language();
         foreach ($string_ids as $string_id) {
             $string_data_query = "SELECT id, context, name, value FROM {$this->wpdb->prefix}icl_strings WHERE id=%d";
             $string_data_prepare = $this->wpdb->prepare($string_data_query, $string_id);
             $string_data = $this->wpdb->get_row($string_data_prepare);
             $word_count += $WPML_String_Translation->estimate_word_count($string_data->value, $source_language);
             $strings[] = $string_data;
         }
         $xliff = new WPML_TM_Xliff_Writer($this->job_factory);
         $res = $project->send_to_translation_batch_mode($xliff->get_strings_xliff_file($strings, $source_language, $target_language), 'String Translations', '', '', $source_language, $target_language, $word_count);
         if ($res) {
             foreach ($strings as $string_data) {
                 $translation_service = TranslationProxy_Service::get_translator_data_from_wpml($translator_id);
                 $added = icl_add_string_translation($string_data->id, $target_language, null, ICL_TM_WAITING_FOR_TRANSLATOR, $translation_service['translator_id'], $translation_service['translation_service'], TranslationProxy_Batch::update_translation_batch($this->basket->get_name()));
                 if ($added) {
                     $data = array('rid' => $res, 'string_translation_id' => $added, 'timestamp' => date('Y-m-d H:i:s'), 'md5' => md5($string_data->value));
                     $this->wpdb->insert($this->wpdb->prefix . 'icl_string_status', $data);
                     //insert rid
                 }
             }
             $this->wpdb->insert($this->wpdb->prefix . 'icl_core_status', array('rid' => $res, 'module' => '', 'origin' => $source_language, 'target' => $target_language, 'status' => ICL_TM_WAITING_FOR_TRANSLATOR));
             if ($project->errors && count($project->errors)) {
                 $res['errors'] = $project->errors;
             }
             return $res;
         }
     }
     return 0;
 }
 function ajax_calls($call, $data)
 {
     require_once WPML_ST_PATH . '/inc/admin-texts/wpml-admin-text-configuration.php';
     switch ($call) {
         case 'icl_st_save_translation':
             $icl_st_complete = isset($data['icl_st_translation_complete']) && $data['icl_st_translation_complete'] ? ICL_TM_COMPLETE : ICL_TM_NOT_TRANSLATED;
             if (get_magic_quotes_gpc()) {
                 $data = stripslashes_deep($data);
             }
             if (icl_st_is_translator()) {
                 $translator_id = get_current_user_id() > 0 ? get_current_user_id() : null;
             } else {
                 $translator_id = null;
             }
             echo icl_add_string_translation($data['icl_st_string_id'], $data['icl_st_language'], stripslashes($data['icl_st_translation']), $icl_st_complete, $translator_id);
             echo '|';
             $ts = icl_update_string_status($data['icl_st_string_id']);
             if (icl_st_is_translator()) {
                 $ts = icl_get_relative_translation_status($data['icl_st_string_id']);
             }
             echo WPML_ST_String_Statuses::get_status($ts);
             break;
         case 'icl_st_delete_strings':
             $arr = explode(',', $data['value']);
             __icl_unregister_string_multi($arr);
             break;
         case 'icl_st_option_writes_form':
             if (!empty($data['icl_admin_options'])) {
                 $wpml_admin_text = wpml_st_load_admin_texts();
                 $wpml_admin_text->icl_register_admin_options($data['icl_admin_options']);
                 echo '1|';
             } else {
                 echo '0' . __('No strings selected', 'wpml-string-translation');
             }
             break;
         case 'icl_st_ow_export':
             // filter empty options out
             do {
                 list($data['icl_admin_options'], $empty_found) = _icl_st_filter_empty_options_out($data['icl_admin_options']);
             } while ($empty_found);
             if (!empty($data['icl_admin_options'])) {
                 foreach ($data['icl_admin_options'] as $k => $opt) {
                     if (!$opt) {
                         unset($data['icl_admin_options'][$k]);
                     }
                 }
                 $wpml_admin_text_config = new WPML_Admin_Text_Configuration();
                 $message = __('Save the following to a wpml-config.xml in the root of your theme or plugin.', 'wpml-string-translation') . "<textarea wrap=\"soft\" spellcheck=\"false\">" . htmlentities($wpml_admin_text_config->get_wpml_config_file($data['icl_admin_options'])) . "</textarea>";
             } else {
                 $message = __('Error: no strings selected', 'wpml-string-translation');
             }
             echo json_encode(array('error' => 0, 'message' => $message));
             break;
     }
 }
 function tl_rescan_p()
 {
     global $wpdb, $sitepress_settings;
     set_time_limit(0);
     if (preg_replace('#M$#', '', ini_get('memory_limit')) < 128) {
         ini_set('memory_limit', '128M');
     }
     $plugins = array();
     if (!empty($_POST['plugin'])) {
         foreach ($_POST['plugin'] as $plugin) {
             $plugins[] = array('file' => $plugin, 'mu' => 0);
             // regular plugins
         }
     }
     if (!empty($_POST['mu-plugin'])) {
         foreach ($_POST['mu-plugin'] as $plugin) {
             $plugins[] = array('file' => $plugin, 'mu' => 1);
             //mu plugins
         }
     }
     $scan_stats = '';
     foreach ($plugins as $p) {
         $plugin = $p['file'];
         if (false !== strpos($plugin, '/') && !$p['mu']) {
             $plugin = dirname($plugin);
         }
         if ($p['mu']) {
             $plugin_path = WPMU_PLUGIN_DIR . '/' . $plugin;
         } else {
             $plugin_path = WP_PLUGIN_DIR . '/' . $plugin;
         }
         $scan_stats .= icl_st_scan_plugin_files($plugin_path);
         if ($_POST['icl_load_mo'] && !$p['mu']) {
             $mo_files = icl_st_get_mo_files($plugin_path);
             foreach ($mo_files as $m) {
                 $i = preg_match('#[-]([a-z_]+)\\.mo$#i', $m, $matches);
                 if ($i && ($lang = $wpdb->get_var("SELECT code FROM {$wpdb->prefix}icl_locale_map WHERE locale='" . $matches[1] . "'"))) {
                     $tr_pairs = icl_st_load_translations_from_mo($m);
                     foreach ($tr_pairs as $original => $translation) {
                         $string_id = icl_get_string_id($original, 'plugin ' . basename($plugin_path));
                         if (!$wpdb->get_var("SELECT id FROM {$wpdb->prefix}icl_string_translations WHERE string_id={$string_id} AND language='{$lang}'")) {
                             icl_add_string_translation($string_id, $lang, $translation, ICL_STRING_TRANSLATION_COMPLETE);
                         }
                     }
                 }
             }
         }
     }
     echo '1|' . $scan_stats;
     exit;
 }
 function translate_product_slug()
 {
     global $sitepress, $wpdb, $woocommerce, $sitepress_settings;
     if (!defined('WOOCOMMERCE_VERSION') || (!isset($GLOBALS['ICL_Pro_Translation']) || is_null($GLOBALS['ICL_Pro_Translation']))) {
         return;
     }
     $permalinks = get_option('woocommerce_permalinks', array('product_base' => ''));
     $slug = get_option('woocommerce_product_slug') != false ? get_option('woocommerce_product_slug') : 'product';
     $string = $wpdb->get_row($wpdb->prepare("SELECT id,status FROM {$wpdb->prefix}icl_strings WHERE name = %s AND value = %s ", 'URL slug: ' . $slug, $slug));
     if (!$string) {
         icl_register_string('WordPress', 'URL slug: ' . $slug, $slug);
         $string = $wpdb->get_row($wpdb->prepare("SELECT id,status FROM {$wpdb->prefix}icl_strings WHERE name = %s AND value = %s ", 'URL slug: ' . $slug, $slug));
     }
     if (isset($sitepress_settings['posts_slug_translation']['types'])) {
         $iclsettings['posts_slug_translation']['types'] = $sitepress_settings['posts_slug_translation']['types'];
     }
     if (!empty($permalinks['product_base']) && isset($sitepress_settings['posts_slug_translation']['types'][$slug])) {
         $iclsettings['posts_slug_translation']['types'][$slug] = 0;
         $sitepress->save_settings($iclsettings);
     }
     if (empty($sitepress_settings['theme_localization_type']) || $sitepress_settings['theme_localization_type'] != 1) {
         $sitepress->save_settings(array('theme_localization_type' => 1));
     }
     if ($string->status != ICL_STRING_TRANSLATION_COMPLETE) {
         //get translations from .mo files
         $current_language = $sitepress->get_current_language();
         $default_language = $sitepress->get_default_language();
         $active_languages = $sitepress->get_active_languages();
         $string_id = $string->id;
         if (empty($string_id)) {
             $string_id = icl_register_string('WordPress', 'URL slug: ' . $slug, $slug);
         }
         foreach ($active_languages as $language) {
             if ($language['code'] != $sitepress_settings['st']['strings_language']) {
                 $sitepress->switch_lang($language['code']);
                 $context = 'slug';
                 $domain = 'woocommerce';
                 $woocommerce->load_plugin_textdomain();
                 $string_text = _x($slug, $context, $domain);
                 unload_textdomain($domain);
                 icl_add_string_translation($string_id, $language['code'], $string_text, ICL_STRING_TRANSLATION_COMPLETE, null);
                 $sitepress->switch_lang($current_language);
             }
         }
         $woocommerce->load_plugin_textdomain();
         $wpdb->update($wpdb->prefix . 'icl_strings', array('status' => ICL_STRING_TRANSLATION_COMPLETE), array('id' => $string_id));
     }
     $iclsettings['posts_slug_translation']['on'] = 1;
     $iclsettings['posts_slug_translation']['types'][$slug] = 1;
     $sitepress->save_settings($iclsettings);
 }
 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 add_default_slug_translations($slug, $name)
 {
     global $woocommerce_wpml, $sitepress, $wpdb;
     $string_id = icl_get_string_id($slug, $this->url_strings_context(), $name);
     if (WPML_SUPPORT_STRINGS_IN_DIFF_LANG) {
         $string_language = $woocommerce_wpml->strings->get_string_language($slug, $this->url_strings_context(), $name);
         // will use a filter in the future wpmlst-529
         $string_object = new WPML_ST_String($string_id, $wpdb);
         $string_translation_statuses = $string_object->get_translation_statuses();
     } else {
         $string_language = $wpdb->get_var($wpdb->prepare("SELECT language FROM {$wpdb->prefix}icl_strings WHERE id=%d", $string_id));
         $string_translation_statuses = $wpdb->get_results($wpdb->prepare("SELECT language, status FROM {$wpdb->prefix}icl_string_translations WHERE string_id=%d", $string_id));
     }
     foreach ($string_translation_statuses as $s) {
         $string_translations[$s->language] = $s->status;
     }
     $languages = $sitepress->get_active_languages();
     foreach ($languages as $language => $language_info) {
         if ($language != $string_language) {
             // check if there's an existing translation
             if (!isset($string_translations[$language])) {
                 $slug_translation = $woocommerce_wpml->strings->get_translation_from_woocommerce_mo_file($slug, $language, false);
                 if ($slug_translation) {
                     // add string translation
                     icl_add_string_translation($string_id, $language, $slug_translation, ICL_STRING_TRANSLATION_COMPLETE);
                 }
             }
         }
     }
 }
 /**
  * Ajax handler for saving label translations from the WPML Taxonomy Translations menu.
  */
 public static function save_labels_translation()
 {
     $general = isset($_POST['plural']) ? $_POST['plural'] : false;
     $singular = isset($_POST['singular']) ? $_POST['singular'] : false;
     $taxonomy = isset($_POST['taxonomy']) ? $_POST['taxonomy'] : false;
     $language = isset($_POST['taxonomy_language_code']) ? $_POST['taxonomy_language_code'] : false;
     if ($singular && $general && $taxonomy && $language) {
         $tax_label_data = WPML_Taxonomy_Translation_Table_Display::get_label_translations($taxonomy);
         if (isset($tax_label_data['id_singular']) && $tax_label_data['id_singular'] && isset($tax_label_data['id_general']) && $tax_label_data['id_general']) {
             $original_id_singular = $tax_label_data['id_singular'];
             $original_id_plural = $tax_label_data['id_general'];
             icl_add_string_translation($original_id_singular, $language, $singular, ICL_STRING_TRANSLATION_COMPLETE);
             $singular_result = (string) icl_get_string_by_id($original_id_singular, $language);
             icl_add_string_translation($original_id_plural, $language, $general, ICL_STRING_TRANSLATION_COMPLETE);
             $plural_result = (string) icl_get_string_by_id($original_id_plural, $language);
             if ($singular_result && $plural_result) {
                 $result = array('singular' => $singular_result, 'general' => $plural_result, 'lang' => $language);
                 wp_send_json_success($result);
             }
         }
     }
     wp_send_json_error();
 }
 function add_translated_document($translation_id, $request_id)
 {
     global $sitepress_settings, $wpdb, $sitepress;
     $iclq = new ICanLocalizeQuery($sitepress_settings['site_id'], $sitepress_settings['access_key']);
     $tinfo = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}icl_translations WHERE translation_id=%d", $translation_id));
     $_lang = $sitepress->get_language_details($tinfo->language_code);
     $translation = $iclq->cms_do_download($request_id, $this->server_languages_map($_lang['english_name']));
     $translation = apply_filters('icl_data_from_pro_translation', $translation);
     $ret = false;
     if (!empty($translation)) {
         $language_code = $wpdb->get_var($wpdb->prepare("\n                SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE translation_id=%d", $translation_id));
         $parts = explode('_', $translation['original_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);
             unset($translation['original_id']);
             foreach ($translation as $field => $value) {
                 if (function_exists('icl_st_is_registered_string')) {
                     $value = str_replace('&#0A;', "\n", $value);
                     $string_id = icl_st_is_registered_string($type, $id . '_' . $field);
                     if (!$string_id) {
                         icl_register_string($type, $id . '_' . $field, $value);
                         $string_id = icl_st_is_registered_string($type, $id . '_' . $field);
                     }
                     if ($string_id) {
                         icl_add_string_translation($string_id, $language_code, $value, ICL_STRING_TRANSLATION_COMPLETE);
                     }
                 }
             }
             $ret = true;
         } else {
             $ret = $this->save_post_translation($translation_id, $translation);
         }
         if ($ret) {
             $lang_details = $sitepress->get_language_details($language_code);
             $language_server = $this->server_languages_map($lang_details['english_name']);
             $iclq->cms_update_request_status($request_id, CMS_TARGET_LANGUAGE_DONE, $language_server);
             $translations = $sitepress->get_element_translations($tinfo->trid, $tinfo->element_type);
             if (isset($translations[$tinfo->language_code])) {
                 $iclq->report_back_permalink($request_id, $language_server, $translations[$tinfo->language_code]);
             }
         }
     }
     return $ret;
 }
 function save_translations($data, $language, $version = false)
 {
     set_time_limit(0);
     if (false === $version) {
         global $wp_version;
         $version = preg_replace('#-(.+)$#', '', $wp_version);
     }
     foreach ($data as $key => $string) {
         $string_id = icl_register_string(self::CONTEXT, $string['name'], $string['string']);
         if ($string_id) {
             icl_add_string_translation($string_id, $language, $string['translation'], ICL_STRING_TRANSLATION_COMPLETE);
         }
     }
     $version_projects = explode(';', $version);
     foreach ($version_projects as $project) {
         $exp = explode('|', $project);
         $this->settings['translations'][$language][$exp[0]]['time'] = time();
         $this->settings['translations'][$language][$exp[0]]['installed'] = $exp[1];
     }
     $this->save_settings();
 }
 /**
  * @param string $element_type_prefix
  * @param object $job
  * todo: Move to ST via an action to make this testable
  */
 private function save_external($element_type_prefix, $job)
 {
     // Translations are saved in the string table for 'external' types
     $element_type_prefix = apply_filters('wpml_get_package_type_prefix', $element_type_prefix, $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($element_type_prefix, $field->field_type);
                 if (!$string_id) {
                     icl_register_string($element_type_prefix, $field->field_type, $this->decode_field_data($field->field_data, $field->field_format));
                     $string_id = icl_st_is_registered_string($element_type_prefix, $field->field_type);
                 }
                 if ($string_id) {
                     icl_add_string_translation($string_id, $job->language_code, $this->decode_field_data($field->field_data_translated, $field->field_format), ICL_TM_COMPLETE);
                 }
             }
         }
     }
 }
 function set_translated_strings($translations)
 {
     global $wpdb;
     $this->sanitize_attributes();
     $package_id = $this->get_package_id();
     if ($package_id) {
         foreach ($translations as $string_name => $languages) {
             $string_id_query = "SELECT id FROM {$wpdb->prefix}icl_strings WHERE name='%s'";
             $string_id_prepare = $wpdb->prepare($string_id_query, $string_name);
             $string_id = $wpdb->get_var($string_id_prepare);
             foreach ($languages as $language_code => $language_data) {
                 icl_add_string_translation($string_id, $language_code, $language_data['value'], $language_data['status']);
             }
         }
     }
 }
 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);
 }
 function save_translation($data)
 {
     global $wpdb, $sitepress, $ICL_Pro_Translation;
     $new_post_id = false;
     $is_incomplete = false;
     $job = null;
     $original_post = null;
     $element_type_prefix = null;
     if (isset($data['job_id'])) {
         $job = $this->get_translation_job($data['job_id'], true);
         $element_type_prefix = $this->get_element_type_prefix_from_job($job);
         $original_post = $this->get_post($job->original_doc_id, $element_type_prefix);
     }
     $data_to_validate = array('original_post' => $original_post, 'type_prefix' => $element_type_prefix, 'data' => $data, 'is_external' => $this->is_external_type($element_type_prefix));
     $is_valid = true;
     $validation_default_results = array('is_valid' => $is_valid, 'messages' => array());
     if (!$job || !$original_post || !$element_type_prefix) {
         $is_valid = false;
         if (!$job) {
             $validation_default_results['messages'][] = _('Job ID is missing', 'sitepress');
         }
         if (!$original_post) {
             $validation_default_results['messages'][] = _('The original post cannot be retrieved', 'sitepress');
         }
         if (!$element_type_prefix) {
             $validation_default_results['messages'][] = _('The type of the post cannot be retrieved', 'sitepress');
         }
     }
     $validation_default_results['is_valid'] = $is_valid;
     $validation_results = apply_filters('wpml_translation_validation_data', $validation_default_results, $data_to_validate);
     $validation_results = array_merge($validation_results, $validation_default_results);
     if (!$is_valid && $validation_results['is_valid']) {
         $validation_results['is_valid'] = $is_valid;
     }
     if (isset($validation_results['is_valid']) && !$validation_results['is_valid']) {
         if (isset($validation_results['messages'])) {
             $messages = (array) $validation_results['messages'];
             if ($messages) {
                 foreach ($messages as $message) {
                     $this->messages[] = array('type' => 'error', 'text' => $message);
                 }
             } else {
                 $this->messages[] = array('type' => 'error', 'text' => __('Submitted data is not valid.', 'sitepress'));
             }
         }
         do_action('wpml_translation_validation_failed', $validation_results, $data_to_validate);
     } else {
         foreach ($data['fields'] as $fieldname => $field) {
             if (substr($fieldname, 0, 6) === 'field-') {
                 $field = apply_filters('wpml_tm_save_translation_cf', $field, $fieldname, $data);
             }
             $this->_save_translation_field($field['tid'], $field);
             if (!isset($field['finished']) || !$field['finished']) {
                 $is_incomplete = true;
             }
         }
         $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));
         if (($is_incomplete === true || empty($data['complete'])) && empty($data['resign'])) {
             $status_update = array('translation_id' => $translation_id, 'status' => ICL_TM_IN_PROGRESS);
             $this->update_translation_status($status_update);
             $wpdb->update($wpdb->prefix . 'icl_translate_job', array('translated' => 0), array('job_id' => $data['job_id']));
         }
         //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) {
             if (defined('XMLRPC_REQUEST') || defined('DOING_AJAX')) {
                 return;
             } else {
                 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']));
             $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 ($this->is_external_type($element_type_prefix)) {
                 // Translations are saved in the string table for 'external' types
                 $element_type_prefix = apply_filters('wpml_get_package_type_prefix', $element_type_prefix, $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($element_type_prefix, $field->field_type);
                             if (!$string_id) {
                                 icl_register_string($element_type_prefix, $field->field_type, self::decode_field_data($field->field_data, $field->field_format));
                                 $string_id = icl_st_is_registered_string($element_type_prefix, $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_TM_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;
                     }
                 }
                 $postarr['post_author'] = $original_post->post_author;
                 $postarr['post_type'] = $original_post->post_type;
                 if ($sitepress->get_setting('sync_comment_status')) {
                     $postarr['comment_status'] = $original_post->comment_status;
                 }
                 if ($sitepress->get_setting('sync_ping_status')) {
                     $postarr['ping_status'] = $original_post->ping_status;
                 }
                 if ($sitepress->get_setting('sync_page_ordering')) {
                     $postarr['menu_order'] = $original_post->menu_order;
                 }
                 if ($sitepress->get_setting('sync_private_flag') && $original_post->post_status == 'private') {
                     $postarr['post_status'] = 'private';
                 }
                 if ($sitepress->get_setting('sync_post_date')) {
                     $postarr['post_date'] = $original_post->post_date;
                 }
                 //set as draft or the same status as original post
                 $postarr['post_status'] = !$sitepress->get_setting('translated_document_status') ? 'draft' : $original_post->post_status;
                 if ($original_post->post_parent) {
                     $post_parent_trid = $wpdb->get_var($wpdb->prepare("\tSELECT trid\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFROM {$wpdb->prefix}icl_translations\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE element_type LIKE 'post%%'\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tAND element_id=%d", $original_post->post_parent));
                     if ($post_parent_trid) {
                         $parent_id = $wpdb->get_var($wpdb->prepare("\tSELECT element_id\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFROM {$wpdb->prefix}icl_translations\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE element_type LIKE 'post%%' AND trid=%d AND language_code=%s", $post_parent_trid, $job->language_code));
                     }
                 }
                 if (isset($parent_id) && $sitepress->get_setting('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);
                 // it's an update and user do not want to translate urls so do not change the url
                 if (isset($element_id) && $sitepress->get_setting('translated_document_page_url') !== 'translate') {
                     $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']);
                 if ($ICL_Pro_Translation) {
                     /** @var WPML_Pro_Translation $ICL_Pro_Translation */
                     $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;
                     }
                 }
                 if (isset($fields_data_translated)) {
                     $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?
                 $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->get_setting('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->get_setting('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->get_setting('sync_post_format')) {
                     $_wp_post_format = get_post_format($original_post->ID);
                     set_post_format($new_post_id, $_wp_post_format);
                 }
                 $package_helper = new WPML_Element_Translation_Package();
                 $package_helper->save_job_custom_fields($job, $new_post_id, (array) $this->settings['custom_fields_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->delete($wpdb->prefix . 'icl_translations', array('element_id' => $new_post_id, 'element_type' => 'post_' . $postarr['post_type']));
                     $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>.';
                 }
             }
             if (isset($user_message)) {
                 $this->add_message(array('type' => 'updated', 'text' => $user_message));
             }
             if ($this->settings['notification']['completed'] != ICL_TM_NOTIFICATION_NONE && $data['job_id']) {
                 do_action('wpml_tm_complete_job_notification', $data['job_id'], !is_null($element_id));
             }
             self::set_page_url($new_post_id);
             if (isset($job) && isset($job->language_code) && isset($job->source_language_code)) {
                 WPML_Translation_Job_Terms::save_terms_from_job($data['job_id'], $job->language_code);
                 $term_helper = wpml_get_term_translation_util();
                 $term_helper->sync_terms($original_post->ID, $job->language_code);
             }
             // Set the posts mime type correctly.
             if (isset($original_post) && isset($original_post->ID) && $original_post->post_type == 'attachment') {
                 $attached_file = get_post_meta($original_post->ID, '_wp_attached_file', false);
                 update_post_meta($new_post_id, '_wp_attached_file', array_pop($attached_file));
                 $mime_type = get_post_mime_type($original_post->ID);
                 if ($mime_type) {
                     $wpdb->update($wpdb->posts, array('post_mime_type' => $mime_type), array('ID' => $new_post_id));
                 }
             }
             do_action('icl_pro_translation_completed', $new_post_id);
             if (defined('XMLRPC_REQUEST') || defined('DOING_AJAX') || isset($_POST['xliff_upload'])) {
                 return;
             } else {
                 $action_type = is_null($element_id) ? 'added' : 'updated';
                 $element_id = is_null($element_id) ? $new_post_id : $element_id;
                 wp_redirect(admin_url(sprintf('admin.php?page=%s&%s=%d&element_type=%s', WPML_TM_FOLDER . '/menu/translations-queue.php', $action_type, $element_id, $element_type_prefix)));
                 exit;
             }
         } else {
             $this->messages[] = array('type' => 'updated', 'text' => __('Translation (incomplete) saved.', 'sitepress'));
         }
     }
 }
 /**
  * Auto translate strings with given context
  *
  * @param $context
  * @param $languages
  * @param $template
  */
 private function translate_strings($context, $languages, $template)
 {
     global $wpdb;
     //get all not translated strings (status <> 1)
     if (0 === strcmp($context, 'all_contexts')) {
         $strings = $wpdb->get_results("SELECT id, language, context, value FROM {$wpdb->prefix}icl_strings");
     } else {
         $strings = $wpdb->get_results($wpdb->prepare("SELECT id, language, context, value FROM {$wpdb->prefix}icl_strings WHERE context=%s", $context));
     }
     //for each string add information
     foreach ($strings as $v) {
         foreach ($languages as $lang) {
             icl_add_string_translation($v->id, $lang, wpml_ctt_prepare_string($template, $v->value, $lang), TRUE);
             icl_update_string_status($v->id);
         }
     }
 }
Exemple #21
0
function icl_translation_add_string_translation($rid, $translation, $lang_code)
{
    global $wpdb, $sitepress_settings, $sitepress;
    foreach ($translation as $key => $value) {
        if (preg_match("/string-(.*?)-value/", $key, $match)) {
            $string_id = $match[1];
            $md5_when_sent = $wpdb->get_var($wpdb->prepare("SELECT md5 FROM {$wpdb->prefix}icl_string_status \r\n                WHERE string_translation_id=%d AND rid=%d", $string_id, $rid));
            $current_string_value = $wpdb->get_var($wpdb->prepare("SELECT value FROM {$wpdb->prefix}icl_strings WHERE id=%d", $string_id));
            if ($md5_when_sent == md5($current_string_value)) {
                $status = ICL_STRING_TRANSLATION_COMPLETE;
            } else {
                $status = ICL_STRING_TRANSLATION_NEEDS_UPDATE;
            }
            $value = str_replace('&#0A;', "\n", $value);
            icl_add_string_translation($string_id, $lang_code, html_entity_decode($value), $status, 0);
        }
    }
    // update translation status
    $wpdb->update($wpdb->prefix . 'icl_core_status', array('status' => CMS_TARGET_LANGUAGE_DONE), array('rid' => $rid, 'target' => $lang_code));
    return true;
}
 function send_strings_to_translation_service($string_ids, $target_language, $basket_name, $translator_id)
 {
     global $wpdb;
     // get all the untranslated strings
     $untranslated = array();
     foreach ($string_ids as $st_id) {
         $untranslated[] = $st_id;
     }
     if (sizeof($untranslated) > 0) {
         $project = TranslationProxy::get_current_project();
         $strings = array();
         $word_count = 0;
         $source_language = $this->get_strings_language();
         foreach ($untranslated as $string_id) {
             $string_data_query = "SELECT id, context, name, value FROM {$wpdb->prefix}icl_strings WHERE id=%d";
             $string_data_prepare = $wpdb->prepare($string_data_query, $string_id);
             $string_data = $wpdb->get_row($string_data_prepare);
             $word_count += $this->estimate_word_count($string_data->value, $source_language);
             $strings[] = $string_data;
         }
         $xliff = new WPML_TM_xliff();
         $file = $xliff->get_strings_xliff_file($strings, $source_language, $target_language);
         $title = "String Translations";
         $cms_id = '';
         $url = '';
         $timestamp = date('Y-m-d H:i:s');
         if (TranslationProxy::is_batch_mode()) {
             $res = $project->send_to_translation_batch_mode($file, $title, $cms_id, $url, $source_language, $target_language, $word_count);
         } else {
             $res = $project->send_to_translation($file, $title, $cms_id, $url, $source_language, $target_language, $word_count);
         }
         if ($res) {
             foreach ($strings as $string_data) {
                 $batch_id = TranslationProxy_Batch::update_translation_batch($basket_name);
                 $translation_service = TranslationProxy_Service::get_translator_data_from_wpml($translator_id);
                 $added = icl_add_string_translation($string_data->id, $target_language, null, ICL_TM_WAITING_FOR_TRANSLATOR, $translation_service['translator_id'], $translation_service['translation_service'], $batch_id);
                 if ($added) {
                     $data = array('rid' => $res, 'string_translation_id' => $added, 'timestamp' => $timestamp, 'md5' => md5($string_data->value));
                     $wpdb->insert($wpdb->prefix . 'icl_string_status', $data);
                     //insert rid
                 } else {
                     $this->add_message(sprintf(__('Unable to add "%s" string in tables', 'sitepress'), $string_data->name), 'error');
                     return 0;
                 }
             }
             $wpdb->insert($wpdb->prefix . 'icl_core_status', array('rid' => $res, 'module' => '', 'origin' => $source_language, 'target' => $target_language, 'status' => CMS_REQUEST_WAITING_FOR_PROJECT_CREATION));
             if ($project->errors && count($project->errors)) {
                 $res['errors'] = $project->errors;
             }
             return $res;
         }
     }
     return 0;
 }
 static function save_sync_options()
 {
     global $sitepress, $wpdb;
     $slug_settings = $sitepress->get_setting('posts_slug_translation');
     if (isset($slug_settings['on']) && $slug_settings['on'] && !empty($_POST['translate_slugs'])) {
         foreach ($_POST['translate_slugs'] as $type => $data) {
             $slug_settings['types'][$type] = isset($data['on']) ? intval(!empty($data['on'])) : false;
             if (empty($slug_settings['types'][$type])) {
                 continue;
             }
             $post_type_obj = get_post_type_object($type);
             $slug = trim($post_type_obj->rewrite['slug'], '/');
             $string_id = $wpdb->get_var(self::get_sql_to_get_string_id($slug));
             $string_id = empty($string_id) ? icl_register_string('WordPress', 'URL slug: ' . $slug, $slug) : $string_id;
             if ($string_id) {
                 foreach ($sitepress->get_active_languages() as $lang) {
                     $string_translation_settings = $sitepress->get_setting('st');
                     if ($lang['code'] != $string_translation_settings['strings_language']) {
                         $data['langs'][$lang['code']] = join('/', array_map(array('WPML_Slug_Translation', 'sanitize'), explode('/', $data['langs'][$lang['code']])));
                         $data['langs'][$lang['code']] = urldecode($data['langs'][$lang['code']]);
                         icl_add_string_translation($string_id, $lang['code'], $data['langs'][$lang['code']], ICL_TM_COMPLETE);
                     }
                 }
                 icl_update_string_status($string_id);
             }
         }
     }
     $sitepress->set_setting('posts_slug_translation', $slug_settings, true);
 }
 public static function save_labels_translation()
 {
     $errors = '';
     if (empty($_POST['singular']) || empty($_POST['general'])) {
         $errors .= __('Please fill in all fields!', 'sitepress') . '<br />';
     }
     $string_id = icl_st_is_registered_string('WordPress', 'taxonomy singular name: ' . $_POST['singular_original']);
     if (!$string_id) {
         $string_id = icl_register_string('WordPress', 'taxonomy singular name: ' . $_POST['singular_original'], $_POST['singular_original']);
     }
     icl_add_string_translation($string_id, $_POST['language'], $_POST['singular'], ICL_STRING_TRANSLATION_COMPLETE);
     $string_id = icl_st_is_registered_string('WordPress', 'taxonomy general name: ' . $_POST['general_original']);
     if (!$string_id) {
         $string_id = icl_register_string('WordPress', 'taxonomy general name: ' . $_POST['general_original'], $_POST['general_original']);
     }
     icl_add_string_translation($string_id, $_POST['language'], $_POST['general'], ICL_STRING_TRANSLATION_COMPLETE);
     $html = '';
     echo json_encode(array('html' => $html, 'errors' => $errors));
     exit;
 }
Exemple #25
0
                         continue;
                     }
                     // assume it is already registered
                     $post_type_obj = get_post_type_object($type);
                     $slug = $post_type_obj->rewrite['slug'];
                     $string_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM {$wpdb->prefix}icl_strings WHERE name = %s AND value = %s ", 'URL slug: ' . $slug, $slug));
                     if (empty($string_id)) {
                         $string_id = icl_register_string('WordPress', 'URL slug: ' . $slug, $slug);
                     }
                     if ($string_id) {
                         foreach ($this->get_active_languages() as $lang) {
                             $string_translation_settings = $this->get_setting('st');
                             if ($lang['code'] != $string_translation_settings['strings_language']) {
                                 $data['langs'][$lang['code']] = join('/', array_map('sanitize_title_with_dashes', explode('/', $data['langs'][$lang['code']])));
                                 $data['langs'][$lang['code']] = urldecode($data['langs'][$lang['code']]);
                                 icl_add_string_translation($string_id, $lang['code'], $data['langs'][$lang['code']], ICL_STRING_TRANSLATION_COMPLETE);
                             }
                         }
                         icl_update_string_status($string_id);
                     }
                 }
             }
         }
         if (isset($iclsettings)) {
             $this->save_settings($iclsettings);
         }
     }
     echo '1|';
     break;
 case 'copy_from_original':
     /*
 protected function copy_old_translations($contexts, $prefix)
 {
     global $wpdb;
     foreach ($contexts as $context) {
         $new_strings = $wpdb->get_results($wpdb->prepare("\n\t\t\t\tSELECT id, name, value\n\t\t\t\tFROM {$wpdb->prefix}icl_strings\n\t\t\t\tWHERE context = %s", $context));
         $new_ids = array();
         foreach ($new_strings as $new_string) {
             $new_ids[] = $new_string->id;
         }
         $new_ids = implode(',', $new_ids);
         if ($new_ids != '') {
             $new_translations = $wpdb->get_results("\n\t\t\t\t\t\t\tSELECT id, string_id, language, status, value\n\t\t\t\t\t\t\tFROM {$wpdb->prefix}icl_string_translations\n\t\t\t\t\t\t\tWHERE string_id IN ({$new_ids})");
         } else {
             $new_translations = array();
         }
         $old_strings = $wpdb->get_results($wpdb->prepare("\n\t\t\t\tSELECT id, name, value\n\t\t\t\tFROM {$wpdb->prefix}icl_strings\n\t\t\t\tWHERE context = %s", $prefix . ' ' . $context));
         $old_ids = array();
         foreach ($old_strings as $old_string) {
             $old_ids[] = $old_string->id;
         }
         $old_ids = implode(',', $old_ids);
         if ($old_ids != '') {
             $old_translations = $wpdb->get_results("\n\t\t\t\t\t\t\tSELECT id, string_id, language, status, value\n\t\t\t\t\t\t\tFROM {$wpdb->prefix}icl_string_translations\n\t\t\t\t\t\t\tWHERE string_id IN ({$old_ids})");
         } else {
             $old_translations = array();
         }
         foreach ($old_translations as $old_translation) {
             // see if we have a new translation.
             $found = false;
             foreach ($new_translations as $new_translation) {
                 if ($new_translation->string_id == $old_translation->string_id && $new_translation->language == $old_translation->language) {
                     $found = true;
                     break;
                 }
             }
             if (!$found) {
                 // Copy the old translation to the new string.
                 // Find the original
                 foreach ($old_strings as $old_string) {
                     if ($old_string->id == $old_translation->string_id) {
                         // See if we have the same string in the new strings
                         foreach ($new_strings as $new_string) {
                             if ($new_string->value == $old_string->value) {
                                 // Add the old translation to new string.
                                 icl_add_string_translation($new_string->id, $old_translation->language, $old_translation->value, ICL_TM_COMPLETE);
                                 break;
                             }
                         }
                         break;
                     }
                 }
             }
         }
     }
 }
Exemple #27
0
function icl_translation_add_string_translation($rid, $translation, $lang_code)
{
    global $wpdb;
    foreach ($translation as $key => $value) {
        if (preg_match("/string-(.*)/", $key, $match)) {
            $string_id = $match[1];
            $string_translation_id = $wpdb->get_var($wpdb->prepare("SELECT id\r\n                                                      FROM {$wpdb->prefix}icl_string_translations\r\n                                                      WHERE string_id=%d AND language=%s", $string_id, $lang_code));
            $md5_when_sent = $wpdb->get_var($wpdb->prepare("\tSELECT md5\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFROM {$wpdb->prefix}icl_string_status\r\n                \t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE rid=%d AND string_translation_id=%d", $rid, $string_translation_id));
            $current_string_value = $wpdb->get_var($wpdb->prepare("\tSELECT value\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFROM {$wpdb->prefix}icl_strings\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE id=%d", $string_id));
            if ($md5_when_sent == md5($current_string_value)) {
                $status = ICL_TM_COMPLETE;
            } else {
                $status = ICL_TM_NEEDS_UPDATE;
            }
            $value = str_replace('&#0A;', "\n", $value);
            icl_add_string_translation($string_id, $lang_code, html_entity_decode($value), $status);
        }
    }
    return true;
}
Exemple #28
0
                         continue;
                     }
                     // assume it is already registered
                     $post_type_obj = get_post_type_object($type);
                     $slug = trim($post_type_obj->rewrite['slug'], '/');
                     $string_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM {$wpdb->prefix}icl_strings WHERE name = %s AND value = %s ", 'URL slug: ' . $slug, $slug));
                     if (empty($string_id)) {
                         $string_id = icl_register_string('WordPress', 'URL slug: ' . $slug, $slug);
                     }
                     if ($string_id) {
                         foreach ($this->get_active_languages() as $lang) {
                             $string_translation_settings = $this->get_setting('st');
                             if ($lang['code'] != $string_translation_settings['strings_language']) {
                                 $data['langs'][$lang['code']] = join('/', array_map('sanitize_title_with_dashes', explode('/', $data['langs'][$lang['code']])));
                                 $data['langs'][$lang['code']] = urldecode($data['langs'][$lang['code']]);
                                 icl_add_string_translation($string_id, $lang['code'], $data['langs'][$lang['code']], ICL_TM_COMPLETE);
                             }
                         }
                         icl_update_string_status($string_id);
                     }
                 }
             }
         }
         if (isset($iclsettings)) {
             $this->save_settings($iclsettings);
         }
     }
     echo '1|';
     break;
 case 'copy_from_original':
     /*
 private function migrate_translations()
 {
     foreach ($this->active_languages as $lang) {
         foreach ($this->not_translated[$lang] as $not_translated) {
             if (isset($this->translations[$lang][$not_translated['value']])) {
                 icl_add_string_translation($not_translated['id'], $lang, $this->translations[$lang][$not_translated['value']], ICL_TM_COMPLETE);
                 break;
             }
         }
     }
 }
 public function test_get_terms_filter()
 {
     global $sitepress, $wpdb, $WPML_String_Translation;
     $orig_lang = 'en';
     $sec_lang = 'fr';
     $name_base = 'The cat ';
     $desc_base = 'The cat description ';
     $taxonomy = 'link_category';
     $wpml_installation = new WPML_Installation($wpdb, $sitepress);
     $wpml_installation->set_active_languages(array($orig_lang, $sec_lang));
     $WPML_String_Translation->init_active_languages();
     $this->instantiate_link_manager('edit-tags.php');
     $sitepress->switch_lang($orig_lang);
     $cats = $cat_ids = array();
     for ($i = 0; $i < 3; $i++) {
         $term = wp_insert_term($name_base . $i, $taxonomy, array('description' => $desc_base . $i));
         $cats[$i] = get_term($term['term_id'], $taxonomy);
         $cat_ids[] = $term['term_id'];
         $context = $this->get_category_string_context($cats[$i]);
         $name_st_id = icl_get_string_id($cats[$i]->name, $context, $this->lm_helper->get_category_string_name('name', $cats[$i]));
         $desc_st_id = icl_get_string_id($cats[$i]->description, $context, $this->lm_helper->get_category_string_name('description', $cats[$i]));
         icl_add_string_translation($name_st_id, $sec_lang, $name_base . $i . $sec_lang, ICL_TM_COMPLETE);
         icl_add_string_translation($desc_st_id, $sec_lang, $desc_base . $i . $sec_lang, ICL_TM_COMPLETE);
     }
     $sitepress->switch_lang($sec_lang);
     $this->instantiate_link_manager('front');
     /** @var WPML_String_Translation $WPML_String_Translation */
     global $WPML_String_Translation;
     $WPML_String_Translation->clear_string_filter('fr');
     $translated_cats = $this->lm->get_terms_filter($cats, array($taxonomy));
     foreach ($translated_cats as $i => $cat) {
         $this->assertEquals($name_base . $i . $sec_lang, $cat->name);
         $this->assertEquals($desc_base . $i . $sec_lang, $cat->description);
     }
 }