Skip to content

Commit

Permalink
feat: share translated content
Browse files Browse the repository at this point in the history
  • Loading branch information
girishpanchal30 committed Jan 31, 2025
1 parent 91cc333 commit 3b618b2
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 39 deletions.
11 changes: 7 additions & 4 deletions includes/admin/class-rop-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1603,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 @@ -1614,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 @@ -1826,7 +1830,6 @@ public function get_languages() {
$publish_languages = $trp_languages->get_language_names( $publish_languages, 'native_name' );
$languages = array();
foreach ( $publish_languages as $key => $publish_language ) {
$key = false !== strpos( $key, '_' ) ? substr( $key, 0, strpos( $key, '_' ) ) : $key;
$languages[ $key ] = array(
'native_name' => $publish_language,
);
Expand Down
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 );
}

}
94 changes: 76 additions & 18 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 Down Expand Up @@ -830,7 +830,7 @@ 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 ) ) {
Expand All @@ -842,8 +842,66 @@ public function rop_wpml_link( $url, $account_id ) {

}

$wpml_url = apply_filters( 'wpml_permalink', $url, $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 );
}

/**
* 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 );
}

/**
* 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 @@ -538,7 +538,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 @@ -627,7 +627,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 );
$new_post['description'] = $post_details['content'] . $this->get_url( $post_details ) . $hashtags;

return array(
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
6 changes: 3 additions & 3 deletions includes/admin/services/class-rop-pinterest-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,14 @@ public function share( $post_details, $args = array() ) {

// Check if image is present.
if ( empty( $post_details['post_image'] ) ) {
$this->logger->alert_error( sprintf( 'No featured image set for %s cannot pin to %s on Pinterest', html_entity_decode( get_the_title( $post_details['post_id'] ) ), $args['id'] ) );
$this->logger->alert_error( sprintf( 'No featured image set for %s cannot pin to %s on Pinterest', html_entity_decode( $post_details['title'] ), $args['id'] ) );

return false;
}

if ( strpos( $post_details['mimetype']['type'], 'image' ) === false ) {

$this->logger->alert_error( sprintf( 'No valid image present in %s to pin to %s for %s', html_entity_decode( get_the_title( $post_details['post_id'] ) ), $args['id'], $post_details['service'] ) );
$this->logger->alert_error( sprintf( 'No valid image present in %s to pin to %s for %s', html_entity_decode( $post_details['title'] ), $args['id'], $post_details['service'] ) );

return false;
}
Expand Down Expand Up @@ -453,7 +453,7 @@ public function share( $post_details, $args = array() ) {
sprintf(
'Successfully pinned %s in %s to %s on %s',
basename( $post_details['post_image'] ),
html_entity_decode( get_the_title( $post_id ) ),
html_entity_decode( $post_details['title'] ),
$args['id'],
$post_details['service']
)
Expand Down
4 changes: 2 additions & 2 deletions includes/admin/services/class-rop-tumblr-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ public function share( $post_details, $args = array() ) {

$new_post['type'] = 'link';
$new_post['url'] = trim( $this->get_url( $post_details ) );
$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 );
$new_post['description'] = $this->strip_excess_blank_lines( html_entity_decode( $post_details['content'], ENT_QUOTES ) );
$new_post['author'] = $this->get_author( $post_id );
$new_post['tags'] = $hashtags;
Expand Down Expand Up @@ -573,7 +573,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 ) ),
html_entity_decode( $post_details['title'] ),
$args['user'],
$post_details['service']
)
Expand Down
2 changes: 1 addition & 1 deletion includes/admin/services/class-rop-twitter-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,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 ) ),
html_entity_decode( $post_details['title'] ),
$args['user'],
$post_details['service']
)
Expand Down
4 changes: 2 additions & 2 deletions includes/admin/services/class-rop-vk-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ private function vk_media_post( $post_details, $hashtags, $args, $owner_id, $cli
private function vk_video_post( $post_details, $hashtags, $attachment_path, $args, $owner_id, $client, $access_token ) {

$params = array(
'name' => get_the_title( $post_details['post_id'] ),
'name' => $post_details['title'],
'description' => $post_details['content'],
);

Expand Down Expand Up @@ -495,7 +495,7 @@ public function share( $post_details, $args = array() ) {
$this->logger->alert_success(
sprintf(
'Successfully shared %s to %s on Vkontakte ',
html_entity_decode( get_the_title( $post_details['post_id'] ) ),
html_entity_decode( $post_details['title'] ),
$args['user']
)
);
Expand Down
Loading

0 comments on commit 3b618b2

Please sign in to comment.