| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Applies visitor-locale-first rendering for frontend suggestion output. |
| 9 |
*/ |
| 10 |
class ABJ_404_Solution_FrontendSuggestionLocaleScope { |
| 11 |
|
| 12 |
/** |
| 13 |
* Switch to the best frontend suggestion locale when WordPress supports it. |
| 14 |
* |
| 15 |
* @return bool True when a locale switch was accepted. |
| 16 |
*/ |
| 17 |
public function switchToFrontendLocale(): bool { |
| 18 |
if (!function_exists('switch_to_locale') || !function_exists('restore_previous_locale')) { |
| 19 |
return false; |
| 20 |
} |
| 21 |
|
| 22 |
$targetLocale = $this->resolveFrontendSuggestionLocale(); |
| 23 |
if ($targetLocale === '') { |
| 24 |
return false; |
| 25 |
} |
| 26 |
|
| 27 |
return switch_to_locale($targetLocale); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Restore the previous locale when this scope switched it. |
| 32 |
* |
| 33 |
* @param bool $didSwitch |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function restore(bool $didSwitch): void { |
| 37 |
if ($didSwitch && function_exists('restore_previous_locale')) { |
| 38 |
restore_previous_locale(); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Resolve frontend locale with visitor-locale-first behavior. |
| 44 |
* |
| 45 |
* @return string |
| 46 |
*/ |
| 47 |
private function resolveFrontendSuggestionLocale(): string { |
| 48 |
$polyLangLocale = $this->resolvePolylangLocale(); |
| 49 |
if ($polyLangLocale !== '') { |
| 50 |
return $polyLangLocale; |
| 51 |
} |
| 52 |
|
| 53 |
$wpmlLocale = $this->resolveWpmlLocale(); |
| 54 |
if ($wpmlLocale !== '') { |
| 55 |
return $wpmlLocale; |
| 56 |
} |
| 57 |
|
| 58 |
return $this->resolveWordPressLocale(); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @return string |
| 63 |
*/ |
| 64 |
private function resolvePolylangLocale(): string { |
| 65 |
if (function_exists('pll_current_language')) { |
| 66 |
$pllLocale = pll_current_language('locale'); |
| 67 |
if (is_string($pllLocale) && $pllLocale !== '') { |
| 68 |
return $pllLocale; |
| 69 |
} |
| 70 |
} |
| 71 |
return ''; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* @return string |
| 76 |
*/ |
| 77 |
private function resolveWpmlLocale(): string { |
| 78 |
if (function_exists('apply_filters') && function_exists('has_filter') && has_filter('wpml_current_language')) { |
| 79 |
$langCode = apply_filters('wpml_current_language', null); |
| 80 |
if (is_string($langCode) && $langCode !== '' && has_filter('wpml_active_languages')) { |
| 81 |
$active = apply_filters('wpml_active_languages', null, 'skip_missing=0'); |
| 82 |
if (is_array($active) && isset($active[$langCode]) && is_array($active[$langCode])) { |
| 83 |
$entry = $active[$langCode]; |
| 84 |
if (!empty($entry['default_locale']) && is_string($entry['default_locale'])) { |
| 85 |
return $entry['default_locale']; |
| 86 |
} |
| 87 |
if (!empty($entry['locale']) && is_string($entry['locale'])) { |
| 88 |
return $entry['locale']; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
return ''; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @return string |
| 98 |
*/ |
| 99 |
private function resolveWordPressLocale(): string { |
| 100 |
if (function_exists('determine_locale')) { |
| 101 |
$locale = determine_locale(); |
| 102 |
if (is_string($locale) && $locale !== '') { |
| 103 |
return $locale; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if (function_exists('get_locale')) { |
| 108 |
$locale = get_locale(); |
| 109 |
if (is_string($locale) && $locale !== '') { |
| 110 |
return $locale; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return ''; |
| 115 |
} |
| 116 |
} |
| 117 |
|