/**
  * @param $item_type_name
  * @param $item_type
  * @param $strings_basket_items
  * @param $translators
  * @param $basket_name
  */
 public static function send_strings_jobs($item_type_name, $item_type, $strings_basket_items, $translators, $basket_name)
 {
     /** @var $iclTranslationManagement TranslationManagement */
     global $iclTranslationManagement;
     $strings_local = array();
     if (!empty($strings_basket_items)) {
         // for every string in cart
         // collect strings for local translation
         // collect string for remote translation
         $strings_remote = array();
         foreach ($strings_basket_items as $basket_item_id => $basket_item) {
             foreach ($basket_item['to_langs'] as $language_code => $action) {
                 if (is_numeric($translators[$language_code])) {
                     $strings_local[$language_code][] = $basket_item_id;
                 } else {
                     $strings_remote[$language_code][] = $basket_item_id;
                 }
             }
         }
         if ($strings_remote) {
             foreach ($strings_remote as $target => $string_ids) {
                 $result = self::send_strings_to_translation_service($string_ids, $target, $basket_name, $translators[$target]);
                 if (isset($result['errors']) && count($result['errors'])) {
                     foreach ($result['errors'] as $error) {
                         $error_message = array('type' => 'error', 'text' => $error);
                         $iclTranslationManagement->add_message($error_message);
                     }
                 }
                 if (!$result) {
                     foreach ($string_ids as $string_id) {
                         $default_string_language = TranslationProxy_Basket::get_source_language();
                         $string = icl_get_string_by_id($string_id, $default_string_language);
                         $message = array('type' => 'error', 'text' => sprintf(__('String "%s" has not been sent.', 'wpml-translation-management'), $string));
                         $iclTranslationManagement->add_message($message);
                     }
                     break;
                 }
             }
         }
         foreach ($strings_local as $target => $string_ids) {
             self::translation_send_strings_local($string_ids, $target, $translators[$target], $basket_name);
         }
     }
 }
 /**
  * Incorporates strings in cart data
  *
  * @param array       $strings
  * @param bool|string $source_language
  *
  * @return boolean | array
  */
 function get_translation_jobs_basket_strings($strings, $source_language = false)
 {
     $final_strings = array();
     if (class_exists('WPML_String_Translation')) {
         global $sitepress;
         $source_language = $source_language ? $source_language : TranslationProxy_Basket::get_source_language();
         foreach ($strings as $string_id => $data) {
             if ($source_language) {
                 // set post_id
                 $final_strings[$string_id] = false;
                 // set post_title
                 $final_strings[$string_id]['post_title'] = icl_get_string_by_id($string_id);
                 // set post_type
                 $final_strings[$string_id]['post_type'] = 'string';
                 // set from_lang
                 $final_strings[$string_id]['from_lang'] = $source_language;
                 $final_strings[$string_id]['from_lang_string'] = ucfirst($sitepress->get_display_language_name($source_language, $sitepress->get_admin_language()));
                 // set to_langs
                 $final_strings[$string_id]['to_langs'] = $data['to_langs'];
                 // set comma separated to_langs -> to_langs_string
                 // set comma separated to_langs -> to_langs_string
                 $language_names = array();
                 foreach ($final_strings[$string_id]['to_langs'] as $language_code => $value) {
                     $language_names[] = ucfirst($sitepress->get_display_language_name($language_code, $sitepress->get_admin_language()));
                 }
                 $final_strings[$string_id]['to_langs_string'] = implode(", ", $language_names);
             }
         }
     }
     return $final_strings;
 }
 private function get_source_language()
 {
     return TranslationProxy_Basket::get_source_language();
 }
    /**
     * @param $translation_action
     * @param $source_language
     *
     * @return bool
     */
    private function validate_basket_package_item($translation_action, $source_language)
    {
        ICL_AdminNotifier::remove_message('the_basket_items_notification');
        $package = $this->package;
        $package_id = $package->ID;
        $basket = TranslationProxy_Basket::get_basket();
        $package_is_valid = true;
        $basket_source_language = TranslationProxy_Basket::get_source_language();
        if ($basket && $basket_source_language) {
            if ($source_language != $basket_source_language) {
                TranslationProxy_Basket::add_message(array('type' => 'update', 'text' => __('You cannot add packages in this language to the basket since it already contains posts, packages or strings of another source language!
					Either submit the current basket and then add the post or delete the posts of differing language in the current basket', 'sitepress')));
                TranslationProxy_Basket::update_basket();
                $package_is_valid = false;
            }
        }
        if (!$translation_action) {
            TranslationProxy_Basket::add_message(array('type' => 'error', 'text' => __('Please select at least one language to translate into.', 'sitepress')));
            $package_is_valid = false;
        }
        if (!$package_id) {
            TranslationProxy_Basket::add_message(array('type' => 'error', 'text' => __('Please select at least one document to translate.', 'sitepress')));
            $package_is_valid = false;
            return $package_is_valid;
        }
        return $package_is_valid;
    }
 private function build_dashboard_filter_arguments()
 {
     global $sitepress, $iclTranslationManagement;
     $this->current_language = $sitepress->get_current_language();
     $this->source_language = TranslationProxy_Basket::get_source_language();
     if (isset($_SESSION['translation_dashboard_filter'])) {
         $this->translation_filter = $_SESSION['translation_dashboard_filter'];
     }
     if ($this->source_language || !isset($this->translation_filter['from_lang'])) {
         if ($this->source_language) {
             $this->translation_filter['from_lang'] = $this->source_language;
         } else {
             $this->translation_filter['from_lang'] = isset($_GET['lang']) ? $_GET['lang'] : $this->current_language;
         }
     }
     if (!isset($this->translation_filter['to_lang'])) {
         $this->translation_filter['to_lang'] = isset($_GET['to_lang']) ? $_GET['to_lang'] : '';
     }
     if ($this->translation_filter['to_lang'] == $this->translation_filter['from_lang']) {
         $this->translation_filter['to_lang'] = false;
     }
     if (!isset($this->translation_filter['tstatus'])) {
         $this->translation_filter['tstatus'] = isset($_GET['tstatus']) ? $_GET['tstatus'] : -1;
         // -1 == All documents
     }
     if (!isset($this->translation_filter['sort_by']) || !$this->translation_filter['sort_by']) {
         $this->translation_filter['sort_by'] = 'date';
     }
     if (!isset($this->translation_filter['sort_order']) || !$this->translation_filter['sort_order']) {
         $this->translation_filter['sort_order'] = 'DESC';
     }
     $sort_order_next = $this->translation_filter['sort_order'] == 'ASC' ? 'DESC' : 'ASC';
     $this->dashboard_title_sort_link = 'admin.php?page=' . WPML_TM_FOLDER . '/menu/main.php&sm=dashboard&icl_tm_action=sort&sort_by=title&sort_order=' . $sort_order_next;
     $this->dashboard_date_sort_link = 'admin.php?page=' . WPML_TM_FOLDER . '/menu/main.php&sm=dashboard&icl_tm_action=sort&sort_by=date&sort_order=' . $sort_order_next;
     $this->post_statuses = array('publish' => __('Published', 'wpml-translation-management'), 'draft' => __('Draft', 'wpml-translation-management'), 'pending' => __('Pending Review', 'wpml-translation-management'), 'future' => __('Scheduled', 'wpml-translation-management'), 'private' => __('Private', 'wpml-translation-management'));
     $this->post_statuses = apply_filters('wpml_tm_dashboard_post_statuses', $this->post_statuses);
     // Get the document types that we can translate
     $this->post_types = $sitepress->get_translatable_documents();
     $this->post_types = apply_filters('wpml_tm_dashboard_translatable_types', $this->post_types);
     $this->build_external_types();
     $this->translation_filter['limit_no'] = isset($_GET['show_all']) && $_GET['show_all'] ? 10000 : ICL_TM_DOCS_PER_PAGE;
     if (!isset($this->translation_filter['parent_type'])) {
         $this->translation_filter['parent_type'] = 'any';
     }
     $this->selected_languages = array();
     if (!empty($iclTranslationManagement->dashboard_select)) {
         $this->selected_posts = $iclTranslationManagement->dashboard_select['post'];
         $this->selected_languages = $iclTranslationManagement->dashboard_select['translate_to'];
     }
     if (isset($this->translation_filter['icl_selected_posts'])) {
         parse_str($this->translation_filter['icl_selected_posts'], $this->selected_posts);
     }
     $this->filter_post_status = isset($this->translation_filter['status']) ? $this->translation_filter['status'] : false;
     if (isset($_GET['type'])) {
         $this->translation_filter['type'] = $_GET['type'];
     }
     $this->filter_translation_type = isset($this->translation_filter['type']) ? $this->translation_filter['type'] : false;
 }
    public static function display_string_menu($lang_filter)
    {
        global $sitepress;
        $target_status = array();
        $target_rate = array();
        $lang_status = $sitepress->get_setting('icl_lang_status');
        $strings_target_languages = $sitepress->get_active_languages();
        if ($lang_status) {
            foreach ($lang_status as $lang) {
                if ($lang['from'] == $sitepress->get_current_language()) {
                    $target_status[$lang['to']] = $lang['have_translators'];
                    $target_rate[$lang['to']] = $lang['max_rate'];
                }
            }
        }
        ?>
		<form method="post" id="icl_st_send_strings" name="icl_st_send_strings"
		      action="">
			<input type="hidden" name="icl_st_action" value="send_strings"/>
			<input type="hidden" name="strings" value=""/>
			<input type="hidden" name="icl-tr-from"
			       value="<?php 
        echo $lang_filter;
        ?>
"/>
			<input type="hidden" name="icl-basket-language"
			       value="<?php 
        echo TranslationProxy_Basket::get_source_language();
        ?>
"/>

			<table id="icl-tr-opt" class="widefat fixed" cellspacing="0"
			       style="width:100%">
				<thead>
				<tr>
					<th><?php 
        _e('Translation options', 'wpml-translation-management');
        ?>
</th>
				</tr>
				</thead>
				<tbody>
				<tr>
					<td>
						<ul id="icl_tm_languages">
							<?php 
        foreach ($strings_target_languages as $lang) {
            if ($lang['code'] == $lang_filter) {
                continue;
            }
            $is_active_language = $sitepress->is_active_language($lang['code']);
            $checked = checked(true, $is_active_language, false);
            $label_class = $is_active_language ? 'active' : 'non-active';
            ?>
								<li>
									<input type="checkbox"
									       id="translate_to[<?php 
            echo $lang['code'];
            ?>
]"
									       name="translate_to[<?php 
            echo $lang['code'];
            ?>
]"
									       value="1"
									       id="icl_st_translate_to_<?php 
            echo $lang['code'];
            ?>
" <?php 
            echo $checked;
            ?>
									       data-language="<?php 
            echo $lang['code'];
            ?>
"
									/>
									<label
										for="translate_to[<?php 
            echo $lang['code'];
            ?>
]"
										class="<?php 
            echo $label_class;
            ?>
">
										<?php 
            printf(__('Translate to %s', 'wpml-translation-management'), $lang['display_name']);
            ?>
									</label>
									<?php 
            if (isset($target_status[$lang['code']]) && $target_status[$lang['code']]) {
                ?>
										<span style="display: none;"
										      id="icl_st_max_rate_<?php 
                echo $lang['code'];
                ?>
"><?php 
                echo $target_rate[$lang['code']];
                ?>
</span>
										<span style="display: none;"
										      id="icl_st_estimate_<?php 
                echo $lang['code'];
                ?>
_wrap"
										      class="icl_st_estimate_wrap">
		                                    &nbsp;(<?php 
                printf(__('Estimated cost: %s USD', 'wpml-translation-management'), '<span id="icl_st_estimate_' . $lang['code'] . '">0</span>');
                ?>
											)</span>
										<?php 
            }
            ?>
								</li>
								<?php 
        }
        ?>
						</ul>
						<?php 
        echo wpml_nonce_field('icl-string-translation');
        ?>
						<input id="icl_send_strings" class="button-primary"
						       type="submit"
						       value="<?php 
        _e('Add to translation basket', 'wpml-translation-management');
        ?>
"
						       disabled="disabled"
						       data-lang-not-active-message="<?php 
        _e('One of the selected strings is in a language that is not activate. It can not be added to the translation basket.', 'wpml-translation-management');
        ?>
"
						       data-more-than-one-lang-message="<?php 
        _e('Strings in different languages are selected. They can not be added to the translation basket.', 'wpml-translation-management');
        ?>
"
						       data-translation-basket-lang-message="<?php 
        _e('You cannot add strings in this language to the basket since it already contains posts or strings of another source language! Either submit the current basket or delete the posts of differing language in the current basket', 'wpml-translation-management');
        ?>
"
						/>

						<div class="update-nag js-translation-message"
						     style="display:none"></div>

						<div style="width: 45%; margin: auto">
							<?php 
        ICL_AdminNotifier::display_messages('string-translation-under-translation-options');
        ICL_AdminNotifier::remove_message('items_added_to_basket');
        ?>
						</div>
					</td>
				</tr>
				</tbody>
			</table>
		</form>
		<?php 
    }
 /**
  * @param bool $source_language
  * @param      $target_languages
  *
  * @internal param bool $name
  * @return bool|TranslationProxy_Batch
  */
 public function create_batch_job($source_language, $target_languages)
 {
     $batch_name = TranslationProxy_Basket::get_basket_name();
     $extra_fields = TranslationProxy_Basket::get_basket_extra_fields();
     if (!$source_language) {
         $source_language = TranslationProxy_Basket::get_source_language();
     }
     if (!$target_languages) {
         $target_languages = TranslationProxy_Basket::get_remote_target_languages();
     }
     if (!$source_language || !$target_languages) {
         return false;
     }
     if (!$batch_name) {
         $batch_name = sprintf(__('%s: WPML Translation Jobs', 'wpml-translation-management'), get_option('blogname'));
     }
     TranslationProxy_Basket::set_basket_name($batch_name);
     $params = array("api_version" => TranslationProxy_Api::API_VERSION, "project_id" => $this->id, "accesskey" => $this->access_key, 'batch' => array('source_language' => $source_language, 'target_languages' => $target_languages, 'name' => $batch_name));
     if ($extra_fields) {
         $params['extra_fields'] = $extra_fields;
     }
     try {
         $response = TranslationProxy_Api::proxy_request('/projects/{project_id}/batches.json', $params, 'POST', false);
         $batch = false;
         if ($response) {
             $batch = $response->batch;
             TranslationProxy_Basket::set_batch_data($batch);
         }
         return $batch;
     } catch (Exception $ex) {
         $this->add_error($ex->getMessage());
         return false;
     }
 }
 function send_strings_to_translation_service($string_ids, $target_language, $basket_name, $translator_id)
 {
     global $wpdb;
     // get all the untranslated strings
     $untranslated = array();
     foreach ($string_ids as $st_id) {
         $untranslated[] = $st_id;
     }
     if (sizeof($untranslated) > 0) {
         $project = TranslationProxy::get_current_project();
         $strings = array();
         $word_count = 0;
         $source_language = TranslationProxy_Basket::get_source_language();
         foreach ($untranslated as $string_id) {
             $string_data_query = "SELECT id, context, name, value FROM {$wpdb->prefix}icl_strings WHERE id=%d";
             $string_data_prepare = $wpdb->prepare($string_data_query, $string_id);
             $string_data = $wpdb->get_row($string_data_prepare);
             $word_count += $this->estimate_word_count($string_data->value, $source_language);
             $strings[] = $string_data;
         }
         $xliff = new WPML_TM_xliff();
         $file = $xliff->get_strings_xliff_file($strings, $source_language, $target_language);
         $title = "String Translations";
         $cms_id = '';
         $url = '';
         $timestamp = date('Y-m-d H:i:s');
         $res = $project->send_to_translation_batch_mode($file, $title, $cms_id, $url, $source_language, $target_language, $word_count);
         if ($res) {
             foreach ($strings as $string_data) {
                 $batch_id = TranslationProxy_Batch::update_translation_batch($basket_name);
                 $translation_service = TranslationProxy_Service::get_translator_data_from_wpml($translator_id);
                 $added = icl_add_string_translation($string_data->id, $target_language, null, ICL_TM_WAITING_FOR_TRANSLATOR, $translation_service['translator_id'], $translation_service['translation_service'], $batch_id);
                 if ($added) {
                     $data = array('rid' => $res, 'string_translation_id' => $added, 'timestamp' => $timestamp, 'md5' => md5($string_data->value));
                     $wpdb->insert($wpdb->prefix . 'icl_string_status', $data);
                     //insert rid
                 } else {
                     $this->add_message(sprintf(__('Unable to add "%s" string in tables', 'sitepress'), $string_data->name), 'error');
                     return 0;
                 }
             }
             $wpdb->insert($wpdb->prefix . 'icl_core_status', array('rid' => $res, 'module' => '', 'origin' => $source_language, 'target' => $target_language, 'status' => CMS_REQUEST_WAITING_FOR_PROJECT_CREATION));
             if ($project->errors && count($project->errors)) {
                 $res['errors'] = $project->errors;
             }
             return $res;
         }
     }
     return 0;
 }