/** * @param array $post clone of $_POST */ public function send_to_basket($post) { $post = stripslashes_deep($post); $string_ids = explode(',', $post['strings']); $translate_to = array(); foreach ($post['translate_to'] as $lang_to => $one) { $translate_to[$lang_to] = $lang_to; } if (!empty($translate_to)) { $this->basket->add_strings_to_basket($string_ids, $post['icl-tr-from'], $translate_to); } }
/** * Returns the name of the batch that contains the given post_id. * * @param int $post_id * * @return null|string */ private function get_batch_name($post_id) { global $wpdb; $name = $wpdb->get_var($wpdb->prepare("\tSELECT b.batch_name\n\t\t\t\tFROM {$wpdb->prefix}icl_translation_batches b\n\t\t\t\tJOIN {$wpdb->prefix}icl_translation_status s\n\t\t\t\t\tON s.batch_id = b.id\n\t\t\t\tJOIN {$wpdb->prefix}icl_translations t\n\t\t\t\t\tON t.translation_id = s.translation_id\n\t\t\t\tJOIN {$wpdb->prefix}icl_translations o\n\t\t\t\t\tON o.trid = t.trid\n\t\t\t\t\t\tAND o.language_code = t.source_language_code\n\t\t\t\tJOIN {$wpdb->posts} p\n\t\t\t\t\tON o.element_id = p.ID\n\t\t\t\t\t\tAND o.element_type = CONCAT('post_', p.post_type)\n\t\t\t\tWHERE o.element_id = %d\n\t\t\t\tORDER BY b.id\n\t\t\t\tLIMIT 1", $post_id)); $this->basket->set_name($name); return $name; }
/** * Creates the message that is shown before committing a batch. * * @see \WPML_Basket_Tab_Ajax::begin_basket_commit * * @param string $basket_name * * @return array */ private function create_remote_batch_message($basket_name) { if ($basket_name) { $this->basket->set_name($basket_name); } $basket = $this->basket->get_basket(); $basket_items_types = $this->basket->get_item_types(); if (!$basket) { $message_content = __('No items found in basket', 'sitepress'); } else { $total_count = 0; $message_content_details = ''; foreach ($basket_items_types as $item_type_name => $item_type) { if (isset($basket[$item_type_name])) { $count_item_type = count($basket[$item_type_name]); $total_count += $count_item_type; $message_content_details .= '<br/>'; $message_content_details .= '- ' . $item_type_name . '(s): ' . $count_item_type; } } $message_content = sprintf(__('%s items in basket:', 'sitepress'), $total_count); $message_content .= $message_content_details; } $container = $message_content; return array('message' => $container, 'basket' => $basket, 'allowed_item_types' => array_keys($basket_items_types)); }
/** * @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, $wpdb, $wpml_translation_job_factory; $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) { $basket = new WPML_Translation_Basket($wpdb); $st_tp_job = new WPML_TP_String_Job($wpdb, $basket, $wpml_translation_job_factory); $result = $st_tp_job->send_strings_to_translation_service($string_ids, $target, $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 = $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); } } }
function send_strings_to_translation_service($string_ids, $target_language, $translator_id) { /** @var WPML_String_Translation $WPML_String_Translation */ global $WPML_String_Translation; if (sizeof($string_ids) > 0) { $project = $this->basket->get_project(); $strings = array(); $word_count = 0; $source_language = $this->basket->get_source_language(); foreach ($string_ids as $string_id) { $string_data_query = "SELECT id, context, name, value FROM {$this->wpdb->prefix}icl_strings WHERE id=%d"; $string_data_prepare = $this->wpdb->prepare($string_data_query, $string_id); $string_data = $this->wpdb->get_row($string_data_prepare); $word_count += $WPML_String_Translation->estimate_word_count($string_data->value, $source_language); $strings[] = $string_data; } $xliff = new WPML_TM_Xliff_Writer($this->job_factory); $res = $project->send_to_translation_batch_mode($xliff->get_strings_xliff_file($strings, $source_language, $target_language), 'String Translations', '', '', $source_language, $target_language, $word_count); if ($res) { foreach ($strings as $string_data) { $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'], TranslationProxy_Batch::update_translation_batch($this->basket->get_name())); if ($added) { $data = array('rid' => $res, 'string_translation_id' => $added, 'timestamp' => date('Y-m-d H:i:s'), 'md5' => md5($string_data->value)); $this->wpdb->insert($this->wpdb->prefix . 'icl_string_status', $data); //insert rid } } $this->wpdb->insert($this->wpdb->prefix . 'icl_core_status', array('rid' => $res, 'module' => '', 'origin' => $source_language, 'target' => $target_language, 'status' => ICL_TM_WAITING_FOR_TRANSLATOR)); if ($project->errors && count($project->errors)) { $res['errors'] = $project->errors; } return $res; } } return 0; }
private function build_translation_options() { global $sitepress, $wpdb; $basket_items_number = TranslationProxy_Basket::get_basket_items_count(true); $basket_name_max_length = TranslationProxy::get_current_service_batch_name_max_length(); if ($basket_items_number > 0) { $source_language = TranslationProxy_Basket::get_source_language(); $basket_name_placeholder = sprintf(__("%s|WPML|%s", 'wpml-translation-management'), get_option('blogname'), $source_language); $basket = new WPML_Translation_Basket($wpdb); $basket_name_placeholder = esc_attr($basket->get_unique_basket_name($basket_name_placeholder, $basket_name_max_length)); ?> <h3>2. <?php _e('Choose translation options', 'wpml-translation-management'); ?> </h3> <form method="post" id="translation-jobs-translators-form" name="translation-jobs-translators" action=""> <input type="hidden" name="icl_tm_action" value="send_all_jobs"/> <label for="basket_name"><strong><?php _e('Batch name', 'wpml-translation-management'); ?> :</strong></label> <input id="basket_name" name="basket_name" type="text" style="width: 40%;" value="<?php echo $basket_name_placeholder; ?> " maxlength="<?php echo $basket_name_max_length; ?> " placeholder="<?php echo $basket_name_placeholder; ?> "> <br/><span class="description"><?php _e('Give a name to the batch. If omitted, the default name will be applied.', 'wpml-translation-management'); ?> </span> <table class="widefat fixed" id="icl-translation-translators" cellspacing="0"> <thead> <tr> <th scope="col" width="15%"><?php _e('Language', 'wpml-translation-management'); ?> </th> <th scope="col"><?php _e('Translator', 'wpml-translation-management'); ?> </th> </tr> </thead> <tfoot> <tr> <th scope="col"><?php _e('Language', 'wpml-translation-management'); ?> </th> <th scope="col"><?php _e('Translator', 'wpml-translation-management'); ?> </th> </tr> </tfoot> <?php $basket_languages = TranslationProxy_Basket::get_target_languages(); if ($basket_languages) { ?> <tbody> <?php $target_languages = $sitepress->get_active_languages(); foreach ($target_languages as $key => $lang) { if (!in_array($lang['code'], $basket_languages)) { unset($target_languages[$key]); } } foreach ($target_languages as $lang) { if ($lang['code'] === TranslationProxy_Basket::get_source_language()) { continue; } ?> <tr> <td><strong><?php echo $lang['display_name']; ?> </strong></td> <td> <label for="<?php echo esc_attr('translator[' . $lang['code'] . ']'); ?> "> <?php _e('Translate by', 'wpml-translation-management'); ?> </label> <?php $selected_translator = isset($icl_selected_translators[$lang['code']]) ? $icl_selected_translators[$lang['code']] : false; if ($selected_translator === false) { $selected_translator = TranslationProxy_Service::get_wpml_translator_id(); } $args = array('from' => TranslationProxy_Basket::get_source_language(), 'to' => $lang['code'], 'name' => 'translator[' . $lang['code'] . ']', 'selected' => $selected_translator, 'services' => array('local', TranslationProxy::get_current_service_id())); TranslationManagement::translators_dropdown($args); ?> <a href="admin.php?page=<?php echo WPML_TM_FOLDER; ?> /menu/main.php&sm=translators"><?php _e('Manage translators', 'wpml-translation-management'); ?> </a> </td> </tr> <?php } ?> </tbody> <?php } ?> </table> <br> <?php echo TranslationProxy_Basket::get_basket_extra_fields_section(); ?> <?php wp_nonce_field('send_basket_items_nonce', '_icl_nonce_send_basket_items'); ?> <?php wp_nonce_field('send_basket_item_nonce', '_icl_nonce_send_basket_item'); ?> <?php wp_nonce_field('send_basket_commit_nonce', '_icl_nonce_send_basket_commit'); ?> <?php wp_nonce_field('check_basket_name_nonce', '_icl_nonce_check_basket_name'); ?> <input type="submit" class="button-primary" name="send-all-jobs-for-translation" value="<?php _e('Send all items for translation', 'wpml-translation-management'); ?> "> </form> <?php } do_action('wpml_translation_basket_page_after'); }