PluginProbe
Bogo / 2.3
Bogo v2.3
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
bogo / includes / functions.php

functions.php in Bogo 2.3, at includes/functions.php

437 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 function bogo_languages() {
4 static $languages = array();
5
6 if ( ! empty( $languages ) ) {
7 return $languages;
8 }
9
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' => __( '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', 'bogo' ),
33 'en_AU' => __( 'Australian English', 'bogo' ),
34 'en_CA' => __( 'Canadian English', 'bogo' ),
35 'en_GB' => __( 'British English', 'bogo' ),
36 'eo' => __( 'Esperanto', 'bogo' ),
37 'es_ES' => __( 'Spanish', 'bogo' ),
38 'es_CL' => __( 'Chilean Spanish', 'bogo' ),
39 'es_MX' => __( 'Mexican Spanish', 'bogo' ),
40 'es_PE' => __( 'Peruvian Spanish', 'bogo' ),
41 'es_VE' => __( 'Venezuelan Spanish', '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', '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', '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' => __( 'Brazilian Portuguese', 'bogo' ),
89 'pt_PT' => __( 'Portuguese', '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', 'bogo' ),
115 'zh_TW' => __( 'Traditional Chinese', 'bogo' )
116 );
117
118 $languages = apply_filters( 'bogo_languages', $languages );
119
120 return $languages;
121 }
122
123 function bogo_get_language( $locale ) {
124 $languages = bogo_languages();
125
126 if ( isset( $languages[$locale] ) )
127 return $languages[$locale];
128
129 return null;
130 }
131
132 function bogo_get_default_locale() {
133 if ( defined( 'WPLANG' ) )
134 $locale = WPLANG;
135
136 if ( is_multisite() ) {
137 if ( defined( 'WP_INSTALLING' ) || ( false === $ms_locale = get_option( 'WPLANG' ) ) )
138 $ms_locale = get_site_option( 'WPLANG' );
139
140 if ( $ms_locale !== false )
141 $locale = $ms_locale;
142 }
143
144 if ( empty( $locale ) )
145 $locale = 'en_US';
146
147 return $locale;
148 }
149
150 function bogo_is_default_locale( $locale ) {
151 $default_locale = bogo_get_default_locale();
152
153 return ! empty( $locale ) && $locale == bogo_get_default_locale();
154 }
155
156 function bogo_available_locales( $args = '' ) {
157 $defaults = array(
158 'exclude' => array(),
159 'current_user_can_access' => false );
160
161 $args = wp_parse_args( $args, $defaults );
162
163 $installed_locales = get_available_languages();
164 $installed_locales[] = bogo_get_default_locale();
165 $installed_locales[] = 'en_US';
166 $installed_locales = array_unique( $installed_locales );
167 $installed_locales = array_filter( $installed_locales );
168
169 $available_locales = array();
170
171 foreach ( $installed_locales as $locale ) {
172 if ( in_array( $locale, (array) $args['exclude'] ) ) {
173 continue;
174 }
175
176 if ( $args['current_user_can_access']
177 && ! current_user_can( 'bogo_access_locale', $locale ) ) {
178 continue;
179 }
180
181 $available_locales[] = $locale;
182 }
183
184 return $available_locales;
185 }
186
187 function bogo_available_languages( $args = '' ) {
188 $defaults = array(
189 'exclude' => array(),
190 'orderby' => 'key',
191 'order' => 'ASC',
192 'current_user_can_access' => false );
193
194 $args = wp_parse_args( $args, $defaults );
195
196 $langs = array();
197
198 $available_locales = bogo_available_locales( $args );
199
200 foreach ( $available_locales as $locale ) {
201 $lang = bogo_get_language( $locale );
202 $langs[$locale] = empty( $lang ) ? "[$locale]" : $lang;
203 }
204
205 if ( 'value' == $args['orderby'] ) {
206 natcasesort( $langs );
207
208 if ( 'DESC' == $args['order'] ) {
209 $langs = array_reverse( $langs );
210 }
211 } else {
212 if ( 'DESC' == $args['order'] ) {
213 krsort( $langs );
214 } else {
215 ksort( $langs );
216 }
217 }
218
219 $langs = apply_filters( 'bogo_available_languages', $langs, $args );
220
221 return $langs;
222 }
223
224 function bogo_is_available_locale( $locale ) {
225 return ! empty( $locale ) && in_array( $locale, bogo_available_locales() );
226 }
227
228 function bogo_filter_locales( $locales, $filter = 'available' ) {
229 return array_intersect( $locales, bogo_available_locales() );
230 }
231
232 function bogo_language_tag( $locale ) {
233 // http://www.ietf.org/rfc/bcp/bcp47.txt
234 $tag = preg_replace( '/[^0-9a-zA-Z]+/', '-', $locale );
235 $tag = trim( $tag, '-' );
236
237 return apply_filters( 'bogo_language_tag', $tag, $locale );
238 }
239
240 function bogo_lang_slug( $locale ) {
241 $tag = bogo_language_tag( $locale );
242 $slug = $tag;
243
244 if ( false !== $pos = strpos( $tag, '-' ) )
245 $slug = substr( $tag, 0, $pos );
246
247 $variations = preg_grep( '/^' . $slug . '/', bogo_available_locales() );
248
249 if ( 1 < count( $variations ) )
250 $slug = $tag;
251
252 return apply_filters( 'bogo_lang_slug', $slug, $locale );
253 }
254
255 function bogo_get_lang_regex() {
256 $langs = array_map( 'bogo_lang_slug', bogo_available_locales() );
257 $langs = array_filter( $langs );
258
259 if ( empty( $langs ) )
260 return '';
261
262 return '(' . implode( '|', $langs ) . ')';
263 }
264
265 function bogo_get_closest_locale( $var ) {
266 $var = strtolower( $var );
267
268 if ( ! preg_match( '/^([a-z]{2})(?:[_-]([a-z]{2}))?/', $var, $matches ) )
269 return false;
270
271 $language_code = $matches[1];
272 $region_code = isset( $matches[2] ) ? $matches[2] : '';
273
274 $locales = bogo_available_locales();
275
276 if ( $region_code ) {
277 $locale = $language_code . '_' . strtoupper( $region_code );
278
279 if ( false !== array_search( $locale, $locales ) )
280 return $locale;
281 }
282
283 $locale = $language_code;
284
285 if ( false !== array_search( $locale, $locales ) )
286 return $locale;
287
288 if ( $matches = preg_grep( "/^{$locale}_/", $locales ) )
289 return array_shift( $matches );
290
291 return false;
292 }
293
294 function bogo_http_accept_languages() {
295 if ( ! isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) )
296 return false;
297
298 $languages = array();
299
300 foreach ( explode( ',', $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) as $lang ) {
301 $lang = trim( strtolower( $lang ) );
302
303 if ( preg_match( '/^([a-z-]+)(?:;q=([0-9.]+))?$/', $lang, $matches ) ) {
304 $language_tag = $matches[1];
305 $qvalue = isset( $matches[2] ) ? 0 + $matches[2] : 1;
306
307 if ( preg_match( '/^([a-z]{2})(?:-([a-z]{2}))?$/', $language_tag, $matches ) ) {
308 $language_tag = $matches[1];
309
310 if ( isset( $matches[2] ) )
311 $language_tag .= '_' . strtoupper( $matches[2] );
312
313 $languages[$language_tag] = $qvalue;
314 }
315 }
316 }
317
318 natsort( $languages );
319
320 return array_reverse( array_keys( $languages ) );
321 }
322
323 function bogo_url( $url = null, $lang = null ) {
324 if ( ! $lang )
325 $lang = get_locale();
326
327 $args = array(
328 'using_permalinks' => (bool) get_option( 'permalink_structure' ) );
329
330 return bogo_get_url_with_lang( $url, $lang, $args );
331 }
332
333 function bogo_get_url_with_lang( $url = null, $lang = null, $args = '' ) {
334 $defaults = array(
335 'using_permalinks' => true );
336
337 $args = wp_parse_args( $args, $defaults );
338
339 if ( ! $url ) {
340 if ( ! $url = redirect_canonical( $url, false ) ) {
341 $url = is_ssl() ? 'https://' : 'http://';
342 $url .= $_SERVER['HTTP_HOST'];
343 $url .= $_SERVER['REQUEST_URI'];
344 }
345
346 if ( $frag = strstr( $url, '#' ) )
347 $url = substr( $url, 0, - strlen( $frag ) );
348
349 if ( $query = @parse_url( $url, PHP_URL_QUERY ) ) {
350 parse_str( $query, $query_vars );
351
352 foreach ( array_keys( $query_vars ) as $qv ) {
353 if ( ! get_query_var( $qv ) )
354 $url = remove_query_arg( $qv, $url );
355 }
356 }
357 }
358
359 $default_locale = bogo_get_default_locale();
360
361 if ( ! $lang )
362 $lang = $default_locale;
363
364 $home = set_url_scheme( get_option( 'home' ) );
365 $home = trailingslashit( $home );
366
367 $url = remove_query_arg( 'lang', $url );
368
369 if ( ! $args['using_permalinks'] ) {
370 if ( $lang != $default_locale )
371 $url = add_query_arg( array( 'lang' => bogo_lang_slug( $lang ) ), $url );
372
373 return $url;
374 }
375
376 $available_languages = array_map( 'bogo_lang_slug', bogo_available_locales() );
377
378 $url = preg_replace(
379 '#^' . preg_quote( $home ) . '((' . implode( '|', $available_languages ) . ')/)?#',
380 $home . ( $lang == $default_locale ? '' : bogo_lang_slug( $lang ) . '/' ),
381 trailingslashit( $url ) );
382
383 return $url;
384 }
385
386 function bogo_get_lang_from_url( $url = '' ) {
387 if ( ! $url ) {
388 $url = is_ssl() ? 'https://' : 'http://';
389 $url .= $_SERVER['HTTP_HOST'];
390 $url .= $_SERVER['REQUEST_URI'];
391 }
392
393 if ( $frag = strstr( $url, '#' ) )
394 $url = substr( $url, 0, - strlen( $frag ) );
395
396 $home = set_url_scheme( get_option( 'home' ) );
397 $home = trailingslashit( $home );
398
399 $available_languages = array_map( 'bogo_lang_slug', bogo_available_locales() );
400
401 $regex = '#^' . preg_quote( $home ) . '(' . implode( '|', $available_languages ) . ')/#';
402
403 if ( preg_match( $regex, trailingslashit( $url ), $matches ) )
404 return $matches[1];
405
406 if ( $query = @parse_url( $url, PHP_URL_QUERY ) ) {
407 parse_str( $query, $query_vars );
408
409 if ( isset( $query_vars['lang'] )
410 && in_array( $query_vars['lang'], $available_languages ) )
411 return $query_vars['lang'];
412 }
413
414 return false;
415 }
416
417 function bogo_get_prop( $prop ) {
418 $option = get_option( 'bogo' );
419
420 if ( ! is_array( $option ) )
421 $option = array();
422
423 return isset( $option[$prop] ) ? $option[$prop] : '';
424 }
425
426 function bogo_set_prop( $prop, $value ) {
427 $option = get_option( 'bogo' );
428
429 if ( ! is_array( $option ) )
430 $option = array();
431
432 $option[$prop] = $value;
433
434 update_option( 'bogo', $option );
435 }
436
437 ?>