public function create_translation($locale) { $client = new Lingotek_API(); if (false === ($translation = $client->get_translation($this->document_id, $locale, $this->source))) { return; } self::$creating_translation = true; $translation = json_decode($translation, true); // wp_insert_post expects array $args = $translation['args']; // update existing translation if ($tr_id = $this->pllm->get_term($this->source, $locale)) { $args['name'] = $translation['name']; wp_update_term($tr_id, $this->type, $args); $this->safe_translation_status_update($locale, 'current'); } else { $tr_lang = $this->pllm->get_language($locale); // translate parent $term = get_term($this->source, $this->type); $args['parent'] = $term->parent && ($tr_parent = $this->pllm->get_translation('term', $term->parent, $locale)) ? $tr_parent : 0; // attempt to get a unique slug in case it already exists in another language if (isset($args['slug']) && term_exists($args['slug'])) { $args['slug'] .= '-' . $tr_lang->slug; } $tr = wp_insert_term($translation['name'], $this->type, $args); if (!is_wp_error($tr)) { $this->pllm->set_term_language($tr['term_id'], $tr_lang); $this->safe_translation_status_update($locale, 'current', array($tr_lang->slug => $tr['term_id'])); wp_set_object_terms($tr['term_id'], $this->term_id, 'term_translations'); } } self::$creating_translation = false; }
public function create_translation($locale, $automatic = false) { // Removes content sanitization so YouTube videos, links, etc don't get removed when inserting translations remove_filter('content_save_pre', 'wp_filter_post_kses'); remove_filter('content_filtered_save_pre', 'wp_filter_post_kses'); $client = new Lingotek_API(); if (false === ($translation = $client->get_translation($this->document_id, $locale, $this->source))) { return; } self::$creating_translation = true; $prefs = Lingotek_Model::get_prefs(); // need an array by default $translation = json_decode($translation, true); // wp_insert_post expects array $tr_post = $translation['post']; $post = get_post($this->source); // source post $tr_post['post_status'] = $prefs['download_post_status'] === self::SAME_AS_SOURCE ? $post->post_status : $prefs['download_post_status']; // status // update existing translation if ($tr_id = $this->pllm->get_post($this->source, $locale)) { $tr_post['ID'] = $tr_id; // copy or ignore metas self::copy_or_ignore_metas($post->ID, $tr_id); // translate metas if (isset($translation['metas'])) { self::copy_translated_metas($translation['metas'], $tr_id); } wp_update_post($tr_post); $this->safe_translation_status_update($locale, 'current'); } else { if ($this->translations[$locale] == 'ready' || $automatic) { unset($post->post_name); // forces the creation of a new default slug if not translated by Lingotek $tr_post = array_merge((array) $post, $tr_post); // copy all untranslated fields from the original post $tr_post['ID'] = null; // will force the creation of a new post // translate parent $tr_post['post_parent'] = $post->post_parent && ($tr_parent = $this->pllm->get_translation('post', $post->post_parent, $locale)) ? $tr_parent : 0; if ('attachment' == $post->post_type) { $tr_id = wp_insert_attachment($tr_post); add_post_meta($tr_id, '_wp_attachment_metadata', get_post_meta($this->source, '_wp_attachment_metadata', true)); add_post_meta($tr_id, '_wp_attached_file', get_post_meta($this->source, '_wp_attached_file', true)); } else { $tr_id = wp_insert_post($tr_post); } if ($tr_id) { $tr_lang = $this->pllm->get_language($locale); $this->pllm->set_post_language($tr_id, $tr_lang); $this->safe_translation_status_update($locale, 'current', array($tr_lang->slug => $tr_id)); wp_set_object_terms($tr_id, $this->term_id, 'post_translations'); // assign terms and metas $GLOBALS['polylang']->sync->copy_post_metas($this->source, $tr_id, $tr_lang->slug); // copy or ignore metas self::copy_or_ignore_metas($post->ID, $tr_id); // translate metas if (isset($translation['metas'])) { self::copy_translated_metas($translation['metas'], $tr_id); } } } } self::$creating_translation = false; // Adds content sanitization back in after Lingotek saves the translation add_filter('content_save_pre', 'wp_filter_post_kses'); add_filter('content_filtered_save_pre', 'wp_filter_post_kses'); }
public function translations_status() { $client = new Lingotek_API(); $translations = $client->get_translations_status($this->document_id); // key are Lingotek locales if ($translations !== false) { foreach ($this->translations as $locale => $status) { $lingotek_locale = $this->pllm->get_language($locale)->lingotek_locale; if ('current' != $status && isset($translations[$lingotek_locale]) && 100 == $translations[$lingotek_locale]) { $this->translations[$locale] = 'ready'; } } unset($this->desc_array['lingotek']['api_errors']['translations_status']); $this->save(); } else { $this->desc_array['lingotek']['api_errors']['translations_status'] = __('Error checking translations status of post ', 'wp-lingotek') . $this->source; $this->save(); } }
public function create_translation($locale) { $client = new Lingotek_API(); if (false === ($translation = $client->get_translation($this->document_id, $locale))) { return; } $strings = wp_list_pluck(PLL_Admin_Strings::get_strings(), 'name', 'string'); // get the strings name for the filter $translations = json_decode($translation, true); // wp_insert_post expects array $language = $this->pllm->get_language($locale); $mo = new PLL_MO(); $mo->import_from_db($language); foreach ($translations as $key => $translation) { $translation = apply_filters('pll_sanitize_string_translation', $translation, $strings[$key], $this->name); $mo->add_entry($mo->make_entry($key, $translation)); } $mo->export_to_db($language); $this->safe_translation_status_update($locale, 'current'); }
case 'default': unset($profiles[$profile_id]['targets'][$language->slug]); } } // hardcode default values for automatic and manual profiles as the process above emptied them $profiles['automatic']['upload'] = $profiles['automatic']['download'] = 'automatic'; $profiles['manual']['upload'] = $profiles['manual']['download'] = 'manual'; $profiles['automatic']['name'] = 'Automatic'; $profiles['manual']['name'] = 'Manual'; $profiles['disabled']['name'] = 'Disabled'; // do not localize names here update_option('lingotek_profiles', $profiles); add_settings_error('lingotek_profile', 'default', __('Your translation profile was sucessfully saved.', 'wp-lingotek'), 'updated'); if (isset($_POST['update_callback'])) { $project_id = isset($profiles[$profile_id]['project_id']) ? $profiles[$profile_id]['project_id'] : $defaults['project_id']; $client = new Lingotek_API(); if ($client->update_callback_url($project_id)) { add_settings_error('lingotek_profile', 'default', __('Your callback url was successfully updated.', 'wp-lingotek'), 'updated'); } } } settings_errors(); } ?> <h3><?php _e('Translation Profiles', 'wp-lingotek'); ?> </h3> <p class="description"><?php _e('Translation profiles allow you to quickly configure and re-use translation settings.', 'wp-lingotek'); ?>
public function set_community_resources($community_id, $update_first_project_callback = FALSE) { $client = new Lingotek_API(); $refresh_success = array('projects' => FALSE, 'workflows' => FALSE); $api_data = $client->get_projects($community_id); $projects = array(); if ($api_data !== FALSE) { foreach ($api_data->entities as $project) { $projects[$project->properties->id] = $project->properties->title; } if ($api_data->properties->total == 1) { if (!$project->properties->callback_url) { $client->update_callback_url($project->properties->id); } } natcasesort($projects); //order by title (case-insensitive) $refresh_success['projects'] = TRUE; } $api_data = $client->get_workflows($community_id); $default_workflows = array('c675bd20-0688-11e2-892e-0800200c9a66' => 'Machine Translation', 'ddf6e3c0-0688-11e2-892e-0800200c9a66' => 'Machine Translation + Post-Edit', '6ff1b470-33fd-11e2-81c1-0800200c9a66' => 'Machine Translation + Translate', '2b5498e0-f3c7-4c49-9afa-cca4b3345af7' => 'Translation + 1 review', '814172a6-3744-4da7-b932-5857c1c20976' => 'Translation + 2 reviews', '2210b148-0c44-4ae2-91d0-ca2ee47c069e' => 'Translation + 3 reviews', '7993b4d7-4ada-46d0-93d5-858db46c4c7d' => 'Translation Only'); $workflows = array(); if ($api_data) { foreach ($api_data->entities as $workflow) { $workflows[$workflow->properties->id] = $workflow->properties->title; } $diff = array_diff_key($workflows, $default_workflows); if (empty($diff)) { $workflows = array('c675bd20-0688-11e2-892e-0800200c9a66' => 'Machine Translation'); } natcasesort($workflows); //order by title (case-insensitive) $refresh_success['workflows'] = TRUE; } $api_data = $client->get_filters(); $filters = array(); if ($api_data && $api_data->properties->total > 0) { foreach ($api_data->entities as $filter) { if (!$filter->properties->is_public) { $filters[$filter->properties->id] = $filter->properties->title; } if ($filter->properties->title == '*****@*****.**' || $filter->properties->title == '*****@*****.**') { $filters[$filter->properties->id] = $filter->properties->title; } } $primary_filter_id = array_search('*****@*****.**', $filters); $secondary_filter_id = array_search('*****@*****.**', $filters); $defaults = get_option('lingotek_defaults'); if ($defaults == NULL) { $defaults['primary_filter_id'] = $primary_filter_id; $defaults['secondary_filter_id'] = $secondary_filter_id; update_option('lingotek_defaults', $defaults); } } $resources = array('projects' => $projects, 'workflows' => $workflows, 'filters' => $filters); if ($refresh_success['projects'] == TRUE || $refresh_success['workflows'] == TRUE) { update_option('lingotek_community_resources', $resources); $this->ensure_valid_defaults(); } return $refresh_success; }
$lingotek_option = 'lingotek_' . $setting; $source_options = get_blog_option($source_site, $lingotek_option); update_blog_option($destination, $lingotek_option, $source_options); $source_options = get_blog_option($source_site, 'lingotek_base_url'); update_blog_option($destination, 'lingotek_base_url', $source_options); } //Updates the chosen option $lingotek_option = 'lingotek_' . $setting; $source_options = get_blog_option($source_site, $lingotek_option); update_blog_option($destination, $lingotek_option, $source_options); } } //Creates a new project based on the name of the selected site if (isset($_POST['new_project'])) { $options = get_blog_option($destination, 'lingotek_defaults'); $client = new Lingotek_API(); $title = htmlspecialchars_decode(get_blog_details($destination)->blogname, ENT_QUOTES); if ($new_id = $client->create_project($title, $community_id = get_blog_option($destination, 'lingotek_community'))) { $options['project_id'] = $new_id; // Adds correct callback URL for new project $args = array('callback_url' => add_query_arg('lingotek', 1, get_blog_details($destination)->siteurl)); $response = $client->patch($client->get_api_url() . '/project/' . $new_id, $args); update_blog_option($destination, 'lingotek_defaults', $options); } } if (isset($_POST['preferences'])) { switch_to_blog($source_site); $preferences = Lingotek_Model::get_prefs(); update_blog_option($destination, 'lingotek_prefs', $preferences); restore_current_blog(); }
public function delete_term($object_id) { if ($document = $this->get_group('term', $object_id)) { $client = new Lingotek_API(); if ($document->source == $object_id) { $client->delete_document($document->document_id, $object_id); } else { $lang = $this->pllm->get_term_language($object_id); $this->pllm->delete_term_language($object_id); $this->pllm->delete_translation('term', $object_id); $client->delete_translation($document->document_id, $lang->lingotek_locale, $object_id); } } }
foreach ($settings as $key => $setting) { if (isset($_POST[$key])) { $options[$key] = $_POST[$key]; } } update_option('lingotek_defaults', $options); add_settings_error('lingotek_defaults', 'defaultgs', __('Your <i>Defaults</i> were sucessfully saved.', 'wp-lingotek'), 'updated'); if (isset($_POST['update_callback'])) { $client = new Lingotek_API(); if ($client->update_callback_url($options['project_id'])) { add_settings_error('lingotek_defaults', 'defaultgs', __('Your callback url was successfully updated.', 'wp-lingotek'), 'updated'); } } //adds new project if text box is filled out if (!empty($_POST['new_project'])) { $client = new Lingotek_API(); $title = stripslashes($_POST['new_project']); if ($new_id = $client->create_project($title, $community_id)) { add_settings_error('lingotek_defaults', 'defaultgs', __('Your new project was successfully created.', 'wp-lingotek'), 'updated'); $this->set_community_resources($community_id); // updates the cache to include the newly created project $options['project_id'] = $new_id; update_option('lingotek_defaults', $options); } } } settings_errors(); } $settings = $this->get_profiles_settings(true); $options = get_option('lingotek_defaults'); // Code to determine which filter scenario will be displayed (Not configured, defaults, custom filters)
<?php $client = new Lingotek_API(); $api_communities = $client->get_communities(); if (!isset($api_communities->entities)) { add_settings_error('lingotek_community_resources', 'error', __('The Lingotek TMS is currently unavailable. Please try again later. If the problem persists, contact Lingotek Support.', 'wp-lingotek'), 'error'); settings_errors(); } if (!$community_id) { $ltk_client = new Lingotek_API(); $ltk_communities = $ltk_client->get_communities(); $ltk_num_communities = $ltk_communities->properties->total; if ($ltk_num_communities == 1) { $ltk_community_id = $ltk_communities->entities[0]->properties->id; $this->set_community_resources($ltk_community_id); echo '<script type="text/javascript">document.body.innerHTML = ""; window.location = "admin.php?page=wp-lingotek_tutorial";</script>'; } } ?> <h3><?php _e('Account', 'wp-lingotek'); ?> </h3> <p class="description"><?php _e('Lingotek account connection and community selection.', 'wp-lingotek'); ?> </p> <table class="form-table"> <tr>
?> <?php wp_nonce_field($page_key, '_wpnonce_' . $page_key); ?> <table class="form-table"> <tr> <th scope="row"><label for="lingotek_community"><?php _e('Community', 'wp-lingotek'); ?> </label></th> <td> <select name="lingotek_community" id="lingotek_community"> <?php $default_community_id = $community_id; $client = new Lingotek_API(); // Community $api_communities = $client->get_communities(); $communities = array(); foreach ($api_communities->entities as $community) { $communities[$community->properties->id] = $community->properties->title; // . ' (' . $community->properties->id . ')'; } $num_communities = count($communities); if ($num_communities == 1 && !$community_id) { update_option('lingotek_community', current(array_keys($communities))); } if (!$community_id && $num_communities > 1) { echo "\n\t" . '<option value="">' . __('Select', 'wp-lingotek') . '...</option>'; } foreach ($communities as $community_id_option => $community_title) {
public function translations_status() { $client = new Lingotek_API(); $translations = $client->get_translations_status($this->document_id, $this->source); // key are Lingotek locales foreach ($this->translations as $locale => $status) { $lingotek_locale = $this->pllm->get_language($locale)->lingotek_locale; if ('current' != $status && isset($translations[$lingotek_locale]) && 100 == $translations[$lingotek_locale]) { $this->translations[$locale] = 'ready'; } } $this->save(); }