Ejemplo n.º 1
0
 public function request($query_vars)
 {
     if (empty($query_vars['lingotek'])) {
         return $query_vars;
     }
     if (isset($_GET['type'], $_GET['document_id']) && ($document = $this->lgtm->get_group_by_id($_GET['document_id']))) {
         // url for in context review
         if (isset($_GET['locale']) && 'get' == $_GET['type']) {
             $locale = Lingotek::map_to_wp_locale($_GET['locale']);
             // map to WP locale
             // posts
             if (post_type_exists($document->type)) {
                 if ($id = $document->pllm->get_post($document->source, $locale)) {
                     wp_redirect(get_permalink($id), 301);
                     exit;
                 } else {
                     wp_redirect(get_permalink($document->source), 302);
                     exit;
                 }
             } elseif (taxonomy_exists($document->type) && ($id = $document->pllm->get_term($document->source, $locale))) {
                 wp_redirect(get_term_link($id, $document->type), 301);
                 exit;
             }
             status_header(404);
             // no document found
             die;
         }
         if ('document_uploaded' == $_GET['type']) {
             $document->source_ready();
             if ($document->is_automatic_upload()) {
                 $document->request_translations();
             }
         }
         if (isset($_GET['locale']) && 'target' == $_GET['type']) {
             // We will need access to PLL_Admin_Sync::copy_post_metas
             global $polylang;
             $polylang->sync = new PLL_Admin_Sync($polylang);
             $locale = Lingotek::map_to_wp_locale($_GET['locale']);
             // map to WP locale
             $document->is_automatic_download($locale) ? $document->create_translation($locale, true) : $document->translation_ready($locale);
         }
         status_header(200);
         // useless as it the default value
         die;
     }
     status_header(404);
     // no document found
     die;
 }
Ejemplo n.º 2
0
 /**
  * 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);
 }