public static function translators_dropdown($args = array())
 {
     $dropdown = '';
     /** @var $from string|false */
     /** @var $to string|false */
     /** @var $classes string|false */
     /** @var $id string|false */
     /** @var $name string|false */
     /** @var $selected bool */
     /** @var $echo bool */
     /** @var $add_label bool */
     /** @var $services array */
     /** @var $show_service bool */
     /** @var $disabled bool */
     /** @var $default_name bool|string */
     /** @var $local_only bool */
     //set default value for variables
     $from = false;
     $to = false;
     $id = 'translator_id';
     $name = 'translator_id';
     $selected = 0;
     $echo = true;
     $add_label = false;
     $services = array('local');
     $show_service = true;
     $disabled = false;
     $default_name = false;
     $local_only = false;
     extract($args, EXTR_OVERWRITE);
     $translators = array();
     try {
         $translation_service = TranslationProxy::get_current_service();
         $translation_service_id = TranslationProxy::get_current_service_id();
         $translation_service_name = TranslationProxy::get_current_service_name();
         $is_service_authenticated = TranslationProxy::is_service_authenticated();
         //if translation service does not support translators choice, always shows first available
         if (isset($translation_service->id) && !TranslationProxy::translator_selection_available() && $is_service_authenticated) {
             $translators[] = (object) array('ID' => TranslationProxy_Service::get_wpml_translator_id($translation_service->id), 'display_name' => __('First available', 'sitepress'), 'service' => $translation_service_name);
         } elseif (in_array($translation_service_id, $services) && $is_service_authenticated) {
             $lang_status = TranslationProxy_Translator::get_language_pairs();
             if (empty($lang_status)) {
                 $lang_status = array();
             }
             foreach ((array) $lang_status as $language_pair) {
                 if ($from && $from != $language_pair['from']) {
                     continue;
                 }
                 if ($to && $to != $language_pair['to']) {
                     continue;
                 }
                 if (!empty($language_pair['translators'])) {
                     if (1 < count($language_pair['translators'])) {
                         $translators[] = (object) array('ID' => TranslationProxy_Service::get_wpml_translator_id($translation_service->id), 'display_name' => __('First available', 'sitepress'), 'service' => $translation_service_name);
                     }
                     foreach ($language_pair['translators'] as $tr) {
                         if (!isset($_icl_translators[$tr['id']])) {
                             $translators[] = $_icl_translators[$tr['id']] = (object) array('ID' => TranslationProxy_Service::get_wpml_translator_id($translation_service->id, $tr['id']), 'display_name' => $tr['nickname'], 'service' => $translation_service_name);
                         }
                     }
                 }
             }
         }
         if (in_array('local', $services)) {
             $translators[] = (object) array('ID' => 0, 'display_name' => __('First available', 'sitepress'));
             $translators = array_merge($translators, self::get_blog_translators(array('from' => $from, 'to' => $to)));
         }
         $translators = apply_filters('wpml_tm_translators_list', $translators);
         $dropdown .= '<select id="' . esc_attr($id) . '" name="' . esc_attr($name) . '" ' . ($disabled ? 'disabled="disabled"' : '') . '>';
         if ($default_name) {
             $dropdown_selected = selected($selected, false, false);
             $dropdown .= '<option value="" ' . $dropdown_selected . '>';
             $dropdown .= esc_html($default_name);
             $dropdown .= '</option>';
         }
         foreach ($translators as $t) {
             if ($local_only && isset($t->service)) {
                 continue;
             }
             $current_translator = $t->ID;
             $dropdown_selected = selected($selected, $current_translator, false);
             $dropdown .= '<option value="' . $current_translator . '" ' . $dropdown_selected . '>';
             $dropdown .= esc_html($t->display_name);
             if ($show_service) {
                 $dropdown .= ' (';
                 $dropdown .= isset($t->service) ? $t->service : __('Local', 'sitepress');
                 $dropdown .= ')';
             }
             $dropdown .= '</option>';
         }
         $dropdown .= '</select>';
     } catch (TranslationProxy_Api_Error $ex) {
         $dropdown .= __('Translation Proxy error', 'sitepress') . ': ' . $ex->getMessage();
     } catch (Exception $ex) {
         $dropdown .= __('Error', 'sitepress') . ': ' . $ex->getMessage();
     }
     if ($add_label) {
         $dropdown = '<label for="' . esc_attr($id) . '">' . __('Translation jobs for:', 'wpml-translation-management') . '</label>&nbsp;' . $dropdown;
     }
     if ($echo) {
         echo $dropdown;
     }
     return $dropdown;
 }
 /**
  * Returns array of remote translators. Works only with ICL as a Translation Service
  *
  * @return array
  */
 public static function translation_service_translators_list()
 {
     $translators = array();
     if (!TranslationProxy::translator_selection_available()) {
         return $translators;
     }
     $project = TranslationProxy::get_current_project();
     if (!$project) {
         return $translators;
     }
     $lang_status = TranslationProxy_Translator::get_language_pairs();
     if (empty($lang_status)) {
         return $translators;
     }
     $action_link_args = array('title' => __('Contact translator', 'sitepress'), 'unload_cb' => 'icl_thickbox_refresh', 'ar' => 1);
     foreach ($lang_status as $language_pair) {
         $language_from = $language_pair['from'];
         $language_pair_translators = $language_pair['translators'];
         if ($language_pair_translators) {
             foreach ($language_pair_translators as $translator) {
                 $translator_item = array();
                 if (isset($translators[$translator['id']])) {
                     $translator_item = $translators[$translator['id']];
                     $translator_item['langs'][$language_from][] = $language_pair['to'];
                 } else {
                     $translator_item['name'] = $translator['nickname'];
                     $translator_item['langs'][$language_from][] = $language_pair['to'];
                     $translator_item['type'] = $project->service->name;
                     $url = $project->translator_contact_iframe_url($translator['id']);
                     $action_link = '';
                     if ($url) {
                         $action_link = TranslationProxy_Popup::get_link($url, $action_link_args) . __('Contact translator', 'sitepress') . '</a>';
                     }
                     $translator_item['action'] = $action_link;
                 }
                 $translators[$translator['id']] = $translator_item;
             }
         }
     }
     return $translators;
 }
