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

Compatibility TranslatePress plugin #1027

Merged
merged 4 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
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
45 changes: 36 additions & 9 deletions includes/admin/class-rop-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,7 @@ private function rop_hide_add_own_app_option() {
*/
public function rop_get_wpml_active_status() {

if ( function_exists( 'icl_object_id' ) ) {
if ( function_exists( 'icl_object_id' ) || class_exists( 'TRP_Translate_Press' ) ) {
return true;
} else {
return false;
Expand Down Expand Up @@ -1572,17 +1572,15 @@ public function rop_get_yoast_seo_active_status() {
public function rop_get_wpml_languages() {

if ( $this->rop_get_wpml_active_status() === false ) {
return;
return;
}

$wpml_active_languages = apply_filters( 'wpml_active_languages', null, array('skip_missing' => 1) );

$languages = $this->get_languages();
$languages_array = array();

foreach ( $wpml_active_languages as $key => $value ) {
foreach ( $languages as $key => $value ) {
$languages_array[] = array( 'code' => $key, 'label' => $value['native_name'] );
}

return $languages_array;
}

Expand All @@ -1605,7 +1603,10 @@ public function rop_wpml_filter_accounts( $post_id, $share_to_accounts ) {
$post_format_model = new Rop_Post_Format_Model();
$filtered_share_to_accounts = array();

$post_lang_code = apply_filters( 'wpml_post_language_details', '', $post_id )['language_code'];
$post_lang_code = '';
if ( function_exists( 'icl_object_id' ) ) {
$post_lang_code = apply_filters( 'wpml_post_language_details', '', $post_id )['language_code'];
}

foreach ( $share_to_accounts as $account_id ) {

Expand All @@ -1616,8 +1617,9 @@ public function rop_wpml_filter_accounts( $post_id, $share_to_accounts ) {
};

$rop_account_lang_code = $rop_account_post_format['wpml_language'];

if ( $post_lang_code === $rop_account_lang_code ) {
if ( class_exists( 'TRP_Translate_Press' ) ) {
$filtered_share_to_accounts[] = $account_id;
} elseif ( $post_lang_code === $rop_account_lang_code ) {
$filtered_share_to_accounts[] = $account_id;
}
}
Expand Down Expand Up @@ -1812,4 +1814,29 @@ public function rop_upgrade_to_pro_plugin_action( $actions, $plugin_file ) {

return $actions;
}

/**
* Get available languages.
*
* @return array
*/
public function get_languages() {
// Get TranslatePress publish plugin languages.
if ( class_exists( 'TRP_Translate_Press' ) ) {
$trp_settings = TRP_Translate_Press::get_trp_instance()->get_component( 'settings' )->get_settings();
if ( $trp_settings ) {
$trp_languages = TRP_Translate_Press::get_trp_instance()->get_component( 'languages' );
$publish_languages = ! empty( $trp_settings['publish-languages'] ) ? $trp_settings['publish-languages'] : array();
$publish_languages = $trp_languages->get_language_names( $publish_languages, 'native_name' );
$languages = array();
foreach ( $publish_languages as $key => $publish_language ) {
$languages[ $key ] = array(
'native_name' => $publish_language,
);
}
return $languages;
}
}
return apply_filters( 'wpml_active_languages', null, array( 'skip_missing' => 1 ) );
}
}
35 changes: 30 additions & 5 deletions includes/admin/helpers/class-rop-post-format-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function get_formated_object( $post_id, $account_id = 0 ) {
}
}

if ( function_exists( 'icl_object_id' ) ) {
if ( function_exists( 'icl_object_id' ) || class_exists( 'TRP_Translate_Press' ) ) {
$selector = new Rop_Posts_Selector_Model;
$post_id = $selector->rop_wpml_id( $post_id, $this->account_id );
}
Expand All @@ -78,7 +78,7 @@ public function get_formated_object( $post_id, $account_id = 0 ) {
$content = $this->build_content( $post_id );
$filtered_post = array();
$filtered_post['post_id'] = $post_id;
$filtered_post['title'] = html_entity_decode( get_the_title( $post_id ) );
$filtered_post['title'] = html_entity_decode( $this->get_translate_content( get_the_title( $post_id ) ) );
$filtered_post['account_id'] = $this->account_id;
$filtered_post['service'] = $service;
$filtered_post['content'] = apply_filters( 'rop_content_filter', $content['display_content'], $post_id, $account_id, $service );
Expand Down Expand Up @@ -289,10 +289,10 @@ public function build_content( $post_id ) {
*/
private function build_base_content( $post_id ) {

$post_title = apply_filters( 'rop_share_post_title', get_the_title( $post_id ), $post_id );
$post_title = apply_filters( 'rop_share_post_title', $this->get_translate_content( get_the_title( $post_id ) ), $post_id );
$post_title = html_entity_decode( $post_title );

$post_content = apply_filters( 'rop_share_post_content', get_post_field( 'post_content', $post_id ), $post_id );
$post_content = apply_filters( 'rop_share_post_content', $this->get_translate_content( get_post_field( 'post_content', $post_id ) ), $post_id );
$content = '';
$post_format = $this->post_format['post_content'];

Expand Down Expand Up @@ -322,9 +322,11 @@ private function build_base_content( $post_id ) {
} else {
$content = $post_content;
}
$content = $this->get_translate_content( $content );
break;
case 'custom_field':
$content = $this->get_custom_field_value( $post_id, $this->post_format['custom_meta_field'] );
$content = $this->get_translate_content( $content );
break;
case 'custom_content':
$content = '';
Expand All @@ -336,6 +338,7 @@ private function build_base_content( $post_id ) {
} else {
$content = $post_title;
}
$content = $this->get_translate_content( $content );
break;
case 'yoast_seo_description':
if ( function_exists( 'YoastSEO' ) ) {
Expand All @@ -348,6 +351,7 @@ private function build_base_content( $post_id ) {
if ( empty( $content ) ) {
$content = $post_content;
}
$content = $this->get_translate_content( $content );
break;
case 'yoast_seo_title_description':
if ( function_exists( 'YoastSEO' ) ) {
Expand All @@ -364,6 +368,7 @@ private function build_base_content( $post_id ) {
} else {
$content = $post_title . apply_filters( 'rop_title_content_separator', ' ' ) . $post_content;
}
$content = $this->get_translate_content( $content );
break;
default:
$content = $post_title;
Expand Down Expand Up @@ -839,7 +844,7 @@ public function build_url( $post_id ) {
}

// WPML compatibility
if ( function_exists( 'icl_object_id' ) ) {
if ( function_exists( 'icl_object_id' ) || class_exists( 'TRP_Translate_Press' ) ) {
$selector = new Rop_Posts_Selector_Model;
$post_url = $selector->rop_wpml_link( $post_url, $this->account_id );
}
Expand Down Expand Up @@ -1074,4 +1079,24 @@ public function remove_divi_shortcodes( $content ) {
return $content;
}

/**
* Get translated content if TranslatePress plugin is activated.
*
* @param string $content Orignal content.
*
* @return string
*/
private function get_translate_content( $content ) {
if ( ! class_exists( 'TRP_Translate_Press' ) || empty( $this->post_format['wpml_language'] ) ) {
return $content;
}

trp_switch_language( $this->post_format['wpml_language'] );

$trp = TRP_Translate_Press::get_trp_instance();
$languages = $trp->get_component( 'languages' );
$translation_render = $trp->get_component( 'translation_render' );
return $translation_render->translate_page( $content );
}

}
104 changes: 77 additions & 27 deletions includes/admin/models/class-rop-posts-selector-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,17 @@ public function get_taxonomies( $data = array() ) {

$taxonomies = array();

$wpml_current_lang = apply_filters( 'wpml_current_language', null );
$wpml_current_lang = $this->get_current_language();

if ( function_exists( 'icl_object_id' ) && ! empty( $language_code ) ) {
if ( ( function_exists( 'icl_object_id' ) || class_exists( 'TRP_Translate_Press' ) ) && ! empty( $language_code ) ) {
// changes the language of global query to use the specfied language
do_action( 'wpml_switch_language', $language_code );
$this->switch_language( $language_code );
}

// Here We are refreshing the taxonomies "on page load"
// This method fires whenever the post format page is brought into view.
// We're refreshing the taxonomies based on whether that first account has a language assigned or not
if ( function_exists( 'icl_object_id' ) && empty( $language_code ) ) {
if ( ( function_exists( 'icl_object_id' ) || class_exists( 'TRP_Translate_Press' ) ) && empty( $language_code ) ) {
// check the first active account and it's post format and see if it has a language code.
$first_account_id = array_keys( $this->data['active_accounts'] )[0];
$post_format_model = new Rop_Post_Format_Model;
Expand All @@ -108,7 +108,7 @@ public function get_taxonomies( $data = array() ) {

if ( ! empty( $first_account_lang ) ) {
// changes the language of global query to use the specfied language
do_action( 'wpml_switch_language', $first_account_lang );
$this->switch_language( $first_account_lang );
}
}

Expand Down Expand Up @@ -154,9 +154,9 @@ public function get_taxonomies( $data = array() ) {
}
}

if ( function_exists( 'icl_object_id' ) && ! ( empty( $language_code ) && empty( $first_account_lang ) ) ) {
if ( ( function_exists( 'icl_object_id' ) || class_exists( 'TRP_Translate_Press' ) ) && ! ( empty( $language_code ) && empty( $first_account_lang ) ) ) {
// set language back to original
do_action( 'wpml_switch_language', $wpml_current_lang );
$this->switch_language( $wpml_current_lang );
}

if ( empty( $taxonomies ) ) {
Expand Down Expand Up @@ -351,10 +351,10 @@ public function select( $account_id = false ) {
$post_format_model = new Rop_Post_Format_Model( $service );
$post_format = $post_format_model->get_post_format( $account_id );

if ( function_exists( 'icl_object_id' ) && ! empty( $post_format['wpml_language'] ) ) {
$wpml_current_lang = apply_filters( 'wpml_current_language', null );
// changes the language of global query to use the specfied language for the account
do_action( 'wpml_switch_language', $post_format['wpml_language'] );
if ( ( function_exists( 'icl_object_id' ) || class_exists( 'TRP_Translate_Press' ) ) && ! empty( $post_format['wpml_language'] ) ) {
$wpml_current_lang = $this->get_current_language();
// changes the language of global query to use the specfied language for the account.
$this->switch_language( $post_format['wpml_language'] );
}

$custom_data = array();
Expand Down Expand Up @@ -430,9 +430,9 @@ public function select( $account_id = false ) {

$this->selection = $results;

if ( function_exists( 'icl_object_id' ) && ! empty( $post_format['wpml_language'] ) ) {
// Sets WP language back to what user set it.
do_action( 'wpml_switch_language', $wpml_current_lang );
if ( ( function_exists( 'icl_object_id' ) || class_exists( 'TRP_Translate_Press' ) ) && ! empty( $post_format['wpml_language'] ) ) {
// Sets WP language back to what user set it.
$this->switch_language( $wpml_current_lang );
}

return $results;
Expand Down Expand Up @@ -490,7 +490,7 @@ private function query_results( $account_id, $post_types, $tax_queries, $exclude
*/
$posts = array_values( $posts );

if ( function_exists( 'icl_object_id' ) ) {
if ( function_exists( 'icl_object_id' ) || class_exists( 'TRP_Translate_Press' ) ) {
$posts = $this->rop_wpml_id( $posts, $account_id );
}

Expand Down Expand Up @@ -767,7 +767,7 @@ public function get_publish_now_posts() {
*/
public function rop_wpml_id( $post_id, $account_id = '' ) {

$default_lang = apply_filters( 'wpml_default_language', null );
$default_lang = $this->get_default_language();
$lang_code = '';

$post = $post_id;
Expand All @@ -777,12 +777,8 @@ public function rop_wpml_id( $post_id, $account_id = '' ) {
$post_format_model = new Rop_Post_Format_Model();
$rop_account_post_format = $post_format_model->get_post_format( $account_id );
// If no language set, use default WPML language
$rop_account_lang_code = ! empty( $rop_account_post_format['wpml_language'] ) ? $rop_account_post_format['wpml_language'] : $default_lang;

}
$lang_code = ! empty( $rop_account_post_format['wpml_language'] ) ? $rop_account_post_format['wpml_language'] : $default_lang;

if ( ! empty( $rop_account_lang_code ) ) {
$lang_code = apply_filters( 'rop_wpml_lang', $rop_account_lang_code );
}

if ( is_array( $post_id ) ) {
Expand Down Expand Up @@ -834,24 +830,78 @@ public function rop_wpml_id( $post_id, $account_id = '' ) {
*/
public function rop_wpml_link( $url, $account_id ) {

$default_lang = apply_filters( 'wpml_default_language', null );
$default_lang = $this->get_default_language();
$lang_code = '';

if ( ! empty( $account_id ) ) {

$post_format_model = new Rop_Post_Format_Model();
$rop_account_post_format = $post_format_model->get_post_format( $account_id );
// If no language set, use default WPML language
$rop_account_lang_code = ! empty( $rop_account_post_format['wpml_language'] ) ? $rop_account_post_format['wpml_language'] : $default_lang;
$lang_code = ! empty( $rop_account_post_format['wpml_language'] ) ? $rop_account_post_format['wpml_language'] : $default_lang;

}

if ( ! empty( $rop_account_lang_code ) ) {
$lang_code = apply_filters( 'rop_wpml_lang', $rop_account_lang_code );
$wpml_url = $this->converter_permalink( $url, $lang_code );

return $wpml_url;
}

/**
* Get default language.
*
* @return string
*/
public function get_default_language() {
if ( class_exists( 'TRP_Translate_Press' ) ) {
$trp_settings = TRP_Translate_Press::get_trp_instance()->get_component( 'settings' )->get_settings();
$default_language = isset( $trp_settings['default-language'] ) ? $trp_settings['default-language'] : '';
return $default_language;
}
return apply_filters( 'wpml_default_language', null );
}

$wpml_url = apply_filters( 'wpml_permalink', $url, $lang_code );
/**
* Converter post permalink using language code.
*
* @param string $url Post URL.
* @param string $lang_code Language code.
*
* @return string
*/
public function converter_permalink( $url, $lang_code ) {
if ( class_exists( 'TRP_Translate_Press' ) ) {
$url_converter = TRP_Translate_Press::get_trp_instance()->get_component( 'url_converter' );
return $url_converter->get_url_for_language( $lang_code, $url );
}
return apply_filters( 'wpml_permalink', $url, $lang_code );
}

return $wpml_url;
/**
* Get current language code.
*
* @return string
*/
public function get_current_language() {
if ( class_exists( 'TRP_Translate_Press' ) ) {
global $TRP_LANGUAGE;
return $TRP_LANGUAGE;
}
return apply_filters( 'wpml_current_language', null );
}

/**
* Language Switch.
*
* @param string $language_code Language code.
*
* @return void
*/
public function switch_language( $language_code ) {
if ( class_exists( 'TRP_Translate_Press' ) ) {
trp_switch_language( $language_code );
} else {
do_action( 'wpml_switch_language', $language_code );
}
}
}
4 changes: 2 additions & 2 deletions includes/admin/services/class-rop-facebook-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ public function share( $post_details, $args = array() ) {
$this->logger->alert_success(
sprintf(
'Successfully shared %s to %s on %s ',
html_entity_decode( get_the_title( $post_id ), ENT_QUOTES ), // TODO Set ENT_QUOTES for all other entity decode occurences in plugin
html_entity_decode( $post_details['title'], ENT_QUOTES ), // TODO Set ENT_QUOTES for all other entity decode occurences in plugin
$args['user'],
$post_details['service']
)
Expand Down Expand Up @@ -635,7 +635,7 @@ private function fb_video_post( $post_details, $hashtags ) {
$image = $this->get_path_by_url( $post_details['post_image'], $post_details['mimetype'] );
$new_post['source'] = $image;
// $new_post['source'] = $api->videoToUpload( $image );
$new_post['title'] = html_entity_decode( get_the_title( $post_details['post_id'] ), ENT_QUOTES );
$new_post['title'] = html_entity_decode( $post_details['title'], ENT_QUOTES );
$post_url = empty( $this->share_link_text ) ? $this->get_url( $post_details ) : '';
$new_post['description'] = $post_details['content'] . $post_url . $hashtags;

Expand Down
2 changes: 1 addition & 1 deletion includes/admin/services/class-rop-gmb-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ public function share( $post_details, $args = array() ) {
$this->logger->alert_success(
sprintf(
'Successfully shared %s to %s on Google My Business ',
html_entity_decode( get_the_title( $post_details['post_id'] ) ),
html_entity_decode( $post_details['title'] ),
$args['user']
)
);
Expand Down
Loading
Loading