| author | ymh <ymh.work@gmail.com> |
| Fri, 05 Sep 2025 18:40:08 +0200 | |
| changeset 21 | 48c4eec2b7e6 |
| parent 19 | 3d72ae0968f4 |
| child 22 | 8c2e4d02f4ef |
| 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. |
|
| 19 | 17 |
* @return array|WP_Error On success an associative array of translations, WP_Error on failure. |
| 5 | 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 |
* |
|
| 19 | 32 |
* @param false|array $result The result array. Default false. |
33 |
* @param string $type The type of translations being requested. |
|
34 |
* @param object $args Translation API arguments. |
|
| 5 | 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 ) ) { |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
62 |
wp_trigger_error( |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
63 |
__FUNCTION__, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
64 |
sprintf( |
| 16 | 65 |
/* translators: %s: Support forums URL. */ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
66 |
__( '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 | 67 |
__( 'https://wordpress.org/support/forums/' ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
68 |
) . ' ' . __( '(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
|
69 |
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
|
70 |
); |
| 5 | 71 |
|
72 |
$request = wp_remote_post( $http_url, $options ); |
|
73 |
} |
|
74 |
||
75 |
if ( is_wp_error( $request ) ) { |
|
| 9 | 76 |
$res = new WP_Error( |
77 |
'translations_api_failed', |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
78 |
sprintf( |
| 16 | 79 |
/* translators: %s: Support forums URL. */ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
80 |
__( '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 | 81 |
__( 'https://wordpress.org/support/forums/' ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
), |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
$request->get_error_message() |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
84 |
); |
| 5 | 85 |
} else { |
86 |
$res = json_decode( wp_remote_retrieve_body( $request ), true ); |
|
87 |
if ( ! is_object( $res ) && ! is_array( $res ) ) { |
|
| 9 | 88 |
$res = new WP_Error( |
89 |
'translations_api_failed', |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
sprintf( |
| 16 | 91 |
/* translators: %s: Support forums URL. */ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
92 |
__( '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 | 93 |
__( 'https://wordpress.org/support/forums/' ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
), |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
95 |
wp_remote_retrieve_body( $request ) |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
96 |
); |
| 5 | 97 |
} |
98 |
} |
|
99 |
} |
|
100 |
||
101 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
102 |
* Filters the Translation Installation API response results. |
| 5 | 103 |
* |
104 |
* @since 4.0.0 |
|
105 |
* |
|
| 19 | 106 |
* @param array|WP_Error $res Response as an associative array or WP_Error. |
107 |
* @param string $type The type of translations being requested. |
|
108 |
* @param object $args Translation API arguments. |
|
| 5 | 109 |
*/ |
110 |
return apply_filters( 'translations_api_result', $res, $type, $args ); |
|
111 |
} |
|
112 |
||
113 |
/** |
|
114 |
* Get available translations from the WordPress.org API. |
|
115 |
* |
|
116 |
* @since 4.0.0 |
|
117 |
* |
|
118 |
* @see translations_api() |
|
119 |
* |
|
| 16 | 120 |
* @return array[] Array of translations, each an array of data, keyed by the language. If the API response results |
121 |
* in an error, an empty array will be returned. |
|
| 5 | 122 |
*/ |
123 |
function wp_get_available_translations() { |
|
| 16 | 124 |
if ( ! wp_installing() ) { |
125 |
$translations = get_site_transient( 'available_translations' ); |
|
126 |
if ( false !== $translations ) { |
|
127 |
return $translations; |
|
128 |
} |
|
| 5 | 129 |
} |
130 |
||
| 16 | 131 |
// Include an unmodified $wp_version. |
132 |
require ABSPATH . WPINC . '/version.php'; |
|
| 5 | 133 |
|
134 |
$api = translations_api( 'core', array( 'version' => $wp_version ) ); |
|
135 |
||
136 |
if ( is_wp_error( $api ) || empty( $api['translations'] ) ) { |
|
137 |
return array(); |
|
138 |
} |
|
139 |
||
140 |
$translations = array(); |
|
141 |
// Key the array with the language code for now. |
|
142 |
foreach ( $api['translations'] as $translation ) { |
|
143 |
$translations[ $translation['language'] ] = $translation; |
|
144 |
} |
|
145 |
||
146 |
if ( ! defined( 'WP_INSTALLING' ) ) { |
|
147 |
set_site_transient( 'available_translations', $translations, 3 * HOUR_IN_SECONDS ); |
|
148 |
} |
|
149 |
||
150 |
return $translations; |
|
151 |
} |
|
152 |
||
153 |
/** |
|
154 |
* Output the select form for the language selection on the installation screen. |
|
155 |
* |
|
156 |
* @since 4.0.0 |
|
157 |
* |
|
| 16 | 158 |
* @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
|
159 |
* |
| 16 | 160 |
* @param array[] $languages Array of available languages (populated via the Translation API). |
| 5 | 161 |
*/ |
162 |
function wp_install_language_form( $languages ) { |
|
163 |
global $wp_local_package; |
|
164 |
||
165 |
$installed_languages = get_available_languages(); |
|
166 |
||
167 |
echo "<label class='screen-reader-text' for='language'>Select a default language</label>\n"; |
|
168 |
echo "<select size='14' name='language' id='language'>\n"; |
|
169 |
echo '<option value="" lang="en" selected="selected" data-continue="Continue" data-installed="1">English (United States)</option>'; |
|
170 |
echo "\n"; |
|
171 |
||
172 |
if ( ! empty( $wp_local_package ) && isset( $languages[ $wp_local_package ] ) ) { |
|
173 |
if ( isset( $languages[ $wp_local_package ] ) ) { |
|
174 |
$language = $languages[ $wp_local_package ]; |
|
| 9 | 175 |
printf( |
176 |
'<option value="%s" lang="%s" data-continue="%s"%s>%s</option>' . "\n", |
|
| 5 | 177 |
esc_attr( $language['language'] ), |
178 |
esc_attr( current( $language['iso'] ) ), |
|
| 16 | 179 |
esc_attr( $language['strings']['continue'] ? $language['strings']['continue'] : 'Continue' ), |
180 |
in_array( $language['language'], $installed_languages, true ) ? ' data-installed="1"' : '', |
|
| 9 | 181 |
esc_html( $language['native_name'] ) |
182 |
); |
|
| 5 | 183 |
|
184 |
unset( $languages[ $wp_local_package ] ); |
|
185 |
} |
|
186 |
} |
|
187 |
||
188 |
foreach ( $languages as $language ) { |
|
| 9 | 189 |
printf( |
190 |
'<option value="%s" lang="%s" data-continue="%s"%s>%s</option>' . "\n", |
|
| 5 | 191 |
esc_attr( $language['language'] ), |
192 |
esc_attr( current( $language['iso'] ) ), |
|
| 16 | 193 |
esc_attr( $language['strings']['continue'] ? $language['strings']['continue'] : 'Continue' ), |
194 |
in_array( $language['language'], $installed_languages, true ) ? ' data-installed="1"' : '', |
|
| 9 | 195 |
esc_html( $language['native_name'] ) |
196 |
); |
|
| 5 | 197 |
} |
198 |
echo "</select>\n"; |
|
199 |
echo '<p class="step"><span class="spinner"></span><input id="language-continue" type="submit" class="button button-primary button-large" value="Continue" /></p>'; |
|
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* Download a language pack. |
|
204 |
* |
|
205 |
* @since 4.0.0 |
|
206 |
* |
|
207 |
* @see wp_get_available_translations() |
|
208 |
* |
|
209 |
* @param string $download Language code to download. |
|
| 18 | 210 |
* @return string|false Returns the language code if successfully downloaded |
211 |
* (or already installed), or false on failure. |
|
| 5 | 212 |
*/ |
213 |
function wp_download_language_pack( $download ) { |
|
214 |
// Check if the translation is already installed. |
|
| 16 | 215 |
if ( in_array( $download, get_available_languages(), true ) ) { |
| 5 | 216 |
return $download; |
217 |
} |
|
218 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
219 |
if ( ! wp_is_file_mod_allowed( 'download_language_pack' ) ) { |
| 5 | 220 |
return false; |
221 |
} |
|
222 |
||
223 |
// Confirm the translation is one we can download. |
|
224 |
$translations = wp_get_available_translations(); |
|
225 |
if ( ! $translations ) { |
|
226 |
return false; |
|
227 |
} |
|
228 |
foreach ( $translations as $translation ) { |
|
229 |
if ( $translation['language'] === $download ) { |
|
230 |
$translation_to_load = true; |
|
231 |
break; |
|
232 |
} |
|
233 |
} |
|
234 |
||
235 |
if ( empty( $translation_to_load ) ) { |
|
236 |
return false; |
|
237 |
} |
|
238 |
$translation = (object) $translation; |
|
239 |
||
240 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
241 |
$skin = new Automatic_Upgrader_Skin(); |
| 9 | 242 |
$upgrader = new Language_Pack_Upgrader( $skin ); |
| 5 | 243 |
$translation->type = 'core'; |
| 9 | 244 |
$result = $upgrader->upgrade( $translation, array( 'clear_update_cache' => false ) ); |
| 5 | 245 |
|
246 |
if ( ! $result || is_wp_error( $result ) ) { |
|
247 |
return false; |
|
248 |
} |
|
249 |
||
250 |
return $translation->language; |
|
251 |
} |
|
252 |
||
253 |
/** |
|
254 |
* Check if WordPress has access to the filesystem without asking for |
|
255 |
* credentials. |
|
256 |
* |
|
257 |
* @since 4.0.0 |
|
258 |
* |
|
259 |
* @return bool Returns true on success, false on failure. |
|
260 |
*/ |
|
261 |
function wp_can_install_language_pack() { |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
262 |
if ( ! wp_is_file_mod_allowed( 'can_install_language_pack' ) ) { |
| 5 | 263 |
return false; |
264 |
} |
|
265 |
||
266 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
267 |
$skin = new Automatic_Upgrader_Skin(); |
| 5 | 268 |
$upgrader = new Language_Pack_Upgrader( $skin ); |
269 |
$upgrader->init(); |
|
270 |
||
271 |
$check = $upgrader->fs_connect( array( WP_CONTENT_DIR, WP_LANG_DIR ) ); |
|
272 |
||
273 |
if ( ! $check || is_wp_error( $check ) ) { |
|
274 |
return false; |
|
275 |
} |
|
276 |
||
277 |
return true; |
|
278 |
} |