public function save_post_actions($post_id, $post, $force_set_status = false)
 {
     global $wpdb, $sitepress, $current_user;
     $trid = isset($_POST['icl_trid']) && is_numeric($_POST['icl_trid']) ? $_POST['icl_trid'] : $sitepress->get_element_trid($post_id, 'post_' . $post->post_type);
     // set trid and lang code if front-end translation creating
     $trid = apply_filters('wpml_tm_save_post_trid_value', isset($trid) ? $trid : '', $post_id);
     $lang = apply_filters('wpml_tm_save_post_lang_value', isset($lang) ? $lang : '', $post_id);
     // is this the original document?
     $is_original = empty($trid) ? false : !(bool) $this->tm_records->icl_translations_by_element_id_and_type_prefix($post_id, 'post_' . $post->post_type)->source_language_code();
     if (!empty($trid) && !$is_original) {
         $lang = $lang ? $lang : $this->get_save_post_lang($lang, $post_id);
         $res = $wpdb->get_row($wpdb->prepare("\n\t\t\t SELECT element_id, language_code FROM {$wpdb->prefix}icl_translations WHERE trid=%d AND source_language_code IS NULL\n\t\t ", $trid));
         if ($res) {
             $original_post_id = $res->element_id;
             $from_lang = $res->language_code;
             $original_post = get_post($original_post_id);
             $md5 = $this->action_helper->post_md5($original_post);
             $translation_id = $this->tm_records->icl_translations_by_trid_and_lang($trid, $lang)->translation_id();
             $user_id = $current_user->ID;
             $this->maybe_add_as_translator($user_id, $lang, $from_lang);
             if ($translation_id) {
                 $translation_package = $this->action_helper->create_translation_package($original_post_id);
                 list($rid, $update) = $this->action_helper->get_tm_instance()->update_translation_status(array('translation_id' => $translation_id, 'status' => isset($force_set_status) && $force_set_status > 0 ? $force_set_status : ICL_TM_COMPLETE, 'translator_id' => $user_id, 'needs_update' => 0, 'md5' => $md5, 'translation_service' => 'local', 'translation_package' => serialize($translation_package)));
                 if (!$update) {
                     $job_id = $this->action_helper->add_translation_job($rid, $user_id, $translation_package);
                 } else {
                     $job_id_sql = "SELECT MAX(job_id) FROM {$wpdb->prefix}icl_translate_job WHERE rid=%d GROUP BY rid";
                     $job_id_prepared = $wpdb->prepare($job_id_sql, $rid);
                     $job_id = $wpdb->get_var($job_id_prepared);
                     $job_id = $job_id ? $job_id : $this->action_helper->add_translation_job($rid, $user_id, $translation_package);
                 }
                 // saving the translation
                 do_action('wpml_save_job_fields_from_post', $job_id);
             }
         }
     }
     if (!empty($trid) && empty($_POST['icl_minor_edit'])) {
         $is_original = false;
         $translations = $sitepress->get_element_translations($trid, 'post_' . $post->post_type);
         foreach ($translations as $translation) {
             if ($translation->original == 1 && $translation->element_id == $post_id) {
                 $is_original = true;
                 break;
             }
         }
         if ($is_original) {
             $md5 = $this->action_helper->post_md5($post_id);
             foreach ($translations as $translation) {
                 if (!$translation->original) {
                     $emd5 = $this->tm_records->icl_translation_status_by_translation_id($translation->translation_id)->md5();
                     if ($md5 !== $emd5) {
                         $translation_package = $this->action_helper->create_translation_package($post_id);
                         $data = array('translation_id' => $translation->translation_id, 'needs_update' => 1, 'md5' => $md5, 'translation_package' => serialize($translation_package));
                         $this->action_helper->get_tm_instance()->update_translation_status($data);
                     }
                 }
             }
         }
     }
 }
 public function save_post_actions($post_id, $post, $force_set_status = false)
 {
     global $wpdb, $sitepress, $current_user;
     if ($post->post_type == 'revision' || $post->post_status == 'auto-draft' || isset($_POST['autosave'])) {
         return;
     }
     $trid = isset($_POST['icl_trid']) && is_numeric($_POST['icl_trid']) ? $_POST['icl_trid'] : $sitepress->get_element_trid($post_id, 'post_' . $post->post_type);
     // set trid and lang code if front-end translation creating
     $trid = apply_filters('wpml_tm_save_post_trid_value', isset($trid) ? $trid : '', $post_id);
     $lang = apply_filters('wpml_tm_save_post_lang_value', isset($lang) ? $lang : '', $post_id);
     // is this the original document?
     $is_original = empty($trid) ? false : $wpdb->get_var($wpdb->prepare("\tSELECT source_language_code IS NULL\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFROM {$wpdb->prefix}icl_translations\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE element_id=%d\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tAND trid=%d LIMIT 1", $post_id, $trid));
     if (!empty($trid) && !$is_original) {
         $lang = $lang ? $lang : $this->get_save_post_lang($lang, $post_id);
         $res = $wpdb->get_row($wpdb->prepare("\n\t\t\t SELECT element_id, language_code FROM {$wpdb->prefix}icl_translations WHERE trid=%d AND source_language_code IS NULL\n\t\t ", $trid));
         if ($res) {
             $original_post_id = $res->element_id;
             $from_lang = $res->language_code;
             $original_post = get_post($original_post_id);
             $md5 = $this->action_helper->post_md5($original_post);
             $translation_id_prepared = $wpdb->prepare("SELECT translation_id FROM {$wpdb->prefix}icl_translations WHERE trid=%d AND language_code=%s LIMIT 1", $trid, $lang);
             $translation_id = $wpdb->get_var($translation_id_prepared);
             get_currentuserinfo();
             $user_id = $current_user->ID;
             $this->maybe_add_as_translator($user_id, $lang, $from_lang);
             if ($translation_id) {
                 $translation_package = $this->action_helper->create_translation_package($original_post_id);
                 list($rid, $update) = $this->action_helper->get_tm_instance()->update_translation_status(array('translation_id' => $translation_id, 'status' => isset($force_set_status) && $force_set_status > 0 ? $force_set_status : ICL_TM_COMPLETE, 'translator_id' => $user_id, 'needs_update' => 0, 'md5' => $md5, 'translation_service' => 'local', 'translation_package' => serialize($translation_package)));
                 if (!$update) {
                     $job_id = $this->action_helper->add_translation_job($rid, $user_id, $translation_package);
                 } else {
                     $job_id_sql = "SELECT MAX(job_id) FROM {$wpdb->prefix}icl_translate_job WHERE rid=%d GROUP BY rid";
                     $job_id_prepared = $wpdb->prepare($job_id_sql, $rid);
                     $job_id = $wpdb->get_var($job_id_prepared);
                     $job_id = $job_id ? $job_id : $this->action_helper->add_translation_job($rid, $user_id, $translation_package);
                 }
                 // saving the translation
                 do_action('wpml_save_job_fields_from_post', $job_id);
             }
         }
     }
     if (!empty($trid) && empty($_POST['icl_minor_edit'])) {
         $is_original = false;
         $translations = $sitepress->get_element_translations($trid, 'post_' . $post->post_type);
         foreach ($translations as $translation) {
             if ($translation->original == 1 && $translation->element_id == $post_id) {
                 $is_original = true;
                 break;
             }
         }
         if ($is_original) {
             $md5 = $this->action_helper->post_md5($post_id);
             foreach ($translations as $translation) {
                 if (!$translation->original) {
                     $emd5_sql = "SELECT md5 FROM {$wpdb->prefix}icl_translation_status WHERE translation_id = %d";
                     $emd5_prepared = $wpdb->prepare($emd5_sql, $translation->translation_id);
                     $emd5 = $wpdb->get_var($emd5_prepared);
                     if ($md5 !== $emd5) {
                         $translation_package = $this->action_helper->create_translation_package($post_id);
                         $data = array('translation_id' => $translation->translation_id, 'needs_update' => 1, 'md5' => $md5, 'translation_package' => serialize($translation_package));
                         $this->action_helper->get_tm_instance()->update_translation_status($data);
                     }
                 }
             }
         }
     }
     // sync copies/duplicates
     $duplicates = $this->action_helper->get_tm_instance()->get_duplicates($post_id);
     foreach ($duplicates as $lang => $_pid) {
         // Avoid infinite recursions
         if (!in_array($post_id . '|' . $lang, $this->duplicate_post_ids)) {
             $this->duplicate_post_ids[] = $post_id . '|' . $lang;
             $this->action_helper->get_tm_instance()->make_duplicate($post_id, $lang);
         }
     }
 }