PluginProbe
Bogo / 2.4
Bogo v2.4
3.9.3 trunk 1.0 1.1 2.0 2.0.1 2.1 2.1.1 2.1.2 2.1.3 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5 2.6 2.6.1 2.7 2.8 2.8.1 2.9 3.0 3.0.1 All 52 releases
← All changes | includes/functions.php +432 -22 trunk2.4 View file →
@@ -1,50 +1,460 @@
1 1 <?php
2 2
3 -function bogo_get_prop( $prop ) {
4 - $option = get_option( 'bogo' );
3 +function bogo_languages() {
4 + static $languages = array();
5 5
6 - if ( ! is_array( $option ) ) {
7 - $option = array();
6 + if ( ! empty( $languages ) ) {
7 + return $languages;
8 8 }
9 9
10 - return $option[$prop] ?? '';
10 + $languages = array(
11 + 'af' => __( 'Afrikaans', 'bogo' ),
12 + 'am' => __( 'Amharic', 'bogo' ),
13 + 'an' => __( 'Aragonese', 'bogo' ),
14 + 'ar' => __( 'Arabic', 'bogo' ),
15 + 'as' => __( 'Assamese', 'bogo' ),
16 + 'az' => __( 'Azerbaijani', 'bogo' ),
17 + 'be_BY' => __( 'Belarusian', 'bogo' ),
18 + 'bg_BG' => __( 'Bulgarian', 'bogo' ),
19 + 'bn_BD' => __( 'Bangla', 'bogo' ),
20 + 'bn_IN' => __( 'Indian Bengali', 'bogo' ),
21 + 'bo' => __( 'Tibetan', 'bogo' ),
22 + 'bs_BA' => __( 'Bosnian', 'bogo' ),
23 + 'ca' => __( 'Catalan', 'bogo' ),
24 + 'ckb' => __( 'Central Kurdish', 'bogo' ),
25 + 'co' => __( 'Corsican', 'bogo' ),
26 + 'cs_CZ' => __( 'Czech', 'bogo' ),
27 + 'cy' => __( 'Welsh', 'bogo' ),
28 + 'da_DK' => __( 'Danish', 'bogo' ),
29 + 'de_DE' => __( 'German', 'bogo' ),
30 + 'dv' => __( 'Dhivehi', 'bogo' ),
31 + 'el' => __( 'Greek', 'bogo' ),
32 + 'en_US' => __( 'English (United States)', 'bogo' ),
33 + 'en_AU' => __( 'English (Australia)', 'bogo' ),
34 + 'en_CA' => __( 'English (Canada)', 'bogo' ),
35 + 'en_GB' => __( 'English (UK)', 'bogo' ),
36 + 'eo' => __( 'Esperanto', 'bogo' ),
37 + 'es_ES' => __( 'Spanish (Spain)', 'bogo' ),
38 + 'es_CL' => __( 'Spanish (Chile)', 'bogo' ),
39 + 'es_MX' => __( 'Mexican Spanish', 'bogo' ),
40 + 'es_PE' => __( 'Spanish (Peru)', 'bogo' ),
41 + 'es_VE' => __( 'Spanish (Venezuela)', 'bogo' ),
42 + 'et' => __( 'Estonian', 'bogo' ),
43 + 'eu' => __( 'Basque', 'bogo' ),
44 + 'fa_IR' => __( 'Persian', 'bogo' ),
45 + 'fi' => __( 'Finnish', 'bogo' ),
46 + 'fo' => __( 'Faroese', 'bogo' ),
47 + 'fr_FR' => __( 'French (France)', 'bogo' ),
48 + 'fy' => __( 'Frisian', 'bogo' ),
49 + 'ga' => __( 'Irish', 'bogo' ),
50 + 'gd' => __( 'Scottish Gaelic', 'bogo' ),
51 + 'gl_ES' => __( 'Galician', 'bogo' ),
52 + 'gu' => __( 'Gujarati', 'bogo' ),
53 + 'he_IL' => __( 'Hebrew', 'bogo' ),
54 + 'hi_IN' => __( 'Hindi', 'bogo' ),
55 + 'hr' => __( 'Croatian', 'bogo' ),
56 + 'ht' => __( 'Haitian', 'bogo' ),
57 + 'hu_HU' => __( 'Hungarian', 'bogo' ),
58 + 'hy' => __( 'Armenian', 'bogo' ),
59 + 'id_ID' => __( 'Indonesian', 'bogo' ),
60 + 'is_IS' => __( 'Icelandic', 'bogo' ),
61 + 'it_IT' => __( 'Italian', 'bogo' ),
62 + 'ja' => __( 'Japanese', 'bogo' ),
63 + 'jv_ID' => __( 'Javanese', 'bogo' ),
64 + 'ka_GE' => __( 'Georgian', 'bogo' ),
65 + 'kk' => __( 'Kazakh', 'bogo' ),
66 + 'km_KH' => __( 'Khmer', 'bogo' ),
67 + 'kn' => __( 'Kannada', 'bogo' ),
68 + 'ko_KR' => __( 'Korean', 'bogo' ),
69 + 'li' => __( 'Limburgish', 'bogo' ),
70 + 'lo' => __( 'Lao', 'bogo' ),
71 + 'lt_LT' => __( 'Lithuanian', 'bogo' ),
72 + 'lv' => __( 'Latvian', 'bogo' ),
73 + 'mg_MG' => __( 'Malagasy', 'bogo' ),
74 + 'mk_MK' => __( 'Macedonian', 'bogo' ),
75 + 'ml_IN' => __( 'Malayalam', 'bogo' ),
76 + 'mn' => __( 'Mongolian', 'bogo' ),
77 + 'mr' => __( 'Marathi', 'bogo' ),
78 + 'ms_MY' => __( 'Malay', 'bogo' ),
79 + 'mt_MT' => __( 'Maltese', 'bogo' ),
80 + 'my_MM' => __( 'Burmese', 'bogo' ),
81 + 'nb_NO' => __( 'Norwegian (Bokmål)', 'bogo' ),
82 + 'ne_NP' => __( 'Nepali', 'bogo' ),
83 + 'ni_ID' => __( 'Nias', 'bogo' ),
84 + 'nl_NL' => __( 'Dutch', 'bogo' ),
85 + 'os' => __( 'Ossetic', 'bogo' ),
86 + 'pa_IN' => __( 'Punjabi', 'bogo' ),
87 + 'pl_PL' => __( 'Polish', 'bogo' ),
88 + 'pt_BR' => __( 'Portuguese (Brazil)', 'bogo' ),
89 + 'pt_PT' => __( 'Portuguese (Portugal)', 'bogo' ),
90 + 'ro_RO' => __( 'Romanian', 'bogo' ),
91 + 'ru_RU' => __( 'Russian', 'bogo' ),
92 + 'sa_IN' => __( 'Sanskrit', 'bogo' ),
93 + 'si_LK' => __( 'Sinhala', 'bogo' ),
94 + 'sk_SK' => __( 'Slovak', 'bogo' ),
95 + 'sl_SI' => __( 'Slovenian', 'bogo' ),
96 + 'so_SO' => __( 'Somali', 'bogo' ),
97 + 'sq' => __( 'Albanian', 'bogo' ),
98 + 'sr_RS' => __( 'Serbian', 'bogo' ),
99 + 'sv_SE' => __( 'Swedish', 'bogo' ),
100 + 'su_ID' => __( 'Sundanese', 'bogo' ),
101 + 'sw' => __( 'Swahili', 'bogo' ),
102 + 'ta_IN' => __( 'Tamil', 'bogo' ),
103 + 'te' => __( 'Telugu', 'bogo' ),
104 + 'tg' => __( 'Tajik', 'bogo' ),
105 + 'th' => __( 'Thai', 'bogo' ),
106 + 'tl' => __( 'Tagalog', 'bogo' ),
107 + 'tr_TR' => __( 'Turkish', 'bogo' ),
108 + 'tt_RU' => __( 'Tatar', 'bogo' ),
109 + 'ug_CN' => __( 'Uighur', 'bogo' ),
110 + 'uk' => __( 'Ukrainian', 'bogo' ),
111 + 'ur' => __( 'Urdu', 'bogo' ),
112 + 'uz_UZ' => __( 'Uzbek', 'bogo' ),
113 + 'vi' => __( 'Vietnamese', 'bogo' ),
114 + 'zh_CN' => __( 'Chinese (China)', 'bogo' ),
115 + 'zh_TW' => __( 'Chinese (Taiwan)', 'bogo' )
116 + );
117 +
118 + asort( $languages, SORT_STRING );
119 +
120 + $languages = apply_filters( 'bogo_languages', $languages );
121 +
122 + return $languages;
11 123 }
12 124
125 +function bogo_get_language( $locale ) {
126 + $languages = bogo_languages();
13 127
14 -function bogo_set_prop( $prop, $value ) {
15 - $option = get_option( 'bogo' );
128 + if ( isset( $languages[$locale] ) )
129 + return $languages[$locale];
16 130
17 - if ( ! is_array( $option ) ) {
18 - $option = array();
131 + return null;
132 +}
133 +
134 +function bogo_get_default_locale() {
135 + if ( defined( 'WPLANG' ) ) {
136 + $locale = WPLANG;
19 137 }
20 138
21 - $option[$prop] = $value;
139 + if ( is_multisite() ) {
140 + if ( defined( 'WP_INSTALLING' )
141 + || ( false === $ms_locale = get_option( 'WPLANG' ) ) ) {
142 + $ms_locale = get_site_option( 'WPLANG' );
143 + }
22 144
23 - update_option( 'bogo', $option );
145 + if ( $ms_locale !== false ) {
146 + $locale = $ms_locale;
147 + }
148 + } else {
149 + $db_locale = get_option( 'WPLANG' );
150 +
151 + if ( $db_locale !== false ) {
152 + $locale = $db_locale;
153 + }
154 + }
155 +
156 + if ( empty( $locale ) ) {
157 + $locale = 'en_US';
158 + }
159 +
160 + return $locale;
24 161 }
25 162
163 +function bogo_is_default_locale( $locale ) {
164 + $default_locale = bogo_get_default_locale();
26 165
27 -function bogo_delete_prop( $prop ) {
28 - $option = get_option( 'bogo' );
166 + return ! empty( $locale ) && $locale == bogo_get_default_locale();
167 +}
29 168
30 - if ( ! is_array( $option ) ) {
31 - $option = array();
169 +function bogo_available_locales( $args = '' ) {
170 + $defaults = array(
171 + 'exclude' => array(),
172 + 'current_user_can_access' => false );
173 +
174 + $args = wp_parse_args( $args, $defaults );
175 +
176 + $installed_locales = get_available_languages();
177 + $installed_locales[] = bogo_get_default_locale();
178 + $installed_locales[] = 'en_US';
179 + $installed_locales = array_unique( $installed_locales );
180 + $installed_locales = array_filter( $installed_locales );
181 +
182 + $available_locales = array();
183 +
184 + foreach ( $installed_locales as $locale ) {
185 + if ( in_array( $locale, (array) $args['exclude'] ) ) {
186 + continue;
187 + }
188 +
189 + if ( $args['current_user_can_access']
190 + && ! current_user_can( 'bogo_access_locale', $locale ) ) {
191 + continue;
192 + }
193 +
194 + $available_locales[] = $locale;
32 195 }
33 196
34 - if ( isset( $option[$prop] ) ) {
35 - unset( $option[$prop] );
197 + return $available_locales;
198 +}
199 +
200 +function bogo_available_languages( $args = '' ) {
201 + $defaults = array(
202 + 'exclude' => array(),
203 + 'orderby' => 'key',
204 + 'order' => 'ASC',
205 + 'current_user_can_access' => false );
206 +
207 + $args = wp_parse_args( $args, $defaults );
208 +
209 + $langs = array();
210 +
211 + $available_locales = bogo_available_locales( $args );
212 +
213 + foreach ( $available_locales as $locale ) {
214 + $lang = bogo_get_language( $locale );
215 + $langs[$locale] = empty( $lang ) ? "[$locale]" : $lang;
36 216 }
37 217
38 - update_option( 'bogo', $option );
218 + if ( 'value' == $args['orderby'] ) {
219 + natcasesort( $langs );
220 +
221 + if ( 'DESC' == $args['order'] ) {
222 + $langs = array_reverse( $langs );
223 + }
224 + } else {
225 + if ( 'DESC' == $args['order'] ) {
226 + krsort( $langs );
227 + } else {
228 + ksort( $langs );
229 + }
230 + }
231 +
232 + $langs = apply_filters( 'bogo_available_languages', $langs, $args );
233 +
234 + return $langs;
39 235 }
40 236
237 +function bogo_is_available_locale( $locale ) {
238 + if ( empty( $locale ) ) {
239 + return false;
240 + }
41 241
42 -function bogo_plugin_url( $path = '' ) {
43 - $url = plugins_url( $path, BOGO_PLUGIN );
242 + static $available_locales = array();
44 243
45 - if ( is_ssl() and 'http:' === substr( $url, 0, 5 ) ) {
46 - $url = 'https:' . substr( $url, 5 );
244 + if ( empty( $available_locales ) ) {
245 + $available_locales = bogo_available_locales();
47 246 }
48 247
248 + return in_array( $locale, $available_locales );
249 +}
250 +
251 +function bogo_filter_locales( $locales, $filter = 'available' ) {
252 + return array_intersect( $locales, bogo_available_locales() );
253 +}
254 +
255 +function bogo_language_tag( $locale ) {
256 + // http://www.ietf.org/rfc/bcp/bcp47.txt
257 + $tag = preg_replace( '/[^0-9a-zA-Z]+/', '-', $locale );
258 + $tag = trim( $tag, '-' );
259 +
260 + return apply_filters( 'bogo_language_tag', $tag, $locale );
261 +}
262 +
263 +function bogo_lang_slug( $locale ) {
264 + $tag = bogo_language_tag( $locale );
265 + $slug = $tag;
266 +
267 + if ( false !== $pos = strpos( $tag, '-' ) )
268 + $slug = substr( $tag, 0, $pos );
269 +
270 + $variations = preg_grep( '/^' . $slug . '/', bogo_available_locales() );
271 +
272 + if ( 1 < count( $variations ) )
273 + $slug = $tag;
274 +
275 + return apply_filters( 'bogo_lang_slug', $slug, $locale );
276 +}
277 +
278 +function bogo_get_lang_regex() {
279 + $langs = array_map( 'bogo_lang_slug', bogo_available_locales() );
280 + $langs = array_filter( $langs );
281 +
282 + if ( empty( $langs ) )
283 + return '';
284 +
285 + return '(' . implode( '|', $langs ) . ')';
286 +}
287 +
288 +function bogo_get_closest_locale( $var ) {
289 + $var = strtolower( $var );
290 +
291 + if ( ! preg_match( '/^([a-z]{2})(?:[_-]([a-z]{2}))?/', $var, $matches ) )
292 + return false;
293 +
294 + $language_code = $matches[1];
295 + $region_code = isset( $matches[2] ) ? $matches[2] : '';
296 +
297 + $locales = bogo_available_locales();
298 +
299 + if ( $region_code ) {
300 + $locale = $language_code . '_' . strtoupper( $region_code );
301 +
302 + if ( false !== array_search( $locale, $locales ) )
303 + return $locale;
304 + }
305 +
306 + $locale = $language_code;
307 +
308 + if ( false !== array_search( $locale, $locales ) )
309 + return $locale;
310 +
311 + if ( $matches = preg_grep( "/^{$locale}_/", $locales ) )
312 + return array_shift( $matches );
313 +
314 + return false;
315 +}
316 +
317 +function bogo_http_accept_languages() {
318 + if ( ! isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) )
319 + return false;
320 +
321 + $languages = array();
322 +
323 + foreach ( explode( ',', $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) as $lang ) {
324 + $lang = trim( strtolower( $lang ) );
325 +
326 + if ( preg_match( '/^([a-z-]+)(?:;q=([0-9.]+))?$/', $lang, $matches ) ) {
327 + $language_tag = $matches[1];
328 + $qvalue = isset( $matches[2] ) ? 0 + $matches[2] : 1;
329 +
330 + if ( preg_match( '/^([a-z]{2})(?:-([a-z]{2}))?$/', $language_tag, $matches ) ) {
331 + $language_tag = $matches[1];
332 +
333 + if ( isset( $matches[2] ) )
334 + $language_tag .= '_' . strtoupper( $matches[2] );
335 +
336 + $languages[$language_tag] = $qvalue;
337 + }
338 + }
339 + }
340 +
341 + natsort( $languages );
342 +
343 + return array_reverse( array_keys( $languages ) );
344 +}
345 +
346 +function bogo_url( $url = null, $lang = null ) {
347 + if ( ! $lang )
348 + $lang = get_locale();
349 +
350 + $args = array(
351 + 'using_permalinks' => (bool) get_option( 'permalink_structure' ) );
352 +
353 + return bogo_get_url_with_lang( $url, $lang, $args );
354 +}
355 +
356 +function bogo_get_url_with_lang( $url = null, $lang = null, $args = '' ) {
357 + $defaults = array(
358 + 'using_permalinks' => true );
359 +
360 + $args = wp_parse_args( $args, $defaults );
361 +
362 + if ( ! $url ) {
363 + if ( ! $url = redirect_canonical( $url, false ) ) {
364 + $url = is_ssl() ? 'https://' : 'http://';
365 + $url .= $_SERVER['HTTP_HOST'];
366 + $url .= $_SERVER['REQUEST_URI'];
367 + }
368 +
369 + if ( $frag = strstr( $url, '#' ) )
370 + $url = substr( $url, 0, - strlen( $frag ) );
371 +
372 + if ( $query = @parse_url( $url, PHP_URL_QUERY ) ) {
373 + parse_str( $query, $query_vars );
374 +
375 + foreach ( array_keys( $query_vars ) as $qv ) {
376 + if ( ! get_query_var( $qv ) )
377 + $url = remove_query_arg( $qv, $url );
378 + }
379 + }
380 + }
381 +
382 + $default_locale = bogo_get_default_locale();
383 +
384 + if ( ! $lang )
385 + $lang = $default_locale;
386 +
387 + $home = set_url_scheme( get_option( 'home' ) );
388 + $home = trailingslashit( $home );
389 +
390 + $url = remove_query_arg( 'lang', $url );
391 +
392 + if ( ! $args['using_permalinks'] ) {
393 + if ( $lang != $default_locale )
394 + $url = add_query_arg( array( 'lang' => bogo_lang_slug( $lang ) ), $url );
395 +
396 + return $url;
397 + }
398 +
399 + $available_languages = array_map( 'bogo_lang_slug', bogo_available_locales() );
400 +
401 + $url = preg_replace(
402 + '#^' . preg_quote( $home ) . '((' . implode( '|', $available_languages ) . ')/)?#',
403 + $home . ( $lang == $default_locale ? '' : bogo_lang_slug( $lang ) . '/' ),
404 + trailingslashit( $url ) );
405 +
49 406 return $url;
50 407 }
408 +
409 +function bogo_get_lang_from_url( $url = '' ) {
410 + if ( ! $url ) {
411 + $url = is_ssl() ? 'https://' : 'http://';
412 + $url .= $_SERVER['HTTP_HOST'];
413 + $url .= $_SERVER['REQUEST_URI'];
414 + }
415 +
416 + if ( $frag = strstr( $url, '#' ) )
417 + $url = substr( $url, 0, - strlen( $frag ) );
418 +
419 + $home = set_url_scheme( get_option( 'home' ) );
420 + $home = trailingslashit( $home );
421 +
422 + $available_languages = array_map( 'bogo_lang_slug', bogo_available_locales() );
423 +
424 + $regex = '#^' . preg_quote( $home ) . '(' . implode( '|', $available_languages ) . ')/#';
425 +
426 + if ( preg_match( $regex, trailingslashit( $url ), $matches ) )
427 + return $matches[1];
428 +
429 + if ( $query = @parse_url( $url, PHP_URL_QUERY ) ) {
430 + parse_str( $query, $query_vars );
431 +
432 + if ( isset( $query_vars['lang'] )
433 + && in_array( $query_vars['lang'], $available_languages ) )
434 + return $query_vars['lang'];
435 + }
436 +
437 + return false;
438 +}
439 +
440 +function bogo_get_prop( $prop ) {
441 + $option = get_option( 'bogo' );
442 +
443 + if ( ! is_array( $option ) )
444 + $option = array();
445 +
446 + return isset( $option[$prop] ) ? $option[$prop] : '';
447 +}
448 +
449 +function bogo_set_prop( $prop, $value ) {
450 + $option = get_option( 'bogo' );
451 +
452 + if ( ! is_array( $option ) )
453 + $option = array();
454 +
455 + $option[$prop] = $value;
456 +
457 + update_option( 'bogo', $option );
458 +}
459 +
460 +?>