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