protected function install_languages()
 {
     if (file_exists($file = ABSPATH . 'wp-admin/includes/translation-install.php')) {
         require_once $file;
     }
     if (!function_exists('wp_download_language_pack')) {
         $this->markTestSkipped('The wp_download_language_pack() function does not exist.');
         return;
     }
     $languages = array('fr' => 'fr_FR', 'uk' => 'en_GB', 'ar' => 'ar');
     foreach ($languages as $url_prefix => $lang) {
         $download = wp_download_language_pack($lang);
         $this->assertNotEmpty($download);
         $this->langs[$lang] = $download;
     }
     $active_langs = array_merge(array('en' => 'en_US'), $languages);
     $public_langs = array_values($active_langs);
     $lang_prefs = array();
     $langs = array();
     $lang_prefs['en'] = (object) array('display_name' => 'en_US', 'url_prefix' => 'en');
     $langs['en_US'] = (object) array('name' => 'en_US', 'code' => 'en_US', 'url_prefix' => 'en', 'text_direction' => 'ltr', 'display_name' => 'en_US');
     foreach ($languages as $url_prefix => $lang) {
         $lang_prefs[$url_prefix] = (object) array('display_name' => $lang, 'url_prefix' => $url_prefix);
         $langs[$lang] = (object) array('name' => $lang, 'code' => $lang, 'url_prefix' => $url_prefix, 'text_direction' => 'ltr', 'display_name' => $lang);
     }
     $default_lang = 'en_US';
     $option = get_option('babble-languages', array());
     $option['langs'] = $langs;
     $option['active_langs'] = $active_langs;
     $option['public_langs'] = $public_langs;
     $option['lang_prefs'] = $lang_prefs;
     $option['default_lang'] = $default_lang;
     update_option('babble-languages', $option);
     Babble::get('languages')->initiate();
 }
Exemplo n.º 2
0
function wpfa_activate()
{
    update_option('WPLANG', 'fa_IR');
    update_option('start_of_week', '6');
    update_option('timezone_string', 'Asia/Tehran');
    if (WPFA_NUMS === false) {
        add_option('wpfa_nums', 'on');
    }
    $inc = ABSPATH . 'wp-admin/includes/translation-install.php';
    if (file_exists($inc)) {
        require_once $inc;
        wp_download_language_pack('fa_IR');
    }
}
 /**
  * Install a given language.
  *
  * <language>
  * : Language code to install.
  *
  * @subcommand install
  */
 public function install($args, $assoc_args)
 {
     list($language_code) = $args;
     $available = wp_get_installed_translations($this->obj_type);
     $available = !empty($available['default']) ? array_keys($available['default']) : array();
     if (in_array($language_code, $available)) {
         \WP_CLI::warning("Language already installed.");
         exit;
     }
     require_once ABSPATH . '/wp-admin/includes/translation-install.php';
     $response = wp_download_language_pack($language_code);
     if ($response == $language_code) {
         \WP_CLI::success("Language installed.");
     } else {
         \WP_CLI::error("Couldn't install language.");
     }
 }
function bogo_load_tools_page()
{
    require_once ABSPATH . 'wp-admin/includes/translation-install.php';
    $action = isset($_GET['action']) ? $_GET['action'] : '';
    if ('install_translation' == $action) {
        check_admin_referer('bogo-tools');
        if (!current_user_can('update_core')) {
            wp_die(__('You are not allowed to install translations.', 'bogo'));
        }
        $locale = isset($_GET['locale']) ? $_GET['locale'] : null;
        if (wp_download_language_pack($locale)) {
            $redirect_to = add_query_arg(array('locale' => $locale, 'message' => 'success'), menu_page_url('bogo-tools', false));
        } else {
            $redirect_to = add_query_arg(array('locale' => $locale, 'message' => 'failed'), menu_page_url('bogo-tools', false));
        }
        wp_safe_redirect($redirect_to);
        exit;
    }
}
 public function load($lang = null)
 {
     if (null !== $lang) {
         require_once ABSPATH . 'wp-admin/includes/translation-install.php';
         global $locale;
         if (!in_array($lang, get_available_languages())) {
             if (array_key_exists($lang, wp_get_available_translations())) {
                 $language = wp_download_language_pack($lang);
                 if ($language) {
                     load_default_textdomain($language);
                     $locale = $language;
                 }
             }
         } else {
             load_default_textdomain($lang);
             $locale = $lang;
         }
     }
     $this->init();
     $this->register_globals();
 }
