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; }
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')); } } }