author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 13:49:49 +0100 | |
changeset 16 | a86126ab1dd4 |
parent 9 | 177826044cd9 |
child 18 | be944660c56a |
permissions | -rw-r--r-- |
5 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* WordPress Translation Installation Administration API |
5 | 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 ) { |
|
16 | 20 |
// Include an unmodified $wp_version. |
21 |
require ABSPATH . WPINC . '/version.php'; |
|
5 | 22 |
|
16 | 23 |
if ( ! in_array( $type, array( 'plugins', 'themes', 'core' ), true ) ) { |
24 |
return new WP_Error( 'invalid_type', __( 'Invalid translation type.' ) ); |
|
5 | 25 |
} |
26 |
||
27 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
28 |
* Allows a plugin to override the WordPress.org Translation Installation API entirely. |
5 | 29 |
* |
30 |
* @since 4.0.0 |
|
31 |
* |
|
32 |
* @param bool|array $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 ) { |
|
16 | 39 |
$url = 'http://api.wordpress.org/translations/' . $type . '/1.0/'; |
40 |
$http_url = $url; |
|
41 |
$ssl = wp_http_supports( array( 'ssl' ) ); |
|
42 |
if ( $ssl ) { |
|
5 | 43 |
$url = set_url_scheme( $url, 'https' ); |
44 |
} |
|
45 |
||
46 |
$options = array( |
|
47 |
'timeout' => 3, |
|
9 | 48 |
'body' => array( |
5 | 49 |
'wp_version' => $wp_version, |
50 |
'locale' => get_locale(), |
|
16 | 51 |
'version' => $args['version'], // Version of plugin, theme or core. |
5 | 52 |
), |
53 |
); |
|
54 |
||
55 |
if ( 'core' !== $type ) { |
|
16 | 56 |
$options['body']['slug'] = $args['slug']; // Plugin or theme slug. |
5 | 57 |
} |
58 |
||
59 |
$request = wp_remote_post( $url, $options ); |
|
60 |
||
61 |
if ( $ssl && is_wp_error( $request ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
62 |
trigger_error( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
63 |
sprintf( |
16 | 64 |
/* translators: %s: Support forums URL. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
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>.' ), |
16 | 66 |
__( 'https://wordpress.org/support/forums/' ) |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
67 |
) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
68 |
headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
69 |
); |
5 | 70 |
|
71 |
$request = wp_remote_post( $http_url, $options ); |
|
72 |
} |
|
73 |
||
74 |
if ( is_wp_error( $request ) ) { |
|
9 | 75 |
$res = new WP_Error( |
76 |
'translations_api_failed', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
77 |
sprintf( |
16 | 78 |
/* translators: %s: Support forums URL. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
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>.' ), |
16 | 80 |
__( 'https://wordpress.org/support/forums/' ) |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
81 |
), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
$request->get_error_message() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
); |
5 | 84 |
} else { |
85 |
$res = json_decode( wp_remote_retrieve_body( $request ), true ); |
|
86 |
if ( ! is_object( $res ) && ! is_array( $res ) ) { |
|
9 | 87 |
$res = new WP_Error( |
88 |
'translations_api_failed', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
89 |
sprintf( |
16 | 90 |
/* translators: %s: Support forums URL. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
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>.' ), |
16 | 92 |
__( 'https://wordpress.org/support/forums/' ) |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
93 |
), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
wp_remote_retrieve_body( $request ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
95 |
); |
5 | 96 |
} |
97 |
} |
|
98 |
} |
|
99 |
||
100 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
101 |
* Filters the Translation Installation API response results. |
5 | 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 |
* |
|
16 | 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. |
|
5 | 121 |
*/ |
122 |
function wp_get_available_translations() { |
|
16 | 123 |
if ( ! wp_installing() ) { |
124 |
$translations = get_site_transient( 'available_translations' ); |
|
125 |
if ( false !== $translations ) { |
|
126 |
return $translations; |
|
127 |
} |
|
5 | 128 |
} |
129 |
||
16 | 130 |
// Include an unmodified $wp_version. |
131 |
require ABSPATH . WPINC . '/version.php'; |
|
5 | 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 |
* |
|
16 | 157 |
* @global string $wp_local_package Locale code of the package. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
158 |
* |
16 | 159 |
* @param array[] $languages Array of available languages (populated via the Translation API). |
5 | 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 ]; |
|
9 | 174 |
printf( |
175 |
'<option value="%s" lang="%s" data-continue="%s"%s>%s</option>' . "\n", |
|
5 | 176 |
esc_attr( $language['language'] ), |
177 |
esc_attr( current( $language['iso'] ) ), |
|
16 | 178 |
esc_attr( $language['strings']['continue'] ? $language['strings']['continue'] : 'Continue' ), |
179 |
in_array( $language['language'], $installed_languages, true ) ? ' data-installed="1"' : '', |
|
9 | 180 |
esc_html( $language['native_name'] ) |
181 |
); |
|
5 | 182 |
|
183 |
unset( $languages[ $wp_local_package ] ); |
|
184 |
} |
|
185 |
} |
|
186 |
||
187 |
foreach ( $languages as $language ) { |
|
9 | 188 |
printf( |
189 |
'<option value="%s" lang="%s" data-continue="%s"%s>%s</option>' . "\n", |
|
5 | 190 |
esc_attr( $language['language'] ), |
191 |
esc_attr( current( $language['iso'] ) ), |
|
16 | 192 |
esc_attr( $language['strings']['continue'] ? $language['strings']['continue'] : 'Continue' ), |
193 |
in_array( $language['language'], $installed_languages, true ) ? ' data-installed="1"' : '', |
|
9 | 194 |
esc_html( $language['native_name'] ) |
195 |
); |
|
5 | 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|bool 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. |
|
16 | 214 |
if ( in_array( $download, get_available_languages(), true ) ) { |
5 | 215 |
return $download; |
216 |
} |
|
217 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
218 |
if ( ! wp_is_file_mod_allowed( 'download_language_pack' ) ) { |
5 | 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'; |
|
9 | 240 |
$skin = new Automatic_Upgrader_Skin; |
241 |
$upgrader = new Language_Pack_Upgrader( $skin ); |
|
5 | 242 |
$translation->type = 'core'; |
9 | 243 |
$result = $upgrader->upgrade( $translation, array( 'clear_update_cache' => false ) ); |
5 | 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() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
261 |
if ( ! wp_is_file_mod_allowed( 'can_install_language_pack' ) ) { |
5 | 262 |
return false; |
263 |
} |
|
264 |
||
265 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
|
9 | 266 |
$skin = new Automatic_Upgrader_Skin; |
5 | 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 |
} |