Exemplo n.º 6
0
}
switch ($step) {
    case -1:
        if (wp_can_install_language_pack() && empty($language) && ($languages = wp_get_available_translations())) {
            setup_config_display_header('language-chooser');
            echo '<h1 class="screen-reader-text">Select a default language</h1>';
            echo '<form id="setup" method="post" action="?step=0">';
            wp_install_language_form($languages);
            echo '</form>';
            break;
        }
        // Deliberately fall through if we can't reach the translations API.
    // Deliberately fall through if we can't reach the translations API.
    case 0:
        if (!empty($language)) {
            $loaded_language = wp_download_language_pack($language);
            if ($loaded_language) {
                load_default_textdomain($loaded_language);
                $GLOBALS['wp_locale'] = new WP_Locale();
            }
        }
        setup_config_display_header();
        $step_1 = 'setup-config.php?step=1';
        if (isset($_REQUEST['noapi'])) {
            $step_1 .= '&amp;noapi';
        }
        if (!empty($loaded_language)) {
            $step_1 .= '&amp;language=' . $loaded_language;
        }
        ?>
<h1 class="screen-reader-text"><?php 
Exemplo n.º 7
0
get_current_screen()->set_help_sidebar('<p><strong>' . __('For more information:') . '</strong></p>' . '<p>' . __('<a href="https://codex.wordpress.org/Network_Admin_Settings_Screen" target="_blank">Documentation on Network Settings</a>') . '</p>' . '<p>' . __('<a href="https://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>');
if ($_POST) {
    /** This action is documented in wp-admin/network/edit.php */
    do_action('wpmuadminedit');
    check_admin_referer('siteoptions');
    $checked_options = array('menu_items' => array(), 'registrationnotification' => 'no', 'upload_space_check_disabled' => 1, 'add_new_users' => 0);
    foreach ($checked_options as $option_name => $option_unchecked_value) {
        if (!isset($_POST[$option_name])) {
            $_POST[$option_name] = $option_unchecked_value;
        }
    }
    $options = array('registrationnotification', 'registration', 'add_new_users', 'menu_items', 'upload_space_check_disabled', 'blog_upload_space', 'upload_filetypes', 'site_name', 'first_post', 'first_page', 'first_comment', 'first_comment_url', 'first_comment_author', 'welcome_email', 'welcome_user_email', 'fileupload_maxk', 'global_terms_enabled', 'illegal_names', 'limited_email_domains', 'banned_email_domains', 'WPLANG', 'admin_email');
    // Handle translation install.
    if (!empty($_POST['WPLANG']) && wp_can_install_language_pack()) {
        // @todo: Skip if already installed
        $language = wp_download_language_pack($_POST['WPLANG']);
        if ($language) {
            $_POST['WPLANG'] = $language;
        }
    }
    foreach ($options as $option_name) {
        if (!isset($_POST[$option_name])) {
            continue;
        }
        $value = wp_unslash($_POST[$option_name]);
        update_site_option($option_name, $value);
    }
    /**
     * Fires after the network options are updated.
     *
     * @since MU
Exemplo n.º 8
0
 if (preg_match('|^([a-zA-Z0-9-])+$|', $blog['domain'])) {
     $domain = strtolower($blog['domain']);
 }
 // If not a subdomain install, make sure the domain isn't a reserved word
 if (!is_subdomain_install()) {
     /** This filter is documented in wp-includes/ms-functions.php */
     $subdirectory_reserved_names = apply_filters('subdirectory_reserved_names', array('page', 'comments', 'blog', 'files', 'feed', 'wp-admin', 'wp-content', 'wp-includes', 'wp-json'));
     if (in_array($domain, $subdirectory_reserved_names)) {
         wp_die(sprintf(__('The following words are reserved for use by WordPress functions and cannot be used as blog names: <code>%s</code>'), implode('</code>, <code>', $subdirectory_reserved_names)));
     }
 }
 $title = $blog['title'];
 $meta = array('public' => 1);
 // Handle translation install for the new site.
 if (!empty($_POST['WPLANG']) && wp_can_install_language_pack()) {
     $language = wp_download_language_pack(wp_unslash($_POST['WPLANG']));
     if ($language) {
         $meta['WPLANG'] = $language;
     }
 }
 if (empty($domain)) {
     wp_die(__('Missing or invalid site address.'));
 }
 if (isset($blog['email']) && '' === trim($blog['email'])) {
     wp_die(__('Missing email address.'));
 }
 $email = sanitize_email($blog['email']);
 if (!is_email($email)) {
     wp_die(__('Invalid email address.'));
 }
 if (is_subdomain_install()) {
Exemplo n.º 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';
 }
Exemplo n.º 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';
 }
Exemplo n.º 11
0
 /**
  * Install a given language.
  *
  * <language>
  * : Language code to install.
  *
  * [--activate]
  * : If set, the language will be activated immediately after install.
  *
  * @subcommand install
  */
 public function install($args, $assoc_args)
 {
     list($language_code) = $args;
     $available = $this->get_installed_languages();
     if (in_array($language_code, $available)) {
         \WP_CLI::warning("Language already installed.");
         exit;
     }
     require_once ABSPATH . '/wp-admin/includes/translation-install.php';
     $response = wp_download_language_pack($language_code);
     if ($response == $language_code) {
         \WP_CLI::success("Language installed.");
         if (\WP_CLI\Utils\get_flag_value($assoc_args, 'activate')) {
             $this->activate(array($language_code), array());
         }
     } else {
         \WP_CLI::error("Couldn't install language.");
     }
 }
Exemplo n.º 12
0
 /**
  * Manages the user input for the languages pages
  *
  * @since 1.9
  *
  * @param string $action
  */
 public function handle_actions($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 '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 '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:
             /**
              * Fires when a non default action has been sent to Polylang settings
              *
              * @since 1.8
              */
             do_action("mlang_action_{$action}");
             break;
     }
 }
 /**
  * Save meta-box.
  * Hook for 'save_post'
  *
  * @from 1.0
  */
 public function save_meta_box($post_id, $post)
 {
     global $sublanguage_admin;
     if ($post->post_type == $sublanguage_admin->language_post_type) {
         if ((!defined('DOING_AUTOSAVE') || !DOING_AUTOSAVE) && current_user_can('edit_language', $post_id)) {
             // save locale (into post_content)
             if (isset($_POST['language_locale_nonce'], $_POST['language_locale']) && wp_verify_nonce($_POST['language_locale_nonce'], 'language_locale_action')) {
                 $locale = esc_attr($_POST['language_locale']);
                 // download language pack
                 if ($post->post_content != $locale) {
                     require_once ABSPATH . 'wp-admin/includes/translation-install.php';
                     wp_download_language_pack($locale);
                     wp_update_post(array('ID' => $post->ID, 'post_content' => $locale));
                 }
                 flush_rewrite_rules();
             }
             // save settings
             if (isset($_POST['language_settings_nonce']) && wp_verify_nonce($_POST['language_settings_nonce'], 'language_settings_action')) {
                 if (isset($_POST['language_rtl']) && $_POST['language_rtl']) {
                     update_post_meta($post->ID, 'rtl', 1);
                 }
             }
         }
     }
 }
 /**
  * Download the chosen fallback language on save and create the language dropdown similar to the default language dropdown
  */
 public function fallback_locale_field()
 {
     $languages = get_available_languages();
     $translations = wp_get_available_translations();
     $fallback_locale = $this->fallback_locale;
     // Handle translation install.
     if (!empty($fallback_locale) && !in_array($fallback_locale, $languages) && (!is_multisite() || is_super_admin())) {
         require_once ABSPATH . 'wp-admin/includes/translation-install.php';
         if (wp_can_install_language_pack()) {
             $language = wp_download_language_pack($fallback_locale);
             if ($language) {
                 $fallback_locale = $language;
             }
         }
     }
     wp_dropdown_languages(array('name' => 'fallback_locale', 'id' => 'fallback_locale', 'selected' => $fallback_locale, 'languages' => $languages, 'translations' => $translations, 'show_available_translations' => (!is_multisite() || is_super_admin()) && wp_can_install_language_pack()));
 }
