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