class-wpml-url-converter.php
6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* Class WPML_URL_Converter
*
* @package wpml-core
* @subpackage url-handling
*/
use WPML\SuperGlobals\Server;
use WPML\UrlHandling\WPLoginUrlConverter;
class WPML_URL_Converter {
/**
* @var IWPML_URL_Converter_Strategy
*/
private $strategy;
/**
* @var string
*/
protected $default_language;
/**
* @var string[]
*/
protected $active_languages;
/**
* @var WPML_URL_Converter_Url_Helper
*/
protected $home_url_helper;
/**
* @var WPML_URL_Converter_Lang_Param_Helper
*/
protected $lang_param;
/**
* @var WPML_Slash_Management
*/
protected $slash_helper;
/**
* @var WPML_Resolve_Object_Url_Helper
*/
protected $object_url_helper;
/**
* @param IWPML_URL_Converter_Strategy $strategy
* @param IWPML_Resolve_Object_Url $object_url_helper
* @param string $default_language
* @param array<string> $active_languages
*/
public function __construct(
IWPML_URL_Converter_Strategy $strategy,
IWPML_Resolve_Object_Url $object_url_helper,
$default_language,
$active_languages
) {
$this->strategy = $strategy;
$this->object_url_helper = $object_url_helper;
$this->default_language = $default_language;
$this->active_languages = $active_languages;
$this->lang_param = new WPML_URL_Converter_Lang_Param_Helper( $active_languages );
$this->slash_helper = new WPML_Slash_Management();
}
/**
* @return IWPML_URL_Converter_Strategy
*/
public function get_strategy() {
return $this->strategy;
}
/**
* @param WPML_URL_Converter_Url_Helper $url_helper
*/
public function set_url_helper( WPML_URL_Converter_Url_Helper $url_helper ) {
$this->home_url_helper = $url_helper;
if ( $this->strategy instanceof WPML_URL_Converter_Abstract_Strategy ) {
$this->strategy->set_url_helper( $url_helper );
}
}
/**
* @return WPML_URL_Converter_Url_Helper
*/
public function get_url_helper() {
if ( ! $this->home_url_helper ) {
$this->home_url_helper = new WPML_URL_Converter_Url_Helper();
}
return $this->home_url_helper;
}
public function get_abs_home() {
return $this->get_url_helper()->get_abs_home();
}
/**
* @param WPML_URL_Converter_Lang_Param_Helper $lang_param_helper
*/
public function set_lang_param_helper( WPML_URL_Converter_Lang_Param_Helper $lang_param_helper ) {
$this->lang_param = $lang_param_helper;
}
/**
* @param WPML_Slash_Management $slash_helper
*/
public function set_slash_helper( WPML_Slash_Management $slash_helper ) {
$this->slash_helper = $slash_helper;
}
public function get_default_site_url() {
return $this->get_url_helper()->get_unfiltered_home_option();
}
/**
* Scope of this function:
* 1. Convert the home URL in the specified language depending on language negotiation:
* 1. Add a language directory
* 2. Change the domain
* 3. Add a language parameter
* 2. If the requested URL is equal to the current URL, the URI will be adapted
* with potential slug translations for:
* - single post slugs
* - taxonomy term slug
*
* WARNING: The URI slugs won't be translated for arbitrary URL (not the current one)
*
* @param string $url
* @param bool $lang_code
*
* @return bool|mixed|string
*/
public function convert_url( $url, $lang_code = false ) {
if ( ! $url ) {
return $url;
}
global $sitepress;
$new_url = false;
if ( ! $lang_code ) {
$lang_code = $sitepress->get_current_language();
}
$language_from_url = $this->get_language_from_url( $url );
if ( $language_from_url === $lang_code || 'all' === $lang_code ) {
$new_url = $url;
} else {
if ( $this->can_resolve_object_url( $url ) ) {
$new_url = $this->object_url_helper->resolve_object_url( $url, $lang_code );
}
if ( false === $new_url ) {
$new_url = $this->strategy->convert_url_string( $url, $lang_code );
}
}
return $new_url;
}
/**
* Takes a URL and returns the language of the document it points at
*
* @param string $url
* @return string
*/
public function get_language_from_url( $url ) {
$http_referer_factory = new WPML_URL_HTTP_Referer_Factory();
$http_referer = $http_referer_factory->create();
$url = $http_referer->get_url( $url );
$language = $this->lang_param->lang_by_param( $url ) ?: $this->get_strategy()->get_lang_from_url_string( $url );
/**
* Filters language code fetched from the current URL and allows to rewrite
* the language to set on front-end
*
* @param string $language language fetched from the current URL
* @param string $url current URL.
*/
$language = apply_filters( 'wpml_get_language_from_url', $language, $url );
return $this->get_strategy()->validate_language( $language, $url );
}
/**
* @param string $url
* @param string $language
*
* @return string
*/
public function get_home_url_relative( $url, $language ) {
return $this->get_strategy()->get_home_url_relative( $url, $language );
}
/**
* @param SitePress $sitepress
*
* @return WPLoginUrlConverter|null
*/
public function get_wp_login_url_converter( $sitepress ) {
return $this->strategy->use_wp_login_url_converter()
? new WPLoginUrlConverter( $sitepress, $this )
: null;
}
/**
* @param string $url
*
* @return bool
*/
private function can_resolve_object_url( $url ) {
$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '';
$server_name = strpos( $request_uri, '/' ) === 0
? untrailingslashit( Server::getServerName() ) : trailingslashit( Server::getServerName() );
$request_url = stripos( get_option( 'siteurl' ), 'https://' ) === 0
? 'https://' . $server_name . $request_uri : 'http://' . $server_name . $request_uri;
$is_request_url = trailingslashit( $request_url ) === trailingslashit( $url );
$is_home_url = trailingslashit( $this->get_url_helper()->get_abs_home() ) === trailingslashit( $url );
$is_home_url_filter = current_filter() === 'home_url';
return $is_request_url && ! $is_home_url && ! $is_home_url_filter;
}
/** @return WPML_URL_Converter */
public static function getGlobalInstance() {
global $wpml_url_converter;
return $wpml_url_converter;
}
}