PluginProbe
404 Solution / 4.3.0
404 Solution v4.3.0
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / services / FrontendSuggestionLocaleScope.php

FrontendSuggestionLocaleScope.php in 404 Solution 4.3.0, at includes/services/FrontendSuggestionLocaleScope.php

117 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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