/**
  * Serves Translation Dashboard form submission and adds posts to basket
  *
  * @param array $data data submitted from form
  *
  * @return boolean
  */
 public static function add_posts_to_basket($data)
 {
     self::get_basket();
     global $sitepress, $iclTranslationManagement, $wpml_translation_job_factory;
     extract($data, EXTR_OVERWRITE);
     ICL_AdminNotifier::remove_message('the_basket_items_notification');
     self::$translation_action = null;
     if (isset($data['tr_action'])) {
         //adapt new format
         self::$translation_action = $data['tr_action'];
     }
     if (!isset($data['tr_action']) && isset($data['translate_to'])) {
         //adapt new format
         $data['tr_action'] = $data['translate_to'];
         self::$translation_action = $data['tr_action'];
         unset($data['translate_to']);
     }
     self::$posts_ids = self::get_elements_ids($data, 'post');
     self::$translate_from = $data['translate_from'];
     // language of the submitted posts transported by hidden field
     $data_is_valid = self::validate_data($data);
     if (!$data_is_valid) {
         return false;
     }
     // check tr_action and do what user decided
     foreach (self::$translation_action as $language_code => $status) {
         $language_name = $sitepress->get_display_language_name($language_code);
         // if he decided duplicate or not to translate for this particular language,
         // try to remove it from wp_options
         $basket_item_type = 'post';
         if ($status == 2) {
             // iterate posts ids, check if they are in wp_options
             // if they are set to translate for this particular language
             // end then remove it
             foreach (self::$posts_ids as $id) {
                 if (isset(self::$basket[$basket_item_type][$id]['to_langs'][$language_code])) {
                     unset(self::$basket[$basket_item_type][$id]['to_langs'][$language_code]);
                 }
                 // if user want to duplicate this post, lets do this
                 if ($status == 2) {
                     $iclTranslationManagement->make_duplicate($id, $language_code);
                 }
             }
         } elseif ($status == 1) {
             foreach (self::$posts_ids as $id) {
                 $send_to_basket = true;
                 $post = self::get_post($id);
                 $post_type = $post->post_type;
                 $post_title = $post->post_title;
                 global $wpdb;
                 $source_language_code = $wpdb->get_var($wpdb->prepare("\tSELECT source_language_code\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_type LIKE 'post_%%'\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tAND element_id = %d", $post->ID));
                 if ($source_language_code != $language_code) {
                     $trid = $sitepress->get_element_trid($id, 'post_' . $post_type);
                     $job_id = $iclTranslationManagement->get_translation_job_id($trid, $language_code);
                     if ($job_id) {
                         /** @var stdClass $job_details */
                         $job_details = $wpml_translation_job_factory->get_translation_job($job_id);
                         if ($job_details->status == ICL_TM_IN_PROGRESS) {
                             self::$messages[] = array('type' => 'update', 'text' => sprintf(__('Post "%s" will be ignored for %s, because translation is already in progress.', 'wpml-translation-management'), $post_title, $language_name));
                             $send_to_basket = false;
                         } elseif ($job_details->status == ICL_TM_WAITING_FOR_TRANSLATOR) {
                             self::$messages[] = array('type' => 'update', 'text' => sprintf(__('Post "%s" will be ignored for %s, because translation is already waiting for translator.', 'wpml-translation-management'), $post_title, $language_name));
                             $send_to_basket = false;
                         }
                     }
                 } else {
                     self::$messages[] = array('type' => 'update', 'text' => sprintf(__('Post "%s" will be ignored for %s, because it is an original post.', 'wpml-translation-management'), $post_title, $language_name));
                     $send_to_basket = false;
                 }
                 if ($send_to_basket) {
                     self::$basket[$basket_item_type][$id]['from_lang'] = self::$translate_from;
                     self::$basket[$basket_item_type][$id]['to_langs'][$language_code] = 1;
                     // set basket language if not already set
                     if (!isset(self::$basket['source_language'])) {
                         self::$basket['source_language'] = self::$translate_from;
                     }
                 }
             }
         }
     }
     self::update_basket();
     return true;
 }