/**
  * 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'));
 }
Example #2
0
 /**
  * Constructor
  *
  * @since 1.2
  *
  * @param object $links_model
  */
 public function __construct(&$links_model)
 {
     parent::__construct($links_model);
     $this->active_tab = !empty($_GET['tab']) ? $_GET['tab'] : 'lang';
     PLL_Admin_Strings::init();
     // FIXME put this as late as possible
     add_action('admin_init', array($this, 'register_settings_modules'));
     // adds screen options and the about box in the languages admin panel
     add_action('load-settings_page_mlang', array($this, 'load_page'));
     // saves per-page value in screen option
     add_filter('set-screen-option', array($this, 'set_screen_option'), 10, 3);
 }
Example #3
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;
 }
Example #4
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 #5
0
function pll_register_string($name, $string, $context = 'polylang', $multiline = false)
{
    if (PLL_ADMIN) {
        PLL_Admin_Strings::register_string($name, $string, $context, $multiline);
    }
}
Example #6
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 #7
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';
 }
Example #8
0
 public static function &get_strings()
 {
     self::$default_strings = array('options' => array('blogname' => __('Site Title'), 'blogdescription' => __('Tagline'), 'date_format' => __('Date Format'), 'time_format' => __('Time Format')), 'widget_title' => __('Widget title', 'polylang'), 'widget_text' => __('Widget text', 'polylang'));
     // WP strings
     foreach (self::$default_strings['options'] as $option => $string) {
         self::register_string($string, get_option($option), 'WordPress');
     }
     // widgets titles
     global $wp_registered_widgets;
     $sidebars = wp_get_sidebars_widgets();
     foreach ($sidebars as $sidebar => $widgets) {
         if ($sidebar == 'wp_inactive_widgets' || empty($widgets)) {
             continue;
         }
         foreach ($widgets as $widget) {
             // nothing can be done if the widget is created using pre WP2.8 API :(
             // there is no object, so we can't access it to get the widget options
             if (!isset($wp_registered_widgets[$widget]['callback'][0]) || !is_object($wp_registered_widgets[$widget]['callback'][0]) || !method_exists($wp_registered_widgets[$widget]['callback'][0], 'get_settings')) {
                 continue;
             }
             $widget_settings = $wp_registered_widgets[$widget]['callback'][0]->get_settings();
             $number = $wp_registered_widgets[$widget]['params'][0]['number'];
             // don't enable widget translation if the widget is visible in only one language or if there is no title
             if (empty($widget_settings[$number]['pll_lang'])) {
                 if (isset($widget_settings[$number]['title']) && ($title = $widget_settings[$number]['title'])) {
                     self::register_string(self::$default_strings['widget_title'], $title, 'Widget');
                 }
                 if (isset($widget_settings[$number]['text']) && ($text = $widget_settings[$number]['text'])) {
                     self::register_string(self::$default_strings['widget_text'], $text, 'Widget', true);
                 }
             }
         }
     }
     // allow plugins to modify our list of strings, mainly for use by our PLL_WPML_Compat class
     self::$strings = apply_filters('pll_get_strings', self::$strings);
     return self::$strings;
 }
Example #9
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);
Example #10
0
function pll_register_string($name, $string, $context = 'polylang', $multiline = false)
{
    global $polylang;
    if ($polylang instanceof PLL_Admin) {
        PLL_Admin_Strings::register_string($name, $string, $context, $multiline);
    }
}