public function __construct($language, $term_language = null)
 {
     // build the object from all properties stored as an array
     if (empty($term_language)) {
         foreach ($language as $prop => $value) {
             $this->{$prop} = $value;
         }
     } else {
         foreach ($language as $prop => $value) {
             $this->{$prop} = in_array($prop, array('term_id', 'term_taxonomy_id', 'count')) ? (int) $language->{$prop} : $language->{$prop};
         }
         // although it would be convenient here, don't assume the term is shared between taxonomies as it may not be the case in future
         // http://make.wordpress.org/core/2013/07/28/potential-roadmap-for-taxonomy-meta-and-post-relationships/
         $this->tl_term_id = (int) $term_language->term_id;
         $this->tl_term_taxonomy_id = (int) $term_language->term_taxonomy_id;
         $this->tl_count = (int) $term_language->count;
         $description = maybe_unserialize($language->description);
         $this->locale = $description['locale'];
         $this->is_rtl = $description['rtl'];
         $this->description =& $this->locale;
         // backward compatibility with Polylang < 1.2
         $this->mo_id = PLL_MO::get_id($this);
         $this->set_flag();
     }
 }
Example #2
0
 /**
  * Adds a new language
  * Creates a default category for this language
  *
  * List of arguments that $args must contain:
  * name           -> language name ( used only for display )
  * slug           -> language code ( ideally 2-letters ISO 639-1 language code )
  * locale         -> WordPress locale. If something wrong is used for the locale, the .mo files will not be loaded...
  * rtl            -> 1 if rtl language, 0 otherwise
  * term_group     -> language order when displayed
  *
  * Optional arguments that $args can contain:
  * no_default_cat -> if set, no default category will be created for this language
  * flag           -> country code, see flags.php
  *
  * @since 1.2
  *
  * @param array $args
  * @return bool true if success / false if failed
  */
 public function add_language($args)
 {
     if (!$this->validate_lang($args)) {
         return false;
     }
     // First the language taxonomy
     $description = serialize(array('locale' => $args['locale'], 'rtl' => (int) $args['rtl'], 'flag_code' => empty($args['flag']) ? '' : $args['flag']));
     $r = wp_insert_term($args['name'], 'language', array('slug' => $args['slug'], 'description' => $description));
     if (is_wp_error($r)) {
         // Avoid an ugly fatal error if something went wrong ( reported once in the forum )
         add_settings_error('general', 'pll_add_language', __('Impossible to add the language.', 'polylang'));
         return false;
     }
     wp_update_term((int) $r['term_id'], 'language', array('term_group' => (int) $args['term_group']));
     // can't set the term group directly in wp_insert_term
     // The term_language taxonomy
     // Don't want shared terms so use a different slug
     wp_insert_term($args['name'], 'term_language', array('slug' => 'pll_' . $args['slug']));
     $this->clean_languages_cache();
     // Udpate the languages list now !
     if (!isset($this->options['default_lang'])) {
         // If this is the first language created, set it as default language
         $this->options['default_lang'] = $args['slug'];
         update_option('polylang', $this->options);
         // And assign default language to default category
         $this->term->set_language((int) get_option('default_category'), (int) $r['term_id']);
     } elseif (empty($args['no_default_cat'])) {
         $this->create_default_category($args['slug']);
     }
     // Init a mo_id for this language
     $mo = new PLL_MO();
     $mo->export_to_db($this->get_language($args['slug']));
     /**
      * Fires when a language is added
      *
      * @since 1.9
      *
      * @param array $args arguments used to create the language
      */
     do_action('pll_add_language', $args);
     $this->clean_languages_cache();
     // Again to set add mo_id in the cached languages list
     flush_rewrite_rules();
     // Refresh rewrite rules
     add_settings_error('general', 'pll_languages_created', __('Language added.', 'polylang'), 'updated');
     return true;
 }
