translation-install.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * WordPress Translation Installation Administration API
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Retrieve translations from WordPress Translation API.
  10. *
  11. * @since 4.0.0
  12. *
  13. * @param string $type Type of translations. Accepts 'plugins', 'themes', 'core'.
  14. * @param array|object $args Translation API arguments. Optional.
  15. * @return array|WP_Error On success an associative array of translations, WP_Error on failure.
  16. */
  17. function translations_api( $type, $args = null ) {
  18. // Include an unmodified $wp_version.
  19. require ABSPATH . WPINC . '/version.php';
  20. if ( ! in_array( $type, array( 'plugins', 'themes', 'core' ), true ) ) {
  21. return new WP_Error( 'invalid_type', __( 'Invalid translation type.' ) );
  22. }
  23. /**
  24. * Allows a plugin to override the WordPress.org Translation Installation API entirely.
  25. *
  26. * @since 4.0.0
  27. *
  28. * @param false|array $result The result array. Default false.
  29. * @param string $type The type of translations being requested.
  30. * @param object $args Translation API arguments.
  31. */
  32. $res = apply_filters( 'translations_api', false, $type, $args );
  33. if ( false === $res ) {
  34. $url = 'http://api.wordpress.org/translations/' . $type . '/1.0/';
  35. $http_url = $url;
  36. $ssl = wp_http_supports( array( 'ssl' ) );
  37. if ( $ssl ) {
  38. $url = set_url_scheme( $url, 'https' );
  39. }
  40. $options = array(
  41. 'timeout' => 3,
  42. 'body' => array(
  43. 'wp_version' => $wp_version,
  44. 'locale' => get_locale(),
  45. 'version' => $args['version'], // Version of plugin, theme or core.
  46. ),
  47. );
  48. if ( 'core' !== $type ) {
  49. $options['body']['slug'] = $args['slug']; // Plugin or theme slug.
  50. }
  51. $request = wp_remote_post( $url, $options );
  52. if ( $ssl && is_wp_error( $request ) ) {
  53. trigger_error(
  54. sprintf(
  55. /* translators: %s: Support forums URL. */
  56. __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
  57. __( 'https://wordpress.org/support/forums/' )
  58. ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
  59. headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
  60. );
  61. $request = wp_remote_post( $http_url, $options );
  62. }
  63. if ( is_wp_error( $request ) ) {
  64. $res = new WP_Error(
  65. 'translations_api_failed',
  66. sprintf(
  67. /* translators: %s: Support forums URL. */
  68. __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
  69. __( 'https://wordpress.org/support/forums/' )
  70. ),
  71. $request->get_error_message()
  72. );
  73. } else {
  74. $res = json_decode( wp_remote_retrieve_body( $request ), true );
  75. if ( ! is_object( $res ) && ! is_array( $res ) ) {
  76. $res = new WP_Error(
  77. 'translations_api_failed',
  78. sprintf(
  79. /* translators: %s: Support forums URL. */
  80. __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
  81. __( 'https://wordpress.org/support/forums/' )
  82. ),
  83. wp_remote_retrieve_body( $request )
  84. );
  85. }
  86. }
  87. }
  88. /**
  89. * Filters the Translation Installation API response results.
  90. *
  91. * @since 4.0.0
  92. *
  93. * @param array|WP_Error $res Response as an associative array or WP_Error.
  94. * @param string $type The type of translations being requested.
  95. * @param object $args Translation API arguments.
  96. */
  97. return apply_filters( 'translations_api_result', $res, $type, $args );
  98. }
  99. /**
  100. * Get available translations from the WordPress.org API.
  101. *
  102. * @since 4.0.0
  103. *
  104. * @see translations_api()
  105. *
  106. * @return array[] Array of translations, each an array of data, keyed by the language. If the API response results
  107. * in an error, an empty array will be returned.
  108. */
  109. function wp_get_available_translations() {
  110. if ( ! wp_installing() ) {
  111. $translations = get_site_transient( 'available_translations' );
  112. if ( false !== $translations ) {
  113. return $translations;
  114. }
  115. }
  116. // Include an unmodified $wp_version.
  117. require ABSPATH . WPINC . '/version.php';
  118. $api = translations_api( 'core', array( 'version' => $wp_version ) );
  119. if ( is_wp_error( $api ) || empty( $api['translations'] ) ) {
  120. return array();
  121. }
  122. $translations = array();
  123. // Key the array with the language code for now.
  124. foreach ( $api['translations'] as $translation ) {
  125. $translations[ $translation['language'] ] = $translation;
  126. }
  127. if ( ! defined( 'WP_INSTALLING' ) ) {
  128. set_site_transient( 'available_translations', $translations, 3 * HOUR_IN_SECONDS );
  129. }
  130. return $translations;
  131. }
  132. /**
  133. * Output the select form for the language selection on the installation screen.
  134. *
  135. * @since 4.0.0
  136. *
  137. * @global string $wp_local_package Locale code of the package.
  138. *
  139. * @param array[] $languages Array of available languages (populated via the Translation API).
  140. */
  141. function wp_install_language_form( $languages ) {
  142. global $wp_local_package;
  143. $installed_languages = get_available_languages();
  144. echo "<label class='screen-reader-text' for='language'>Select a default language</label>\n";
  145. echo "<select size='14' name='language' id='language'>\n";
  146. echo '<option value="" lang="en" selected="selected" data-continue="Continue" data-installed="1">English (United States)</option>';
  147. echo "\n";
  148. if ( ! empty( $wp_local_package ) && isset( $languages[ $wp_local_package ] ) ) {
  149. if ( isset( $languages[ $wp_local_package ] ) ) {
  150. $language = $languages[ $wp_local_package ];
  151. printf(
  152. '<option value="%s" lang="%s" data-continue="%s"%s>%s</option>' . "\n",
  153. esc_attr( $language['language'] ),
  154. esc_attr( current( $language['iso'] ) ),
  155. esc_attr( $language['strings']['continue'] ? $language['strings']['continue'] : 'Continue' ),
  156. in_array( $language['language'], $installed_languages, true ) ? ' data-installed="1"' : '',
  157. esc_html( $language['native_name'] )
  158. );
  159. unset( $languages[ $wp_local_package ] );
  160. }
  161. }
  162. foreach ( $languages as $language ) {
  163. printf(
  164. '<option value="%s" lang="%s" data-continue="%s"%s>%s</option>' . "\n",
  165. esc_attr( $language['language'] ),
  166. esc_attr( current( $language['iso'] ) ),
  167. esc_attr( $language['strings']['continue'] ? $language['strings']['continue'] : 'Continue' ),
  168. in_array( $language['language'], $installed_languages, true ) ? ' data-installed="1"' : '',
  169. esc_html( $language['native_name'] )
  170. );
  171. }
  172. echo "</select>\n";
  173. echo '<p class="step"><span class="spinner"></span><input id="language-continue" type="submit" class="button button-primary button-large" value="Continue" /></p>';
  174. }
  175. /**
  176. * Download a language pack.
  177. *
  178. * @since 4.0.0
  179. *
  180. * @see wp_get_available_translations()
  181. *
  182. * @param string $download Language code to download.
  183. * @return string|false Returns the language code if successfully downloaded
  184. * (or already installed), or false on failure.
  185. */
  186. function wp_download_language_pack( $download ) {
  187. // Check if the translation is already installed.
  188. if ( in_array( $download, get_available_languages(), true ) ) {
  189. return $download;
  190. }
  191. if ( ! wp_is_file_mod_allowed( 'download_language_pack' ) ) {
  192. return false;
  193. }
  194. // Confirm the translation is one we can download.
  195. $translations = wp_get_available_translations();
  196. if ( ! $translations ) {
  197. return false;
  198. }
  199. foreach ( $translations as $translation ) {
  200. if ( $translation['language'] === $download ) {
  201. $translation_to_load = true;
  202. break;
  203. }
  204. }
  205. if ( empty( $translation_to_load ) ) {
  206. return false;
  207. }
  208. $translation = (object) $translation;
  209. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  210. $skin = new Automatic_Upgrader_Skin;
  211. $upgrader = new Language_Pack_Upgrader( $skin );
  212. $translation->type = 'core';
  213. $result = $upgrader->upgrade( $translation, array( 'clear_update_cache' => false ) );
  214. if ( ! $result || is_wp_error( $result ) ) {
  215. return false;
  216. }
  217. return $translation->language;
  218. }
  219. /**
  220. * Check if WordPress has access to the filesystem without asking for
  221. * credentials.
  222. *
  223. * @since 4.0.0
  224. *
  225. * @return bool Returns true on success, false on failure.
  226. */
  227. function wp_can_install_language_pack() {
  228. if ( ! wp_is_file_mod_allowed( 'can_install_language_pack' ) ) {
  229. return false;
  230. }
  231. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  232. $skin = new Automatic_Upgrader_Skin;
  233. $upgrader = new Language_Pack_Upgrader( $skin );
  234. $upgrader->init();
  235. $check = $upgrader->fs_connect( array( WP_CONTENT_DIR, WP_LANG_DIR ) );
  236. if ( ! $check || is_wp_error( $check ) ) {
  237. return false;
  238. }
  239. return true;
  240. }