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