Skip to content

Add basic language install logic #201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions class-languages.php
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 12 additions & 0 deletions templates-admin/options-available-languages.php
Original file line number Diff line number Diff line change
@@ -95,6 +95,18 @@
</tbody>
</table>

<?php if ( $installable_langs ) { ?>
<p>
<label for="add_lang"><?php esc_html_e( 'Add New Language', 'babble' ); ?></label>
<select name="add_lang">
<option disabled="disabled" selected="selected"><?php esc_html_e( 'Select Language', 'babble' ); ?></option>
<?php foreach ( $installable_langs as $lang ) { ?>
<option value="<?php echo esc_attr( $lang['language'] ); ?>"><?php echo esc_html( $lang['native_name'] ); ?></option>
<?php } ?>
</select>
</p>
<?php } ?>

<?php submit_button( __( 'Save Changes', 'babble' ) ); ?>

</form>