translation-install.php 8.5 KB

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