Пример #1
0
 /**
  * Constructor
  *
  * @since 0.6
  *
  * @param array $languages list of languages
  */
 function __construct($languages)
 {
     parent::__construct(array('plural' => 'Strings translations', 'ajax' => false));
     $this->languages = $languages;
     $this->strings = PLL_Admin_Strings::get_strings();
     $this->groups = array_unique(wp_list_pluck($this->strings, 'context'));
     $this->selected_group = empty($_GET['group']) || !in_array($_GET['group'], $this->groups) ? -1 : $_GET['group'];
     add_action('mlang_action_string-translation', array($this, 'save_translations'));
 }
Пример #2
0
 public static function get_strings()
 {
     static $strings = array();
     if (empty($strings)) {
         PLL_Admin_Strings::init();
         // enables sanitization filter
         foreach (PLL_Admin_Strings::get_strings() as $string) {
             $strings[$string['context']]['context'] = $string['context'];
             $strings[$string['context']]['count'] = empty($strings[$string['context']]['count']) ? 1 : $strings[$string['context']]['count'] + 1;
         }
         $strings = array_values($strings);
     }
     return $strings;
 }
Пример #3
0
 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');
 }
Пример #4
0
 public function languages_page()
 {
     // prepare the list of tabs
     $tabs = array('lang' => __('Languages', 'polylang'));
     // only if at least one language has been created
     if ($listlanguages = $this->model->get_languages_list()) {
         $tabs['strings'] = __('Strings translation', 'polylang');
         $tabs['settings'] = __('Settings', 'polylang');
     }
     // allows plugins to add tabs
     $tabs = apply_filters('pll_settings_tabs', $tabs);
     switch ($this->active_tab) {
         case 'lang':
             // prepare the list table of languages
             $list_table = new PLL_Table_Languages();
             $list_table->prepare_items($listlanguages);
             break;
         case 'strings':
             // get the strings to translate
             $data = PLL_Admin_Strings::get_strings();
             // get the groups
             foreach ($data as $key => $row) {
                 $groups[] = $row['context'];
             }
             $groups = array_unique($groups);
             $selected = empty($_GET['group']) || !in_array($_GET['group'], $groups) ? -1 : $_GET['group'];
             $s = empty($_GET['s']) ? '' : wp_unslash($_GET['s']);
             // filter for search string
             foreach ($data as $key => $row) {
                 if (-1 != $selected && $row['context'] != $selected || !empty($s) && stripos($row['name'], $s) === false && stripos($row['string'], $s) === false) {
                     unset($data[$key]);
                 }
             }
             // load translations
             foreach ($listlanguages as $language) {
                 // filters by language if requested
                 if (($lg = get_user_meta(get_current_user_id(), 'pll_filter_content', true)) && $language->slug != $lg) {
                     continue;
                 }
                 $mo = new PLL_MO();
                 $mo->import_from_db($language);
                 foreach ($data as $key => $row) {
                     $data[$key]['translations'][$language->slug] = $mo->translate($row['string']);
                     $data[$key]['row'] = $key;
                     // store the row number for convenience
                 }
             }
             // get an array with language slugs as keys, names as values
             $languages = array_combine(wp_list_pluck($listlanguages, 'slug'), wp_list_pluck($listlanguages, 'name'));
             $string_table = new PLL_Table_String(compact('languages', 'groups', 'selected'));
             $string_table->prepare_items($data);
             break;
         case 'settings':
             $post_types = get_post_types(array('public' => true, '_builtin' => false));
             $post_types = array_diff($post_types, get_post_types(array('_pll' => true)));
             $post_types = array_unique(apply_filters('pll_get_post_types', $post_types, true));
             $taxonomies = get_taxonomies(array('public' => true, '_builtin' => false));
             $taxonomies = array_diff($taxonomies, get_taxonomies(array('_pll' => true)));
             $taxonomies = array_unique(apply_filters('pll_get_taxonomies', $taxonomies, true));
             break;
     }
     $action = isset($_REQUEST['pll_action']) ? $_REQUEST['pll_action'] : '';
     switch ($action) {
         case 'add':
             check_admin_referer('add-lang', '_wpnonce_add-lang');
             if ($this->model->add_language($_POST) && 'en_US' != $_POST['locale']) {
                 // attempts to install the language pack
                 require_once ABSPATH . 'wp-admin/includes/translation-install.php';
                 if (!wp_download_language_pack($_POST['locale'])) {
                     add_settings_error('general', 'pll_download_mo', __('The language was created, but the WordPress language file was not downloaded. Please install it manually.', 'polylang'));
                 }
                 // force checking for themes and plugins translations updates
                 wp_clean_themes_cache();
                 wp_clean_plugins_cache();
             }
             self::redirect();
             // to refresh the page ( possible thanks to the $_GET['noheader']=true )
             break;
         case 'delete':
             check_admin_referer('delete-lang');
             if (!empty($_GET['lang'])) {
                 $this->model->delete_language((int) $_GET['lang']);
             }
             self::redirect();
             // to refresh the page ( possible thanks to the $_GET['noheader']=true )
             break;
         case 'edit':
             if (!empty($_GET['lang'])) {
                 $edit_lang = $this->model->get_language((int) $_GET['lang']);
             }
             break;
         case 'update':
             check_admin_referer('add-lang', '_wpnonce_add-lang');
             $error = $this->model->update_language($_POST);
             self::redirect();
             // to refresh the page ( possible thanks to the $_GET['noheader']=true )
             break;
         case 'default-lang':
             check_admin_referer('default-lang');
             if ($lang = $this->model->get_language((int) $_GET['lang'])) {
                 $this->model->update_default_lang($lang->slug);
             }
             self::redirect();
             // to refresh the page ( possible thanks to the $_GET['noheader']=true )
             break;
         case 'content-default-lang':
             check_admin_referer('content-default-lang');
             if ($nolang = $this->model->get_objects_with_no_lang()) {
                 if (!empty($nolang['posts'])) {
                     $this->model->set_language_in_mass('post', $nolang['posts'], $this->options['default_lang']);
                 }
                 if (!empty($nolang['terms'])) {
                     $this->model->set_language_in_mass('term', $nolang['terms'], $this->options['default_lang']);
                 }
             }
             self::redirect();
             // to refresh the page ( possible thanks to the $_GET['noheader']=true )
             break;
         case 'string-translation':
             if (!empty($_POST['submit'])) {
                 check_admin_referer('string-translation', '_wpnonce_string-translation');
                 $strings = PLL_Admin_Strings::get_strings();
                 foreach ($this->model->get_languages_list() as $language) {
                     if (empty($_POST['translation'][$language->slug])) {
                         // in case the language filter is active ( thanks to John P. Bloch )
                         continue;
                     }
                     $mo = new PLL_MO();
                     $mo->import_from_db($language);
                     foreach ($_POST['translation'][$language->slug] as $key => $translation) {
                         $translation = apply_filters('pll_sanitize_string_translation', $translation, $strings[$key]['name'], $strings[$key]['context']);
                         $mo->add_entry($mo->make_entry($strings[$key]['string'], $translation));
                     }
                     // clean database ( removes all strings which were registered some day but are no more )
                     if (!empty($_POST['clean'])) {
                         $new_mo = new PLL_MO();
                         foreach ($strings as $string) {
                             $new_mo->add_entry($mo->make_entry($string['string'], $mo->translate($string['string'])));
                         }
                     }
                     isset($new_mo) ? $new_mo->export_to_db($language) : $mo->export_to_db($language);
                 }
                 add_settings_error('general', 'pll_strings_translations_updated', __('Translations updated.', 'polylang'), 'updated');
                 do_action('pll_save_strings_translations');
             }
             // unregisters strings registered through WPML API
             if ($string_table->current_action() == 'delete' && !empty($_POST['strings']) && function_exists('icl_unregister_string')) {
                 check_admin_referer('string-translation', '_wpnonce_string-translation');
                 $strings = PLL_Admin_Strings::get_strings();
                 foreach ($_POST['strings'] as $key) {
                     icl_unregister_string($strings[$key]['context'], $strings[$key]['name']);
                 }
             }
             // to refresh the page ( possible thanks to the $_GET['noheader']=true )
             $args = array_intersect_key($_REQUEST, array_flip(array('s', 'paged', 'group')));
             if (!empty($args['s'])) {
                 $args['s'] = urlencode($args['s']);
                 // searched string needs to be encoded as it comes from $_POST
             }
             self::redirect($args);
             break;
         case 'activate':
             check_admin_referer('pll_activate');
             $this->modules[$_GET['module']]->activate();
             self::redirect();
             break;
         case 'deactivate':
             check_admin_referer('pll_deactivate');
             $this->modules[$_GET['module']]->deactivate();
             self::redirect();
             break;
         default:
             do_action("mlang_action_{$action}");
             break;
     }
     // displays the page
     include PLL_SETTINGS_INC . '/view-languages.php';
 }
