Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

265 строки
8.3 KiB

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