Example #3
0
 function process_posts()
 {
     $menu_items = $mo_posts = array();
     // store this for future usage as parent function unset $this->posts
     foreach ($this->posts as $post) {
         if ('nav_menu_item' == $post['post_type']) {
             $menu_items[] = $post;
         }
         if (0 === strpos($post['post_title'], 'polylang_mo_')) {
             $mo_posts[] = $post;
         }
     }
     if (!empty($mo_posts)) {
         new PLL_MO();
     }
     // just to register the polylang_mo post type before processing posts
     parent::process_posts();
     global $polylang;
     $polylang->model->clean_languages_cache();
     // to update the posts count in (cached) languages list
     $this->remap_translations($this->post_translations, $this->processed_posts);
     unset($this->post_translations);
     // language switcher menu items
     foreach ($menu_items as $item) {
         foreach ($item['postmeta'] as $meta) {
             if ('_pll_menu_item' == $meta['key']) {
                 update_post_meta($this->processed_menu_items[$item['post_id']], '_pll_menu_item', maybe_unserialize($meta['value']));
             }
         }
     }
     // merge strings translations
     foreach ($mo_posts as $post) {
         $lang_id = (int) substr($post['post_title'], 12);
         if (!empty($this->processed_terms[$lang_id])) {
             if ($strings = unserialize($post['post_content'])) {
                 $mo = new PLL_MO();
                 $mo->import_from_db($this->processed_terms[$lang_id]);
                 foreach ($strings as $msg) {
                     $mo->add_entry_or_merge($mo->make_entry($msg[0], $msg[1]));
                 }
                 $mo->export_to_db($this->processed_terms[$lang_id]);
             }
         }
         // delete the now useless imported post
         wp_delete_post($this->processed_posts[$post['post_id']], true);
     }
 }
 protected function upgrade_1_2_1()
 {
     // strings translations
     foreach (get_terms('language', array('hide_empty' => 0)) as $lang) {
         if ($strings = get_option('polylang_mo' . $lang->term_id)) {
             $mo = new PLL_MO();
             foreach ($strings as $msg) {
                 $mo->add_entry($mo->make_entry($msg[0], $msg[1]));
             }
             $mo->export_to_db($lang);
         }
     }
 }
Example #5
0
 public function load_strings_translations()
 {
     $mo = new PLL_MO();
     $mo->import_from_db($this->model->get_language(get_locale()));
     $GLOBALS['l10n']['pll_string'] =& $mo;
 }
Example #6
0
 /**
  * Reload text domains with order locale.
  *
  * @param string $language Language slug (e.g. en, de )
  */
 public function switchLanguage($language)
 {
     if (class_exists('Polylang')) {
         global $locale, $polylang, $woocommerce;
         static $cache;
         // Polylang string translations cache object to avoid loading the same translations object several times
         // Cache object not found. Create one...
         if (empty($cache)) {
             $cache = new \PLL_Cache();
         }
         //$current_language = pll_current_language( 'locale' );
         // unload plugin's textdomains
         unload_textdomain('default');
         unload_textdomain('woocommerce');
         // set locale to order locale
         $locale = apply_filters('locale', $language);
         $polylang->curlang->locale = $language;
         // Cache miss
         if (false === ($mo = $cache->get($language))) {
             $mo = new \PLL_MO();
             $mo->import_from_db($GLOBALS['polylang']->model->get_language($language));
             $GLOBALS['l10n']['pll_string'] =& $mo;
             // Add to cache
             $cache->set($language, $mo);
         }
         // (re-)load plugin's textdomain with order locale
         load_default_textdomain($language);
         $woocommerce->load_plugin_textdomain();
         $wp_locale = new \WP_Locale();
     }
 }
Example #7
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');
 }
Example #8
0
function pll_translate_string($string, $lang)
{
    if (pll_current_language() == $lang) {
        return pll__($string);
    }
    static $cache;
    // cache object to avoid loading the same translations object several times
    if (empty($cache)) {
        $cache = new PLL_Cache();
    }
    if (false === ($mo = $cache->get($lang))) {
        $mo = new PLL_MO();
        $mo->import_from_db(PLL()->model->get_language($lang));
        $cache->set($lang, $mo);
    }
    return $mo->translate($string);
}
Example #9
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';
 }