Пример #5
0
 public function languages_page()
 {
     // prepare the list of tabs
     $tabs = array('lang' => __('Languages', 'polylang'));
     // only if at least one language has been created
     if ($listlanguages = $this->model->get_languages_list()) {
         $tabs['strings'] = __('Strings translation', 'polylang');
         $tabs['settings'] = __('Settings', 'polylang');
     }
     $tabs = apply_filters('pll_settings_tabs', $tabs);
     switch ($this->active_tab) {
         case 'lang':
             // prepare the list table of languages
             $list_table = new PLL_Table_Languages();
             $list_table->prepare_items($listlanguages);
             break;
         case 'strings':
             // get the strings to translate
             $data = PLL_Admin_Strings::get_strings();
             // get the groups
             foreach ($data as $key => $row) {
                 $groups[] = $row['context'];
             }
             $groups = array_unique($groups);
             $selected = empty($_REQUEST['group']) || !in_array($_REQUEST['group'], $groups) ? -1 : $_REQUEST['group'];
             $s = empty($_REQUEST['s']) ? '' : wp_unslash($_REQUEST['s']);
             // filter for search string
             foreach ($data as $key => $row) {
                 if ($selected != -1 && $row['context'] != $selected || !empty($s) && stripos($row['name'], $s) === false && stripos($row['string'], $s) === false) {
                     unset($data[$key]);
                 }
             }
             // load translations
             foreach ($listlanguages as $language) {
                 // filters by language if requested
                 if (($lg = get_user_meta(get_current_user_id(), 'pll_filter_content', true)) && $language->slug != $lg) {
                     continue;
                 }
                 $mo = new PLL_MO();
                 // print_r($language);
                 $mo->import_from_db($language);
                 foreach ($data as $key => $row) {
                     $data[$key]['translations'][$language->slug] = $mo->translate($row['string']);
                     $data[$key]['row'] = $key;
                     // store the row number for convenience
                 }
             }
             // get an array with language slugs as keys, names as values
             $languages = array_combine(wp_list_pluck($listlanguages, 'slug'), wp_list_pluck($listlanguages, 'name'));
             $string_table = new PLL_Table_String(compact('languages', 'groups', 'selected'));
             $string_table->prepare_items($data);
             break;
         case 'settings':
             $post_types = get_post_types(array('public' => true, '_builtin' => false));
             $post_types = array_diff($post_types, get_post_types(array('_pll' => true)));
             $post_types = array_unique(apply_filters('pll_get_post_types', $post_types, true));
             $taxonomies = get_taxonomies(array('public' => true, '_builtin' => false));
             $taxonomies = array_diff($taxonomies, get_taxonomies(array('_pll' => true)));
             $taxonomies = array_unique(apply_filters('pll_get_taxonomies', $taxonomies, true));
             break;
         default:
             break;
     }
     $action = isset($_REQUEST['pll_action']) ? $_REQUEST['pll_action'] : '';
     switch ($action) {
         case 'add':
             check_admin_referer('add-lang', '_wpnonce_add-lang');
             if ($this->model->add_language($_POST)) {
                 // backward compatibility WP < 4.0
                 if (version_compare($GLOBALS['wp_version'], '4.0', '<')) {
                     PLL_Admin::download_mo($_POST['locale']);
                 } elseif ('en_US' != $_POST['locale']) {
                     // attempts to install the language pack
                     require_once ABSPATH . 'wp-admin/includes/translation-install.php';
                     if (!wp_download_language_pack($_POST['locale'])) {
                         add_settings_error('general', 'pll_download_mo', __('The language was created, but the WordPress language file was not downloaded. Please install it manually.', 'polylang'));
                     }
                     // force checking for themes and plugins translations updates
                     wp_update_themes();
                     wp_update_plugins();
                 }
             }
             $this->redirect();
             // to refresh the page (possible thanks to the $_GET['noheader']=true)
             break;
         case 'delete':
             check_admin_referer('delete-lang');
             if (!empty($_GET['lang'])) {
                 $this->model->delete_language((int) $_GET['lang']);
             }
             $this->redirect();
             // to refresh the page (possible thanks to the $_GET['noheader']=true)
             break;
         case 'edit':
             if (!empty($_GET['lang'])) {
                 $edit_lang = $this->model->get_language((int) $_GET['lang']);
             }
             break;
         case 'update':
             check_admin_referer('add-lang', '_wpnonce_add-lang');
             $error = $this->model->update_language($_POST);
             $this->redirect();
             // to refresh the page (possible thanks to the $_GET['noheader']=true)
             break;
         case 'string-translation':
             if (!empty($_REQUEST['submit'])) {
                 check_admin_referer('string-translation', '_wpnonce_string-translation');
                 $strings = PLL_Admin_Strings::get_strings();
                 foreach ($this->model->get_languages_list() as $language) {
                     if (empty($_POST['translation'][$language->slug])) {
                         // in case the language filter is active (thanks to John P. Bloch)
                         continue;
                     }
                     $mo = new PLL_MO();
                     $mo->import_from_db($language);
                     foreach ($_POST['translation'][$language->slug] as $key => $translation) {
                         $translation = apply_filters('pll_sanitize_string_translation', $translation, $strings[$key]['name'], $strings[$key]['context']);
                         $mo->add_entry($mo->make_entry($strings[$key]['string'], $translation));
                     }
                     // clean database (removes all strings which were registered some day but are no more)
                     if (!empty($_POST['clean'])) {
                         $new_mo = new PLL_MO();
                         foreach ($strings as $string) {
                             $new_mo->add_entry($mo->make_entry($string['string'], $mo->translate($string['string'])));
                         }
                     }
                     isset($new_mo) ? $new_mo->export_to_db($language) : $mo->export_to_db($language);
                 }
                 add_settings_error('general', 'pll_strings_translations_updated', __('Translations updated.', 'polylang'), 'updated');
             }
             do_action('pll_save_strings_translations');
             // unregisters strings registered through WPML API
             if ($string_table->current_action() == 'delete' && !empty($_REQUEST['strings']) && function_exists('icl_unregister_string')) {
                 check_admin_referer('string-translation', '_wpnonce_string-translation');
                 $strings = PLL_Admin_Strings::get_strings();
                 foreach ($_REQUEST['strings'] as $key) {
                     icl_unregister_string($strings[$key]['context'], $strings[$key]['name']);
                 }
             }
             // to refresh the page (possible thanks to the $_GET['noheader']=true)
             $this->redirect(array_intersect_key($_REQUEST, array_flip(array('s', 'paged', 'group'))));
             break;
         case 'options':
             check_admin_referer('options-lang', '_wpnonce_options-lang');
             $this->options['default_lang'] = sanitize_title($_POST['default_lang']);
             // we have slug as value
             foreach (array('force_lang', 'rewrite') as $key) {
                 $this->options[$key] = isset($_POST[$key]) ? (int) $_POST[$key] : 0;
             }
             if (3 == $this->options['force_lang'] && isset($_POST['domains']) && is_array($_POST['domains'])) {
                 foreach ($_POST['domains'] as $key => $domain) {
                     $this->options['domains'][$key] = esc_url_raw(trim($domain));
                 }
             }
             foreach (array('browser', 'hide_default', 'redirect_lang', 'media_support') as $key) {
                 $this->options[$key] = isset($_POST[$key]) ? 1 : 0;
             }
             if (3 == $this->options['force_lang']) {
                 $this->options['browser'] = $this->options['hide_default'] = 0;
             }
             foreach (array('sync', 'post_types', 'taxonomies') as $key) {
                 $this->options[$key] = empty($_POST[$key]) ? array() : array_keys($_POST[$key], 1);
             }
             update_option('polylang', $this->options);
             // refresh rewrite rules in case rewrite,  hide_default, post types or taxonomies options have been modified
             // it seems useless to refresh permastruct here
             flush_rewrite_rules();
             // refresh language cache in case home urls have been modified
             $this->model->clean_languages_cache();
             // fills existing posts & terms with default language
             if (isset($_POST['fill_languages']) && ($nolang = $this->model->get_objects_with_no_lang())) {
                 if (!empty($nolang['posts'])) {
                     $this->model->set_language_in_mass('post', $nolang['posts'], $this->options['default_lang']);
                 }
                 if (!empty($nolang['terms'])) {
                     $this->model->set_language_in_mass('term', $nolang['terms'], $this->options['default_lang']);
                 }
             }
             add_settings_error('general', 'settings_updated', __('Settings saved.'), 'updated');
             $this->redirect();
             break;
         default:
             break;
     }
     // displays the page
     include PLL_ADMIN_INC . '/view-languages.php';
 }
