Skip to content

Commit afef3b6

Browse files
remyperonavmanthos
andauthored
Closes #4190 TranslatePress compatibility enhancements (#6047)
Co-authored-by: Vasilis Manthos <vmanthos@gmail.com>
1 parent b73125b commit afef3b6

32 files changed

+1109
-330
lines changed

composer.json

Lines changed: 217 additions & 217 deletions
Large diffs are not rendered by default.
Lines changed: 277 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace WP_Rocket\ThirdParty\Plugins\I18n;
35

4-
use TRP_Url_Converter;
5-
use TRP_Settings;
6+
use TRP_Translate_Press;
67
use WP_Rocket\Event_Management\Subscriber_Interface;
78

89
class TranslatePress implements Subscriber_Interface {
@@ -13,12 +14,22 @@ class TranslatePress implements Subscriber_Interface {
1314
* @return array
1415
*/
1516
public static function get_subscribed_events() {
16-
if ( ! class_exists( 'TRP_Url_Converter' ) || ! class_exists( 'TRP_Settings' ) ) {
17+
if ( ! class_exists( 'TRP_Translate_Press' ) ) {
1718
return [];
1819
}
1920

2021
return [
21-
'rocket_rucss_is_home_url' => [ 'detect_homepage', 10, 2 ],
22+
'rocket_rucss_is_home_url' => [ 'detect_homepage', 10, 2 ],
23+
'rocket_has_i18n' => 'is_translatepress',
24+
'rocket_i18n_admin_bar_menu' => 'add_langs_to_admin_bar',
25+
'rocket_i18n_current_language' => 'set_current_language',
26+
'rocket_get_i18n_uri' => 'get_active_languages_uri',
27+
'rocket_get_i18n_code' => 'get_active_languages_codes',
28+
'rocket_i18n_subdomains' => 'get_active_languages_uri',
29+
'rocket_i18n_home_url' => [ 'get_home_url_for_lang', 10, 2 ],
30+
'rocket_i18n_translated_post_urls' => [ 'get_translated_post_urls', 10, 4 ],
31+
'post_updated' => 'clear_post_languages',
32+
'trp_save_editor_translations_regular_strings' => [ 'clear_post_after_updating_translation', 10, 2 ],
2233
];
2334
}
2435

@@ -30,12 +41,270 @@ public static function get_subscribed_events() {
3041
* @return string
3142
*/
3243
public function detect_homepage( $home_url, $url ) {
44+
$translatepress = TRP_Translate_Press::get_trp_instance();
45+
$converter = $translatepress->get_component( 'url_converter' );
3346

34-
$url_converter = new TRP_Url_Converter( ( new TRP_Settings() )->get_settings() );
35-
$language = $url_converter->get_lang_from_url_string( $url );
36-
37-
$url_language = $url_converter->get_url_for_language( $language, home_url() );
47+
$language = $converter->get_lang_from_url_string( $url );
48+
$url_language = $converter->get_url_for_language( $language, home_url() );
3849

3950
return untrailingslashit( $url ) === untrailingslashit( $url_language ) ? $url : $home_url;
4051
}
52+
53+
/**
54+
* Adds TranslatePress as identifier for i18n detection
55+
*
56+
* @param string|bool $identifier An identifier value, false otherwise.
57+
*
58+
* @return string|bool
59+
*/
60+
public function is_translatepress( $identifier ) {
61+
if (
62+
function_exists( 'trp_get_languages' )
63+
&&
64+
! empty( trp_get_languages( 'nodefault' ) )
65+
) {
66+
return 'translatepress';
67+
}
68+
69+
return $identifier;
70+
}
71+
72+
/**
73+
* Adds languages to the admin bar menu
74+
*
75+
* @param array $langlinks Array of languages.
76+
*
77+
* @return array
78+
*/
79+
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' );
85+
$trp_settings = $settings->get_settings();
86+
87+
$languages_to_display = $trp_settings['publish-languages'];
88+
$published_languages = $languages->get_language_names( $languages_to_display );
89+
90+
foreach ( $published_languages as $code => $name ) {
91+
$langlinks[ $code ] = [
92+
'code' => $trp_settings['url-slugs'][ $code ],
93+
'flag' => $language_switcher->add_flag( $code, $name ),
94+
'anchor' => $name,
95+
];
96+
}
97+
98+
return $langlinks;
99+
}
100+
101+
/**
102+
* Sets the current language value
103+
*
104+
* @param string|bool $current_language Current language.
105+
*
106+
* @return string|bool
107+
*/
108+
public function set_current_language( $current_language ) {
109+
if ( empty( $GLOBALS['TRP_LANGUAGE'] ) ) {
110+
return $current_language;
111+
}
112+
113+
return $GLOBALS['TRP_LANGUAGE'];
114+
}
115+
116+
/**
117+
* Gets URLs for active languages
118+
*
119+
* @param array $urls Array of active languages URI.
120+
*
121+
* @return array
122+
*/
123+
public function get_active_languages_uri( $urls ) {
124+
if ( ! is_array( $urls ) ) {
125+
$urls = (array) $urls;
126+
}
127+
128+
$home_url = home_url();
129+
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' );
135+
$trp_settings = $settings->get_settings();
136+
137+
$languages_to_display = $trp_settings['publish-languages'];
138+
$published_languages = $languages->get_language_names( $languages_to_display );
139+
140+
foreach ( $published_languages as $code => $name ) {
141+
$urls[] = $converter->get_url_for_language( $code, $home_url );
142+
}
143+
144+
return $urls;
145+
}
146+
147+
/**
148+
* Gets the active languages slugs
149+
*
150+
* @param Array $codes Array of languages codes.
151+
*
152+
* @return array
153+
*/
154+
public function get_active_languages_codes( $codes ) {
155+
if ( ! is_array( $codes ) ) {
156+
$codes = (array) $codes;
157+
}
158+
159+
$translatepress = TRP_Translate_Press::get_trp_instance();
160+
161+
$settings = $translatepress->get_component( 'settings' );
162+
$languages = $translatepress->get_component( 'languages' );
163+
$trp_settings = $settings->get_settings();
164+
165+
$languages_to_display = $trp_settings['publish-languages'];
166+
$published_languages = $languages->get_language_names( $languages_to_display );
167+
168+
foreach ( $published_languages as $code => $name ) {
169+
$codes[] = $trp_settings['url-slugs'][ $code ];
170+
}
171+
172+
return $codes;
173+
}
174+
175+
/**
176+
* Gets home URL in given language
177+
*
178+
* @param string $home_url Home URL.
179+
* @param string $lang Language code.
180+
*
181+
* @return string
182+
*/
183+
public function get_home_url_for_lang( $home_url, $lang ) {
184+
if ( empty( $lang ) ) {
185+
return $home_url;
186+
}
187+
188+
$translatepress = TRP_Translate_Press::get_trp_instance();
189+
$converter = $translatepress->get_component( 'url_converter' );
190+
$settings = $translatepress->get_component( 'settings' );
191+
$trp_settings = $settings->get_settings();
192+
193+
$code = '';
194+
195+
add_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );
196+
197+
foreach ( $trp_settings['url-slugs'] as $index => $slug ) {
198+
if ( $lang === $slug ) {
199+
$code = $index;
200+
break;
201+
}
202+
}
203+
204+
$url = $converter->get_url_for_language( $code, $home_url );
205+
206+
remove_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );
207+
208+
return $url;
209+
}
210+
211+
/**
212+
* Gets all translations URLs for a post
213+
*
214+
* @param array $urls Array of translated URLs.
215+
* @param string $url URL to use.
216+
* @param string $post_type Post type.
217+
* @param string $regex Pattern to include at the end.
218+
*
219+
* @return array
220+
*/
221+
public function get_translated_post_urls( $urls, $url, $post_type, $regex ) {
222+
if ( ! is_array( $urls ) ) {
223+
$urls = (array) $urls;
224+
}
225+
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' );
231+
$trp_settings = $settings->get_settings();
232+
233+
$languages_to_display = $trp_settings['publish-languages'];
234+
$published_languages = $languages->get_language_names( $languages_to_display );
235+
236+
foreach ( $published_languages as $code => $name ) {
237+
$urls[] = wp_parse_url( $converter->get_url_for_language( $code, $url ), PHP_URL_PATH ) . $regex;
238+
}
239+
240+
return $urls;
241+
}
242+
243+
/**
244+
* Clear all languages of a specific post
245+
*
246+
* @param int $post_id Post ID.
247+
*
248+
* @return void
249+
*/
250+
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' );
255+
$trp_settings = $settings->get_settings();
256+
257+
add_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );
258+
259+
$clear_urls = [];
260+
261+
$default_permalink = get_permalink( $post_id );
262+
263+
foreach ( $trp_settings['translation-languages'] as $language ) {
264+
if ( $language === $trp_settings['default-language'] ) {
265+
continue;
266+
}
267+
268+
$clear_urls[] = $converter->get_url_for_language( $language, $default_permalink, '' );
269+
}
270+
271+
remove_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );
272+
273+
if ( empty( $clear_urls ) ) {
274+
return;
275+
}
276+
277+
rocket_clean_files( $clear_urls );
278+
}
279+
280+
/**
281+
* Clear the post cache when the translation is updated
282+
*
283+
* @param array $update_strings Array of updated strings.
284+
* @param array $settings Array of settings.
285+
*
286+
* @return void
287+
*/
288+
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+
293+
if ( empty( $_POST['url'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
294+
return;
295+
}
296+
297+
$url = esc_url_raw( wp_unslash( $_POST['url'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
298+
299+
$clear_urls = [];
300+
$current_url = remove_query_arg( 'trp-edit-translation', $url );
301+
302+
foreach ( $settings['translation-languages'] as $language ) {
303+
if ( ! empty( $update_strings[ $language ] ) ) {
304+
$clear_urls[] = $converter->get_url_for_language( $language, $current_url, '' );
305+
}
306+
}
307+
308+
rocket_clean_files( $clear_urls );
309+
}
41310
}

inc/common/admin-bar.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ function rocket_admin_bar( $wp_admin_bar ) {
8585
]
8686
);
8787

88+
$langlinks_default = [];
89+
8890
// Add submenu for each active langs.
8991
switch ( $i18n_plugin ) {
9092
case 'wpml':
@@ -100,7 +102,16 @@ function rocket_admin_bar( $wp_admin_bar ) {
100102
$langlinks = get_rocket_polylang_langs_for_admin_bar();
101103
break;
102104
default:
103-
$langlinks = [];
105+
/**
106+
* Filters the value of the lang links menu
107+
*
108+
* @param array $langlinks Array of languages.
109+
*/
110+
$langlinks = apply_filters( 'rocket_i18n_admin_bar_menu', [] );
111+
112+
if ( ! is_array( $langlinks ) ) {
113+
$langlinks = $langlinks_default;
114+
}
104115
}
105116

106117
if ( $langlinks ) {

inc/common/purge.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ function rocket_clean_post( $post_id, $post = null ) {
209209

210210
// Get the post language.
211211
$i18n_plugin = rocket_has_i18n();
212-
$lang = false;
212+
$lang = '';
213213

214214
if ( 'wpml' === $i18n_plugin && ! rocket_is_plugin_active( 'woocommerce-multilingual/wpml-woocommerce.php' ) ) {
215215
// WPML.

0 commit comments

Comments
 (0)