| 1 |
<?php |
| 2 |
|
| 3 |
function bogo_languages() { |
| 4 |
static $languages = array(); |
| 5 |
static $textdomain_loaded = false; |
| 6 |
|
| 7 |
if ( $languages && $textdomain_loaded ) { |
| 8 |
return apply_filters( 'bogo_languages', $languages ); |
| 9 |
} |
| 10 |
|
| 11 |
$languages = array( |
| 12 |
'af' => __( 'Afrikaans', 'bogo' ), |
| 13 |
'am' => __( 'Amharic', 'bogo' ), |
| 14 |
'an' => __( 'Aragonese', 'bogo' ), |
| 15 |
'ar' => __( 'Arabic', 'bogo' ), |
| 16 |
'ary' => __( 'Moroccan Arabic', 'bogo' ), |
| 17 |
'as' => __( 'Assamese', 'bogo' ), |
| 18 |
'az' => __( 'Azerbaijani', 'bogo' ), |
| 19 |
'azb' => __( 'South Azerbaijani', 'bogo' ), |
| 20 |
'be_BY' => __( 'Belarusian', 'bogo' ), |
| 21 |
'bg_BG' => __( 'Bulgarian', 'bogo' ), |
| 22 |
'bn_BD' => __( 'Bangla', 'bogo' ), |
| 23 |
'bn_IN' => __( 'Indian Bengali', 'bogo' ), |
| 24 |
'bo' => __( 'Tibetan', 'bogo' ), |
| 25 |
'bs_BA' => __( 'Bosnian', 'bogo' ), |
| 26 |
'ca' => __( 'Catalan', 'bogo' ), |
| 27 |
'ceb' => __( 'Cebuano', 'bogo' ), |
| 28 |
'ckb' => __( 'Central Kurdish', 'bogo' ), |
| 29 |
'co' => __( 'Corsican', 'bogo' ), |
| 30 |
'cs_CZ' => __( 'Czech', 'bogo' ), |
| 31 |
'cy' => __( 'Welsh', 'bogo' ), |
| 32 |
'da_DK' => __( 'Danish', 'bogo' ), |
| 33 |
'de_DE' => __( 'German', 'bogo' ), |
| 34 |
'de_DE_formal' => __( 'German (Formal)', 'bogo' ), |
| 35 |
'de_CH' => __( 'German (Switzerland)', 'bogo' ), |
| 36 |
'de_CH_informal' => __( 'German (Switzerland) (Informal)', 'bogo' ), |
| 37 |
'dv' => __( 'Dhivehi', 'bogo' ), |
| 38 |
'el' => __( 'Greek', 'bogo' ), |
| 39 |
'en_US' => __( 'English (United States)', 'bogo' ), |
| 40 |
'en_AU' => __( 'English (Australia)', 'bogo' ), |
| 41 |
'en_CA' => __( 'English (Canada)', 'bogo' ), |
| 42 |
'en_GB' => __( 'English (UK)', 'bogo' ), |
| 43 |
'en_NZ' => __( 'English (New Zealand)', 'bogo' ), |
| 44 |
'en_ZA' => __( 'English (South Africa)', 'bogo' ), |
| 45 |
'eo' => __( 'Esperanto', 'bogo' ), |
| 46 |
'es_ES' => __( 'Spanish (Spain)', 'bogo' ), |
| 47 |
'es_AR' => __( 'Spanish (Argentina)', 'bogo' ), |
| 48 |
'es_CL' => __( 'Spanish (Chile)', 'bogo' ), |
| 49 |
'es_CO' => __( 'Spanish (Colombia)', 'bogo' ), |
| 50 |
'es_GT' => __( 'Spanish (Guatemala)', 'bogo' ), |
| 51 |
'es_MX' => __( 'Spanish (Mexico)', 'bogo' ), |
| 52 |
'es_PE' => __( 'Spanish (Peru)', 'bogo' ), |
| 53 |
'es_VE' => __( 'Spanish (Venezuela)', 'bogo' ), |
| 54 |
'et' => __( 'Estonian', 'bogo' ), |
| 55 |
'eu' => __( 'Basque', 'bogo' ), |
| 56 |
'fa_IR' => __( 'Persian', 'bogo' ), |
| 57 |
'fi' => __( 'Finnish', 'bogo' ), |
| 58 |
'fo' => __( 'Faroese', 'bogo' ), |
| 59 |
'fr_FR' => __( 'French (France)', 'bogo' ), |
| 60 |
'fr_BE' => __( 'French (Belgium)', 'bogo' ), |
| 61 |
'fr_CA' => __( 'French (Canada)', 'bogo' ), |
| 62 |
'fy' => __( 'Frisian', 'bogo' ), |
| 63 |
'ga' => __( 'Irish', 'bogo' ), |
| 64 |
'gd' => __( 'Scottish Gaelic', 'bogo' ), |
| 65 |
'gl_ES' => __( 'Galician', 'bogo' ), |
| 66 |
'gu' => __( 'Gujarati', 'bogo' ), |
| 67 |
'haz' => __( 'Hazaragi', 'bogo' ), |
| 68 |
'he_IL' => __( 'Hebrew', 'bogo' ), |
| 69 |
'hi_IN' => __( 'Hindi', 'bogo' ), |
| 70 |
'hr' => __( 'Croatian', 'bogo' ), |
| 71 |
'ht' => __( 'Haitian', 'bogo' ), |
| 72 |
'hu_HU' => __( 'Hungarian', 'bogo' ), |
| 73 |
'hy' => __( 'Armenian', 'bogo' ), |
| 74 |
'id_ID' => __( 'Indonesian', 'bogo' ), |
| 75 |
'is_IS' => __( 'Icelandic', 'bogo' ), |
| 76 |
'it_IT' => __( 'Italian', 'bogo' ), |
| 77 |
'ja' => __( 'Japanese', 'bogo' ), |
| 78 |
'jv_ID' => __( 'Javanese', 'bogo' ), |
| 79 |
'ka_GE' => __( 'Georgian', 'bogo' ), |
| 80 |
'kk' => __( 'Kazakh', 'bogo' ), |
| 81 |
'km' => __( 'Khmer', 'bogo' ), |
| 82 |
'kn' => __( 'Kannada', 'bogo' ), |
| 83 |
'ko_KR' => __( 'Korean', 'bogo' ), |
| 84 |
'li' => __( 'Limburgish', 'bogo' ), |
| 85 |
'lo' => __( 'Lao', 'bogo' ), |
| 86 |
'lt_LT' => __( 'Lithuanian', 'bogo' ), |
| 87 |
'lv' => __( 'Latvian', 'bogo' ), |
| 88 |
'mg_MG' => __( 'Malagasy', 'bogo' ), |
| 89 |
'mk_MK' => __( 'Macedonian', 'bogo' ), |
| 90 |
'ml_IN' => __( 'Malayalam', 'bogo' ), |
| 91 |
'mn' => __( 'Mongolian', 'bogo' ), |
| 92 |
'mr' => __( 'Marathi', 'bogo' ), |
| 93 |
'ms_MY' => __( 'Malay', 'bogo' ), |
| 94 |
'mt_MT' => __( 'Maltese', 'bogo' ), |
| 95 |
'my_MM' => __( 'Burmese', 'bogo' ), |
| 96 |
'nb_NO' => __( 'Norwegian (Bokmål)', 'bogo' ), |
| 97 |
'ne_NP' => __( 'Nepali', 'bogo' ), |
| 98 |
'ni_ID' => __( 'Nias', 'bogo' ), |
| 99 |
'nl_NL' => __( 'Dutch', 'bogo' ), |
| 100 |
'nl_NL_formal' => __( 'Dutch (Formal)', 'bogo' ), |
| 101 |
'nn_NO' => __( 'Norwegian (Nynorsk)', 'bogo' ), |
| 102 |
'oci' => __( 'Occitan', 'bogo' ), |
| 103 |
'os' => __( 'Ossetic', 'bogo' ), |
| 104 |
'pa_IN' => __( 'Punjabi', 'bogo' ), |
| 105 |
'pl_PL' => __( 'Polish', 'bogo' ), |
| 106 |
'ps' => __( 'Pashto', 'bogo' ), |
| 107 |
'pt_BR' => __( 'Portuguese (Brazil)', 'bogo' ), |
| 108 |
'pt_PT' => __( 'Portuguese (Portugal)', 'bogo' ), |
| 109 |
'ro_RO' => __( 'Romanian', 'bogo' ), |
| 110 |
'ru_RU' => __( 'Russian', 'bogo' ), |
| 111 |
'sa_IN' => __( 'Sanskrit', 'bogo' ), |
| 112 |
'si_LK' => __( 'Sinhala', 'bogo' ), |
| 113 |
'sk_SK' => __( 'Slovak', 'bogo' ), |
| 114 |
'sl_SI' => __( 'Slovenian', 'bogo' ), |
| 115 |
'so_SO' => __( 'Somali', 'bogo' ), |
| 116 |
'sq' => __( 'Albanian', 'bogo' ), |
| 117 |
'sr_RS' => __( 'Serbian', 'bogo' ), |
| 118 |
'sv_SE' => __( 'Swedish', 'bogo' ), |
| 119 |
'su_ID' => __( 'Sundanese', 'bogo' ), |
| 120 |
'sw' => __( 'Swahili', 'bogo' ), |
| 121 |
'szl' => __( 'Silesian', 'bogo' ), |
| 122 |
'ta_IN' => __( 'Tamil', 'bogo' ), |
| 123 |
'te' => __( 'Telugu', 'bogo' ), |
| 124 |
'tg' => __( 'Tajik', 'bogo' ), |
| 125 |
'th' => __( 'Thai', 'bogo' ), |
| 126 |
'tl' => __( 'Tagalog', 'bogo' ), |
| 127 |
'tr_TR' => __( 'Turkish', 'bogo' ), |
| 128 |
'tt_RU' => __( 'Tatar', 'bogo' ), |
| 129 |
'ug_CN' => __( 'Uighur', 'bogo' ), |
| 130 |
'uk' => __( 'Ukrainian', 'bogo' ), |
| 131 |
'ur' => __( 'Urdu', 'bogo' ), |
| 132 |
'uz_UZ' => __( 'Uzbek', 'bogo' ), |
| 133 |
'vi' => __( 'Vietnamese', 'bogo' ), |
| 134 |
'zh_CN' => __( 'Chinese (China)', 'bogo' ), |
| 135 |
'zh_HK' => __( 'Chinese (Hong Kong)', 'bogo' ), |
| 136 |
'zh_TW' => __( 'Chinese (Taiwan)', 'bogo' ) |
| 137 |
); |
| 138 |
|
| 139 |
if ( is_textdomain_loaded( 'bogo' ) ) { |
| 140 |
$textdomain_loaded = true; |
| 141 |
} |
| 142 |
|
| 143 |
asort( $languages, SORT_STRING ); |
| 144 |
|
| 145 |
return apply_filters( 'bogo_languages', $languages ); |
| 146 |
} |
| 147 |
|
| 148 |
function bogo_get_language( $locale ) { |
| 149 |
$languages = bogo_languages(); |
| 150 |
|
| 151 |
if ( isset( $languages[$locale] ) ) |
| 152 |
return $languages[$locale]; |
| 153 |
|
| 154 |
return null; |
| 155 |
} |
| 156 |
|
| 157 |
function bogo_get_language_native_name( $locale ) { |
| 158 |
if ( 'en_US' == $locale ) { |
| 159 |
return 'English (United States)'; |
| 160 |
} |
| 161 |
|
| 162 |
if ( ! function_exists( 'wp_get_available_translations' ) ) { |
| 163 |
require_once ABSPATH . 'wp-admin/includes/translation-install.php'; |
| 164 |
} |
| 165 |
|
| 166 |
static $available_translations = array(); |
| 167 |
|
| 168 |
if ( empty( $available_translations ) ) { |
| 169 |
$available_translations = wp_get_available_translations(); |
| 170 |
} |
| 171 |
|
| 172 |
if ( isset( $available_translations[$locale]['native_name'] ) ) { |
| 173 |
return $available_translations[$locale]['native_name']; |
| 174 |
} |
| 175 |
|
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
function bogo_get_default_locale() { |
| 180 |
static $locale = ''; |
| 181 |
|
| 182 |
if ( ! empty( $locale ) ) { |
| 183 |
return $locale; |
| 184 |
} |
| 185 |
|
| 186 |
if ( defined( 'WPLANG' ) ) { |
| 187 |
$locale = WPLANG; |
| 188 |
} |
| 189 |
|
| 190 |
if ( is_multisite() ) { |
| 191 |
if ( wp_installing() |
| 192 |
|| ( false === $ms_locale = get_option( 'WPLANG' ) ) ) { |
| 193 |
$ms_locale = get_site_option( 'WPLANG' ); |
| 194 |
} |
| 195 |
|
| 196 |
if ( $ms_locale !== false ) { |
| 197 |
$locale = $ms_locale; |
| 198 |
} |
| 199 |
} else { |
| 200 |
$db_locale = get_option( 'WPLANG' ); |
| 201 |
|
| 202 |
if ( $db_locale !== false ) { |
| 203 |
$locale = $db_locale; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
if ( ! empty( $locale ) ) { |
| 208 |
return $locale; |
| 209 |
} |
| 210 |
|
| 211 |
return 'en_US'; |
| 212 |
} |
| 213 |
|
| 214 |
function bogo_is_default_locale( $locale ) { |
| 215 |
$default_locale = bogo_get_default_locale(); |
| 216 |
|
| 217 |
return ! empty( $locale ) && $locale == bogo_get_default_locale(); |
| 218 |
} |
| 219 |
|
| 220 |
function bogo_is_enus_deactivated() { |
| 221 |
if ( bogo_is_default_locale( 'en_US' ) ) { |
| 222 |
return false; |
| 223 |
} |
| 224 |
|
| 225 |
return (bool) bogo_get_prop( 'enus_deactivated' ); |
| 226 |
} |
| 227 |
|
| 228 |
function bogo_available_locales( $args = '' ) { |
| 229 |
$defaults = array( |
| 230 |
'exclude' => array(), |
| 231 |
'exclude_enus_if_inactive' => false, |
| 232 |
'current_user_can_access' => false ); |
| 233 |
|
| 234 |
$args = wp_parse_args( $args, $defaults ); |
| 235 |
|
| 236 |
static $installed_locales = array(); |
| 237 |
|
| 238 |
if ( empty( $installed_locales ) ) { |
| 239 |
$installed_locales = get_available_languages(); |
| 240 |
$installed_locales[] = bogo_get_default_locale(); |
| 241 |
$installed_locales[] = 'en_US'; |
| 242 |
} |
| 243 |
|
| 244 |
$available_locales = $installed_locales; |
| 245 |
|
| 246 |
if ( $args['current_user_can_access'] |
| 247 |
&& ! current_user_can( 'bogo_access_all_locales' ) ) { |
| 248 |
$user_accessible_locales = bogo_get_user_accessible_locales( |
| 249 |
get_current_user_id() ); |
| 250 |
|
| 251 |
$available_locales = array_intersect( $available_locales, |
| 252 |
(array) $user_accessible_locales ); |
| 253 |
} |
| 254 |
|
| 255 |
if ( ! empty( $args['exclude'] ) ) { |
| 256 |
$available_locales = array_diff( $available_locales, |
| 257 |
(array) $args['exclude'] ); |
| 258 |
} |
| 259 |
|
| 260 |
if ( $args['exclude_enus_if_inactive'] |
| 261 |
&& bogo_is_enus_deactivated() ) { |
| 262 |
$available_locales = array_diff( $available_locales, |
| 263 |
array( 'en_US' ) ); |
| 264 |
} |
| 265 |
|
| 266 |
return array_unique( array_filter( $available_locales ) ); |
| 267 |
} |
| 268 |
|
| 269 |
function bogo_available_languages( $args = '' ) { |
| 270 |
$defaults = array( |
| 271 |
'exclude' => array(), |
| 272 |
'orderby' => 'key', |
| 273 |
'order' => 'ASC', |
| 274 |
'current_user_can_access' => false ); |
| 275 |
|
| 276 |
$args = wp_parse_args( $args, $defaults ); |
| 277 |
|
| 278 |
$langs = array(); |
| 279 |
|
| 280 |
$available_locales = bogo_available_locales( $args ); |
| 281 |
|
| 282 |
foreach ( $available_locales as $locale ) { |
| 283 |
$lang = bogo_get_language( $locale ); |
| 284 |
$langs[$locale] = empty( $lang ) ? "[$locale]" : $lang; |
| 285 |
} |
| 286 |
|
| 287 |
if ( 'value' == $args['orderby'] ) { |
| 288 |
natcasesort( $langs ); |
| 289 |
|
| 290 |
if ( 'DESC' == $args['order'] ) { |
| 291 |
$langs = array_reverse( $langs ); |
| 292 |
} |
| 293 |
} else { |
| 294 |
if ( 'DESC' == $args['order'] ) { |
| 295 |
krsort( $langs ); |
| 296 |
} else { |
| 297 |
ksort( $langs ); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
$langs = apply_filters( 'bogo_available_languages', $langs, $args ); |
| 302 |
|
| 303 |
return $langs; |
| 304 |
} |
| 305 |
|
| 306 |
function bogo_is_available_locale( $locale ) { |
| 307 |
if ( empty( $locale ) ) { |
| 308 |
return false; |
| 309 |
} |
| 310 |
|
| 311 |
static $available_locales = array(); |
| 312 |
|
| 313 |
if ( empty( $available_locales ) ) { |
| 314 |
$available_locales = bogo_available_locales(); |
| 315 |
} |
| 316 |
|
| 317 |
return in_array( $locale, $available_locales ); |
| 318 |
} |
| 319 |
|
| 320 |
function bogo_filter_locales( $locales, $filter = 'available' ) { |
| 321 |
return array_intersect( $locales, bogo_available_locales() ); |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* @see http://www.ietf.org/rfc/bcp/bcp47.txt |
| 326 |
*/ |
| 327 |
function bogo_language_tag( $locale ) { |
| 328 |
$tag = preg_replace( '/[^0-9a-zA-Z]+/', '-', $locale ); |
| 329 |
$tag = trim( $tag, '-' ); |
| 330 |
|
| 331 |
if ( '-formal' == substr( $tag, -7 ) ) { |
| 332 |
$tag = substr( $tag, 0, -7 ); |
| 333 |
} |
| 334 |
|
| 335 |
return apply_filters( 'bogo_language_tag', $tag, $locale ); |
| 336 |
} |
| 337 |
|
| 338 |
function bogo_lang_slug( $locale ) { |
| 339 |
$tag = bogo_language_tag( $locale ); |
| 340 |
$slug = $tag; |
| 341 |
|
| 342 |
if ( false !== $pos = strpos( $tag, '-' ) ) |
| 343 |
$slug = substr( $tag, 0, $pos ); |
| 344 |
|
| 345 |
$variations = preg_grep( '/^' . $slug . '/', bogo_available_locales() ); |
| 346 |
|
| 347 |
if ( 1 < count( $variations ) ) |
| 348 |
$slug = $tag; |
| 349 |
|
| 350 |
return apply_filters( 'bogo_lang_slug', $slug, $locale ); |
| 351 |
} |
| 352 |
|
| 353 |
function bogo_get_lang_regex() { |
| 354 |
$langs = array_map( 'bogo_lang_slug', bogo_available_locales() ); |
| 355 |
$langs = array_filter( $langs ); |
| 356 |
|
| 357 |
if ( empty( $langs ) ) |
| 358 |
return ''; |
| 359 |
|
| 360 |
return '(' . implode( '|', $langs ) . ')'; |
| 361 |
} |
| 362 |
|
| 363 |
function bogo_get_closest_locale( $var ) { |
| 364 |
$var = strtolower( $var ); |
| 365 |
|
| 366 |
if ( ! preg_match( '/^([a-z]{2})(?:[_-]([a-z]{2}))?/', $var, $matches ) ) |
| 367 |
return false; |
| 368 |
|
| 369 |
$language_code = $matches[1]; |
| 370 |
$region_code = isset( $matches[2] ) ? $matches[2] : ''; |
| 371 |
|
| 372 |
$locales = bogo_available_locales(); |
| 373 |
|
| 374 |
if ( $region_code ) { |
| 375 |
$locale = $language_code . '_' . strtoupper( $region_code ); |
| 376 |
|
| 377 |
if ( false !== array_search( $locale, $locales ) ) |
| 378 |
return $locale; |
| 379 |
} |
| 380 |
|
| 381 |
$locale = $language_code; |
| 382 |
|
| 383 |
if ( false !== array_search( $locale, $locales ) ) |
| 384 |
return $locale; |
| 385 |
|
| 386 |
if ( $matches = preg_grep( "/^{$locale}_/", $locales ) ) |
| 387 |
return array_shift( $matches ); |
| 388 |
|
| 389 |
return false; |
| 390 |
} |
| 391 |
|
| 392 |
function bogo_http_accept_languages() { |
| 393 |
if ( ! isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) |
| 394 |
return false; |
| 395 |
|
| 396 |
$languages = array(); |
| 397 |
|
| 398 |
foreach ( explode( ',', $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) as $lang ) { |
| 399 |
$lang = trim( strtolower( $lang ) ); |
| 400 |
|
| 401 |
if ( preg_match( '/^([a-z-]+)(?:;q=([0-9.]+))?$/', $lang, $matches ) ) { |
| 402 |
$language_tag = $matches[1]; |
| 403 |
$qvalue = isset( $matches[2] ) ? 0 + $matches[2] : 1; |
| 404 |
|
| 405 |
if ( preg_match( '/^([a-z]{2})(?:-([a-z]{2}))?$/', $language_tag, $matches ) ) { |
| 406 |
$language_tag = $matches[1]; |
| 407 |
|
| 408 |
if ( isset( $matches[2] ) ) |
| 409 |
$language_tag .= '_' . strtoupper( $matches[2] ); |
| 410 |
|
| 411 |
$languages[$language_tag] = $qvalue; |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
natsort( $languages ); |
| 417 |
|
| 418 |
return array_reverse( array_keys( $languages ) ); |
| 419 |
} |
| 420 |
|
| 421 |
function bogo_url( $url = null, $lang = null ) { |
| 422 |
if ( ! $lang ) |
| 423 |
$lang = get_locale(); |
| 424 |
|
| 425 |
$args = array( |
| 426 |
'using_permalinks' => (bool) get_option( 'permalink_structure' ) ); |
| 427 |
|
| 428 |
return bogo_get_url_with_lang( $url, $lang, $args ); |
| 429 |
} |
| 430 |
|
| 431 |
function bogo_get_url_with_lang( $url = null, $lang = null, $args = '' ) { |
| 432 |
$defaults = array( |
| 433 |
'using_permalinks' => true ); |
| 434 |
|
| 435 |
$args = wp_parse_args( $args, $defaults ); |
| 436 |
|
| 437 |
if ( ! $url ) { |
| 438 |
if ( ! $url = redirect_canonical( $url, false ) ) { |
| 439 |
$url = is_ssl() ? 'https://' : 'http://'; |
| 440 |
$url .= $_SERVER['HTTP_HOST']; |
| 441 |
$url .= $_SERVER['REQUEST_URI']; |
| 442 |
} |
| 443 |
|
| 444 |
if ( $frag = strstr( $url, '#' ) ) { |
| 445 |
$url = substr( $url, 0, - strlen( $frag ) ); |
| 446 |
} |
| 447 |
|
| 448 |
if ( $query = @parse_url( $url, PHP_URL_QUERY ) ) { |
| 449 |
parse_str( $query, $query_vars ); |
| 450 |
|
| 451 |
foreach ( array_keys( $query_vars ) as $qv ) { |
| 452 |
if ( ! get_query_var( $qv ) ) { |
| 453 |
$url = remove_query_arg( $qv, $url ); |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
$default_locale = bogo_get_default_locale(); |
| 460 |
|
| 461 |
if ( ! $lang ) { |
| 462 |
$lang = $default_locale; |
| 463 |
} |
| 464 |
|
| 465 |
$use_implicit_lang = apply_filters( 'bogo_use_implicit_lang', true ); |
| 466 |
|
| 467 |
$lang_slug = ( $use_implicit_lang && $lang == $default_locale ) |
| 468 |
? '' |
| 469 |
: bogo_lang_slug( $lang ); |
| 470 |
|
| 471 |
$home = set_url_scheme( get_option( 'home' ) ); |
| 472 |
$home = trailingslashit( $home ); |
| 473 |
|
| 474 |
$url = remove_query_arg( 'lang', $url ); |
| 475 |
|
| 476 |
if ( ! $args['using_permalinks'] ) { |
| 477 |
if ( $lang_slug ) { |
| 478 |
$url = add_query_arg( array( 'lang' => $lang_slug ), $url ); |
| 479 |
} |
| 480 |
|
| 481 |
return $url; |
| 482 |
} |
| 483 |
|
| 484 |
$available_languages = array_map( 'bogo_lang_slug', bogo_available_locales() ); |
| 485 |
|
| 486 |
$tail_slashed = ( '/' == substr( $url, -1 ) ); |
| 487 |
|
| 488 |
$url = preg_replace( |
| 489 |
'#^' . preg_quote( $home ) . '((' . implode( '|', $available_languages ) . ')/)?#', |
| 490 |
$home . ( $lang_slug ? trailingslashit( $lang_slug ) : '' ), |
| 491 |
trailingslashit( $url ) ); |
| 492 |
|
| 493 |
if ( ! $tail_slashed ) { |
| 494 |
$url = untrailingslashit( $url ); |
| 495 |
} |
| 496 |
|
| 497 |
return $url; |
| 498 |
} |
| 499 |
|
| 500 |
function bogo_get_lang_from_url( $url = '' ) { |
| 501 |
if ( ! $url ) { |
| 502 |
$url = is_ssl() ? 'https://' : 'http://'; |
| 503 |
$url .= $_SERVER['HTTP_HOST']; |
| 504 |
$url .= $_SERVER['REQUEST_URI']; |
| 505 |
} |
| 506 |
|
| 507 |
if ( $frag = strstr( $url, '#' ) ) |
| 508 |
$url = substr( $url, 0, - strlen( $frag ) ); |
| 509 |
|
| 510 |
$home = set_url_scheme( get_option( 'home' ) ); |
| 511 |
$home = trailingslashit( $home ); |
| 512 |
|
| 513 |
$available_languages = array_map( 'bogo_lang_slug', bogo_available_locales() ); |
| 514 |
|
| 515 |
$regex = '#^' . preg_quote( $home ) . '(' . implode( '|', $available_languages ) . ')/#'; |
| 516 |
|
| 517 |
if ( preg_match( $regex, trailingslashit( $url ), $matches ) ) |
| 518 |
return $matches[1]; |
| 519 |
|
| 520 |
if ( $query = @parse_url( $url, PHP_URL_QUERY ) ) { |
| 521 |
parse_str( $query, $query_vars ); |
| 522 |
|
| 523 |
if ( isset( $query_vars['lang'] ) |
| 524 |
&& in_array( $query_vars['lang'], $available_languages ) ) |
| 525 |
return $query_vars['lang']; |
| 526 |
} |
| 527 |
|
| 528 |
return false; |
| 529 |
} |
| 530 |
|
| 531 |
function bogo_get_prop( $prop ) { |
| 532 |
$option = get_option( 'bogo' ); |
| 533 |
|
| 534 |
if ( ! is_array( $option ) ) { |
| 535 |
$option = array(); |
| 536 |
} |
| 537 |
|
| 538 |
return isset( $option[$prop] ) ? $option[$prop] : ''; |
| 539 |
} |
| 540 |
|
| 541 |
function bogo_set_prop( $prop, $value ) { |
| 542 |
$option = get_option( 'bogo' ); |
| 543 |
|
| 544 |
if ( ! is_array( $option ) ) { |
| 545 |
$option = array(); |
| 546 |
} |
| 547 |
|
| 548 |
$option[$prop] = $value; |
| 549 |
|
| 550 |
update_option( 'bogo', $option ); |
| 551 |
} |
| 552 |
|
| 553 |
function bogo_delete_prop( $prop ) { |
| 554 |
$option = get_option( 'bogo' ); |
| 555 |
|
| 556 |
if ( ! is_array( $option ) ) { |
| 557 |
$option = array(); |
| 558 |
} |
| 559 |
|
| 560 |
if ( isset( $option[$prop] ) ) { |
| 561 |
unset( $option[$prop] ); |
| 562 |
} |
| 563 |
|
| 564 |
update_option( 'bogo', $option ); |
| 565 |
} |
| 566 |
|
| 567 |
function bogo_plugin_url( $path = '' ) { |
| 568 |
$url = plugins_url( $path, BOGO_PLUGIN ); |
| 569 |
|
| 570 |
if ( is_ssl() && 'http:' == substr( $url, 0, 5 ) ) { |
| 571 |
$url = 'https:' . substr( $url, 5 ); |
| 572 |
} |
| 573 |
|
| 574 |
return $url; |
| 575 |
} |
| 576 |
|