Exemplo n.º 15
0
 /**
  * Lingotek
  */
 function ajax_language_dashboard()
 {
     global $polylang;
     $request_method = isset($_REQUEST['_method']) ? $_REQUEST['_method'] : (isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET');
     $response = array('method' => $request_method);
     switch ($request_method) {
         case 'POST':
             if (isset($_REQUEST['code'], $_REQUEST['native'], $_REQUEST['direction'])) {
                 $name = $_REQUEST['native'];
                 $slug = substr($_REQUEST['code'], 0, strpos($_REQUEST['code'], '_'));
                 // 3rd parameter of strstr needs PHP 5.3
                 $locale = Lingotek::map_to_wp_locale($_REQUEST['code']);
                 // avoid conflicts between language slugs
                 $existing_slugs = $polylang->model->get_languages_list(array('fields' => 'slug'));
                 if (!empty($existing_slugs) && in_array($slug, $existing_slugs)) {
                     $slug = strtolower(str_replace('_', '-', $locale));
                 }
                 $rtl = $_REQUEST['direction'] == 'RTL';
                 $term_group = 0;
                 // adds the language
                 $polylang->model->add_language(compact('name', 'slug', 'locale', 'rtl', 'term_group'));
                 // attempts to install the language pack
                 require_once ABSPATH . 'wp-admin/includes/translation-install.php';
                 wp_download_language_pack($locale);
                 // force checking for themes and plugins translations updates
                 wp_update_themes();
                 wp_update_plugins();
                 $response = array('request' => 'POST: add target language to CMS and Lingotek Project Language', 'locale' => $_REQUEST['code'], 'xcode' => $locale, 'active' => 1, 'enabled' => 1, 'source' => self::get_counts_by_type($locale, 'sources'), 'target' => self::get_counts_by_type($locale, 'targets'));
                 status_header(200);
             }
             break;
         case 'DELETE':
             $body = file_get_contents("php://input");
             $code = str_replace('code=', '', $body);
             $lang = $polylang->model->get_language(Lingotek::map_to_wp_locale($code));
             // map code to WP locales to find the language
             // prevents deleting the last language as it would break the Lingotek dashboard
             if (1 == count($polylang->model->get_languages_list())) {
                 $response = array('request' => sprintf('DELETE: remove language from CMS and project (%s)', $code), 'code' => $code, 'success' => false, 'message' => __('You must keep at least one language.', 'wp-lingotek'));
                 status_header(403);
             } elseif (!self::has_language_content($lang)) {
                 $default_category = pll_get_term(get_option('default_category'), $lang->slug);
                 $polylang->model->delete_language((int) $lang->term_id);
                 wp_delete_term($default_category, 'category');
                 // delete the default category after the language
                 // Deletes the translation status so when re-adding a language the string groups translations won't display as current
                 $lingotek_model = new Lingotek_Model();
                 $strings = $lingotek_model->get_strings();
                 foreach ($strings as $string) {
                     $group = $lingotek_model->get_group('string', $string['context']);
                     unset($group->translations[$lang->locale]);
                     $group->save();
                 }
                 $response = array('request' => sprintf('DELETE: remove language from CMS and project (%s)', $code), 'code' => $code, 'active' => false, 'success' => true);
                 status_header(204);
             } else {
                 $response = array('request' => sprintf('DELETE: remove language from CMS and project (%s)', $code), 'code' => $code, 'success' => false, 'message' => __('The language can only be removed when no existing content is using it.  If you would like to remove this language from the site, then first remove any content assigned to this language.', 'wp-lingotek'));
                 status_header(403);
             }
             break;
         case 'GET':
         default:
             $locale_code = isset($_REQUEST['code']) ? $_REQUEST['code'] : NULL;
             $response = $response + $this->get_language_details($locale_code);
             break;
     }
     wp_send_json($response);
 }