예제 #3
0
 function save_post_translation($translation_id, $translation)
 {
     global $wpdb, $sitepress_settings, $sitepress, $icl_adjust_id_url_filter_off;
     $icl_adjust_id_url_filter_off = true;
     $translation_info = $wpdb->get_row($wpdb->prepare("\n                SELECT * FROM {$wpdb->prefix}icl_translations tr\n                    JOIN {$wpdb->prefix}icl_translation_status ts ON ts.translation_id = tr.translation_id\n                WHERE tr.translation_id=%d", $translation_id));
     $lang_code = $translation_info->language_code;
     $trid = $translation_info->trid;
     $original_post_details = $wpdb->get_row("\n            SELECT p.post_author, p.post_type, p.post_status, p.comment_status, p.ping_status, p.post_parent, p.menu_order, p.post_date, t.language_code\n            FROM {$wpdb->prefix}icl_translations t \n            JOIN {$wpdb->posts} p ON t.element_id = p.ID AND CONCAT('post_',p.post_type) = t.element_type\n            WHERE trid='{$trid}' AND p.ID = '{$translation['original_id']}'\n        ");
     //is the original post a sticky post?
     $sticky_posts = get_option('sticky_posts');
     $is_original_sticky = $original_post_details->post_type == 'post' && in_array($translation['original_id'], $sticky_posts);
     $this->_content_fix_image_paths_in_body($translation);
     $this->_content_fix_relative_link_paths_in_body($translation);
     $this->_content_decode_shortcodes($translation);
     // handle the page parent and set it to the translated parent if we have one.
     if ($original_post_details->post_parent) {
         $post_parent_trid = $wpdb->get_var($wpdb->prepare("\tSELECT trid\n\t\t\t\t\t\tFROM {$wpdb->prefix}icl_translations\n\t\t\t\t\t\tWHERE element_type= %s AND element_id = %d ", 'post_' . $original_post_details->post_type, $original_post_details->post_parent));
         if ($post_parent_trid) {
             $parent_id = $wpdb->get_var($wpdb->prepare("SELECT element_id\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t FROM {$wpdb->prefix}icl_translations\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t WHERE element_type = %s\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t  AND trid = %d\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t  AND language_code = %s ", 'post_' . $original_post_details->post_type, $post_parent_trid, $lang_code));
         }
     }
     // determine post id based on trid
     $post_id = $translation_info->element_id;
     if ($post_id) {
         // see if the post really exists - make sure it wasn't deleted while the plugin was
         if (!$wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE ID = %d ", $post_id))) {
             $is_update = false;
             $q = "DELETE FROM {$wpdb->prefix}icl_translations WHERE element_type=%s AND element_id=%d";
             $q_prepared = $wpdb->prepare($q, array('post_' . $original_post_details->post_type, $post_id));
             $wpdb->query($q_prepared);
         } else {
             $is_update = true;
             $postarr['ID'] = $_POST['post_ID'] = $post_id;
         }
     } else {
         $is_update = false;
     }
     $postarr['post_title'] = $translation['title'];
     if ($sitepress_settings['translated_document_page_url'] == 'translate' && isset($translation['URL'])) {
         $postarr['post_name'] = $translation['URL'];
     }
     $postarr['post_content'] = $translation['body'];
     if (isset($translation['excerpt']) && $translation['excerpt'] != "") {
         $postarr['post_excerpt'] = $translation['excerpt'];
     }
     if (isset($translated_taxonomies) && is_array($translated_taxonomies)) {
         foreach ($translated_taxonomies as $taxonomy => $values) {
             $postarr['tax_input'][$taxonomy] = join(',', (array) $values);
         }
     }
     $postarr['post_author'] = $original_post_details->post_author;
     $postarr['post_type'] = $original_post_details->post_type;
     if ($sitepress_settings['sync_comment_status']) {
         $postarr['comment_status'] = $original_post_details->comment_status;
     }
     if ($sitepress_settings['sync_ping_status']) {
         $postarr['ping_status'] = $original_post_details->ping_status;
     }
     if ($sitepress_settings['sync_page_ordering']) {
         $postarr['menu_order'] = $original_post_details->menu_order;
     }
     if ($sitepress_settings['sync_private_flag'] && $original_post_details->post_status == 'private') {
         $postarr['post_status'] = 'private';
     }
     if (!$is_update) {
         $postarr['post_status'] = !$sitepress_settings['translated_document_status'] ? 'draft' : $original_post_details->post_status;
     } else {
         // set post_status to the current post status.
         $postarr['post_status'] = $wpdb->get_var($wpdb->prepare("SELECT post_status\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t FROM {$wpdb->prefix}posts\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t WHERE ID = %d ", $post_id));
     }
     if ($sitepress_settings['sync_post_date']) {
         $postarr['post_date'] = $original_post_details->post_date;
     }
     if (isset($parent_id) && $sitepress_settings['sync_page_parent']) {
         $_POST['post_parent'] = $postarr['post_parent'] = $parent_id;
         $_POST['parent_id'] = $postarr['parent_id'] = $parent_id;
     }
     if ($is_update) {
         $postarr['post_name'] = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM {$wpdb->posts} WHERE ID=%d", $post_id));
     }
     $_POST['trid'] = $trid;
     $_POST['lang'] = $lang_code;
     $_POST['skip_sitepress_actions'] = true;
     global $wp_rewrite;
     if (!isset($wp_rewrite)) {
         $wp_rewrite = new WP_Rewrite();
     }
     kses_remove_filters();
     $postarr = apply_filters('icl_pre_save_pro_translation', $postarr);
     $new_post_id = wp_insert_post($postarr);
     do_action('icl_pro_translation_saved', $new_post_id);
     // set stickiness
     if ($is_original_sticky && $sitepress_settings['sync_sticky_flag']) {
         stick_post($new_post_id);
     } else {
         if ($original_post_details->post_type == 'post' && $is_update) {
             unstick_post($new_post_id);
             //just in case - if this is an update and the original post stckiness has changed since the post was sent to translation
         }
     }
     foreach ((array) $sitepress_settings['translation-management']['custom_fields_translation'] as $cf => $op) {
         if ($op == 1) {
             $sitepress->_sync_custom_field($translation['original_id'], $new_post_id, $cf);
         } elseif ($op == 2 && isset($translation['field-' . $cf])) {
             $field_translation = $translation['field-' . $cf];
             $field_type = $translation['field-' . $cf . '-type'];
             if ($field_type == 'custom_field') {
                 $field_translation = str_replace('&#0A;', "\n", $field_translation);
                 // always decode html entities  eg decode &amp; to &
                 $field_translation = html_entity_decode($field_translation);
                 update_post_meta($new_post_id, $cf, $field_translation);
             }
         }
     }
     // set specific custom fields
     $copied_custom_fields = array('_top_nav_excluded', '_cms_nav_minihome');
     foreach ($copied_custom_fields as $ccf) {
         $val = get_post_meta($translation['original_id'], $ccf, true);
         update_post_meta($new_post_id, $ccf, $val);
     }
     // sync _wp_page_template
     if ($sitepress_settings['sync_page_template']) {
         $_wp_page_template = get_post_meta($translation['original_id'], '_wp_page_template', true);
         update_post_meta($new_post_id, '_wp_page_template', $_wp_page_template);
     }
     // sync post format
     if ($sitepress_settings['sync_post_format']) {
         $_wp_post_format = get_post_format($translation['original_id']);
         set_post_format($new_post_id, $_wp_post_format);
     }
     if (!$new_post_id) {
         return false;
     }
     if (!$is_update) {
         $wpdb->update($wpdb->prefix . 'icl_translations', array('element_id' => $new_post_id), array('translation_id' => $translation_id));
     }
     update_post_meta($new_post_id, '_icl_translation', 1);
     TranslationManagement::set_page_url($new_post_id);
     global $iclTranslationManagement;
     $ts = array('status' => ICL_TM_COMPLETE, 'needs_update' => 0, 'translation_id' => $translation_id);
     $translator_id = $wpdb->get_var($wpdb->prepare("SELECT translator_id FROM {$wpdb->prefix}icl_translation_status WHERE translation_id=%d", $translation_id));
     if (!$translator_id) {
         $lang_status = TranslationProxy_Translator::get_language_pairs();
         foreach ($lang_status as $languages_pair) {
             if ($languages_pair['from'] == $original_post_details->language_code && $languages_pair['to'] == $lang_code && isset($languages_pair['translators'][0]['id'])) {
                 $ts['translator_id'] = $languages_pair['translators'][0]['id'];
                 break;
             }
         }
     }
     // update translation status
     $iclTranslationManagement->update_translation_status($ts);
     // add new translation job
     //$translation_package = $iclTranslationManagement->create_translation_package(get_post($translation['original_id']));
     //$job_id = $iclTranslationManagement->add_translation_job($translation_info->rid, $translation_info->translator_id, $translation_package);
     $job_id = $iclTranslationManagement->get_translation_job_id($trid, $lang_code);
     // save the translation
     $iclTranslationManagement->mark_job_done($job_id);
     $parts = explode('_', $translation['original_id']);
     if ($parts[0] != 'external') {
         $iclTranslationManagement->save_job_fields_from_post($job_id, get_post($new_post_id));
         $this->_content_fix_links_to_translated_content($new_post_id, $lang_code, "post_{$original_post_details->post_type}");
         // Now try to fix links in other translated content that may link to this post.
         $sql = "SELECT\n                        tr.element_id\n                    FROM\n                        {$wpdb->prefix}icl_translations tr\n                    JOIN\n                        {$wpdb->prefix}icl_translation_status ts\n                    ON\n                        tr.translation_id = ts.translation_id\n                    WHERE\n                        ts.links_fixed = 0 AND tr.element_type = %s AND tr.language_code = %s AND tr.element_id IS NOT NULL";
         $sql_prepared = $wpdb->prepare($sql, array('post_' . $original_post_details->post_type, $lang_code));
         $needs_fixing = $wpdb->get_results($sql_prepared);
         foreach ($needs_fixing as $id) {
             if ($id->element_id != $new_post_id) {
                 // fix all except the new_post_id. We have already done this.
                 $this->_content_fix_links_to_translated_content($id->element_id, $lang_code, "post_{$original_post_details->post_type}");
             }
         }
         // if this is a parent page then make sure it's children point to this.
         $this->fix_translated_children($translation['original_id'], $new_post_id, $lang_code);
     }
     WPML_Translation_Job_Terms::save_terms_from_job($job_id, $lang_code);
     do_action('icl_pro_translation_completed', $new_post_id);
     return true;
 }