"Fossies" - the Fresh Open Source Software Archive 
Member "wordpress/wp-admin/includes/translation-install.php" (3 Jan 2021, 8870 Bytes) of package /linux/www/wordpress-5.7-RC1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "translation-install.php" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
5.6.2_vs_5.7-RC1.
1 <?php
2 /**
3 * WordPress Translation Installation 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 an unmodified $wp_version.
21 require ABSPATH . WPINC . '/version.php';
22
23 if ( ! in_array( $type, array( 'plugins', 'themes', 'core' ), true ) ) {
24 return new WP_Error( 'invalid_type', __( 'Invalid translation type.' ) );
25 }
26
27 /**
28 * Allows a plugin to override the WordPress.org Translation Installation API entirely.
29 *
30 * @since 4.0.0
31 *
32 * @param false|object $result The result object. Default false.
33 * @param string $type The type of translations being requested.
34 * @param object $args Translation API arguments.
35 */
36 $res = apply_filters( 'translations_api', false, $type, $args );
37
38 if ( false === $res ) {
39 $url = 'http://api.wordpress.org/translations/' . $type . '/1.0/';
40 $http_url = $url;
41 $ssl = wp_http_supports( array( 'ssl' ) );
42 if ( $ssl ) {
43 $url = set_url_scheme( $url, 'https' );
44 }
45
46 $options = array(
47 'timeout' => 3,
48 'body' => array(
49 'wp_version' => $wp_version,
50 'locale' => get_locale(),
51 'version' => $args['version'], // Version of plugin, theme or core.
52 ),
53 );
54
55 if ( 'core' !== $type ) {
56 $options['body']['slug'] = $args['slug']; // Plugin or theme slug.
57 }
58
59 $request = wp_remote_post( $url, $options );
60
61 if ( $ssl && is_wp_error( $request ) ) {
62 trigger_error(
63 sprintf(
64 /* translators: %s: Support forums URL. */
65 __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
66 __( 'https://wordpress.org/support/forums/' )
67 ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
68 headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
69 );
70
71 $request = wp_remote_post( $http_url, $options );
72 }
73
74 if ( is_wp_error( $request ) ) {
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’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 $request->get_error_message()
83 );
84 } else {
85 $res = json_decode( wp_remote_retrieve_body( $request ), true );
86 if ( ! is_object( $res ) && ! is_array( $res ) ) {
87 $res = new WP_Error(
88 'translations_api_failed',
89 sprintf(
90 /* translators: %s: Support forums URL. */
91 __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
92 __( 'https://wordpress.org/support/forums/' )
93 ),
94 wp_remote_retrieve_body( $request )
95 );
96 }
97 }
98 }
99
100 /**
101 * Filters the Translation Installation API response results.
102 *
103 * @since 4.0.0
104 *
105 * @param object|WP_Error $res Response object or WP_Error.
106 * @param string $type The type of translations being requested.
107 * @param object $args Translation API arguments.
108 */
109 return apply_filters( 'translations_api_result', $res, $type, $args );
110 }
111
112 /**
113 * Get available translations from the WordPress.org API.
114 *
115 * @since 4.0.0
116 *
117 * @see translations_api()
118 *
119 * @return array[] Array of translations, each an array of data, keyed by the language. If the API response results
120 * in an error, an empty array will be returned.
121 */
122 function wp_get_available_translations() {
123 if ( ! wp_installing() ) {
124 $translations = get_site_transient( 'available_translations' );
125 if ( false !== $translations ) {
126 return $translations;
127 }
128 }
129
130 // Include an unmodified $wp_version.
131 require ABSPATH . WPINC . '/version.php';
132
133 $api = translations_api( 'core', array( 'version' => $wp_version ) );
134
135 if ( is_wp_error( $api ) || empty( $api['translations'] ) ) {
136 return array();
137 }
138
139 $translations = array();
140 // Key the array with the language code for now.
141 foreach ( $api['translations'] as $translation ) {
142 $translations[ $translation['language'] ] = $translation;
143 }
144
145 if ( ! defined( 'WP_INSTALLING' ) ) {
146 set_site_transient( 'available_translations', $translations, 3 * HOUR_IN_SECONDS );
147 }
148
149 return $translations;
150 }
151
152 /**
153 * Output the select form for the language selection on the installation screen.
154 *
155 * @since 4.0.0
156 *
157 * @global string $wp_local_package Locale code of the package.
158 *
159 * @param array[] $languages Array of available languages (populated via the Translation API).
160 */
161 function wp_install_language_form( $languages ) {
162 global $wp_local_package;
163
164 $installed_languages = get_available_languages();
165
166 echo "<label class='screen-reader-text' for='language'>Select a default language</label>\n";
167 echo "<select size='14' name='language' id='language'>\n";
168 echo '<option value="" lang="en" selected="selected" data-continue="Continue" data-installed="1">English (United States)</option>';
169 echo "\n";
170
171 if ( ! empty( $wp_local_package ) && isset( $languages[ $wp_local_package ] ) ) {
172 if ( isset( $languages[ $wp_local_package ] ) ) {
173 $language = $languages[ $wp_local_package ];
174 printf(
175 '<option value="%s" lang="%s" data-continue="%s"%s>%s</option>' . "\n",
176 esc_attr( $language['language'] ),
177 esc_attr( current( $language['iso'] ) ),
178 esc_attr( $language['strings']['continue'] ? $language['strings']['continue'] : 'Continue' ),
179 in_array( $language['language'], $installed_languages, true ) ? ' data-installed="1"' : '',
180 esc_html( $language['native_name'] )
181 );
182
183 unset( $languages[ $wp_local_package ] );
184 }
185 }
186
187 foreach ( $languages as $language ) {
188 printf(
189 '<option value="%s" lang="%s" data-continue="%s"%s>%s</option>' . "\n",
190 esc_attr( $language['language'] ),
191 esc_attr( current( $language['iso'] ) ),
192 esc_attr( $language['strings']['continue'] ? $language['strings']['continue'] : 'Continue' ),
193 in_array( $language['language'], $installed_languages, true ) ? ' data-installed="1"' : '',
194 esc_html( $language['native_name'] )
195 );
196 }
197 echo "</select>\n";
198 echo '<p class="step"><span class="spinner"></span><input id="language-continue" type="submit" class="button button-primary button-large" value="Continue" /></p>';
199 }
200
201 /**
202 * Download a language pack.
203 *
204 * @since 4.0.0
205 *
206 * @see wp_get_available_translations()
207 *
208 * @param string $download Language code to download.
209 * @return string|false Returns the language code if successfully downloaded
210 * (or already installed), or false on failure.
211 */
212 function wp_download_language_pack( $download ) {
213 // Check if the translation is already installed.
214 if ( in_array( $download, get_available_languages(), true ) ) {
215 return $download;
216 }
217
218 if ( ! wp_is_file_mod_allowed( 'download_language_pack' ) ) {
219 return false;
220 }
221
222 // Confirm the translation is one we can download.
223 $translations = wp_get_available_translations();
224 if ( ! $translations ) {
225 return false;
226 }
227 foreach ( $translations as $translation ) {
228 if ( $translation['language'] === $download ) {
229 $translation_to_load = true;
230 break;
231 }
232 }
233
234 if ( empty( $translation_to_load ) ) {
235 return false;
236 }
237 $translation = (object) $translation;
238
239 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
240 $skin = new Automatic_Upgrader_Skin;
241 $upgrader = new Language_Pack_Upgrader( $skin );
242 $translation->type = 'core';
243 $result = $upgrader->upgrade( $translation, array( 'clear_update_cache' => false ) );
244
245 if ( ! $result || is_wp_error( $result ) ) {
246 return false;
247 }
248
249 return $translation->language;
250 }
251
252 /**
253 * Check if WordPress has access to the filesystem without asking for
254 * credentials.
255 *
256 * @since 4.0.0
257 *
258 * @return bool Returns true on success, false on failure.
259 */
260 function wp_can_install_language_pack() {
261 if ( ! wp_is_file_mod_allowed( 'can_install_language_pack' ) ) {
262 return false;
263 }
264
265 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
266 $skin = new Automatic_Upgrader_Skin;
267 $upgrader = new Language_Pack_Upgrader( $skin );
268 $upgrader->init();
269
270 $check = $upgrader->fs_connect( array( WP_CONTENT_DIR, WP_LANG_DIR ) );
271
272 if ( ! $check || is_wp_error( $check ) ) {
273 return false;
274 }
275
276 return true;
277 }