8
8
9
9
class TranslatePress implements Subscriber_Interface {
10
10
/**
11
+ * Translate Press Instance
12
+ *
11
13
* @var TRP_Translate_Press|null
12
14
*/
13
15
protected $ translatepress ;
14
16
15
17
/**
18
+ * URL Converter component from TranslatePress
19
+ *
16
20
* @var mixed|\TRP_Language_Switcher|\TRP_Languages|\TRP_Settings|\TRP_Url_Converter|null
17
21
*/
18
22
protected $ url_converter ;
19
23
24
+ /**
25
+ * Constructor.
26
+ */
20
27
public function __construct () {
28
+ if ( ! class_exists ( 'TRP_Translate_Press ' ) ) {
29
+ return ;
30
+ }
31
+
21
32
$ this ->translatepress = TRP_Translate_Press::get_trp_instance ();
22
- $ this ->url_converter = $ this ->translatepress ->get_component ( 'url_converter ' );
33
+ $ this ->url_converter = $ this ->translatepress ->get_component ( 'url_converter ' );
23
34
}
24
35
/**
25
36
* Returns an array of events that this subscriber wants to listen to.
@@ -54,9 +65,10 @@ public static function get_subscribed_events() {
54
65
* @return string
55
66
*/
56
67
public function detect_homepage ( $ home_url , $ url ) {
68
+ add_filter ( 'trp_add_language_to_home_url_check_for_admin ' , '__return_false ' );
57
69
$ language = $ this ->url_converter ->get_lang_from_url_string ( $ url );
58
70
$ url_language = $ this ->url_converter ->get_url_for_language ( $ language , home_url () );
59
-
71
+ remove_filter ( ' trp_add_language_to_home_url_check_for_admin ' , ' __return_false ' );
60
72
return untrailingslashit ( $ url ) === untrailingslashit ( $ url_language ) ? $ url : $ home_url ;
61
73
}
62
74
@@ -141,10 +153,12 @@ public function get_active_languages_uri( $urls ) {
141
153
142
154
$ languages_to_display = $ trp_settings ['publish-languages ' ];
143
155
$ published_languages = $ languages ->get_language_names ( $ languages_to_display );
156
+ add_filter ( 'trp_add_language_to_home_url_check_for_admin ' , '__return_false ' );
144
157
145
158
foreach ( $ published_languages as $ code => $ name ) {
146
159
$ urls [] = $ this ->url_converter ->get_url_for_language ( $ code , $ home_url );
147
160
}
161
+ remove_filter ( 'trp_add_language_to_home_url_check_for_admin ' , '__return_false ' );
148
162
149
163
return $ urls ;
150
164
}
@@ -188,22 +202,19 @@ public function get_home_url_for_lang( $home_url, $lang ) {
188
202
return $ home_url ;
189
203
}
190
204
191
- $ settings = $ this ->translatepress ->get_component ( 'settings ' );
192
- $ trp_settings = $ settings ->get_settings ();
205
+ $ settings = $ this ->translatepress ->get_component ( 'settings ' );
206
+ $ trp_settings = $ settings ->get_settings ();
193
207
194
208
$ code = '' ;
195
209
196
210
add_filter ( 'trp_add_language_to_home_url_check_for_admin ' , '__return_false ' );
197
-
198
211
foreach ( $ trp_settings ['url-slugs ' ] as $ index => $ slug ) {
199
212
if ( $ lang === $ slug ) {
200
213
$ code = $ index ;
201
214
break ;
202
215
}
203
216
}
204
-
205
217
$ url = $ this ->url_converter ->get_url_for_language ( $ code , $ home_url );
206
-
207
218
remove_filter ( 'trp_add_language_to_home_url_check_for_admin ' , '__return_false ' );
208
219
209
220
return $ url ;
@@ -230,10 +241,12 @@ public function get_translated_post_urls( $urls, $url, $post_type, $regex ) {
230
241
231
242
$ languages_to_display = $ trp_settings ['publish-languages ' ];
232
243
$ published_languages = $ languages ->get_language_names ( $ languages_to_display );
244
+ add_filter ( 'trp_add_language_to_home_url_check_for_admin ' , '__return_false ' );
233
245
234
246
foreach ( $ published_languages as $ code => $ name ) {
235
247
$ urls [] = wp_parse_url ( $ this ->url_converter ->get_url_for_language ( $ code , $ url ), PHP_URL_PATH ) . $ regex ;
236
248
}
249
+ remove_filter ( 'trp_add_language_to_home_url_check_for_admin ' , '__return_false ' );
237
250
238
251
return $ urls ;
239
252
}
@@ -290,11 +303,13 @@ public function clear_post_after_updating_translation( $update_strings, $setting
290
303
$ clear_urls = [];
291
304
$ current_url = remove_query_arg ( 'trp-edit-translation ' , $ url );
292
305
306
+ add_filter ( 'trp_add_language_to_home_url_check_for_admin ' , '__return_false ' );
293
307
foreach ( $ settings ['translation-languages ' ] as $ language ) {
294
308
if ( ! empty ( $ update_strings [ $ language ] ) ) {
295
309
$ clear_urls [] = $ this ->url_converter ->get_url_for_language ( $ language , $ current_url , '' );
296
310
}
297
311
}
312
+ remove_filter ( 'trp_add_language_to_home_url_check_for_admin ' , '__return_false ' );
298
313
299
314
rocket_clean_files ( $ clear_urls );
300
315
}
0 commit comments