PageRenderTime 71ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/wp-content/plugins/sitepress-multilingual-cms/inc/comments-translation/functions.php

https://bitbucket.org/dkrzos/phc
PHP | 1022 lines | 763 code | 137 blank | 122 comment | 129 complexity | b751a0bf47730dd326e1dac6aac89868 MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. // disabled
  3. //define('MACHINE_TRANSLATE_API_URL',"http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%s&langpair=%s|%s");
  4. require_once ICL_PLUGIN_PATH . '/inc/comments-translation/google_languages_map.php';
  5. class IclCommentsTranslation{
  6. var $enable_comments_translation;
  7. var $enable_replies_translation;
  8. var $user_language;
  9. var $is_visitor = false;
  10. function __construct(){
  11. add_action('init', array($this, 'init'));
  12. }
  13. function init(){
  14. global $current_user, $sitepress_settings, $sitepress, $pagenow, $wpdb;
  15. if(!empty($current_user->ID)){
  16. $this->enable_comments_translation = get_user_meta($current_user->data->ID,'icl_enable_comments_translation',true);
  17. $this->enable_replies_translation = get_user_meta($current_user->data->ID,'icl_enable_replies_translation',true);
  18. $this->user_language = $sitepress->get_user_admin_language($current_user->data->ID);
  19. if(!$this->user_language){
  20. $this->user_language = $sitepress_settings['admin_default_language'];
  21. if($this->user_language == '_default_') $this->user_language = $sitepress->get_default_language();
  22. }
  23. }else{
  24. $this->is_visitor = true;
  25. $this->user_language = $sitepress->get_current_language();
  26. }
  27. /* google translate API is shutting down
  28. if(defined('WP_ADMIN')){
  29. add_action('show_user_profile', array($this, 'show_user_options'));
  30. add_action('personal_options_update', array($this, 'save_user_options'));
  31. }
  32. */
  33. if(defined('WP_ADMIN') && $this->enable_comments_translation){
  34. add_action('admin_print_scripts', array($this,'js_scripts_setup'));
  35. }
  36. add_action('manage_comments_nav', array($this,'use_comments_array_filter'));
  37. add_filter('comments_array', array($this,'comments_array_filter'));
  38. add_filter('comment_feed_join', array($this, 'comment_feed_join'));
  39. add_filter('query', array($this, 'filter_queries'));
  40. //add_filter('comment_feed_where', array($this, 'comment_feed_where'));
  41. add_action('delete_comment', array($this, 'delete_comment_actions'));
  42. add_action('wp_set_comment_status', array($this, 'wp_set_comment_status_actions'), 1, 2);
  43. if(isset($_POST['action']) && $_POST['action']=='editedcomment'){
  44. add_action('transition_comment_status', array($this, 'transition_comment_status_actions'), 1, 3);
  45. }
  46. if('comment.php' == $pagenow){
  47. $row = $wpdb->get_row("
  48. SELECT c.user_id, t.language_code
  49. FROM {$wpdb->comments} c JOIN {$wpdb->prefix}icl_translations t ON c.comment_ID = t.element_id AND element_type='comment'
  50. WHERE c.comment_ID=".intval($_GET['c'])
  51. );
  52. $comment_author = $row->user_id;
  53. $comment_lang = $row->language_code;
  54. if($current_user->data->ID == $comment_author && $comment_lang == $this->user_language){
  55. add_action('admin_head', array($this, 'admin_head_actions'));
  56. }
  57. }
  58. add_action('edit_comment', array($this, 'edit_comment_actions'));
  59. add_action('comment_form', array($this, 'comment_form_options'));
  60. //add_action('comment_post', array($this, 'comment_post'));
  61. add_action('wp_insert_comment', array($this, 'wp_insert_comment'), 10, 2);
  62. if(defined('WP_ADMIN') && $this->enable_comments_translation){
  63. add_filter('comment_row_actions', array($this,'comment_row_actions'),1, 2);
  64. }
  65. if(defined('WP_ADMIN')){
  66. add_filter('comment_text', array($this, 'comment_text_filter_admin'));
  67. }else{
  68. add_filter('comment_text', array($this, 'comment_text_filter'));
  69. }
  70. add_filter('xmlrpc_methods',array($this, 'add_custom_xmlrpc_methods'));
  71. add_filter('get_comments_number', array($this, 'get_comments_number_filter'));
  72. global $wpml_add_message_translation_callbacks;
  73. $wpml_add_message_translation_callbacks['comment'][] = array($this, 'add_comment_translation');
  74. if(isset($_GET['retry_mtr'])){
  75. global $wpdb;
  76. $nonce = wp_create_nonce('machine-translation-failed'.$_GET['retry_mtr']);
  77. if($_GET['nonce']==$nonce){
  78. $wpdb->query("DELETE FROM {$wpdb->comments} WHERE comment_ID=" . intval($_GET['retry_mtr']));
  79. $wpdb->query("DELETE FROM {$wpdb->prefix}icl_translations WHERE element_type='comment' AND element_id=" . intval($_GET['retry_mtr']));
  80. if(defined('WP_ADMIN')){
  81. wp_redirect(rtrim(preg_replace('@retry_mtr=([0-9]+)&nonce=([0-9a-z]+)@','',$_SERVER['REQUEST_URI']),'?'));
  82. }else{
  83. add_action('template_redirect', array($this, '__reload_page'));
  84. }
  85. }
  86. }
  87. require_once ICL_PLUGIN_PATH . '/inc/cache.php';
  88. $this->icl_comment_count_cache = new icl_cache();
  89. add_filter('views_edit-comments', array($this, 'pending_by_language'));
  90. }
  91. function pending_by_language($views){
  92. global $wpdb, $sitepress;
  93. if(isset($views['moderated'])){
  94. preg_match('#<span class="pending-count">([0-9]+)</span>#', $views[ 'moderated' ], $matches);
  95. if($matches[1] > 0){
  96. $results = $wpdb->get_results($wpdb->prepare("
  97. SELECT tr.language_code, COUNT(tr.element_id) AS c
  98. FROM {$wpdb->comments} c
  99. JOIN {$wpdb->prefix}icl_translations tr ON tr.element_id = c.comment_ID
  100. WHERE c.comment_approved = '0' AND tr.element_type = 'comment'
  101. GROUP BY tr.language_code
  102. HAVING c > 0
  103. "));
  104. if(!empty($results)){
  105. foreach($results as $r){
  106. $_ldetails = $sitepress->get_language_details($r->language_code);
  107. $lbreak[] = sprintf('<a %s href="%s">%s</a>(%d)',
  108. $sitepress->get_current_language() == $r->language_code ? ' class="current"' : '',
  109. admin_url('edit-comments.php?comment_status=moderated&lang=' . $r->language_code),
  110. $_ldetails['display_name'], $r->c);
  111. }
  112. $views[ 'moderated' ] .= ' - ' . join(', ', $lbreak);
  113. }
  114. }
  115. }
  116. if(isset($views['spam'])){
  117. preg_match('#<span class="spam-count">([0-9]+)</span>#', $views[ 'spam' ], $matches);
  118. if($matches[1] > 0){
  119. $results = $wpdb->get_results($wpdb->prepare("
  120. SELECT tr.language_code, COUNT(tr.element_id) AS c
  121. FROM {$wpdb->comments} c
  122. JOIN {$wpdb->prefix}icl_translations tr ON tr.element_id = c.comment_ID
  123. WHERE c.comment_approved = 'spam' AND tr.element_type = 'comment'
  124. GROUP BY tr.language_code
  125. HAVING c > 0
  126. "));
  127. if(!empty($results)){
  128. foreach($results as $r){
  129. $_ldetails = $sitepress->get_language_details($r->language_code);
  130. $lbreak[] = sprintf('<a %s href="%s">%s</a>(%d)',
  131. $sitepress->get_current_language() == $r->language_code ? ' class="current"' : '',
  132. admin_url('edit-comments.php?comment_status=spam&lang=' . $r->language_code),
  133. $_ldetails['display_name'], $r->c);
  134. }
  135. $views[ 'spam' ] .= ' - ' . join(', ', $lbreak);
  136. }
  137. }
  138. }
  139. return $views;
  140. }
  141. function __reload_page(){
  142. wp_redirect(get_permalink());
  143. }
  144. function admin_head_actions(){
  145. add_meta_box('comment', __('Translate', 'sitepress'), array($this, 'comment_form_options'), 'comment', 'normal', 'high', 1);
  146. }
  147. function js_scripts_setup(){
  148. global $pagenow, $sitepress;
  149. if($pagenow == 'index.php' || $pagenow == 'edit-comments.php' || $pagenow == 'post.php'):
  150. $user_lang_info = $sitepress->get_language_details($this->user_language);
  151. ?>
  152. <script type="text/javascript">
  153. var icl_comment_original_language = new Array();
  154. <?php if($this->enable_replies_translation): ?>
  155. function icl_comment_reply_options(){
  156. for(i in icl_comment_original_language){
  157. oc = icl_comment_original_language[i];
  158. jQuery('#replycontainer').prepend('<input type="hidden" name="icl_comment_language_'+oc.c+'" value="'+oc.lang+'" />');
  159. }
  160. var content_ro = '<label id="icl_translate_from_lang" style="cursor:pointer">';
  161. content_ro += '<input type="hidden" name="icl_user_language" value="<?php echo $this->user_language ?>" />';
  162. content_ro += '<input style="width:15px;" type="checkbox" name="icl_translate_reply" <?php if($this->enable_replies_translation):?>checked="checked"<?php endif;?> />';
  163. content_ro += '<?php echo sprintf(__('Translate from %s', 'sitepress'),$user_lang_info['display_name']); ?>';
  164. content_ro += '</label><br clear="all" /><br />';
  165. jQuery('#replysubmit').prepend(content_ro);
  166. jQuery('input[name="icl_translate_reply"]').click(function(){
  167. jQuery(this).val(jQuery(this).attr('checked')?1:0);
  168. });
  169. jQuery('.vim-r').click(function(){
  170. var oc = jQuery(this).parent().parent().parent().parent().attr('id').split('-');
  171. if(jQuery('input[name="icl_comment_language_'+oc[1]+'"]').length){
  172. jQuery('input[name="icl_translate_reply"]').attr('checked','checked');
  173. jQuery('#icl_translate_from_lang').show();
  174. }else{
  175. jQuery('input[name="icl_translate_reply"]').removeAttr('checked');
  176. jQuery('#icl_translate_from_lang').hide();
  177. }
  178. });
  179. jQuery('.vim-q').click(function(){
  180. jQuery('#icl_translate_from_lang').hide();
  181. })
  182. }
  183. addLoadEvent(icl_comment_reply_options);
  184. <?php endif; ?>
  185. </script>
  186. <?php endif;
  187. }
  188. function comment_row_actions($actions, $comment){
  189. global $sitepress, $wpdb;
  190. $ctrid = (int)$sitepress->get_element_trid($comment->comment_ID, 'comment');
  191. $original_comment_language = $wpdb->get_row("
  192. SELECT t.language_code, lt.name
  193. FROM {$wpdb->prefix}icl_translations t
  194. JOIN {$wpdb->prefix}icl_languages_translations lt ON t.language_code = lt.language_code
  195. WHERE trid={$ctrid} AND element_type='comment' AND element_id<>{$comment->comment_ID}
  196. AND lt.display_language_code='".$sitepress->get_current_language()."'
  197. ");
  198. if(empty($original_comment_language)){
  199. return $actions;
  200. }
  201. ?>
  202. <script type="text/javascript">
  203. icl_comment_original_language.push({c:<?php echo $comment->comment_ID ?>,lang:'<?php echo $original_comment_language->language_code ?>',lang_name:'<?php echo $original_comment_language->name ?>'});
  204. </script>
  205. <div style="float:right;margin-top:4px;"><small>
  206. <?php if($this->user_language == $original_comment_language->language_code): ?>
  207. <a href="#c<?php echo $comment->comment_ID ?>" class="icl_original_comment_link"><?php _e('Back to translated version', 'sitepress') ?></a></small>
  208. <?php else: ?>
  209. <a href="#c<?php echo $comment->comment_ID ?>" class="icl_original_comment_link"><?php printf(__('Original language: %s', 'sitepress'),$original_comment_language->name) ?></a></small>
  210. <?php endif; ?>
  211. </div>
  212. <?php
  213. return $actions;
  214. }
  215. /* //not using Google's Translate API (shutting down)
  216. function show_user_options(){
  217. global $sitepress;
  218. ?>
  219. <table class="form-table">
  220. <tbody>
  221. <tr>
  222. <th><?php _e('Comments Translation:', 'sitepress') ?></th>
  223. <td>
  224. <p><label><input type="checkbox" name="icl_enable_comments_translation" id="icl_enable_comments_translation" value="1"
  225. <?php if($this->enable_comments_translation): ?> checked="checked" <?php endif?> />
  226. <?php _e('Show translated comments.', 'sitepress') ?></label></p>
  227. <span class="description"><?php _e("This enables you to see the comments translated in the language that the post was originally written in. The translation is automatic (made by a machine) so it might not be 100% accurate. It's also free.", 'sitepress')?></span>
  228. <?php /*
  229. <br />
  230. <p><label><input type="checkbox" name="icl_enable_replies_translation" id="icl_enable_replies_translation" value="1"
  231. <?php if($this->enable_replies_translation && $sitepress->get_icl_translation_enabled() && $sitepress->icl_account_configured()): ?> checked="checked" <?php endif?> <?php if(!$sitepress->get_icl_translation_enabled() || !$sitepress->icl_account_configured()) echo 'disabled="disabled"' ?> />
  232. <?php _e('Translate my replies.', 'sitepress') ?></label>
  233. <?php if(!$sitepress->get_icl_translation_enabled() || !$sitepress->icl_account_configured()): ?>
  234. <?php printf(__('To translate your replies, you need to enable the <a href="%s">professional translation</a>.','sitepress'),'http://wpml.org/?page_id=1169'); ?>
  235. <?php endif; ?>
  236. </p>
  237. <span class="description"><?php _e("When this is checked you can write comments in the post's original language. They will not be published immediately but sent to the ICanLocalize translation server and translated. Once translated they are published automatically on your blog.", 'sitepress')?></span>
  238. \*\/ ?>
  239. </td>
  240. </tr>
  241. </tbody>
  242. </table>
  243. <?php
  244. }
  245. */
  246. /* //not using Google's Translate API (shutting down)
  247. function save_user_options(){
  248. $user_id = $_POST['user_id'];
  249. if($user_id){
  250. update_user_meta($user_id,'icl_enable_comments_translation',$_POST['icl_enable_comments_translation']);
  251. update_user_meta($user_id,'icl_enable_replies_translation',$_POST['icl_enable_replies_translation']);
  252. }
  253. }
  254. */
  255. public function machine_translate($from_language, $to_language, $text){
  256. //not using Google's Translate API (shutting down)
  257. return $text;
  258. /*
  259. global $ican_google_translation_request_fail_flag;
  260. if($ican_google_translation_request_fail_flag) return '';
  261. $url = sprintf(MACHINE_TRANSLATE_API_URL, urlencode($text), $from_language, $to_language);
  262. $url = str_replace('|','%7C',$url);
  263. $client = new WP_Http();
  264. $response = $client->request($url);
  265. if(!is_wp_error($response) && ($response['response']['code']=='200')){
  266. $translation = json_decode($response['body']);
  267. $translation = $translation->responseData->translatedText;
  268. }else{
  269. $ican_google_translation_request_fail_flag = 1;
  270. $translation ='';
  271. }
  272. return $translation;
  273. */
  274. }
  275. function delete_comment_actions($comment_id){
  276. global $sitepress;
  277. $trid = $sitepress->get_element_trid($comment_id, 'comment');
  278. if($trid){
  279. $translations = $sitepress->get_element_translations($trid, 'comment');
  280. $sitepress->delete_element_translation($trid, 'comment');
  281. foreach($translations as $t){
  282. if(isset($t->element_id) && $t->element_id != $comment_id){
  283. wp_delete_comment($t->element_id);
  284. }
  285. }
  286. }
  287. }
  288. function wp_set_comment_status_actions($comment_id, $status){
  289. global $sitepress;
  290. static $ids_processed = array(); // using this for avoiding the infinite loop
  291. $trid = $sitepress->get_element_trid($comment_id, 'comment');
  292. if($trid){
  293. $translations = $sitepress->get_element_translations($trid, 'comment');
  294. foreach($translations as $t){
  295. if(isset($t->element_id) && $t->element_id != $comment_id && !in_array($t->element_id,$ids_processed)){
  296. wp_set_comment_status($t->element_id, $status);
  297. $ids_processed[] = $t->element_id;
  298. }
  299. }
  300. }
  301. }
  302. function edit_comment_actions($comment_id){
  303. // we'll use this hook ONLY for updating comments - not for new comments
  304. if($_POST['icl_translate_reply']){
  305. global $wpdb;
  306. $res = $wpdb->get_row("
  307. SELECT MD5(c.comment_content)<> ms.md5 AND ms.md5 IS NOT NULL AS updated, ms.to_language
  308. FROM {$wpdb->comments} c
  309. JOIN {$wpdb->prefix}icl_message_status ms ON c.comment_ID
  310. WHERE c.comment_ID = {$comment_id} AND ms.object_type='comment'
  311. ");
  312. if(isset($res->updated) && $res->updated){
  313. $this->send_comment_to_translation($comment_id, $res->to_language);
  314. }
  315. }
  316. }
  317. function transition_comment_status_actions($new_status, $old_status, $comment){
  318. global $sitepress, $wpdb;
  319. $comment_id = $comment->comment_ID;
  320. static $ids_processed_tr = array(); // using this for avoiding the infinite loop
  321. $trid = $sitepress->get_element_trid($comment_id, 'comment');
  322. if($trid){
  323. $translations = $sitepress->get_element_translations($trid, 'comment');
  324. foreach($translations as $t){
  325. if(isset($t->element_id) && $t->element_id != $comment_id && !in_array($t->element_id,$ids_processed_tr)){
  326. //wp_set_comment_status($t->element_id, $comment->comment_approved);
  327. $wpdb->update($wpdb->comments, array('comment_approved'=>$comment->comment_approved), array('comment_id'=>$t->element_id));
  328. $ids_processed_tr[] = $t->element_id;
  329. }
  330. }
  331. }
  332. }
  333. function comment_form_options(){
  334. global $wpdb, $post, $userdata, $sitepress;
  335. $user_lang_info = $sitepress->get_language_details($this->user_language);
  336. if(empty($post)){ //edit comment
  337. global $comment;
  338. $ctrid = $sitepress->get_element_trid($comment->comment_ID, 'comment');
  339. // original comment language
  340. $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");
  341. $post_type = $wpdb->get_var("SELECT post_type FROM {$wpdb->posts} WHERE ID='" . intval($comment->comment_post_ID) . "'");
  342. $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}'");
  343. }else{ // add new comment on front end
  344. $cur_lang = $sitepress->get_current_language();
  345. $comment_language = $sitepress->get_current_language();
  346. }
  347. $page_lang_info = $wpdb->get_var("
  348. SELECT name
  349. FROM {$wpdb->prefix}icl_languages_translations
  350. WHERE language_code='".$cur_lang."' AND display_language_code='".$this->user_language."'
  351. ");
  352. if($sitepress->have_icl_translator($this->user_language, $cur_lang)){
  353. $disabled = '';
  354. }else{
  355. $disabled = ' disabled="disabled"';
  356. }
  357. ?>
  358. <input type="hidden" name="icl_comment_language" value="<?php echo $comment_language ?>" />
  359. <?php if($this->enable_replies_translation && $userdata->user_level > 7 && $user_lang_info['code'] != $cur_lang): ?>
  360. <label style="cursor:pointer">
  361. <input type="hidden" name="icl_user_language" value="<?php echo $this->user_language ?>" />
  362. <input style="width:15px;" type="checkbox" name="icl_translate_reply" checked="checked"<?php echo $disabled ?> />
  363. <span><?php echo sprintf(__('Translate from %s into %s', 'sitepress'),$user_lang_info['display_name'], $page_lang_info); ?>
  364. <?php if($disabled): ?>
  365. <br /><small><?php printf(__('There is no translator for this language pair. <a href="%s">Details</a>.', 'sitepress'), get_option('siteurl') . '/wp-admin/admin.php?page=' . basename(ICL_PLUGIN_PATH) . '/menu/content-translation.php'); ?></small>
  366. <?php endif; ?>
  367. </span>
  368. </label>
  369. <?php endif; ?>
  370. <?php
  371. }
  372. function comments_array_filter($comments){
  373. if(defined('__comments_array_filter_runonce')){
  374. return $comments;
  375. }
  376. global $wpdb, $sitepress, $google_languages_map;
  377. define('__comments_array_filter_runonce', true);
  378. if(empty($comments)){
  379. return $comments;
  380. }
  381. foreach($comments as $c){
  382. $cids[] = $c->comment_ID;
  383. }
  384. if($this->is_visitor){
  385. // get comments in visitor's language
  386. if(!empty($cids)){
  387. $comment_ids = $wpdb->get_col("
  388. SELECT element_id
  389. FROM {$wpdb->prefix}icl_translations
  390. WHERE element_type='comment' AND element_id IN(".join(',', $cids).")
  391. AND language_code = '{$this->user_language}'
  392. ");
  393. foreach($comments as $k=>$c){
  394. if(!in_array($c->comment_ID , (array)$comment_ids)){
  395. unset($comments[$k]);
  396. }
  397. }
  398. }
  399. }elseif(!$this->enable_comments_translation){
  400. // show only original comments regardless of the user language
  401. if(!empty($cids)){
  402. $comment_ids = $wpdb->get_col("
  403. SELECT element_id
  404. FROM {$wpdb->prefix}icl_translations
  405. WHERE element_type='comment' AND element_id IN(".join(',', $cids).")
  406. AND source_language_code IS NULL
  407. ");
  408. }
  409. foreach($comments as $k=>$c){
  410. if(!in_array($c->comment_ID , (array)$comment_ids)){
  411. unset($comments[$k]);
  412. }
  413. }
  414. //filter for this language
  415. /*
  416. if(!empty($cids)){
  417. $comment_ids = $wpdb->get_col("
  418. SELECT element_id
  419. FROM {$wpdb->prefix}icl_translations
  420. WHERE element_type='comment' AND element_id IN(".join(',', $cids).")
  421. AND language_code='{$this->user_language}'
  422. ");
  423. }
  424. if($comments){
  425. $_trids = $wpdb->get_col("SELECT DISTINCT trid FROM {$wpdb->prefix}icl_translations WHERE element_type='comment' AND element_id IN(".join(",",$cids).")");
  426. $_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}'");
  427. $_utrids = array_diff($_trids, $_ttrids);
  428. if(!empty($_utrids)){
  429. $_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");
  430. }
  431. }
  432. foreach($comments as $k=>$c){
  433. if(!in_array($c->comment_ID , (array)$comment_ids) && !in_array($c->comment_ID, (array)$_untranslated_elids)){
  434. unset($comments[$k]);
  435. }
  436. }
  437. */
  438. }else{
  439. foreach($comments as $c){
  440. $comment_ids[] = $c->comment_ID;
  441. $comments_by_id[$c->comment_ID] = $c;
  442. }
  443. $trids = $wpdb->get_col("
  444. SELECT DISTINCT trid
  445. FROM {$wpdb->prefix}icl_translations
  446. WHERE element_type='comment' AND element_id IN (".join(',',$comment_ids).")
  447. ");
  448. // filter comments in the user's language
  449. $translated_comments_trids = array(0);
  450. if(!empty($trids)){
  451. $res = $wpdb->get_results("
  452. SELECT element_id, trid
  453. FROM {$wpdb->prefix}icl_translations
  454. WHERE element_type='comment' AND trid IN (".join(',',$trids).") AND language_code = '{$this->user_language}'
  455. ");
  456. foreach($res as $row){
  457. $comments_in_the_users_language[] = $row->element_id;
  458. $translated_comments_trids[] = $row->trid;
  459. }
  460. }
  461. $comments_not_translated_trids = array_diff($trids, $translated_comments_trids);
  462. if($comments_not_translated_trids){
  463. $comments_not_translated = $wpdb->get_col("
  464. SELECT element_id
  465. FROM {$wpdb->prefix}icl_translations
  466. WHERE element_type='comment' AND trid IN (".join(',',$comments_not_translated_trids).") AND language_code <> '{$this->user_language}'
  467. ");
  468. }
  469. if(!empty($comments_not_translated)){
  470. $res = $wpdb->get_results("
  471. SELECT element_id, trid, language_code
  472. FROM {$wpdb->prefix}icl_translations
  473. WHERE element_type='comment' AND element_id IN (".join(',',$comments_not_translated).")
  474. ");
  475. $wp_comments_cols = array_keys($wpdb->get_row("SELECT * FROM {$wpdb->comments} LIMIT 1", ARRAY_A));
  476. foreach($res as $original_comment){
  477. $comment_content = $comments_by_id[$original_comment->element_id]->comment_content;
  478. $machine_translation = $this->machine_translate($original_comment->language_code, $this->user_language, $comment_content);
  479. $comment_new = clone $comments_by_id[$original_comment->element_id];
  480. $comment_new->comment_content = $machine_translation;
  481. unset($comment_new->comment_ID);
  482. $wpdb->insert($wpdb->comments, array_intersect_key((array)$comment_new, array_flip($wp_comments_cols)));
  483. $new_comment_id = $wpdb->insert_id;
  484. $sitepress->set_element_language_details($new_comment_id, 'comment', $original_comment->trid, $this->user_language);
  485. $comment_new->comment_ID = $new_comment_id;
  486. if($original_comment_parent = $comments_by_id[$original_comment->element_id]->comment_parent){
  487. // check for the comment parent in the user language
  488. $cptrid = $sitepress->get_element_trid($original_comment_parent, 'comment');
  489. $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}'");
  490. $wpdb->update($wpdb->comments, array('comment_parent'=>$comment_new->comment_parent), array('comment_ID'=>$new_comment_id));
  491. }
  492. if(!$machine_translation){
  493. $nonce = wp_create_nonce('machine-translation-failed'.$new_comment_id);
  494. $comment_new->comment_content = '<i>' . sprintf(__('Machine translation failed. <a%s>retry</a>','sitepress'), ' onclick="icl_retry_mtr(this)" id="icl_retry_mtr_'.$comment_new->comment_ID.'_'.$nonce.'" href="#"') . '</i>';
  495. $wpdb->update($wpdb->comments, array('comment_content'=>$comment_new->comment_content), array('comment_ID'=>$new_comment_id));
  496. }
  497. $comments_in_the_users_language[] = $new_comment_id;
  498. $comments[] = $comment_new;
  499. }
  500. }
  501. //filter out comments in other languages than the user's
  502. foreach((array)$comments as $k=>$c){
  503. if(!in_array($c->comment_ID , (array)$comments_in_the_users_language)){
  504. unset($comments[$k]);
  505. }
  506. }
  507. }
  508. return array_values($comments);
  509. }
  510. function use_comments_array_filter(){
  511. global $comments;
  512. $comments = $this->comments_array_filter($comments);
  513. }
  514. function comment_feed_join($join){
  515. global $wpdb, $sitepress;
  516. $lang = $this->enable_comments_translation ? $this->user_language : $sitepress->get_current_language();
  517. $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}'";
  518. return $join;
  519. }
  520. function filter_queries($sql){
  521. global $pagenow, $wpdb;
  522. static $_untranslated_elids;
  523. if($pagenow == 'index.php'){
  524. if(preg_match('#SELECT \* FROM (.+)comments ORDER BY comment_date_gmt DESC LIMIT ([0-9]+), ([0-9]+)#i',$sql,$matches)){
  525. $res = mysql_query($sql);
  526. while($row = mysql_fetch_object($res)){
  527. $comments[] = $row;
  528. }
  529. if(!$this->enable_comments_translation && $comments){
  530. // show only original comments regardless of the user language
  531. foreach($comments as $c){
  532. $cids[] = $c->comment_ID;
  533. }
  534. $comment_ids = array(0);
  535. if(!empty($cids)){
  536. $comment_ids = $wpdb->get_col("
  537. SELECT element_id
  538. FROM {$wpdb->prefix}icl_translations
  539. WHERE element_type='comment' AND element_id IN(".join(',', $cids).")
  540. AND source_language_code IS NULL
  541. ");
  542. if(!empty($comment_ids)){
  543. $sql = "SELECT * FROM {$matches[1]}comments c
  544. WHERE c.comment_ID IN (".join(',',$comment_ids).")
  545. ORDER BY c.comment_date_gmt DESC LIMIT {$matches[2]}, {$matches[3]}";
  546. }
  547. }
  548. }else{
  549. $this->comments_array_filter($comments);
  550. unset($comments);
  551. $sql = "
  552. SELECT * FROM {$matches[1]}comments c
  553. LEFT JOIN {$matches[1]}icl_translations t ON t.element_id=c.comment_ID
  554. WHERE
  555. (t.element_type='comment' AND t.language_code='{$this->user_language}')
  556. ORDER BY c.comment_date_gmt DESC LIMIT {$matches[2]}, {$matches[3]}";
  557. }
  558. }
  559. }elseif( isset($_POST['action']) && $_POST['action']=='get-comments' && isset($_POST['mode']) && $_POST['mode']=='single'){
  560. global $sitepress;
  561. 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)){
  562. $post_type = $wpdb->get_var("SELECT post_type FROM {$wpdb->posts} WHERE ID='{$_POST['post_ID']}'");
  563. $res = mysql_query("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_id={$_POST['post_ID']} AND element_type='post_{$post_type}'");
  564. $row = mysql_fetch_row($res);
  565. $c_language = $row[0];
  566. if($this->enable_comments_translation){
  567. $res = $wpdb->get_results("
  568. SELECT element_id, trid FROM {$wpdb->prefix}icl_translations t
  569. JOIN {$wpdb->comments} c ON t.element_id = c.comment_ID AND comment_POST_ID = {$_POST['post_ID']}
  570. WHERE t.element_type='comment' AND language_code='{$c_language}'");
  571. foreach($res as $r){
  572. $trids_orig[$r->trid] = $r->element_id;
  573. }
  574. $res = $wpdb->get_results("
  575. SELECT element_id, trid FROM {$wpdb->prefix}icl_translations t
  576. JOIN {$wpdb->comments} c ON t.element_id = c.comment_ID AND comment_POST_ID = {$_POST['post_ID']}
  577. WHERE t.element_type='comment' AND language_code='{$this->user_language}'");
  578. foreach($res as $r){
  579. $trids_tr[$r->trid] = $r->element_id;
  580. }
  581. $wp_comments_cols = array_keys($wpdb->get_row("SELECT * FROM {$wpdb->comments} LIMIT 1", ARRAY_A));
  582. foreach($trids_orig as $o_trid=>$o_cid){
  583. if(!isset($trids_tr[$o_trid])){
  584. $original_comment = get_comment($trids_orig[$o_trid]);
  585. $machine_translation = $this->machine_translate($c_language, $this->user_language, $original_comment->comment_content);
  586. $comment_new = clone $original_comment;
  587. $comment_new->comment_content = $machine_translation;
  588. unset($comment_new->comment_ID);
  589. $wpdb->insert($wpdb->comments, array_intersect_key((array)$comment_new, array_flip($wp_comments_cols)));
  590. $new_comment_id = $wpdb->insert_id;
  591. $sitepress->set_element_language_details($new_comment_id, 'comment', $o_trid, $this->user_language);
  592. if($original_comment->comment_parent){
  593. $cptrid = $sitepress->get_element_trid($original_comment->comment_parent, 'comment');
  594. $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}'");
  595. $wpdb->update($wpdb->comments, array('comment_parent'=>$comment_new->comment_parent), array('comment_ID'=>$new_comment_id));
  596. }
  597. if(!$machine_translation){
  598. $nonce = wp_create_nonce('machine-translation-failed'.$new_comment_id);
  599. $comment_new->comment_content = '<i>' . sprintf(__('Machine translation failed. <a%s>retry</a>','sitepress'), ' onclick="icl_retry_mtr(this)" id="icl_retry_mtr_'.$comment_new->comment_ID.'_'.$nonce.'" href="#"') . '</i>';
  600. $wpdb->update($wpdb->comments, array('comment_content'=>$comment_new->comment_content), array('comment_ID'=>$new_comment_id));
  601. }
  602. }
  603. }
  604. $c_language = $this->user_language;
  605. }
  606. $sql = "
  607. SELECT *
  608. FROM {$matches[1]}comments c USE INDEX (comment_date_gmt)
  609. JOIN {$matches[1]}icl_translations t ON t.element_id=c.comment_ID
  610. WHERE
  611. t.element_type='comment' AND t.language_code='{$c_language}' AND
  612. ( comment_approved = '0' OR comment_approved = '1' ) AND
  613. comment_post_ID = '{$matches[2]}'
  614. ORDER BY comment_date_gmt ASC
  615. LIMIT {$matches[3]}, {$matches[4]}";
  616. }
  617. }
  618. return $sql;
  619. }
  620. /*
  621. function comment_feed_where($where){
  622. return $where;
  623. }
  624. */
  625. function wp_insert_comment($comment_id, $comment_data){
  626. global $sitepress, $wpdb;
  627. if(isset($comment_data->comment_post_ID)){
  628. $comment_post_ID = $comment_data->comment_post_ID;
  629. }elseif(isset($_POST['comment_post_ID'])){
  630. $comment_post_ID = $_POST['comment_post_ID'];
  631. }else{
  632. return false;
  633. }
  634. $post_type = $wpdb->get_var($wpdb->prepare("SELECT post_type FROM {$wpdb->posts} WHERE ID=%d", $comment_post_ID));
  635. if(isset($_POST['icl_user_language'])){
  636. if($_POST['icl_translate_reply']){
  637. $lang = $_POST['icl_user_language'];
  638. // send the comment to translation
  639. $this->send_comment_to_translation($comment_id, $wpdb->get_var($wpdb->prepare("
  640. SELECT language_code FROM {$wpdb->prefix}icl_translations
  641. WHERE element_type='post_{$post_type}' AND element_id=%d", $comment_post_ID)));
  642. }else{
  643. //$lang = $_POST['icl_comment_language'];
  644. $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));
  645. // sync comment parent
  646. // look for comment parent in original language and set for the comment that's been added
  647. if(isset($_POST['comment_parent'])){
  648. $comment_parent = $_POST['comment_parent'];
  649. }else{
  650. $comment_parent = $_POST['comment_ID'];
  651. }
  652. if($comment_parent){
  653. $ctrid = (int)$sitepress->get_element_trid($comment_parent,'comment');
  654. $original_comment_parent = $wpdb->get_var("
  655. SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid={$ctrid}
  656. AND element_type='comment' AND language_code='{$lang}'");
  657. $wpdb->update($wpdb->comments, array('comment_parent'=>$original_comment_parent), array('comment_ID'=>$comment_id));
  658. }
  659. }
  660. }else{
  661. $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));
  662. if(!$lang){
  663. $lang = $this->user_language; // just in case
  664. }
  665. }
  666. if(!$lang){
  667. $lang = $this->user_language;
  668. }
  669. $translation_id = $sitepress->set_element_language_details($comment_id, 'comment', null, $lang);
  670. }
  671. function send_comment_to_translation($comment_id, $to_language){
  672. global $wpdb, $sitepress_settings, $sitepress;
  673. $iclq = new ICanLocalizeQuery($sitepress_settings['site_id'], $sitepress_settings['access_key']);
  674. $from_lang = $sitepress->get_language_details($this->user_language);
  675. $to_lang = $sitepress->get_language_details($to_language);
  676. $from_lang_server = ICL_Pro_Translation::server_languages_map($from_lang['english_name']);
  677. $to_lang_server = ICL_Pro_Translation::server_languages_map($to_lang['english_name']);
  678. $body = $wpdb->get_var("SELECT comment_content FROM {$wpdb->comments} WHERE comment_ID={$comment_id}");
  679. $rid = $iclq->cms_create_message($body, $from_lang_server, $to_lang_server);
  680. if($rid > 0){
  681. // does this comment already exist in the messages status queue?
  682. $msid = $wpdb->get_var("SELECT id FROM {$wpdb->prefix}icl_message_status WHERE object_type='comment' AND object_id={$comment_id}");
  683. if($msid){
  684. $wpdb->update($wpdb->prefix.'icl_message_status',
  685. array('rid'=>$rid, 'md5' => md5($body), 'status' => MESSAGE_TRANSLATION_IN_PROGRESS),
  686. array('id' => $msid)
  687. );
  688. }else{
  689. $wpdb->insert($wpdb->prefix.'icl_message_status', array(
  690. 'rid' => $rid,
  691. 'object_id' => $comment_id,
  692. 'from_language' => $this->user_language,
  693. 'to_language' => $to_language,
  694. 'md5' => md5($body),
  695. 'object_type' => 'comment',
  696. 'status' => MESSAGE_TRANSLATION_IN_PROGRESS
  697. ));
  698. }
  699. }
  700. }
  701. function add_comment_translation($object_id, $to_language, $translation){
  702. global $wpdb, $sitepress, $sitepress_settings;
  703. $original_comment = $wpdb->get_row("
  704. SELECT *
  705. FROM {$wpdb->comments} WHERE comment_ID = {$object_id}
  706. ", ARRAY_A);
  707. $new_comment = $original_comment;
  708. //sync comment parent
  709. if($original_comment['comment_parent']){
  710. $ctrid = (int)$sitepress->get_element_trid($original_comment['comment_parent'],'comment');
  711. $new_comment['comment_parent'] = (int) $wpdb->get_var("
  712. SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid={$ctrid}
  713. AND element_type='comment' AND language_code='{$to_language}'");
  714. }
  715. $original_comment_id = $original_comment['comment_ID'];
  716. unset($original_comment);
  717. $new_comment['comment_content'] = $translation;
  718. unset($new_comment['comment_ID']);
  719. //remove_action('wp_insert_comment', array($this, 'wp_insert_comment'));
  720. //check whether a translation for this comment in this language already exists
  721. $trid = $sitepress->get_element_trid($original_comment_id, 'comment');
  722. $existing_comment_id = $wpdb->get_var("
  723. SELECT element_id FROM {$wpdb->prefix}icl_translations
  724. WHERE trid={$trid} AND element_type='comment' AND language_code='{$to_language}'");
  725. if(!$existing_comment_id){
  726. remove_action('wp_insert_comment', array($this, 'wp_insert_comment'));
  727. $new_id = wp_insert_comment($new_comment);
  728. add_action('wp_insert_comment', array($this, 'wp_insert_comment'), 10, 2);
  729. }else{
  730. $new_id = $new_comment['comment_ID'] = $existing_comment_id;
  731. remove_action('transition_comment_status', array($this, 'transition_comment_status_actions'));
  732. wp_update_comment($new_comment);
  733. }
  734. $sitepress->set_element_language_details($new_id, 'comment', $trid, $to_language);
  735. return 1; //success
  736. }
  737. function add_custom_xmlrpc_methods($methods){
  738. //$methods['icanlocalize.notify_comment_translation'] = array($this, 'add_comment_translation');
  739. return $methods;
  740. }
  741. function comment_text_filter($comment_text){
  742. global $sitepress, $comment, $wp_query, $wpdb;
  743. static $comment_ids, $comment_originals, $page_language, $translation_status;
  744. //if($this->enable_comments_translation && $this->user_language != $sitepress->get_current_language() ){
  745. if($this->enable_comments_translation){
  746. // run this block once
  747. if(empty($comment_ids)){
  748. $page_language = $wpdb->get_var("
  749. SELECT name
  750. FROM {$wpdb->prefix}icl_languages_translations
  751. WHERE language_code='".$sitepress->get_current_language()."' AND display_language_code='".$this->user_language."'
  752. ");
  753. foreach($wp_query->comments as $c){
  754. $comment_ids[] = (int)$c->comment_ID;
  755. }
  756. $res = $wpdb->get_results("
  757. SELECT t.element_id, t.trid, ms.status
  758. FROM {$wpdb->prefix}icl_translations t
  759. LEFT JOIN {$wpdb->prefix}icl_message_status ms ON t.element_id=ms.object_id
  760. 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)");
  761. foreach($res as $row){
  762. $tridsmap[$row->trid] = $row->element_id;
  763. if($row->status){
  764. $translation_status[$row->element_id] = $row->status;
  765. }
  766. }
  767. if(!empty($tridsmap)){
  768. $res = $wpdb->get_results("
  769. SELECT t.trid, c.comment_content
  770. FROM {$wpdb->prefix}icl_translations t
  771. JOIN {$wpdb->comments} c ON t.element_id = c.comment_ID
  772. WHERE t.trid IN(".join(',',array_keys($tridsmap)).") AND t.element_type='comment' AND t.language_code='".$sitepress->get_current_language()."'");
  773. foreach($res as $row){
  774. $comment_originals[$tridsmap[$row->trid]] = $row->comment_content;
  775. }
  776. }
  777. }
  778. $str = '';
  779. if(isset($comment_originals[$comment->comment_ID])){
  780. $str .= '<div style="font-weight:normal;display:none;border:1px dotted #aaa;background-color:#f0f0f0;padding:4px;margin-bottom:0;" id="icl_olc_'.$comment->comment_ID.'">' . htmlspecialchars($comment_originals[$comment->comment_ID]) . '</div>';
  781. }
  782. if($translation_status[$comment->comment_ID] || isset($comment_originals[$comment->comment_ID])){
  783. $str .= '<p style="margin-top:1px;">';
  784. }
  785. if(isset($translation_status[$comment->comment_ID])){
  786. if($translation_status[$comment->comment_ID] == MESSAGE_TRANSLATION_IN_PROGRESS){
  787. $str .= '<i>' . __('Translation in progress', 'sitepress' ) . '</i> ';
  788. }elseif($translation_status[$comment->comment_ID] == MESSAGE_TRANSLATION_COMPLETE){
  789. $str .= '<i>' . __('Translation complete', 'sitepress' ) . '</i> ';
  790. }
  791. }
  792. if(isset($comment_originals[$comment->comment_ID])){
  793. $str .= '<i><a href="#" onclick="var iclcst = document.getElementById(\'icl_olc_'.$comment->comment_ID.'\').style; iclcst.display = iclcst.display == \'block\' ? \'none\' :\'block\' ;return false;">'. sprintf(__('Comment in %s', 'sitepress'),$page_language).'</a></i>';
  794. }
  795. if($translation_

Large files files are truncated, but you can click here to view the full file