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