Example #10
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';
 }
 /**
  * Saves the strings translations in DB
  * Optionaly clean the DB
  *
  * @since 1.9
  */
 public function save_translations()
 {
     check_admin_referer('string-translation', '_wpnonce_string-translation');
     if (!empty($_POST['submit'])) {
         foreach ($this->languages 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) {
                 /**
                  * Filter the string translation before it is saved in DB
                  * Allows to sanitize strings registered with pll_register_string
                  *
                  * @since 1.6
                  *
                  * @param string $translation the string translation
                  * @param string $name        the name as defined in pll_register_string
                  * @param string $context     the context as defined in pll_register_string
                  */
                 $translation = apply_filters('pll_sanitize_string_translation', $translation, $this->strings[$key]['name'], $this->strings[$key]['context']);
                 $mo->add_entry($mo->make_entry($this->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 ($this->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');
         /**
          * Fires after the strings translations are saved in DB
          *
          * @since 1.2
          */
         do_action('pll_save_strings_translations');
     }
     // Unregisters strings registered through WPML API
     if ($this->current_action() === 'delete' && !empty($_POST['strings']) && function_exists('icl_unregister_string')) {
         foreach ($_POST['strings'] as $key) {
             icl_unregister_string($this->strings[$key]['context'], $this->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($_GET['paged']) && !empty($_POST['submit'])) {
         $args['paged'] = (int) $_GET['paged'];
         // Don't rely on $_REQUEST['paged'] or $_POST['paged']. See #14
     }
     if (!empty($args['s'])) {
         $args['s'] = urlencode($args['s']);
         // Searched string needs to be encoded as it comes from $_POST
     }
     PLL_Settings::redirect($args);
 }
Example #12
0
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);
    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 Lingotek_Table_String(compact('languages', 'groups', 'selected'));
$string_table->prepare_items($data);
?>

<div class="form-wrap">
	<form id="string-translation" method="post" action="admin.php?page=mlang&amp;tab=strings&amp;noheader=true">
Example #13
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');
     }
     $active_tab = !empty($_GET['tab']) ? $_GET['tab'] : 'lang';
     switch ($active_tab) {
         case 'lang':
             // prepare the list table of languages
             $list_table = new PLL_Table_Languages();
             $list_table->prepare_items($listlanguages);
             // error messages for data validation
             $errors[1] = __('Enter a valid WordPress locale', 'polylang');
             $errors[2] = __('The language code contains invalid characters', 'polylang');
             $errors[3] = __('The language code must be unique', 'polylang');
             $errors[4] = __('The language must have a name', 'polylang');
             $errors[5] = __('The language was created, but the WordPress language file was not downloaded. Please install it manually.', 'polylang');
             break;
         case 'strings':
             // get the strings to translate
             $data = $this->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);
                 foreach ($data as $key => $row) {
                     $data[$key]['translations'][$language->name] = $mo->translate($row['string']);
                     $data[$key]['row'] = $key;
                     // store the row number for convenience
                 }
             }
             $string_table = new PLL_Table_String($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;
     }
     $using_permalinks = $GLOBALS['wp_rewrite']->using_permalinks();
     $action = isset($_REQUEST['pll_action']) ? $_REQUEST['pll_action'] : '';
     switch ($action) {
         case 'add':
             check_admin_referer('add-lang', '_wpnonce_add-lang');
             $error = $this->model->add_language($_POST);
             if (0 == $error && !PLL_Admin::download_mo($_POST['locale'])) {
                 $error = 5;
             }
             wp_redirect('admin.php?page=mlang' . ($error ? '&error=' . $error : ''));
             // to refresh the page (possible thanks to the $_GET['noheader']=true)
             exit;
             break;
         case 'delete':
             check_admin_referer('delete-lang');
             if (!empty($_GET['lang'])) {
                 $this->model->delete_language((int) $_GET['lang']);
             }
             wp_redirect('admin.php?page=mlang');
             // to refresh the page (possible thanks to the $_GET['noheader']=true)
             exit;
             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);
             wp_redirect('admin.php?page=mlang' . ($error ? '&error=' . $error : ''));
             // to refresh the page (possible thanks to the $_GET['noheader']=true)
             exit;
             break;
         case 'string-translation':
             if (!empty($_REQUEST['submit'])) {
                 check_admin_referer('string-translation', '_wpnonce_string-translation');
                 $strings = $this->get_strings();
                 foreach ($this->model->get_languages_list() as $language) {
                     if (empty($_POST['translation'][$language->name])) {
                         // 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->name] as $key => $translation) {
                         $mo->add_entry($mo->make_entry($strings[$key]['string'], stripslashes($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);
                 }
             }
             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 = $this->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)
             $url = 'admin.php?page=mlang&tab=strings';
             foreach (array('s', 'paged', 'group') as $qv) {
                 $url = empty($_REQUEST[$qv]) ? $url : $url . '&' . $qv . '=' . $_REQUEST[$qv];
             }
             wp_redirect($url);
             exit;
             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;
             }
             // FIXME : TODO error message if not a valid url
             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));
                 }
                 $this->options['domains'][$this->options['default_lang']] = get_option('home');
             }
             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']);
                 }
             }
             wp_redirect('admin.php?page=mlang&tab=settings&updated=true');
             // updated=true interpreted by WP
             exit;
             break;
         default:
             break;
     }
     // displays the page
     include PLL_ADMIN_INC . '/view-languages.php';
 }
Example #14
0
 /**
  * Upgrades if the previous version is < 2.1
  * Moves strings translations from polylang_mo post_content to post meta _pll_strings_translations
  *
  * @since 2.0.8
  */
 protected function upgrade_2_1()
 {
     foreach (get_terms('language', array('hide_empty' => 0)) as $lang) {
         $mo_id = PLL_MO::get_id($lang);
         $meta = get_post_meta($mo_id, '_pll_strings_translations', true);
         if (empty($meta)) {
             $post = get_post($mo_id, OBJECT);
             $strings = unserialize($post->post_content);
             if (is_array($strings)) {
                 update_post_meta($mo_id, '_pll_strings_translations', $strings);
             }
         }
     }
 }