Пример #6
0
<h3 style="padding-top:0; margin-top:0; margin-bottom:-25px;"><?php 
_e('Strings', 'wp-lingotek');
?>
 <a href="options-general.php?page=mlang&amp;tab=strings" title="<?php 
_e('Edit on Polylang Strings Translation page', 'wp-lingotek');
?>
" class="dashicons dashicons-edit"></a></h3>

<?php 
$listlanguages = $GLOBALS['polylang']->model->get_languages_list();
// FIXME now mainly copy paste of Polylang
// get the strings to translate
$data = PLL_Admin_Strings::get_strings();
$selected = empty($_REQUEST['group']) ? -1 : $_REQUEST['group'];
foreach ($data as $key => $row) {
    $groups[] = $row['context'];
    // get the groups
    // filter for search string
    if ($selected != -1 && $row['context'] != $selected || !empty($_REQUEST['s']) && stripos($row['name'], $_REQUEST['s']) === false && stripos($row['string'], $_REQUEST['s']) === false) {
        unset($data[$key]);
    }
}
$groups = array_unique($groups);
// load translations
foreach ($listlanguages as $language) {
    // filters by language if requested
    if (($lg = get_user_meta(get_current_user_id(), 'pll_filter_content', true)) && $language->slug != $lg) {
        continue;
    }
    $mo = new PLL_MO();
    $mo->import_from_db($language);