PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/translation-install.php

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