function save_post_translation($translation_id, $translation) { global $wpdb, $sitepress_settings, $sitepress, $wp_taxonomies, $icl_adjust_id_url_filter_off; $icl_adjust_id_url_filter_off = true; $taxonomies = array_diff(array_keys((array) $wp_taxonomies), array('post_tag', 'category')); $tinfo = $wpdb->get_row($wpdb->prepare("\r\n SELECT * FROM {$wpdb->prefix}icl_translations tr\r\n JOIN {$wpdb->prefix}icl_translation_status ts ON ts.translation_id = tr.translation_id\r\n WHERE tr.translation_id=%d", $translation_id)); $lang_code = $tinfo->language_code; $trid = $tinfo->trid; $original_post_details = $wpdb->get_row("\r\n SELECT p.post_author, p.post_type, p.post_status, p.comment_status, p.ping_status, p.post_parent, p.menu_order, p.post_date, t.language_code\r\n FROM {$wpdb->prefix}icl_translations t \r\n JOIN {$wpdb->posts} p ON t.element_id = p.ID AND CONCAT('post_',p.post_type) = t.element_type\r\n WHERE trid='{$trid}' AND p.ID = '{$translation['original_id']}'\r\n "); //is the original post a sticky post? remove_filter('option_sticky_posts', array($sitepress, 'option_sticky_posts')); // remove filter used to get language relevant stickies. get them all $sticky_posts = get_option('sticky_posts'); $is_original_sticky = $original_post_details->post_type == 'post' && in_array($translation['original_id'], $sticky_posts); $this->_content_fix_image_paths_in_body($translation); $this->_content_fix_relative_link_paths_in_body($translation); $this->_content_decode_shortcodes($translation); // deal with tags if (isset($translation['tags'])) { $translated_tags = $translation['tags']; $translated_tag_ids = explode(',', $translation['tag_ids']); foreach ($translated_tags as $k => $v) { $tag_trid = $wpdb->get_var("SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_id='{$translated_tag_ids[$k]}' AND element_type='tax_post_tag'"); // before adding the new term make sure that another tag with the same name doesn't exist. If it does append @lang // same term name exists in a different language? $term_different_language = $wpdb->get_var("\r\n SELECT tm.term_id \r\n FROM {$wpdb->term_taxonomy} tx\r\n JOIN {$wpdb->terms} tm ON tx.term_id = tm.term_id \r\n JOIN {$wpdb->prefix}icl_translations tr ON tx.term_taxonomy_id = tr.element_id\r\n WHERE tm.name='" . $wpdb->escape($v) . "' AND tr.element_type LIKE 'tax\\_%' AND tr.language_code <> '{$lang_code}'\r\n "); if ($term_different_language) { $v .= ' @' . $lang_code; } //tag exists? (in the current language) $etag = get_term_by('name', htmlspecialchars($v), 'post_tag'); if (!$etag) { $etag = get_term_by('name', htmlspecialchars($v) . ' @' . $lang_code, 'post_tag'); } if (!$etag) { $tmp = wp_insert_term($v, 'post_tag'); if (!is_wp_error($tmp) && isset($tmp['term_taxonomy_id'])) { $wpdb->update($wpdb->prefix . 'icl_translations', array('language_code' => $lang_code, 'trid' => $tag_trid, 'source_language_code' => $original_post_details->language_code), array('element_type' => 'tax_post_tag', 'element_id' => $tmp['term_taxonomy_id'])); } } else { $term_taxonomy_id = $etag->term_taxonomy_id; // check whether we have an orphan translation - the same trid and language but a different element id $__translation_id = $wpdb->get_var("\r\n SELECT translation_id FROM {$wpdb->prefix}icl_translations \r\n WHERE trid = '{$tag_trid}' \r\n AND language_code = '{$lang_code}' \r\n AND element_id <> '{$term_taxonomy_id}'\r\n "); if ($__translation_id) { $wpdb->query("DELETE FROM {$wpdb->prefix}icl_translations WHERE translation_id={$__translation_id}"); } $tag_translation_id = $wpdb->get_var("SELECT translation_id FROM {$wpdb->prefix}icl_translations WHERE element_id={$term_taxonomy_id} AND element_type='tax_post_tag'"); if ($tag_translation_id) { $wpdb->update($wpdb->prefix . 'icl_translations', array('language_code' => $lang_code, 'trid' => $tag_trid, 'source_language_code' => $original_post_details->language_code), array('element_type' => 'tax_post_tag', 'translation_id' => $tag_translation_id)); } else { $wpdb->insert($wpdb->prefix . 'icl_translations', array('language_code' => $lang_code, 'trid' => $tag_trid, 'element_type' => 'tax_post_tag', 'element_id' => $term_taxonomy_id, 'source_language_code' => $original_post_details->language_code)); } } } } $original_post_tags = array(); foreach (wp_get_object_terms($translation['original_id'], 'post_tag') as $t) { $original_post_tags[] = $t->term_taxonomy_id; } if ($original_post_tags) { $tag_trids = $wpdb->get_col("SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_type='tax_post_tag' AND element_id IN (" . join(',', $original_post_tags) . ")"); if (!empty($tag_trids)) { $tag_tr_tts = $wpdb->get_col("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE element_type='tax_post_tag' AND language_code='{$lang_code}' AND trid IN (" . join(',', $tag_trids) . ")"); } if (!empty($tag_tr_tts)) { $translated_tags = $wpdb->get_col("SELECT t.name FROM {$wpdb->terms} t JOIN {$wpdb->term_taxonomy} tx ON tx.term_id = t.term_id WHERE tx.taxonomy='post_tag' AND tx.term_taxonomy_id IN (" . join(',', $tag_tr_tts) . ")"); } } // deal with categories if (isset($translation['categories'])) { $translated_cats = $translation['categories']; $translated_cats_ids = explode(',', $translation['category_ids']); foreach ($translated_cats as $k => $v) { //$v = trim(str_replace('<p>', '', $v)); $cat_trid = $wpdb->get_var("SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_id='{$translated_cats_ids[$k]}' AND element_type='tax_category'"); // before adding the new term make sure that another tag with the same name doesn't exist. If it does append @lang // same term name exists in a different language? $term_different_language = $wpdb->get_var("\r\n SELECT tm.term_id \r\n FROM {$wpdb->term_taxonomy} tx\r\n JOIN {$wpdb->terms} tm ON tx.term_id = tm.term_id \r\n JOIN {$wpdb->prefix}icl_translations tr ON tx.term_taxonomy_id = tr.element_id\r\n WHERE tm.name='" . $wpdb->escape($v) . "' AND tr.element_type LIKE 'tax\\_%' AND tr.language_code <> '{$lang_code}'\r\n "); if ($term_different_language) { $v .= ' @' . $lang_code; } //cat exists? $ecat = get_term_by('name', htmlspecialchars($v), 'category'); if (!$ecat) { $ecat = get_term_by('name', htmlspecialchars($v) . ' @' . $lang_code, 'category'); } if (!$ecat) { // get original category parent id $original_category_parent_id = $wpdb->get_var($wpdb->prepare("SELECT parent FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id=%d", $translated_cats_ids[$k])); if ($original_category_parent_id) { $_op_tax_id = $wpdb->get_var($wpdb->prepare("SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE taxonomy='category' AND term_id=%d", $original_category_parent_id)); $_op_trid = $wpdb->get_var($wpdb->prepare("SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_type='tax_category' AND element_id=%d", $_op_tax_id)); // get id of the translated category parent $_tp_tax_id = $wpdb->get_var($wpdb->prepare("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE language_code='{$lang_code}' AND trid=%d", $_op_trid)); if ($_tp_tax_id) { $category_parent_id = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->term_taxonomy} WHERE taxonomy='category' AND term_taxonomy_id=%d", $_tp_tax_id)); } else { $category_parent_id = 0; } } else { $category_parent_id = 0; } $tmp = wp_insert_term($v, 'category', array('parent' => $category_parent_id)); if (!is_wp_error($tmp) && isset($tmp['term_taxonomy_id'])) { $wpdb->update($wpdb->prefix . 'icl_translations', array('language_code' => $lang_code, 'trid' => $cat_trid, 'source_language_code' => $original_post_details->language_code), array('element_type' => 'tax_category', 'element_id' => $tmp['term_taxonomy_id'])); // if this is a parent category, make sure that nesting is correct for all translations $orig_cat_tax_id = $wpdb->get_var($wpdb->prepare("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid=%d AND source_language_code IS NULL", $cat_trid)); $orig_cat_term_id = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id=%d AND taxonomy='category'", $orig_cat_tax_id)); $orig_cat_children = $wpdb->get_col($wpdb->prepare("SELECT term_id FROM {$wpdb->term_taxonomy} WHERE parent=%d", $orig_cat_term_id)); if (!empty($orig_cat_children)) { foreach ($orig_cat_children as $ch) { $_tr_child = icl_object_id($ch, 'category', false, $lang_code); if ($_tr_child) { $wpdb->update($wpdb->term_taxonomy, array('parent' => $tmp['term_id']), array('taxonomy' => 'category', 'term_id' => $_tr_child)); } } } delete_option('category_children'); } } else { $term_taxonomy_id = $ecat->term_taxonomy_id; // check whether we have an orphan translation - the same trid and language but a different element id $__translation_id = $wpdb->get_var("\r\n SELECT translation_id FROM {$wpdb->prefix}icl_translations \r\n WHERE trid = '{$cat_trid}' \r\n AND language_code = '{$lang_code}' \r\n AND element_id <> '{$term_taxonomy_id}'\r\n "); if ($__translation_id) { $wpdb->query("DELETE FROM {$wpdb->prefix}icl_translations WHERE translation_id={$__translation_id}"); } $cat_translation_id = $wpdb->get_var("SELECT translation_id FROM {$wpdb->prefix}icl_translations WHERE element_id={$term_taxonomy_id} AND element_type='tax_category'"); if ($cat_translation_id) { $wpdb->update($wpdb->prefix . 'icl_translations', array('language_code' => $lang_code, 'trid' => $cat_trid, 'source_language_code' => $original_post_details->language_code), array('element_type' => 'tax_category', 'translation_id' => $cat_translation_id)); } else { $wpdb->insert($wpdb->prefix . 'icl_translations', array('language_code' => $lang_code, 'trid' => $cat_trid, 'element_type' => 'tax_category', 'element_id' => $term_taxonomy_id, 'source_language_code' => $original_post_details->language_code)); } } } } $original_post_cats = array(); foreach (wp_get_object_terms($translation['original_id'], 'category') as $t) { $original_post_cats[] = $t->term_taxonomy_id; } if ($original_post_cats) { $cat_trids = $wpdb->get_col("SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_type='tax_category' AND element_id IN (" . join(',', $original_post_cats) . ")"); if (!empty($cat_trids)) { $cat_tr_tts = $wpdb->get_col("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE element_type='tax_category' AND language_code='{$lang_code}' AND trid IN (" . join(',', $cat_trids) . ")"); } if (!empty($cat_tr_tts)) { $translated_cats_ids = $wpdb->get_col("SELECT t.term_id FROM {$wpdb->terms} t JOIN {$wpdb->term_taxonomy} tx ON tx.term_id = t.term_id WHERE tx.taxonomy='category' AND tx.term_taxonomy_id IN (" . join(',', $cat_tr_tts) . ")"); } } // deal with custom taxonomies if (!empty($sitepress_settings['taxonomies_sync_option'])) { foreach ($sitepress_settings['taxonomies_sync_option'] as $taxonomy => $value) { if ($value == 1 && isset($translation[$taxonomy])) { $translated_taxs[$taxonomy] = $translation[$taxonomy]; $translated_tax_ids[$taxonomy] = explode(',', $translation[$taxonomy . '_ids']); foreach ($translated_taxs[$taxonomy] as $k => $v) { $tax_trid = $wpdb->get_var("\r\n SELECT trid FROM {$wpdb->prefix}icl_translations \r\n WHERE element_id='{$translated_tax_ids[$taxonomy][$k]}' AND element_type='tax_{$taxonomy}'"); // before adding the new term make sure that another tag with the same name doesn't exist. If it does append @lang // same term name exists in a different language? $term_different_language = $wpdb->get_var("\r\n SELECT tm.term_id \r\n FROM {$wpdb->term_taxonomy} tx\r\n JOIN {$wpdb->terms} tm ON tx.term_id = tm.term_id \r\n JOIN {$wpdb->prefix}icl_translations tr ON tx.term_taxonomy_id = tr.element_id\r\n WHERE tm.name='" . $wpdb->escape($v) . "' AND tr.element_type LIKE 'tax\\_%' AND tr.language_code <> '{$lang_code}'\r\n "); if ($term_different_language) { $v .= ' @' . $lang_code; } //tax exists? (in the current language) $etag = get_term_by('name', htmlspecialchars($v), $taxonomy); if (!$etag) { $etag = get_term_by('name', htmlspecialchars($v) . ' @' . $lang_code, $taxonomy); } if (!$etag) { // get original category parent id $original_t_parent_id = $wpdb->get_var($wpdb->prepare("SELECT parent FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id=%d", $translated_tax_ids[$taxonomy][$k])); if ($original_t_parent_id) { $_op_tax_id = $wpdb->get_var($wpdb->prepare("SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE taxonomy='category' AND term_id=%d", $original_t_parent_id)); $_op_trid = $wpdb->get_var($wpdb->prepare("SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_type='tax_category' AND element_id=%d", $_op_tax_id)); // get id of the translated category parent $_tp_tax_id = $wpdb->get_var($wpdb->prepare("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE language_code='{$lang_code}' AND trid=%d", $_op_trid)); if ($_tp_tax_id) { $t_parent_id = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->term_taxonomy} WHERE taxonomy='category' AND term_taxonomy_id=%d", $_tp_tax_id)); } else { $t_parent_id = 0; } } else { $t_parent_id = 0; } $tmp = wp_insert_term($v, $taxonomy); if (!is_wp_error($tmp) && isset($tmp['term_taxonomy_id'])) { $wpdb->update($wpdb->prefix . 'icl_translations', array('language_code' => $lang_code, 'trid' => $tax_trid, 'source_language_code' => $original_post_details->language_code), array('element_type' => 'tax_' . $taxonomy, 'element_id' => $tmp['term_taxonomy_id'])); // if this is a parent category, make sure that nesting is correct for all translations $orig_tax_id = $wpdb->get_var($wpdb->prepare("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid=%d AND source_language_code IS NULL", $tax_trid)); $orig_term_id = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id=%d AND taxonomy='{$taxonomy}'", $orig_tax_id)); $orig_tax_children = $wpdb->get_col($wpdb->prepare("SELECT term_id FROM {$wpdb->term_taxonomy} WHERE parent=%d", $orig_term_id)); if (!empty($orig_tax_children)) { foreach ($orig_tax_children as $ch) { $_tr_child = icl_object_id($ch, $taxonomy, false, $lang_code); if ($_tr_child) { $wpdb->update($wpdb->term_taxonomy, array('parent' => $tmp['term_id']), array('taxonomy' => $taxonomy, 'term_id' => $_tr_child)); } } } delete_option($taxonomy . '_children'); } } else { $term_taxonomy_id = $etag->term_taxonomy_id; // check whether we have an orphan translation - the same trid and language but a different element id $__translation_id = $wpdb->get_var("\r\n SELECT translation_id FROM {$wpdb->prefix}icl_translations \r\n WHERE trid = '{$tax_trid}' \r\n AND language_code = '{$lang_code}' \r\n AND element_id <> '{$term_taxonomy_id}'\r\n "); if ($__translation_id) { $wpdb->query("DELETE FROM {$wpdb->prefix}icl_translations WHERE translation_id={$__translation_id}"); } $tax_translation_id = $wpdb->get_var("\r\n SELECT translation_id FROM {$wpdb->prefix}icl_translations \r\n WHERE element_id={$term_taxonomy_id} AND element_type='tax_{$taxonomy}'"); if ($tax_translation_id) { $wpdb->update($wpdb->prefix . 'icl_translations', array('language_code' => $lang_code, 'trid' => $tax_trid, 'source_language_code' => $original_post_details->language_code), array('element_type' => 'tax_' . $taxonomy, 'translation_id' => $tax_translation_id)); } else { $wpdb->insert($wpdb->prefix . 'icl_translations', array('language_code' => $lang_code, 'trid' => $tax_trid, 'element_type' => 'tax_' . $taxonomy, 'element_id' => $term_taxonomy_id, 'source_language_code' => $original_post_details->language_code)); } } } } $oterms = wp_get_object_terms($translation['original_id'], $taxonomy); if (!is_wp_error($oterms)) { foreach ($oterms as $t) { $original_post_taxs[$taxonomy][] = $t->term_taxonomy_id; } } if (!empty($original_post_taxs[$taxonomy])) { $tax_trids = $wpdb->get_col("SELECT trid FROM {$wpdb->prefix}icl_translations \r\n WHERE element_type='tax_{$taxonomy}' AND element_id IN (" . join(',', $original_post_taxs[$taxonomy]) . ")"); if (!empty($tax_trids)) { $tax_tr_tts = $wpdb->get_col("SELECT element_id FROM {$wpdb->prefix}icl_translations \r\n WHERE element_type='tax_{$taxonomy}' AND language_code='{$lang_code}' AND trid IN (" . join(',', $tax_trids) . ")"); } if (!empty($tax_tr_tts)) { if ($wp_taxonomies[$taxonomy]->hierarchical) { $translated_tax_ids[$taxonomy] = $wpdb->get_col("SELECT term_id FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id IN (" . join(',', $tax_tr_tts) . ")"); } else { $translated_taxs[$taxonomy] = $wpdb->get_col("SELECT t.name FROM {$wpdb->terms} t \r\n JOIN {$wpdb->term_taxonomy} tx ON tx.term_id = t.term_id \r\n WHERE tx.taxonomy='{$taxonomy}' AND tx.term_taxonomy_id IN (" . join(',', $tax_tr_tts) . ")"); } } } } } // handle the page parent and set it to the translated parent if we have one. if ($original_post_details->post_parent) { $post_parent_trid = $wpdb->get_var("SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_type='post_{$original_post_details->post_type}' AND element_id='{$original_post_details->post_parent}'"); if ($post_parent_trid) { $parent_id = $wpdb->get_var("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE element_type='post_{$original_post_details->post_type}' AND trid='{$post_parent_trid}' AND language_code='{$lang_code}'"); } } // determine post id based on trid $post_id = $tinfo->element_id; if ($post_id) { // see if the post really exists - make sure it wasn't deleted while the plugin was if (!$wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE ID={$post_id}")) { $is_update = false; $wpdb->query("DELETE FROM {$wpdb->prefix}icl_translations WHERE element_type='post_{$original_post_details->post_type}' AND element_id={$post_id}"); } else { $is_update = true; $postarr['ID'] = $_POST['post_ID'] = $post_id; } } else { $is_update = false; } $postarr['post_title'] = $translation['title']; if ($sitepress_settings['translated_document_page_url'] == 'translate' && isset($translation['URL'])) { $postarr['post_name'] = $translation['URL']; } $postarr['post_content'] = $translation['body']; if (isset($translation['excerpt']) && $translation['excerpt'] != "") { $postarr['post_excerpt'] = $translation['excerpt']; } if (@is_array($translated_tags)) { $postarr['tags_input'] = join(',', (array) $translated_tags); } if (@is_array($translated_taxs)) { foreach ($translated_taxs as $taxonomy => $values) { $postarr['tax_input'][$taxonomy] = join(',', (array) $values); } } if (@is_array($translated_tax_ids)) { $postarr['tax_input'] = $translated_tax_ids; } if (isset($translated_cats_ids)) { $postarr['post_category'] = $translated_cats_ids; } $postarr['post_author'] = $original_post_details->post_author; $postarr['post_type'] = $original_post_details->post_type; if ($sitepress_settings['sync_comment_status']) { $postarr['comment_status'] = $original_post_details->comment_status; } if ($sitepress_settings['sync_ping_status']) { $postarr['ping_status'] = $original_post_details->ping_status; } if ($sitepress_settings['sync_page_ordering']) { $postarr['menu_order'] = $original_post_details->menu_order; } if ($sitepress_settings['sync_private_flag'] && $original_post_details->post_status == 'private') { $postarr['post_status'] = 'private'; } if (!$is_update) { $postarr['post_status'] = !$sitepress_settings['translated_document_status'] ? 'draft' : $original_post_details->post_status; } else { // set post_status to the current post status. $postarr['post_status'] = $wpdb->get_var("SELECT post_status FROM {$wpdb->prefix}posts WHERE ID = " . $post_id); } if ($sitepress_settings['sync_post_date']) { $postarr['post_date'] = $original_post_details->post_date; } if (isset($parent_id) && $sitepress_settings['sync_page_parent']) { $_POST['post_parent'] = $postarr['post_parent'] = $parent_id; $_POST['parent_id'] = $postarr['parent_id'] = $parent_id; } if ($is_update) { $postarr['post_name'] = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM {$wpdb->posts} WHERE ID=%d", $post_id)); } $_POST['trid'] = $trid; $_POST['lang'] = $lang_code; $_POST['skip_sitepress_actions'] = true; global $wp_rewrite; if (!isset($wp_rewrite)) { $wp_rewrite = new WP_Rewrite(); } kses_remove_filters(); $postarr = apply_filters('icl_pre_save_pro_translation', $postarr); $new_post_id = wp_insert_post($postarr); do_action('icl_pro_translation_saved', $new_post_id); // associate custom taxonomies by hand if (!empty($postarr['tax_input'])) { foreach ($postarr['tax_input'] as $taxonomy => $tags) { if ($wp_taxonomies[$taxonomy]->hierarchical) { wp_set_post_terms($new_post_id, $tags, $taxonomy); } else { wp_set_post_terms($new_post_id, $translated_taxs[$taxonomy], $taxonomy); } } } // set stickiness if ($is_original_sticky && $sitepress_settings['sync_sticky_flag']) { stick_post($new_post_id); } else { if ($original_post_details->post_type == 'post' && $is_update) { unstick_post($new_post_id); //just in case - if this is an update and the original post stckiness has changed since the post was sent to translation } } foreach ((array) $sitepress_settings['translation-management']['custom_fields_translation'] as $cf => $op) { if ($op == 1) { $sitepress->_sync_custom_field($translation['original_id'], $new_post_id, $cf); } elseif ($op == 2 && isset($translation['field-' . $cf])) { $field_translation = $translation['field-' . $cf]; $field_type = $translation['field-' . $cf . '-type']; if ($field_type == 'custom_field') { $field_translation = str_replace('�A;', "\n", $field_translation); // always decode html entities eg decode & to & $field_translation = html_entity_decode($field_translation); update_post_meta($new_post_id, $cf, $field_translation); } } } // set specific custom fields $copied_custom_fields = array('_top_nav_excluded', '_cms_nav_minihome'); foreach ($copied_custom_fields as $ccf) { $val = get_post_meta($translation['original_id'], $ccf, true); update_post_meta($new_post_id, $ccf, $val); } // sync _wp_page_template if ($sitepress_settings['sync_page_template']) { $_wp_page_template = get_post_meta($translation['original_id'], '_wp_page_template', true); update_post_meta($new_post_id, '_wp_page_template', $_wp_page_template); } if (!$new_post_id) { return false; } if (!$is_update) { $wpdb->update($wpdb->prefix . 'icl_translations', array('element_id' => $new_post_id), array('translation_id' => $translation_id)); } update_post_meta($new_post_id, '_icl_translation', 1); TranslationManagement::set_page_url($new_post_id); global $iclTranslationManagement; $ts = array('status' => ICL_TM_COMPLETE, 'needs_update' => 0, 'translation_id' => $translation_id); $translator_id = $wpdb->get_var($wpdb->prepare("SELECT translator_id FROM {$wpdb->prefix}icl_translation_status WHERE translation_id=%d", $translation_id)); if (!$translator_id) { foreach ($sitepress_settings['icl_lang_status'] as $lpair) { if ($lpair['from'] == $original_post_details->language_code && $lpair['to'] == $lang_code && isset($lpair['translators'][0]['id'])) { $ts['translator_id'] = $lpair['translators'][0]['id']; break; } } } // update translation status $iclTranslationManagement->update_translation_status($ts); // add new translation job //$translation_package = $iclTranslationManagement->create_translation_package(get_post($translation['original_id'])); //$job_id = $iclTranslationManagement->add_translation_job($tinfo->rid, $tinfo->translator_id, $translation_package); $job_id = $iclTranslationManagement->get_translation_job_id($trid, $lang_code); // save the translation $iclTranslationManagement->mark_job_done($job_id); $parts = explode('_', $translation['original_id']); if ($parts[0] != 'external') { $iclTranslationManagement->save_job_fields_from_post($job_id, get_post($new_post_id)); $this->_content_fix_links_to_translated_content($new_post_id, $lang_code, "post_{$original_post_details->post_type}"); if (function_exists('icl_st_fix_links_in_strings')) { icl_st_fix_links_in_strings($new_post_id); } // Now try to fix links in other translated content that may link to this post. $sql = "SELECT\r\n tr.element_id\r\n FROM\r\n {$wpdb->prefix}icl_translations tr\r\n JOIN\r\n {$wpdb->prefix}icl_translation_status ts\r\n ON\r\n tr.translation_id = ts.translation_id\r\n WHERE\r\n ts.links_fixed = 0 AND tr.element_type = 'post_{$original_post_details->post_type}' AND tr.language_code = '{$lang_code}' AND tr.element_id IS NOT NULL"; $needs_fixing = $wpdb->get_results($sql); foreach ($needs_fixing as $id) { if ($id->element_id != $new_post_id) { // fix all except the new_post_id. We have already done this. $this->_content_fix_links_to_translated_content($id->element_id, $lang_code, "post_{$original_post_details->post_type}"); } } // if this is a parent page then make sure it's children point to this. $this->fix_translated_children($translation['original_id'], $new_post_id, $lang_code); } return true; }
function save_post_translation($translation_id, $translation) { global $wpdb, $sitepress_settings, $sitepress, $icl_adjust_id_url_filter_off; $icl_adjust_id_url_filter_off = true; $translation_info = $wpdb->get_row($wpdb->prepare("\n SELECT * FROM {$wpdb->prefix}icl_translations tr\n JOIN {$wpdb->prefix}icl_translation_status ts ON ts.translation_id = tr.translation_id\n WHERE tr.translation_id=%d", $translation_id)); $lang_code = $translation_info->language_code; $trid = $translation_info->trid; $original_post_details = $wpdb->get_row("\n SELECT p.post_author, p.post_type, p.post_status, p.comment_status, p.ping_status, p.post_parent, p.menu_order, p.post_date, t.language_code\n FROM {$wpdb->prefix}icl_translations t \n JOIN {$wpdb->posts} p ON t.element_id = p.ID AND CONCAT('post_',p.post_type) = t.element_type\n WHERE trid='{$trid}' AND p.ID = '{$translation['original_id']}'\n "); //is the original post a sticky post? $sticky_posts = get_option('sticky_posts'); $is_original_sticky = $original_post_details->post_type == 'post' && in_array($translation['original_id'], $sticky_posts); $this->_content_fix_image_paths_in_body($translation); $this->_content_fix_relative_link_paths_in_body($translation); $this->_content_decode_shortcodes($translation); // handle the page parent and set it to the translated parent if we have one. if ($original_post_details->post_parent) { $post_parent_trid = $wpdb->get_var($wpdb->prepare("\tSELECT trid\n\t\t\t\t\t\tFROM {$wpdb->prefix}icl_translations\n\t\t\t\t\t\tWHERE element_type= %s AND element_id = %d ", 'post_' . $original_post_details->post_type, $original_post_details->post_parent)); if ($post_parent_trid) { $parent_id = $wpdb->get_var($wpdb->prepare("SELECT element_id\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t FROM {$wpdb->prefix}icl_translations\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t WHERE element_type = %s\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t AND trid = %d\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t AND language_code = %s ", 'post_' . $original_post_details->post_type, $post_parent_trid, $lang_code)); } } // determine post id based on trid $post_id = $translation_info->element_id; if ($post_id) { // see if the post really exists - make sure it wasn't deleted while the plugin was if (!$wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE ID = %d ", $post_id))) { $is_update = false; $q = "DELETE FROM {$wpdb->prefix}icl_translations WHERE element_type=%s AND element_id=%d"; $q_prepared = $wpdb->prepare($q, array('post_' . $original_post_details->post_type, $post_id)); $wpdb->query($q_prepared); } else { $is_update = true; $postarr['ID'] = $_POST['post_ID'] = $post_id; } } else { $is_update = false; } $postarr['post_title'] = $translation['title']; if ($sitepress_settings['translated_document_page_url'] == 'translate' && isset($translation['URL'])) { $postarr['post_name'] = $translation['URL']; } $postarr['post_content'] = $translation['body']; if (isset($translation['excerpt']) && $translation['excerpt'] != "") { $postarr['post_excerpt'] = $translation['excerpt']; } if (isset($translated_taxonomies) && is_array($translated_taxonomies)) { foreach ($translated_taxonomies as $taxonomy => $values) { $postarr['tax_input'][$taxonomy] = join(',', (array) $values); } } $postarr['post_author'] = $original_post_details->post_author; $postarr['post_type'] = $original_post_details->post_type; if ($sitepress_settings['sync_comment_status']) { $postarr['comment_status'] = $original_post_details->comment_status; } if ($sitepress_settings['sync_ping_status']) { $postarr['ping_status'] = $original_post_details->ping_status; } if ($sitepress_settings['sync_page_ordering']) { $postarr['menu_order'] = $original_post_details->menu_order; } if ($sitepress_settings['sync_private_flag'] && $original_post_details->post_status == 'private') { $postarr['post_status'] = 'private'; } if (!$is_update) { $postarr['post_status'] = !$sitepress_settings['translated_document_status'] ? 'draft' : $original_post_details->post_status; } else { // set post_status to the current post status. $postarr['post_status'] = $wpdb->get_var($wpdb->prepare("SELECT post_status\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t FROM {$wpdb->prefix}posts\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t WHERE ID = %d ", $post_id)); } if ($sitepress_settings['sync_post_date']) { $postarr['post_date'] = $original_post_details->post_date; } if (isset($parent_id) && $sitepress_settings['sync_page_parent']) { $_POST['post_parent'] = $postarr['post_parent'] = $parent_id; $_POST['parent_id'] = $postarr['parent_id'] = $parent_id; } if ($is_update) { $postarr['post_name'] = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM {$wpdb->posts} WHERE ID=%d", $post_id)); } $_POST['trid'] = $trid; $_POST['lang'] = $lang_code; $_POST['skip_sitepress_actions'] = true; global $wp_rewrite; if (!isset($wp_rewrite)) { $wp_rewrite = new WP_Rewrite(); } kses_remove_filters(); $postarr = apply_filters('icl_pre_save_pro_translation', $postarr); $new_post_id = wp_insert_post($postarr); do_action('icl_pro_translation_saved', $new_post_id); // set stickiness if ($is_original_sticky && $sitepress_settings['sync_sticky_flag']) { stick_post($new_post_id); } else { if ($original_post_details->post_type == 'post' && $is_update) { unstick_post($new_post_id); //just in case - if this is an update and the original post stckiness has changed since the post was sent to translation } } foreach ((array) $sitepress_settings['translation-management']['custom_fields_translation'] as $cf => $op) { if ($op == 1) { $sitepress->_sync_custom_field($translation['original_id'], $new_post_id, $cf); } elseif ($op == 2 && isset($translation['field-' . $cf])) { $field_translation = $translation['field-' . $cf]; $field_type = $translation['field-' . $cf . '-type']; if ($field_type == 'custom_field') { $field_translation = str_replace('�A;', "\n", $field_translation); // always decode html entities eg decode & to & $field_translation = html_entity_decode($field_translation); update_post_meta($new_post_id, $cf, $field_translation); } } } // set specific custom fields $copied_custom_fields = array('_top_nav_excluded', '_cms_nav_minihome'); foreach ($copied_custom_fields as $ccf) { $val = get_post_meta($translation['original_id'], $ccf, true); update_post_meta($new_post_id, $ccf, $val); } // sync _wp_page_template if ($sitepress_settings['sync_page_template']) { $_wp_page_template = get_post_meta($translation['original_id'], '_wp_page_template', true); update_post_meta($new_post_id, '_wp_page_template', $_wp_page_template); } // sync post format if ($sitepress_settings['sync_post_format']) { $_wp_post_format = get_post_format($translation['original_id']); set_post_format($new_post_id, $_wp_post_format); } if (!$new_post_id) { return false; } if (!$is_update) { $wpdb->update($wpdb->prefix . 'icl_translations', array('element_id' => $new_post_id), array('translation_id' => $translation_id)); } update_post_meta($new_post_id, '_icl_translation', 1); TranslationManagement::set_page_url($new_post_id); global $iclTranslationManagement; $ts = array('status' => ICL_TM_COMPLETE, 'needs_update' => 0, 'translation_id' => $translation_id); $translator_id = $wpdb->get_var($wpdb->prepare("SELECT translator_id FROM {$wpdb->prefix}icl_translation_status WHERE translation_id=%d", $translation_id)); if (!$translator_id) { $lang_status = TranslationProxy_Translator::get_language_pairs(); foreach ($lang_status as $languages_pair) { if ($languages_pair['from'] == $original_post_details->language_code && $languages_pair['to'] == $lang_code && isset($languages_pair['translators'][0]['id'])) { $ts['translator_id'] = $languages_pair['translators'][0]['id']; break; } } } // update translation status $iclTranslationManagement->update_translation_status($ts); // add new translation job //$translation_package = $iclTranslationManagement->create_translation_package(get_post($translation['original_id'])); //$job_id = $iclTranslationManagement->add_translation_job($translation_info->rid, $translation_info->translator_id, $translation_package); $job_id = $iclTranslationManagement->get_translation_job_id($trid, $lang_code); // save the translation $iclTranslationManagement->mark_job_done($job_id); $parts = explode('_', $translation['original_id']); if ($parts[0] != 'external') { $iclTranslationManagement->save_job_fields_from_post($job_id, get_post($new_post_id)); $this->_content_fix_links_to_translated_content($new_post_id, $lang_code, "post_{$original_post_details->post_type}"); // Now try to fix links in other translated content that may link to this post. $sql = "SELECT\n tr.element_id\n FROM\n {$wpdb->prefix}icl_translations tr\n JOIN\n {$wpdb->prefix}icl_translation_status ts\n ON\n tr.translation_id = ts.translation_id\n WHERE\n ts.links_fixed = 0 AND tr.element_type = %s AND tr.language_code = %s AND tr.element_id IS NOT NULL"; $sql_prepared = $wpdb->prepare($sql, array('post_' . $original_post_details->post_type, $lang_code)); $needs_fixing = $wpdb->get_results($sql_prepared); foreach ($needs_fixing as $id) { if ($id->element_id != $new_post_id) { // fix all except the new_post_id. We have already done this. $this->_content_fix_links_to_translated_content($id->element_id, $lang_code, "post_{$original_post_details->post_type}"); } } // if this is a parent page then make sure it's children point to this. $this->fix_translated_children($translation['original_id'], $new_post_id, $lang_code); } WPML_Translation_Job_Terms::save_terms_from_job($job_id, $lang_code); do_action('icl_pro_translation_completed', $new_post_id); return true; }