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);
                     }
                 }
             }
         }
     }
 }
 /**
  * @param int   $user_id
  * @param array $args
  *
  * @return bool
  */
 function is_translator($user_id, $args = array())
 {
     $admin_override = true;
     extract($args, EXTR_OVERWRITE);
     $is_translator = $this->sitepress->get_wp_api()->user_can($user_id, 'translate');
     // check if user is administrator and return true if he is
     if ($admin_override && $this->sitepress->get_wp_api()->user_can($user_id, 'manage_options')) {
         $is_translator = true;
     } else {
         if (isset($lang_from) && isset($lang_to)) {
             $user_language_pairs = $this->get_language_pairs($user_id);
             if (!empty($user_language_pairs)) {
                 foreach ($user_language_pairs as $user_lang_from => $user_lang_to) {
                     if (array_key_exists($lang_to, $user_lang_to)) {
                         $is_translator = true;
                         break;
                     }
                 }
             } else {
                 $is_translator = false;
             }
         }
         if (isset($job_id)) {
             $job_record = $this->tm_records->icl_translate_job_by_job_id($job_id);
             $translator_id = in_array($job_record->service(), array('local', 0)) ? $job_record->translator_id() : -1;
             $is_translator = $translator_id == $user_id || $is_translator && empty($translator_id);
         }
     }
     return apply_filters('wpml_override_is_translator', $is_translator, $user_id, $args);
 }
 /**
  * WPML_TM_ICL_Translations constructor.
  *
  * @throws InvalidArgumentException if given data does not correspond to a
  * record in icl_translations
  *
  * @param WPML_TM_Records $tm_records
  * @param int|array       $id
  * @param string          $type translation id, trid_lang or id_prefix for now
  */
 public function __construct(&$tm_records, $id, $type = 'translation_id')
 {
     $this->wpdb = $tm_records->wpdb();
     parent::__construct($tm_records);
     if ($id > 0 && $type === 'translation_id') {
         $this->{$type} = $id;
     } elseif ($type === 'id_type_prefix' && isset($id['element_id']) && isset($id['type_prefix'])) {
         $this->select_translation_id(" element_id = %d AND element_type LIKE %s ", array($id['element_id'], $id['type_prefix'] . '%'));
     } elseif ($type === 'trid_lang' && isset($id['trid']) && isset($id['language_code'])) {
         $this->select_translation_id(" trid = %d AND language_code = %s ", array($id['trid'], $id['language_code']));
     } else {
         throw new InvalidArgumentException('Unknown column: ' . $type . ' or invalid id: ' . serialize($id));
     }
 }
 private function get_validation_results($job, $data_to_validate)
 {
     $is_valid = true;
     $original_post = $data_to_validate['original_post'];
     $element_type_prefix = $data_to_validate['type_prefix'];
     $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');
         }
     } elseif (!$this->tm_records->icl_translate_job_by_job_id($job->job_id)->is_open()) {
         $is_valid = false;
         $validation_default_results['messages'][] = __('This job cannot be edited anymore because a newer job for this element exists.', 'wpml-translation-management');
     }
     $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_default_results, $validation_results);
     if (!$is_valid && $validation_results['is_valid']) {
         $validation_results['is_valid'] = $is_valid;
     }
     return $validation_results;
 }
 /**
  * WPML_TM_ICL_Translations constructor.
  *
  * @throws InvalidArgumentException if given data does not correspond to a
  * record in icl_translations
  *
  * @param WPML_TM_Records $tm_records
  * @param int|array       $id
  * @param string          $type translation id, trid_lang or id_prefix for now
  */
 public function __construct(&$tm_records, $id, $type = 'translation_id')
 {
     $this->wpdb = $tm_records->wpdb();
     $this->post_translations = $tm_records->get_post_translations();
     $this->term_translations = $tm_records->get_term_translations();
     parent::__construct($tm_records);
     if ($id > 0 && $type === 'translation_id') {
         $this->{$type} = $id;
     } elseif ($type === 'id_type_prefix' && isset($id['element_id']) && isset($id['type_prefix'])) {
         $this->build_from_element_id($id);
     } elseif ($type === 'trid_lang' && isset($id['trid']) && isset($id['language_code'])) {
         $this->build_from_trid($id);
     } else {
         throw new InvalidArgumentException('Unknown column: ' . $type . ' or invalid id: ' . serialize($id));
     }
 }
 /**
  * @param int   $user_id
  * @param array $args
  *
  * @return bool
  */
 function is_translator($user_id, $args = array())
 {
     $admin_override = true;
     extract($args, EXTR_OVERWRITE);
     $is_translator = $this->sitepress->get_wp_api()->user_can($user_id, 'translate');
     // check if user is administrator and return true if he is
     if ($admin_override && $this->sitepress->get_wp_api()->user_can($user_id, 'activate_plugins')) {
         $is_translator = true;
     } else {
         if (isset($lang_from) && isset($lang_to)) {
             $um = $this->get_language_pairs($user_id);
             $is_translator = $is_translator && isset($um[$lang_from]) && isset($um[$lang_from][$lang_to]) && $um[$lang_from][$lang_to];
         }
         if (isset($job_id)) {
             $job_record = $this->tm_records->icl_translate_job_by_job_id($job_id);
             $translator_id = in_array($job_record->service(), array('local', 0)) ? $job_record->translator_id() : -1;
             $is_translator = $translator_id == $user_id || $is_translator && empty($translator_id);
         }
     }
     return apply_filters('wpml_override_is_translator', $is_translator, $user_id, $args);
 }
 /**
  *
  * @return $this
  */
 public function delete()
 {
     $this->tm_records->icl_translation_status_by_translation_id($this->translation_id)->delete();
     $this->wpdb->delete($this->wpdb->prefix . $this->table, $this->get_args());
     return $this;
 }
 /**
  * @param int $translation_id
  *
  * @return bool|stdClass|WPML_Element_Translation_Job
  */
 public function job_by_translation_id($translation_id)
 {
     $row = $this->tm_records->icl_translations_by_translation_id($translation_id);
     return $row ? $this->get_translation_job($this->job_id_by_trid_and_lang($row->trid(), $row->language_code()), false, 0, true) : 0;
 }