Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #5640: fix translatePress compatibility #6219

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 51 additions & 45 deletions inc/ThirdParty/Plugins/I18n/TranslatePress.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,31 @@
use WP_Rocket\Event_Management\Subscriber_Interface;

class TranslatePress implements Subscriber_Interface {
/**
* Translate Press Instance
*
* @var TRP_Translate_Press|null
*/
protected $translatepress;

/**
* URL Converter component from TranslatePress
*
* @var mixed|\TRP_Language_Switcher|\TRP_Languages|\TRP_Settings|\TRP_Url_Converter|null
*/
protected $url_converter;

/**
* Constructor.
*/
public function __construct() {
if ( ! class_exists( 'TRP_Translate_Press' ) ) {
return;
}

$this->translatepress = TRP_Translate_Press::get_trp_instance();
$this->url_converter = $this->translatepress->get_component( 'url_converter' );
}
/**
* Returns an array of events that this subscriber wants to listen to.
*
Expand Down Expand Up @@ -41,12 +65,10 @@ public static function get_subscribed_events() {
* @return string
*/
public function detect_homepage( $home_url, $url ) {
$translatepress = TRP_Translate_Press::get_trp_instance();
$converter = $translatepress->get_component( 'url_converter' );

$language = $converter->get_lang_from_url_string( $url );
$url_language = $converter->get_url_for_language( $language, home_url() );

add_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );
$language = $this->url_converter->get_lang_from_url_string( $url );
$url_language = $this->url_converter->get_url_for_language( $language, home_url() );
remove_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );
return untrailingslashit( $url ) === untrailingslashit( $url_language ) ? $url : $home_url;
}

Expand Down Expand Up @@ -77,11 +99,9 @@ function_exists( 'trp_get_languages' )
* @return array
*/
public function add_langs_to_admin_bar( $langlinks ) {
$translatepress = TRP_Translate_Press::get_trp_instance();

$language_switcher = $translatepress->get_component( 'language_switcher' );
$settings = $translatepress->get_component( 'settings' );
$languages = $translatepress->get_component( 'languages' );
$language_switcher = $this->translatepress->get_component( 'language_switcher' );
$settings = $this->translatepress->get_component( 'settings' );
$languages = $this->translatepress->get_component( 'languages' );
$trp_settings = $settings->get_settings();

$languages_to_display = $trp_settings['publish-languages'];
Expand Down Expand Up @@ -127,19 +147,18 @@ public function get_active_languages_uri( $urls ) {

$home_url = home_url();

$translatepress = TRP_Translate_Press::get_trp_instance();

$settings = $translatepress->get_component( 'settings' );
$languages = $translatepress->get_component( 'languages' );
$converter = $translatepress->get_component( 'url_converter' );
$settings = $this->translatepress->get_component( 'settings' );
$languages = $this->translatepress->get_component( 'languages' );
$trp_settings = $settings->get_settings();

$languages_to_display = $trp_settings['publish-languages'];
$published_languages = $languages->get_language_names( $languages_to_display );
add_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );

foreach ( $published_languages as $code => $name ) {
$urls[] = $converter->get_url_for_language( $code, $home_url );
$urls[] = $this->url_converter->get_url_for_language( $code, $home_url );
}
remove_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );

return $urls;
}
Expand All @@ -156,10 +175,8 @@ public function get_active_languages_codes( $codes ) {
$codes = (array) $codes;
}

$translatepress = TRP_Translate_Press::get_trp_instance();

$settings = $translatepress->get_component( 'settings' );
$languages = $translatepress->get_component( 'languages' );
$settings = $this->translatepress->get_component( 'settings' );
$languages = $this->translatepress->get_component( 'languages' );
$trp_settings = $settings->get_settings();

$languages_to_display = $trp_settings['publish-languages'];
Expand All @@ -185,24 +202,19 @@ public function get_home_url_for_lang( $home_url, $lang ) {
return $home_url;
}

$translatepress = TRP_Translate_Press::get_trp_instance();
$converter = $translatepress->get_component( 'url_converter' );
$settings = $translatepress->get_component( 'settings' );
$trp_settings = $settings->get_settings();
$settings = $this->translatepress->get_component( 'settings' );
$trp_settings = $settings->get_settings();

$code = '';

add_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );

foreach ( $trp_settings['url-slugs'] as $index => $slug ) {
if ( $lang === $slug ) {
$code = $index;
break;
}
}

$url = $converter->get_url_for_language( $code, $home_url );

$url = $this->url_converter->get_url_for_language( $code, $home_url );
remove_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );

return $url;
Expand All @@ -223,19 +235,18 @@ public function get_translated_post_urls( $urls, $url, $post_type, $regex ) {
$urls = (array) $urls;
}

$translatepress = TRP_Translate_Press::get_trp_instance();

$settings = $translatepress->get_component( 'settings' );
$languages = $translatepress->get_component( 'languages' );
$converter = $translatepress->get_component( 'url_converter' );
$settings = $this->translatepress->get_component( 'settings' );
$languages = $this->translatepress->get_component( 'languages' );
$trp_settings = $settings->get_settings();

$languages_to_display = $trp_settings['publish-languages'];
$published_languages = $languages->get_language_names( $languages_to_display );
add_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );

foreach ( $published_languages as $code => $name ) {
$urls[] = wp_parse_url( $converter->get_url_for_language( $code, $url ), PHP_URL_PATH ) . $regex;
$urls[] = wp_parse_url( $this->url_converter->get_url_for_language( $code, $url ), PHP_URL_PATH ) . $regex;
}
remove_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );

return $urls;
}
Expand All @@ -248,10 +259,7 @@ public function get_translated_post_urls( $urls, $url, $post_type, $regex ) {
* @return void
*/
public function clear_post_languages( $post_id ) {
$translatepress = TRP_Translate_Press::get_trp_instance();

$converter = $translatepress->get_component( 'url_converter' );
$settings = $translatepress->get_component( 'settings' );
$settings = $this->translatepress->get_component( 'settings' );
$trp_settings = $settings->get_settings();

add_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );
Expand All @@ -265,7 +273,7 @@ public function clear_post_languages( $post_id ) {
continue;
}

$clear_urls[] = $converter->get_url_for_language( $language, $default_permalink, '' );
$clear_urls[] = $this->url_converter->get_url_for_language( $language, $default_permalink, '' );
}

remove_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );
Expand All @@ -286,10 +294,6 @@ public function clear_post_languages( $post_id ) {
* @return void
*/
public function clear_post_after_updating_translation( $update_strings, $settings ) {
$translatepress = TRP_Translate_Press::get_trp_instance();

$converter = $translatepress->get_component( 'url_converter' );

if ( empty( $_POST['url'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
return;
}
Expand All @@ -299,11 +303,13 @@ public function clear_post_after_updating_translation( $update_strings, $setting
$clear_urls = [];
$current_url = remove_query_arg( 'trp-edit-translation', $url );

add_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );
foreach ( $settings['translation-languages'] as $language ) {
if ( ! empty( $update_strings[ $language ] ) ) {
$clear_urls[] = $converter->get_url_for_language( $language, $current_url, '' );
$clear_urls[] = $this->url_converter->get_url_for_language( $language, $current_url, '' );
}
}
remove_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );

rocket_clean_files( $clear_urls );
}
Expand Down