get_icl_translation_enabled() || !$sitepress->icl_account_configured()): ?>
professional translation.','sitepress'),'http://wpml.org/?page_id=1169'); ?>
\*\/ ?>
request($url);
if(!is_wp_error($response) && ($response['response']['code']=='200')){
$translation = json_decode($response['body']);
$translation = $translation->responseData->translatedText;
}else{
$ican_google_translation_request_fail_flag = 1;
$translation ='';
}
return $translation;
*/
}
function delete_comment_actions($comment_id){
global $sitepress;
$trid = $sitepress->get_element_trid($comment_id, 'comment');
if($trid){
$translations = $sitepress->get_element_translations($trid, 'comment');
$sitepress->delete_element_translation($trid, 'comment');
foreach($translations as $t){
if(isset($t->element_id) && $t->element_id != $comment_id){
wp_delete_comment($t->element_id);
}
}
}
}
function wp_set_comment_status_actions($comment_id, $status){
global $sitepress;
static $ids_processed = array(); // using this for avoiding the infinite loop
$trid = $sitepress->get_element_trid($comment_id, 'comment');
if($trid){
$translations = $sitepress->get_element_translations($trid, 'comment');
foreach($translations as $t){
if(isset($t->element_id) && $t->element_id != $comment_id && !in_array($t->element_id,$ids_processed)){
wp_set_comment_status($t->element_id, $status);
$ids_processed[] = $t->element_id;
}
}
}
}
function edit_comment_actions($comment_id){
// we'll use this hook ONLY for updating comments - not for new comments
if($_POST['icl_translate_reply']){
global $wpdb;
$res = $wpdb->get_row("
SELECT MD5(c.comment_content)<> ms.md5 AND ms.md5 IS NOT NULL AS updated, ms.to_language
FROM {$wpdb->comments} c
JOIN {$wpdb->prefix}icl_message_status ms ON c.comment_ID
WHERE c.comment_ID = {$comment_id} AND ms.object_type='comment'
");
if(isset($res->updated) && $res->updated){
$this->send_comment_to_translation($comment_id, $res->to_language);
}
}
}
function transition_comment_status_actions($new_status, $old_status, $comment){
global $sitepress, $wpdb;
$comment_id = $comment->comment_ID;
static $ids_processed_tr = array(); // using this for avoiding the infinite loop
$trid = $sitepress->get_element_trid($comment_id, 'comment');
if($trid){
$translations = $sitepress->get_element_translations($trid, 'comment');
foreach($translations as $t){
if(isset($t->element_id) && $t->element_id != $comment_id && !in_array($t->element_id,$ids_processed_tr)){
//wp_set_comment_status($t->element_id, $comment->comment_approved);
$wpdb->update($wpdb->comments, array('comment_approved'=>$comment->comment_approved), array('comment_id'=>$t->element_id));
$ids_processed_tr[] = $t->element_id;
}
}
}
}
function comment_form_options(){
global $wpdb, $post, $userdata, $sitepress;
$user_lang_info = $sitepress->get_language_details($this->user_language);
if(empty($post)){ //edit comment
global $comment;
$ctrid = $sitepress->get_element_trid($comment->comment_ID, 'comment');
// original comment language
$comment_language = $wpdb->get_var("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE trid={$ctrid} AND element_type='comment' AND source_language_code IS NULL");
$post_type = $wpdb->get_var("SELECT post_type FROM {$wpdb->posts} WHERE ID='" . intval($comment->comment_post_ID) . "'");
$cur_lang = $wpdb->get_var("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_id='" . intval($comment->comment_post_ID) . "' AND element_type='post_{$post_type}'");
}else{ // add new comment on front end
$cur_lang = $sitepress->get_current_language();
$comment_language = $sitepress->get_current_language();
}
$page_lang_info = $wpdb->get_var("
SELECT name
FROM {$wpdb->prefix}icl_languages_translations
WHERE language_code='".$cur_lang."' AND display_language_code='".$this->user_language."'
");
if($sitepress->have_icl_translator($this->user_language, $cur_lang)){
$disabled = '';
}else{
$disabled = ' disabled="disabled"';
}
?>
enable_replies_translation && $userdata->user_level > 7 && $user_lang_info['code'] != $cur_lang): ?>
comment_ID;
}
if($this->is_visitor){
// get comments in visitor's language
if(!empty($cids)){
$comment_ids = $wpdb->get_col("
SELECT element_id
FROM {$wpdb->prefix}icl_translations
WHERE element_type='comment' AND element_id IN(".join(',', $cids).")
AND language_code = '{$this->user_language}'
");
foreach($comments as $k=>$c){
if(!in_array($c->comment_ID , (array)$comment_ids)){
unset($comments[$k]);
}
}
}
}elseif(!$this->enable_comments_translation){
// show only original comments regardless of the user language
if(!empty($cids)){
$comment_ids = $wpdb->get_col("
SELECT element_id
FROM {$wpdb->prefix}icl_translations
WHERE element_type='comment' AND element_id IN(".join(',', $cids).")
AND source_language_code IS NULL
");
}
foreach($comments as $k=>$c){
if(!in_array($c->comment_ID , (array)$comment_ids)){
unset($comments[$k]);
}
}
//filter for this language
/*
if(!empty($cids)){
$comment_ids = $wpdb->get_col("
SELECT element_id
FROM {$wpdb->prefix}icl_translations
WHERE element_type='comment' AND element_id IN(".join(',', $cids).")
AND language_code='{$this->user_language}'
");
}
if($comments){
$_trids = $wpdb->get_col("SELECT DISTINCT trid FROM {$wpdb->prefix}icl_translations WHERE element_type='comment' AND element_id IN(".join(",",$cids).")");
$_ttrids = $wpdb->get_col("SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_type='comment' AND trid IN(".join(",",$_trids).") AND language_code='{$this->user_language}'");
$_utrids = array_diff($_trids, $_ttrids);
if(!empty($_utrids)){
$_untranslated_elids = $wpdb->get_col("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE element_type='comment' AND trid IN(".join(",",$_utrids).") AND source_language_code IS NULL");
}
}
foreach($comments as $k=>$c){
if(!in_array($c->comment_ID , (array)$comment_ids) && !in_array($c->comment_ID, (array)$_untranslated_elids)){
unset($comments[$k]);
}
}
*/
}else{
foreach($comments as $c){
$comment_ids[] = $c->comment_ID;
$comments_by_id[$c->comment_ID] = $c;
}
$trids = $wpdb->get_col("
SELECT DISTINCT trid
FROM {$wpdb->prefix}icl_translations
WHERE element_type='comment' AND element_id IN (".join(',',$comment_ids).")
");
// filter comments in the user's language
$translated_comments_trids = array(0);
if(!empty($trids)){
$res = $wpdb->get_results("
SELECT element_id, trid
FROM {$wpdb->prefix}icl_translations
WHERE element_type='comment' AND trid IN (".join(',',$trids).") AND language_code = '{$this->user_language}'
");
foreach($res as $row){
$comments_in_the_users_language[] = $row->element_id;
$translated_comments_trids[] = $row->trid;
}
}
$comments_not_translated_trids = array_diff($trids, $translated_comments_trids);
if($comments_not_translated_trids){
$comments_not_translated = $wpdb->get_col("
SELECT element_id
FROM {$wpdb->prefix}icl_translations
WHERE element_type='comment' AND trid IN (".join(',',$comments_not_translated_trids).") AND language_code <> '{$this->user_language}'
");
}
if(!empty($comments_not_translated)){
$res = $wpdb->get_results("
SELECT element_id, trid, language_code
FROM {$wpdb->prefix}icl_translations
WHERE element_type='comment' AND element_id IN (".join(',',$comments_not_translated).")
");
$wp_comments_cols = array_keys($wpdb->get_row("SELECT * FROM {$wpdb->comments} LIMIT 1", ARRAY_A));
foreach($res as $original_comment){
$comment_content = $comments_by_id[$original_comment->element_id]->comment_content;
$machine_translation = $this->machine_translate($original_comment->language_code, $this->user_language, $comment_content);
$comment_new = clone $comments_by_id[$original_comment->element_id];
$comment_new->comment_content = $machine_translation;
unset($comment_new->comment_ID);
$wpdb->insert($wpdb->comments, array_intersect_key((array)$comment_new, array_flip($wp_comments_cols)));
$new_comment_id = $wpdb->insert_id;
$sitepress->set_element_language_details($new_comment_id, 'comment', $original_comment->trid, $this->user_language);
$comment_new->comment_ID = $new_comment_id;
if($original_comment_parent = $comments_by_id[$original_comment->element_id]->comment_parent){
// check for the comment parent in the user language
$cptrid = $sitepress->get_element_trid($original_comment_parent, 'comment');
$comment_new->comment_parent = $wpdb->get_var("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid={$cptrid} AND element_type='comment' AND language_code='{$this->user_language}'");
$wpdb->update($wpdb->comments, array('comment_parent'=>$comment_new->comment_parent), array('comment_ID'=>$new_comment_id));
}
if(!$machine_translation){
$nonce = wp_create_nonce('machine-translation-failed'.$new_comment_id);
$comment_new->comment_content = '' . sprintf(__('Machine translation failed. retry','sitepress'), ' onclick="icl_retry_mtr(this)" id="icl_retry_mtr_'.$comment_new->comment_ID.'_'.$nonce.'" href="#"') . '';
$wpdb->update($wpdb->comments, array('comment_content'=>$comment_new->comment_content), array('comment_ID'=>$new_comment_id));
}
$comments_in_the_users_language[] = $new_comment_id;
$comments[] = $comment_new;
}
}
//filter out comments in other languages than the user's
foreach((array)$comments as $k=>$c){
if(!in_array($c->comment_ID , (array)$comments_in_the_users_language)){
unset($comments[$k]);
}
}
}
return array_values($comments);
}
function use_comments_array_filter(){
global $comments;
$comments = $this->comments_array_filter($comments);
}
function comment_feed_join($join){
global $wpdb, $sitepress;
$lang = $this->enable_comments_translation ? $this->user_language : $sitepress->get_current_language();
$join .= " JOIN {$wpdb->prefix}icl_translations tc ON {$wpdb->comments}.comment_ID = tc.element_id AND tc.element_type='comment' AND tc.language_code='{$lang}'";
return $join;
}
function filter_queries($sql){
global $pagenow, $wpdb;
static $_untranslated_elids;
if($pagenow == 'index.php'){
if(preg_match('#SELECT \* FROM (.+)comments ORDER BY comment_date_gmt DESC LIMIT ([0-9]+), ([0-9]+)#i',$sql,$matches)){
$res = mysql_query($sql);
while($row = mysql_fetch_object($res)){
$comments[] = $row;
}
if(!$this->enable_comments_translation && $comments){
// show only original comments regardless of the user language
foreach($comments as $c){
$cids[] = $c->comment_ID;
}
$comment_ids = array(0);
if(!empty($cids)){
$comment_ids = $wpdb->get_col("
SELECT element_id
FROM {$wpdb->prefix}icl_translations
WHERE element_type='comment' AND element_id IN(".join(',', $cids).")
AND source_language_code IS NULL
");
if(!empty($comment_ids)){
$sql = "SELECT * FROM {$matches[1]}comments c
WHERE c.comment_ID IN (".join(',',$comment_ids).")
ORDER BY c.comment_date_gmt DESC LIMIT {$matches[2]}, {$matches[3]}";
}
}
}else{
$this->comments_array_filter($comments);
unset($comments);
$sql = "
SELECT * FROM {$matches[1]}comments c
LEFT JOIN {$matches[1]}icl_translations t ON t.element_id=c.comment_ID
WHERE
(t.element_type='comment' AND t.language_code='{$this->user_language}')
ORDER BY c.comment_date_gmt DESC LIMIT {$matches[2]}, {$matches[3]}";
}
}
}elseif( isset($_POST['action']) && $_POST['action']=='get-comments' && isset($_POST['mode']) && $_POST['mode']=='single'){
global $sitepress;
if(preg_match('#SELECT \* FROM (.+)comments USE INDEX \(comment_date_gmt\) WHERE \( comment_approved = \'0\' OR comment_approved = \'1\' \) AND comment_post_ID = \'([0-9]+)\' ORDER BY comment_date_gmt ASC LIMIT ([0-9]+), ([0-9]+)#i',$sql,$matches)){
$post_type = $wpdb->get_var("SELECT post_type FROM {$wpdb->posts} WHERE ID='{$_POST['post_ID']}'");
$res = mysql_query("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_id={$_POST['post_ID']} AND element_type='post_{$post_type}'");
$row = mysql_fetch_row($res);
$c_language = $row[0];
if($this->enable_comments_translation){
$res = $wpdb->get_results("
SELECT element_id, trid FROM {$wpdb->prefix}icl_translations t
JOIN {$wpdb->comments} c ON t.element_id = c.comment_ID AND comment_POST_ID = {$_POST['post_ID']}
WHERE t.element_type='comment' AND language_code='{$c_language}'");
foreach($res as $r){
$trids_orig[$r->trid] = $r->element_id;
}
$res = $wpdb->get_results("
SELECT element_id, trid FROM {$wpdb->prefix}icl_translations t
JOIN {$wpdb->comments} c ON t.element_id = c.comment_ID AND comment_POST_ID = {$_POST['post_ID']}
WHERE t.element_type='comment' AND language_code='{$this->user_language}'");
foreach($res as $r){
$trids_tr[$r->trid] = $r->element_id;
}
$wp_comments_cols = array_keys($wpdb->get_row("SELECT * FROM {$wpdb->comments} LIMIT 1", ARRAY_A));
foreach($trids_orig as $o_trid=>$o_cid){
if(!isset($trids_tr[$o_trid])){
$original_comment = get_comment($trids_orig[$o_trid]);
$machine_translation = $this->machine_translate($c_language, $this->user_language, $original_comment->comment_content);
$comment_new = clone $original_comment;
$comment_new->comment_content = $machine_translation;
unset($comment_new->comment_ID);
$wpdb->insert($wpdb->comments, array_intersect_key((array)$comment_new, array_flip($wp_comments_cols)));
$new_comment_id = $wpdb->insert_id;
$sitepress->set_element_language_details($new_comment_id, 'comment', $o_trid, $this->user_language);
if($original_comment->comment_parent){
$cptrid = $sitepress->get_element_trid($original_comment->comment_parent, 'comment');
$comment_new->comment_parent = $wpdb->get_var("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid={$cptrid} AND element_type='comment' AND language_code='{$this->user_language}'");
$wpdb->update($wpdb->comments, array('comment_parent'=>$comment_new->comment_parent), array('comment_ID'=>$new_comment_id));
}
if(!$machine_translation){
$nonce = wp_create_nonce('machine-translation-failed'.$new_comment_id);
$comment_new->comment_content = '' . sprintf(__('Machine translation failed. retry','sitepress'), ' onclick="icl_retry_mtr(this)" id="icl_retry_mtr_'.$comment_new->comment_ID.'_'.$nonce.'" href="#"') . '';
$wpdb->update($wpdb->comments, array('comment_content'=>$comment_new->comment_content), array('comment_ID'=>$new_comment_id));
}
}
}
$c_language = $this->user_language;
}
$sql = "
SELECT *
FROM {$matches[1]}comments c USE INDEX (comment_date_gmt)
JOIN {$matches[1]}icl_translations t ON t.element_id=c.comment_ID
WHERE
t.element_type='comment' AND t.language_code='{$c_language}' AND
( comment_approved = '0' OR comment_approved = '1' ) AND
comment_post_ID = '{$matches[2]}'
ORDER BY comment_date_gmt ASC
LIMIT {$matches[3]}, {$matches[4]}";
}
}
return $sql;
}
/*
function comment_feed_where($where){
return $where;
}
*/
function wp_insert_comment($comment_id, $comment_data){
global $sitepress, $wpdb;
$post_type = $wpdb->get_var("SELECT post_type FROM {$wpdb->posts} WHERE ID=" . intval($_POST['comment_post_ID']));
if(isset($_POST['icl_user_language'])){
if($_POST['icl_translate_reply']){
$lang = $_POST['icl_user_language'];
// send the comment to translation
$this->send_comment_to_translation($comment_id,$wpdb->get_var("
SELECT language_code FROM {$wpdb->prefix}icl_translations
WHERE element_type='post_{$post_type}' AND element_id=".intval($_POST['comment_post_ID'])));
}else{
//$lang = $_POST['icl_comment_language'];
$lang = $wpdb->get_var("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_type='post_{$post_type}' AND element_id={$_POST['comment_post_ID']}");
// sync comment parent
// look for comment parent in original language and set for the comment that's been added
if(isset($_POST['comment_parent'])){
$comment_parent = $_POST['comment_parent'];
}else{
$comment_parent = $_POST['comment_ID'];
}
if($comment_parent){
$ctrid = (int)$sitepress->get_element_trid($comment_parent,'comment');
$original_comment_parent = $wpdb->get_var("
SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid={$ctrid}
AND element_type='comment' AND language_code='{$lang}'");
$wpdb->update($wpdb->comments, array('comment_parent'=>$original_comment_parent), array('comment_ID'=>$comment_id));
}
}
}else{
$comment_post_ID = isset($_POST['comment_post_ID']) ? $_POST['comment_post_ID'] : $comment_data->comment_post_ID;
$lang = $wpdb->get_var($wpdb->prepare("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_type='post_{$post_type}' AND element_id=%d", $comment_post_ID));
if(!$lang){
$lang = $this->user_language; // just in case
}
}
if(!$lang){
$lang = $this->user_language;
}
$translation_id = $sitepress->set_element_language_details($comment_id, 'comment', null, $lang);
}
function send_comment_to_translation($comment_id, $to_language){
global $wpdb, $sitepress_settings, $sitepress;
$iclq = new ICanLocalizeQuery($sitepress_settings['site_id'], $sitepress_settings['access_key']);
$from_lang = $sitepress->get_language_details($this->user_language);
$to_lang = $sitepress->get_language_details($to_language);
$from_lang_server = ICL_Pro_Translation::server_languages_map($from_lang['english_name']);
$to_lang_server = ICL_Pro_Translation::server_languages_map($to_lang['english_name']);
$body = $wpdb->get_var("SELECT comment_content FROM {$wpdb->comments} WHERE comment_ID={$comment_id}");
$rid = $iclq->cms_create_message($body, $from_lang_server, $to_lang_server);
if($rid > 0){
// does this comment already exist in the messages status queue?
$msid = $wpdb->get_var("SELECT id FROM {$wpdb->prefix}icl_message_status WHERE object_type='comment' AND object_id={$comment_id}");
if($msid){
$wpdb->update($wpdb->prefix.'icl_message_status',
array('rid'=>$rid, 'md5' => md5($body), 'status' => MESSAGE_TRANSLATION_IN_PROGRESS),
array('id' => $msid)
);
}else{
$wpdb->insert($wpdb->prefix.'icl_message_status', array(
'rid' => $rid,
'object_id' => $comment_id,
'from_language' => $this->user_language,
'to_language' => $to_language,
'md5' => md5($body),
'object_type' => 'comment',
'status' => MESSAGE_TRANSLATION_IN_PROGRESS
));
}
}
}
function add_comment_translation($object_id, $to_language, $translation){
global $wpdb, $sitepress, $sitepress_settings;
$original_comment = $wpdb->get_row("
SELECT *
FROM {$wpdb->comments} WHERE comment_ID = {$object_id}
", ARRAY_A);
$new_comment = $original_comment;
//sync comment parent
if($original_comment['comment_parent']){
$ctrid = (int)$sitepress->get_element_trid($original_comment['comment_parent'],'comment');
$new_comment['comment_parent'] = (int) $wpdb->get_var("
SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid={$ctrid}
AND element_type='comment' AND language_code='{$to_language}'");
}
$original_comment_id = $original_comment['comment_ID'];
unset($original_comment);
$new_comment['comment_content'] = $translation;
unset($new_comment['comment_ID']);
//remove_action('wp_insert_comment', array($this, 'wp_insert_comment'));
//check whether a translation for this comment in this language already exists
$trid = $sitepress->get_element_trid($original_comment_id, 'comment');
$existing_comment_id = $wpdb->get_var("
SELECT element_id FROM {$wpdb->prefix}icl_translations
WHERE trid={$trid} AND element_type='comment' AND language_code='{$to_language}'");
if(!$existing_comment_id){
remove_action('wp_insert_comment', array($this, 'wp_insert_comment'));
$new_id = wp_insert_comment($new_comment);
add_action('wp_insert_comment', array($this, 'wp_insert_comment'), 10, 2);
}else{
$new_id = $new_comment['comment_ID'] = $existing_comment_id;
remove_action('transition_comment_status', array($this, 'transition_comment_status_actions'));
wp_update_comment($new_comment);
}
$sitepress->set_element_language_details($new_id, 'comment', $trid, $to_language);
return 1; //success
}
function add_custom_xmlrpc_methods($methods){
//$methods['icanlocalize.notify_comment_translation'] = array($this, 'add_comment_translation');
return $methods;
}
function comment_text_filter($comment_text){
global $sitepress, $comment, $wp_query, $wpdb;
static $comment_ids, $comment_originals, $page_language, $translation_status;
//if($this->enable_comments_translation && $this->user_language != $sitepress->get_current_language() ){
if($this->enable_comments_translation){
// run this block once
if(empty($comment_ids)){
$page_language = $wpdb->get_var("
SELECT name
FROM {$wpdb->prefix}icl_languages_translations
WHERE language_code='".$sitepress->get_current_language()."' AND display_language_code='".$this->user_language."'
");
foreach($wp_query->comments as $c){
$comment_ids[] = (int)$c->comment_ID;
}
$res = $wpdb->get_results("
SELECT t.element_id, t.trid, ms.status
FROM {$wpdb->prefix}icl_translations t
LEFT JOIN {$wpdb->prefix}icl_message_status ms ON t.element_id=ms.object_id
WHERE t.element_id IN(".join(',',$comment_ids).") AND t.element_type='comment' AND t.language_code <> '".$sitepress->get_current_language()."' AND (ms.object_type='comment' OR ms.object_type IS NULL)");
foreach($res as $row){
$tridsmap[$row->trid] = $row->element_id;
if($row->status){
$translation_status[$row->element_id] = $row->status;
}
}
if(!empty($tridsmap)){
$res = $wpdb->get_results("
SELECT t.trid, c.comment_content
FROM {$wpdb->prefix}icl_translations t
JOIN {$wpdb->comments} c ON t.element_id = c.comment_ID
WHERE t.trid IN(".join(',',array_keys($tridsmap)).") AND t.element_type='comment' AND t.language_code='".$sitepress->get_current_language()."'");
foreach($res as $row){
$comment_originals[$tridsmap[$row->trid]] = $row->comment_content;
}
}
}
$str = '';
if(isset($comment_originals[$comment->comment_ID])){
$str .= '