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