From 69839fafde4c75cfa47349a120bf8bff52ddcca1 Mon Sep 17 00:00:00 2001 From: Barry Ceelen Date: Fri, 26 Dec 2014 14:12:59 +0100 Subject: [PATCH] Add basic language install logic --- class-languages.php | 54 +++++++++++++++++++ .../options-available-languages.php | 12 +++++ 2 files changed, 66 insertions(+) diff --git a/class-languages.php b/class-languages.php index 29b0e3a..afbcc77 100644 --- a/class-languages.php +++ b/class-languages.php @@ -174,6 +174,7 @@ public function options() { $vars[ 'langs' ] = $langs; $vars[ 'default_lang' ] = $this->default_lang; $vars[ 'active_langs' ] = $this->get_active_langs(); + $vars[ 'installable_langs' ] = $this->get_installable_langs(); $this->render_admin( 'options-available-languages.php', $vars ); } @@ -384,6 +385,14 @@ protected function maybe_process_languages() { $this->set_admin_error( __( 'You must set your default language as public.', 'babble' ) ); } } + // Add new language + if ( @ $_POST['add_lang'] ) { + require_once( ABSPATH . 'wp-admin/includes/translation-install.php' ); + $download = wp_download_language_pack( $_POST['add_lang'] ); + if ( ! $download ) { + $this->set_admin_error( __( 'The selected language could not be added.', 'babble' ) ); + } + } // Finish up, redirecting if we're all OK if ( ! $this->errors ) { // Save the public languages @@ -525,6 +534,51 @@ protected function set_defaults() { $this->default_lang = $locale; $this->public_langs = array( $locale ); } + + /** + * Check if we can install language packs. + * + * As long as this plugin supports WordPress versions below 4.0.0, + * we should check for availability of the translations api. + * If the translations api is available, use its + * wp_can_install_language_pack function. + * + * @see wp_can_install_language_pack() + * + * @return bool + **/ + protected function can_install_language_pack() { + if ( ! file_exists( ABSPATH . 'wp-admin/includes/translation-install.php' ) ) { + return false; + } + require_once( ABSPATH . 'wp-admin/includes/translation-install.php' ); + return wp_can_install_language_pack(); + } + + /** + * Return an array of languages which can be installed via the + * translations api, minus the already available languages. + * + * @see get_available_languages() + * @see wp_get_available_translations() + * + * @return array + **/ + protected function get_installable_langs() { + + if ( ! $this->can_install_language_pack() ) { + return array(); + } + + require_once( ABSPATH . 'wp-admin/includes/translation-install.php' ); + + // Refresh the current languages + $this->parse_available_languages(); + // Merge in our previously set language settings + $langs = $this->merge_lang_sets( $this->available_langs, $this->lang_prefs ); + $available_translations = wp_get_available_translations(); + return array_diff_key( $available_translations, $langs ); + } } global $bbl_languages; diff --git a/templates-admin/options-available-languages.php b/templates-admin/options-available-languages.php index 8aa3921..1bf71e2 100644 --- a/templates-admin/options-available-languages.php +++ b/templates-admin/options-available-languages.php @@ -95,6 +95,18 @@ + +

+ + +

+ +