public static function lingotek_edit_meta_box_html() { wp_enqueue_script('lingotek_defaults', LINGOTEK_URL . '/js/defaults.js'); global $post; $post_type = get_post_type($post->ID); $lgtm = new Lingotek_Model(); $group = $lgtm->get_group('post', $post->ID); $profiles = Lingotek::get_profiles(); $content_profiles = get_option('lingotek_content_type'); $language_profiles = self::retrieve_lang_Profiles($post_type, $profiles, $content_profiles); $default_name = empty($content_profiles) == false ? $profiles[$content_profiles[$post->post_type]['profile']]['name'] : ($post_type == 'page' ? __('Manual', 'wp-lingotek') : __('Automatic', 'wp-lingotek')); $content_default_profile = array('default' => array('name' => __('Content Default', 'wp-lingotek') . ' (' . $default_name . ')')); $language_profiles['defaults'] = array('content_default' => $default_name, 'title' => __('Content Default', 'wp-lingotek')); $profiles = array_merge($content_default_profile, $profiles); $post_profile = self::get_post_profile($post->ID); if (isset($post_profile)) { $selected[$post_profile->description] = $profiles[$post_profile->description]; unset($profiles[$post_profile->description]); $profiles = array_merge($selected, $profiles); } if (isset($group->source)) { // Disables selection of a different profile if content has been uploaded to Lingotek $args = array('document_id' => $group->document_id, 'action' => 'lingotek-delete', 'noheader' => true); if ($post_type == 'page') { $args['lingotek_redirect'] = true; } $site_id = get_current_blog_id(); $url = $post_type == 'page' ? get_site_url($site_id, '/wp-admin/edit.php?post_type=page') : get_site_url($site_id, '/wp-admin/edit.php'); $disassociate_url = wp_nonce_url(add_query_arg($args, $url), 'lingotek-delete'); $remove_post = 'post=' . $post->ID; $disassociate_url = str_replace($remove_post, '', $disassociate_url); $prefs = Lingotek_Model::get_prefs(); $confirm_message = isset($prefs['delete_document_from_tms']) === false ? __('Are you sure you want to do this?', 'wp-lingotek') : __('Are you sure you want to do this? The document will be deleted from Lingotek TMS.', 'wp-lingotek'); $confirm_message = sprintf(' onclick = "return confirm(\'%s\');"', $confirm_message); printf('<strong>%s</strong><br><br>', __('Translation Profile', 'wp-lingotek')); printf('<em>%s</em><br>', __('Disassociate this content to change the Translation Profile', 'wp-lingotek')); printf('<a class="button button-small" href="%s" %s>%s</a><br><br>', esc_url($disassociate_url), $confirm_message, __('Disassociate', 'wp-lingotek')); printf('<select disabled class="lingotek-profile-setting" name="%1$s" id="%1$s">', 'lingotek_profile_meta'); } else { printf('<strong>%s</strong><br><br>', __('Translation Profile', 'wp-lingotek')); printf('<select class="lingotek-profile-setting" name="%1$s" id="%1$s">', 'lingotek_profile_meta'); } foreach ($profiles as $key => $profile) { echo "\n\t<option value=" . esc_attr($key) . ">" . esc_attr($profile['name']) . '</option>'; } echo '</select>'; echo '<div id="lingotek-language-profiles" style="display: none;">' . json_encode($language_profiles) . '</div>'; }
/** * Lingotek */ function ajax_language_dashboard() { global $polylang; $request_method = isset($_REQUEST['_method']) ? $_REQUEST['_method'] : (isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'); $response = array('method' => $request_method); switch ($request_method) { case 'POST': if (isset($_REQUEST['code'], $_REQUEST['native'], $_REQUEST['direction'])) { $name = $_REQUEST['native']; $slug = substr($_REQUEST['code'], 0, strpos($_REQUEST['code'], '_')); // 3rd parameter of strstr needs PHP 5.3 $locale = Lingotek::map_to_wp_locale($_REQUEST['code']); // avoid conflicts between language slugs $existing_slugs = $polylang->model->get_languages_list(array('fields' => 'slug')); if (!empty($existing_slugs) && in_array($slug, $existing_slugs)) { $slug = strtolower(str_replace('_', '-', $locale)); } $rtl = $_REQUEST['direction'] == 'RTL'; $term_group = 0; // adds the language $polylang->model->add_language(compact('name', 'slug', 'locale', 'rtl', 'term_group')); // attempts to install the language pack require_once ABSPATH . 'wp-admin/includes/translation-install.php'; wp_download_language_pack($locale); // force checking for themes and plugins translations updates wp_update_themes(); wp_update_plugins(); $response = array('request' => 'POST: add target language to CMS and Lingotek Project Language', 'locale' => $_REQUEST['code'], 'xcode' => $locale, 'active' => 1, 'enabled' => 1, 'source' => self::get_counts_by_type($locale, 'sources'), 'target' => self::get_counts_by_type($locale, 'targets')); status_header(200); } break; case 'DELETE': $body = file_get_contents("php://input"); $code = str_replace('code=', '', $body); $lang = $polylang->model->get_language(Lingotek::map_to_wp_locale($code)); // map code to WP locales to find the language // prevents deleting the last language as it would break the Lingotek dashboard if (1 == count($polylang->model->get_languages_list())) { $response = array('request' => sprintf('DELETE: remove language from CMS and project (%s)', $code), 'code' => $code, 'success' => false, 'message' => __('You must keep at least one language.', 'wp-lingotek')); status_header(403); } elseif (!self::has_language_content($lang)) { $default_category = pll_get_term(get_option('default_category'), $lang->slug); $polylang->model->delete_language((int) $lang->term_id); wp_delete_term($default_category, 'category'); // delete the default category after the language // Deletes the translation status so when re-adding a language the string groups translations won't display as current $lingotek_model = new Lingotek_Model(); $strings = $lingotek_model->get_strings(); foreach ($strings as $string) { $group = $lingotek_model->get_group('string', $string['context']); unset($group->translations[$lang->locale]); $group->save(); } $response = array('request' => sprintf('DELETE: remove language from CMS and project (%s)', $code), 'code' => $code, 'active' => false, 'success' => true); status_header(204); } else { $response = array('request' => sprintf('DELETE: remove language from CMS and project (%s)', $code), 'code' => $code, 'success' => false, 'message' => __('The language can only be removed when no existing content is using it. If you would like to remove this language from the site, then first remove any content assigned to this language.', 'wp-lingotek')); status_header(403); } break; case 'GET': default: $locale_code = isset($_REQUEST['code']) ? $_REQUEST['code'] : NULL; $response = $response + $this->get_language_details($locale_code); break; } wp_send_json($response); }