2014-08-26 21:59:16 +02:00
< ? php
/**
2017-08-22 13:52:48 +02:00
* WordPress Translation Installation Administration API
2014-08-26 21:59:16 +02:00
*
* @ package WordPress
* @ subpackage Administration
*/
/**
* Retrieve translations from WordPress Translation API .
*
* @ since 4.0 . 0
*
* @ param string $type Type of translations . Accepts 'plugins' , 'themes' , 'core' .
* @ param array | object $args Translation API arguments . Optional .
* @ return object | WP_Error On success an object of translations , WP_Error on failure .
*/
function translations_api ( $type , $args = null ) {
include ( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
if ( ! in_array ( $type , array ( 'plugins' , 'themes' , 'core' ) ) ) {
2017-12-01 00:11:00 +01:00
return new WP_Error ( 'invalid_type' , __ ( 'Invalid translation type.' ) );
2014-08-26 21:59:16 +02:00
}
/**
2017-08-22 13:52:48 +02:00
* Allows a plugin to override the WordPress . org Translation Installation API entirely .
2014-08-26 21:59:16 +02:00
*
* @ since 4.0 . 0
*
* @ param bool | array $result The result object . Default false .
* @ param string $type The type of translations being requested .
* @ param object $args Translation API arguments .
*/
$res = apply_filters ( 'translations_api' , false , $type , $args );
if ( false === $res ) {
2019-07-01 14:52:01 +02:00
$url = 'http://api.wordpress.org/translations/' . $type . '/1.0/' ;
$http_url = $url ;
$ssl = wp_http_supports ( array ( 'ssl' ) );
if ( $ssl ) {
2014-08-26 21:59:16 +02:00
$url = set_url_scheme ( $url , 'https' );
}
$options = array (
'timeout' => 3 ,
2017-12-01 00:11:00 +01:00
'body' => array (
2014-08-26 21:59:16 +02:00
'wp_version' => $wp_version ,
'locale' => get_locale (),
'version' => $args [ 'version' ], // Version of plugin, theme or core
),
);
if ( 'core' !== $type ) {
$options [ 'body' ][ 'slug' ] = $args [ 'slug' ]; // Plugin or theme slug
}
$request = wp_remote_post ( $url , $options );
if ( $ssl && is_wp_error ( $request ) ) {
2016-11-21 02:52:32 +01:00
trigger_error (
sprintf (
/* translators: %s: support forums URL */
__ ( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
2019-05-25 16:24:50 +02:00
__ ( 'https://wordpress.org/support/forums/' )
2016-11-21 02:52:32 +01:00
) . ' ' . __ ( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
headers_sent () || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
);
2014-08-26 21:59:16 +02:00
$request = wp_remote_post ( $http_url , $options );
}
if ( is_wp_error ( $request ) ) {
2017-12-01 00:11:00 +01:00
$res = new WP_Error (
'translations_api_failed' ,
2016-11-21 02:52:32 +01:00
sprintf (
/* translators: %s: support forums URL */
__ ( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
2019-05-25 16:24:50 +02:00
__ ( 'https://wordpress.org/support/forums/' )
2016-11-21 02:52:32 +01:00
),
$request -> get_error_message ()
);
2014-08-26 21:59:16 +02:00
} else {
$res = json_decode ( wp_remote_retrieve_body ( $request ), true );
if ( ! is_object ( $res ) && ! is_array ( $res ) ) {
2017-12-01 00:11:00 +01:00
$res = new WP_Error (
'translations_api_failed' ,
2016-11-21 02:52:32 +01:00
sprintf (
/* translators: %s: support forums URL */
__ ( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
2019-05-25 16:24:50 +02:00
__ ( 'https://wordpress.org/support/forums/' )
2016-11-21 02:52:32 +01:00
),
wp_remote_retrieve_body ( $request )
);
2014-08-26 21:59:16 +02:00
}
}
}
/**
2017-08-22 13:52:48 +02:00
* Filters the Translation Installation API response results .
2014-08-26 21:59:16 +02:00
*
* @ since 4.0 . 0
*
* @ param object | WP_Error $res Response object or WP_Error .
* @ param string $type The type of translations being requested .
* @ param object $args Translation API arguments .
*/
return apply_filters ( 'translations_api_result' , $res , $type , $args );
}
/**
* Get available translations from the WordPress . org API .
*
* @ since 4.0 . 0
*
* @ see translations_api ()
*
* @ return array Array of translations , each an array of data . If the API response results
* in an error , an empty array will be returned .
*/
function wp_get_available_translations () {
2019-07-01 14:52:01 +02:00
if ( ! wp_installing () ) {
$translations = get_site_transient ( 'available_translations' );
if ( false !== $translations ) {
return $translations ;
}
2014-08-26 21:59:16 +02:00
}
include ( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
$api = translations_api ( 'core' , array ( 'version' => $wp_version ) );
if ( is_wp_error ( $api ) || empty ( $api [ 'translations' ] ) ) {
return array ();
}
$translations = array ();
// Key the array with the language code for now.
foreach ( $api [ 'translations' ] as $translation ) {
$translations [ $translation [ 'language' ] ] = $translation ;
}
if ( ! defined ( 'WP_INSTALLING' ) ) {
set_site_transient ( 'available_translations' , $translations , 3 * HOUR_IN_SECONDS );
}
return $translations ;
}
/**
* Output the select form for the language selection on the installation screen .
*
* @ since 4.0 . 0
*
2015-05-29 04:06:31 +02:00
* @ global string $wp_local_package
*
2014-08-26 21:59:16 +02:00
* @ param array $languages Array of available languages ( populated via the Translation API ) .
*/
function wp_install_language_form ( $languages ) {
global $wp_local_package ;
$installed_languages = get_available_languages ();
echo " <label class='screen-reader-text' for='language'>Select a default language</label> \n " ;
echo " <select size='14' name='language' id='language'> \n " ;
echo '<option value="" lang="en" selected="selected" data-continue="Continue" data-installed="1">English (United States)</option>' ;
echo " \n " ;
if ( ! empty ( $wp_local_package ) && isset ( $languages [ $wp_local_package ] ) ) {
if ( isset ( $languages [ $wp_local_package ] ) ) {
$language = $languages [ $wp_local_package ];
2017-12-01 00:11:00 +01:00
printf (
'<option value="%s" lang="%s" data-continue="%s"%s>%s</option>' . " \n " ,
2014-08-27 00:27:16 +02:00
esc_attr ( $language [ 'language' ] ),
2015-02-13 12:23:26 +01:00
esc_attr ( current ( $language [ 'iso' ] ) ),
2014-08-27 00:27:16 +02:00
esc_attr ( $language [ 'strings' ][ 'continue' ] ),
in_array ( $language [ 'language' ], $installed_languages ) ? ' data-installed="1"' : '' ,
2017-12-01 00:11:00 +01:00
esc_html ( $language [ 'native_name' ] )
);
2014-08-27 00:27:16 +02:00
2014-08-26 21:59:16 +02:00
unset ( $languages [ $wp_local_package ] );
}
}
foreach ( $languages as $language ) {
2017-12-01 00:11:00 +01:00
printf (
'<option value="%s" lang="%s" data-continue="%s"%s>%s</option>' . " \n " ,
2014-08-26 21:59:16 +02:00
esc_attr ( $language [ 'language' ] ),
2015-02-13 12:23:26 +01:00
esc_attr ( current ( $language [ 'iso' ] ) ),
2014-08-26 21:59:16 +02:00
esc_attr ( $language [ 'strings' ][ 'continue' ] ),
in_array ( $language [ 'language' ], $installed_languages ) ? ' data-installed="1"' : '' ,
2017-12-01 00:11:00 +01:00
esc_html ( $language [ 'native_name' ] )
);
2014-08-26 21:59:16 +02:00
}
echo " </select> \n " ;
echo '<p class="step"><span class="spinner"></span><input id="language-continue" type="submit" class="button button-primary button-large" value="Continue" /></p>' ;
}
/**
* Download a language pack .
*
* @ since 4.0 . 0
*
* @ see wp_get_available_translations ()
*
* @ param string $download Language code to download .
* @ return string | bool Returns the language code if successfully downloaded
* ( or already installed ), or false on failure .
*/
function wp_download_language_pack ( $download ) {
// Check if the translation is already installed.
if ( in_array ( $download , get_available_languages () ) ) {
return $download ;
}
2017-05-11 21:24:41 +02:00
if ( ! wp_is_file_mod_allowed ( 'download_language_pack' ) ) {
2014-08-26 21:59:16 +02:00
return false ;
}
// Confirm the translation is one we can download.
$translations = wp_get_available_translations ();
if ( ! $translations ) {
return false ;
}
foreach ( $translations as $translation ) {
if ( $translation [ 'language' ] === $download ) {
$translation_to_load = true ;
break ;
}
}
if ( empty ( $translation_to_load ) ) {
return false ;
}
$translation = ( object ) $translation ;
2016-08-31 18:31:29 +02:00
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ;
2017-12-01 00:11:00 +01:00
$skin = new Automatic_Upgrader_Skin ;
$upgrader = new Language_Pack_Upgrader ( $skin );
2014-08-26 21:59:16 +02:00
$translation -> type = 'core' ;
2017-12-01 00:11:00 +01:00
$result = $upgrader -> upgrade ( $translation , array ( 'clear_update_cache' => false ) );
2014-09-02 20:07:18 +02:00
if ( ! $result || is_wp_error ( $result ) ) {
return false ;
}
2014-08-26 21:59:16 +02:00
return $translation -> language ;
}
2014-09-02 20:07:18 +02:00
/**
* Check if WordPress has access to the filesystem without asking for
* credentials .
*
* @ since 4.0 . 0
*
* @ return bool Returns true on success , false on failure .
*/
function wp_can_install_language_pack () {
2017-05-11 21:24:41 +02:00
if ( ! wp_is_file_mod_allowed ( 'can_install_language_pack' ) ) {
2014-09-02 20:07:18 +02:00
return false ;
}
2016-08-31 18:31:29 +02:00
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ;
2017-12-01 00:11:00 +01:00
$skin = new Automatic_Upgrader_Skin ;
2014-09-02 20:07:18 +02:00
$upgrader = new Language_Pack_Upgrader ( $skin );
2015-01-08 00:43:24 +01:00
$upgrader -> init ();
2014-09-02 20:07:18 +02:00
$check = $upgrader -> fs_connect ( array ( WP_CONTENT_DIR , WP_LANG_DIR ) );
if ( ! $check || is_wp_error ( $check ) ) {
return false ;
}
return true ;
}