Skip to content

Commit 64f2b5b

Browse files
committed
refactor
1 parent 89fcfb1 commit 64f2b5b

File tree

1 file changed

+31
-40
lines changed

1 file changed

+31
-40
lines changed

inc/ThirdParty/Plugins/I18n/TranslatePress.php

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,20 @@
77
use WP_Rocket\Event_Management\Subscriber_Interface;
88

99
class TranslatePress implements Subscriber_Interface {
10+
/**
11+
* @var TRP_Translate_Press|null
12+
*/
13+
protected $translatepress;
1014

15+
/**
16+
* @var mixed|\TRP_Language_Switcher|\TRP_Languages|\TRP_Settings|\TRP_Url_Converter|null
17+
*/
18+
protected $url_converter;
19+
20+
public function __construct() {
21+
$this->translatepress = TRP_Translate_Press::get_trp_instance();
22+
$this->url_converter = $this->translatepress->get_component( 'url_converter' );
23+
}
1124
/**
1225
* Returns an array of events that this subscriber wants to listen to.
1326
*
@@ -41,11 +54,8 @@ public static function get_subscribed_events() {
4154
* @return string
4255
*/
4356
public function detect_homepage( $home_url, $url ) {
44-
$translatepress = TRP_Translate_Press::get_trp_instance();
45-
$converter = $translatepress->get_component( 'url_converter' );
46-
47-
$language = $converter->get_lang_from_url_string( $url );
48-
$url_language = $converter->get_url_for_language( $language, home_url() );
57+
$language = $this->url_converter->get_lang_from_url_string( $url );
58+
$url_language = $this->url_converter->get_url_for_language( $language, home_url() );
4959

5060
return untrailingslashit( $url ) === untrailingslashit( $url_language ) ? $url : $home_url;
5161
}
@@ -77,11 +87,9 @@ function_exists( 'trp_get_languages' )
7787
* @return array
7888
*/
7989
public function add_langs_to_admin_bar( $langlinks ) {
80-
$translatepress = TRP_Translate_Press::get_trp_instance();
81-
82-
$language_switcher = $translatepress->get_component( 'language_switcher' );
83-
$settings = $translatepress->get_component( 'settings' );
84-
$languages = $translatepress->get_component( 'languages' );
90+
$language_switcher = $this->translatepress->get_component( 'language_switcher' );
91+
$settings = $this->translatepress->get_component( 'settings' );
92+
$languages = $this->translatepress->get_component( 'languages' );
8593
$trp_settings = $settings->get_settings();
8694

8795
$languages_to_display = $trp_settings['publish-languages'];
@@ -127,18 +135,15 @@ public function get_active_languages_uri( $urls ) {
127135

128136
$home_url = home_url();
129137

130-
$translatepress = TRP_Translate_Press::get_trp_instance();
131-
132-
$settings = $translatepress->get_component( 'settings' );
133-
$languages = $translatepress->get_component( 'languages' );
134-
$converter = $translatepress->get_component( 'url_converter' );
138+
$settings = $this->translatepress->get_component( 'settings' );
139+
$languages = $this->translatepress->get_component( 'languages' );
135140
$trp_settings = $settings->get_settings();
136141

137142
$languages_to_display = $trp_settings['publish-languages'];
138143
$published_languages = $languages->get_language_names( $languages_to_display );
139144

140145
foreach ( $published_languages as $code => $name ) {
141-
$urls[] = $converter->get_url_for_language( $code, $home_url );
146+
$urls[] = $this->url_converter->get_url_for_language( $code, $home_url );
142147
}
143148

144149
return $urls;
@@ -156,10 +161,8 @@ public function get_active_languages_codes( $codes ) {
156161
$codes = (array) $codes;
157162
}
158163

159-
$translatepress = TRP_Translate_Press::get_trp_instance();
160-
161-
$settings = $translatepress->get_component( 'settings' );
162-
$languages = $translatepress->get_component( 'languages' );
164+
$settings = $this->translatepress->get_component( 'settings' );
165+
$languages = $this->translatepress->get_component( 'languages' );
163166
$trp_settings = $settings->get_settings();
164167

165168
$languages_to_display = $trp_settings['publish-languages'];
@@ -185,9 +188,7 @@ public function get_home_url_for_lang( $home_url, $lang ) {
185188
return $home_url;
186189
}
187190

188-
$translatepress = TRP_Translate_Press::get_trp_instance();
189-
$converter = $translatepress->get_component( 'url_converter' );
190-
$settings = $translatepress->get_component( 'settings' );
191+
$settings = $this->translatepress->get_component( 'settings' );
191192
$trp_settings = $settings->get_settings();
192193

193194
$code = '';
@@ -201,7 +202,7 @@ public function get_home_url_for_lang( $home_url, $lang ) {
201202
}
202203
}
203204

204-
$url = $converter->get_url_for_language( $code, $home_url );
205+
$url = $this->url_converter->get_url_for_language( $code, $home_url );
205206

206207
remove_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );
207208

@@ -223,18 +224,15 @@ public function get_translated_post_urls( $urls, $url, $post_type, $regex ) {
223224
$urls = (array) $urls;
224225
}
225226

226-
$translatepress = TRP_Translate_Press::get_trp_instance();
227-
228-
$settings = $translatepress->get_component( 'settings' );
229-
$languages = $translatepress->get_component( 'languages' );
230-
$converter = $translatepress->get_component( 'url_converter' );
227+
$settings = $this->translatepress->get_component( 'settings' );
228+
$languages = $this->translatepress->get_component( 'languages' );
231229
$trp_settings = $settings->get_settings();
232230

233231
$languages_to_display = $trp_settings['publish-languages'];
234232
$published_languages = $languages->get_language_names( $languages_to_display );
235233

236234
foreach ( $published_languages as $code => $name ) {
237-
$urls[] = wp_parse_url( $converter->get_url_for_language( $code, $url ), PHP_URL_PATH ) . $regex;
235+
$urls[] = wp_parse_url( $this->url_converter->get_url_for_language( $code, $url ), PHP_URL_PATH ) . $regex;
238236
}
239237

240238
return $urls;
@@ -248,10 +246,7 @@ public function get_translated_post_urls( $urls, $url, $post_type, $regex ) {
248246
* @return void
249247
*/
250248
public function clear_post_languages( $post_id ) {
251-
$translatepress = TRP_Translate_Press::get_trp_instance();
252-
253-
$converter = $translatepress->get_component( 'url_converter' );
254-
$settings = $translatepress->get_component( 'settings' );
249+
$settings = $this->translatepress->get_component( 'settings' );
255250
$trp_settings = $settings->get_settings();
256251

257252
add_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );
@@ -265,7 +260,7 @@ public function clear_post_languages( $post_id ) {
265260
continue;
266261
}
267262

268-
$clear_urls[] = $converter->get_url_for_language( $language, $default_permalink, '' );
263+
$clear_urls[] = $this->url_converter->get_url_for_language( $language, $default_permalink, '' );
269264
}
270265

271266
remove_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );
@@ -286,10 +281,6 @@ public function clear_post_languages( $post_id ) {
286281
* @return void
287282
*/
288283
public function clear_post_after_updating_translation( $update_strings, $settings ) {
289-
$translatepress = TRP_Translate_Press::get_trp_instance();
290-
291-
$converter = $translatepress->get_component( 'url_converter' );
292-
293284
if ( empty( $_POST['url'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
294285
return;
295286
}
@@ -301,7 +292,7 @@ public function clear_post_after_updating_translation( $update_strings, $setting
301292

302293
foreach ( $settings['translation-languages'] as $language ) {
303294
if ( ! empty( $update_strings[ $language ] ) ) {
304-
$clear_urls[] = $converter->get_url_for_language( $language, $current_url, '' );
295+
$clear_urls[] = $this->url_converter->get_url_for_language( $language, $current_url, '' );
305296
}
306297
}
307298

0 commit comments

Comments
 (0)