PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/acipriani/madeinapulia.com
PHP | 1050 lines | 787 code | 141 blank | 122 comment | 135 complexity | 908e0ff4a667b332e217945a0db939f5 MD5 | raw file
Possible License(s): GPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-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. $c = false;
  48. if ( isset( $_GET[ 'c' ] ) ) {
  49. $c = intval( $_GET[ 'c' ] );
  50. } elseif ( isset( $_POST[ 'c' ] ) ) {
  51. $c = intval( $_POST[ 'c' ] );
  52. }
  53. if ( $c !== false ) {
  54. $row = $wpdb->get_row( "
  55. SELECT c.user_id, t.language_code
  56. FROM {$wpdb->comments} c JOIN {$wpdb->prefix}icl_translations t ON c.comment_ID = t.element_id AND element_type='comment'
  57. WHERE c.comment_ID=" . $c );
  58. }
  59. if ( $row && isset( $row->user_id ) && isset( $row->language_code ) ) {
  60. $comment_author = $row->user_id;
  61. $comment_lang = $row->language_code;
  62. if ( $current_user->data->ID == $comment_author && $comment_lang == $this->user_language ) {
  63. add_action( 'admin_head', array( $this, 'admin_head_actions' ) );
  64. }
  65. }
  66. }
  67. add_action('edit_comment', array($this, 'edit_comment_actions'));
  68. add_action('comment_form', array($this, 'comment_form_options'));
  69. //add_action('comment_post', array($this, 'comment_post'));
  70. add_action('wp_insert_comment', array($this, 'wp_insert_comment'), 10, 2);
  71. if(defined('WP_ADMIN') && $this->enable_comments_translation){
  72. add_filter('comment_row_actions', array($this,'comment_row_actions'),1, 2);
  73. }
  74. if(defined('WP_ADMIN')){
  75. add_filter('comment_text', array($this, 'comment_text_filter_admin'));
  76. }else{
  77. add_filter('comment_text', array($this, 'comment_text_filter'));
  78. }
  79. add_filter('xmlrpc_methods',array($this, 'add_custom_xmlrpc_methods'));
  80. add_filter('get_comments_number', array($this, 'get_comments_number_filter'));
  81. global $wpml_add_message_translation_callbacks;
  82. $wpml_add_message_translation_callbacks['comment'][] = array($this, 'add_comment_translation');
  83. if(isset($_GET['retry_mtr'])){
  84. global $wpdb;
  85. $nonce = wp_create_nonce('machine-translation-failed'.$_GET['retry_mtr']);
  86. if($_GET['nonce']==$nonce){
  87. $delete_query = "DELETE FROM {$wpdb->comments} WHERE comment_ID=%d";
  88. $delete_query_prepared = $wpdb->prepare($delete_query, $_GET['retry_mtr']);
  89. $wpdb->query($delete_query_prepared);
  90. $delete_query = "DELETE FROM {$wpdb->prefix}icl_translations WHERE element_type='comment' AND element_id=%d";
  91. $delete_query_prepared = $wpdb->prepare($delete_query, $_GET['retry_mtr']);
  92. $wpdb->query($delete_query_prepared);
  93. if(defined('WP_ADMIN')){
  94. wp_redirect(rtrim(preg_replace('@retry_mtr=([0-9]+)&nonce=([0-9a-z]+)@','',$_SERVER['REQUEST_URI']),'?'));
  95. }else{
  96. add_action('template_redirect', array($this, '__reload_page'));
  97. }
  98. }
  99. }
  100. require_once ICL_PLUGIN_PATH . '/inc/cache.php';
  101. $this->icl_comment_count_cache = new icl_cache();
  102. add_filter('views_edit-comments', array($this, 'pending_by_language'));
  103. }
  104. function pending_by_language( $views ) {
  105. global $wpdb, $sitepress;
  106. $current_language = $sitepress->get_current_language();
  107. if ( isset( $views[ 'moderated' ] ) ) {
  108. preg_match( '#<span class="pending-count">([0-9,]+)</span>#', $views[ 'moderated' ], $matches );
  109. if ( $matches[ 1 ] > 0 ) {
  110. $results_sql = "
  111. SELECT tr.language_code, COUNT(tr.element_id) AS c
  112. FROM {$wpdb->comments} c
  113. JOIN {$wpdb->prefix}icl_translations tr ON tr.element_id = c.comment_ID
  114. WHERE c.comment_approved = '0' AND tr.element_type = 'comment'
  115. GROUP BY tr.language_code
  116. HAVING c > 0
  117. ";
  118. $results_prepared = $results_sql;
  119. $results = $wpdb->get_results( $results_prepared );
  120. if ( !empty( $results ) ) {
  121. $language_break = array();
  122. foreach ( $results as $r ) {
  123. $language_details = $sitepress->get_language_details( $r->language_code );
  124. $language_break[ ] = sprintf( '<a %s href="%s">%s</a>(%d)', $current_language == $r->language_code ? ' class="current"' : '', admin_url( 'edit-comments.php?comment_status=moderated&lang=' .
  125. $r->language_code ), $language_details[ 'display_name' ], $r->c );
  126. }
  127. $views[ 'moderated' ] .= ' - ' . join( ', ', $language_break );
  128. }
  129. }
  130. }
  131. return $views;
  132. }
  133. function __reload_page(){
  134. wp_redirect(get_permalink());
  135. }
  136. function admin_head_actions(){
  137. add_meta_box('comment', __('Translate', 'sitepress'), array($this, 'comment_form_options'), 'comment', 'normal', 'high', array(1));
  138. }
  139. function js_scripts_setup(){
  140. global $pagenow, $sitepress;
  141. if($pagenow == 'index.php' || $pagenow == 'edit-comments.php' || $pagenow == 'post.php'):
  142. $user_lang_info = $sitepress->get_language_details($this->user_language);
  143. ?>
  144. <script type="text/javascript">
  145. var icl_comment_original_language = [];
  146. <?php if($this->enable_replies_translation): ?>
  147. function icl_comment_reply_options(){
  148. var oc;
  149. icl_comment_original_language.forEach(function (item) {
  150. oc = item;
  151. jQuery('#replycontainer').prepend('<input type="hidden" name="icl_comment_language_' + oc.c + '" value="' + oc.lang + '" />');
  152. });
  153. var content_ro = '<label id="icl_translate_from_lang" style="cursor:pointer">';
  154. content_ro += '<input type="hidden" name="icl_user_language" value="<?php echo $this->user_language ?>" />';
  155. content_ro += '<input style="width:15px;" type="checkbox" name="icl_translate_reply" <?php if($this->enable_replies_translation):?>checked="checked"<?php endif;?> />';
  156. content_ro += '<?php echo sprintf(__('Translate from %s', 'sitepress'),$user_lang_info['display_name']); ?>';
  157. content_ro += '</label><br clear="all" /><br />';
  158. jQuery('#replysubmit').prepend(content_ro);
  159. jQuery('input[name="icl_translate_reply"]').click(function(){
  160. jQuery(this).val(jQuery(this).attr('checked')?1:0);
  161. });
  162. jQuery('.vim-r').click(function(){
  163. var oc = jQuery(this).parent().parent().parent().parent().attr('id').split('-');
  164. if(jQuery('input[name="icl_comment_language_'+oc[1]+'"]').length){
  165. jQuery('input[name="icl_translate_reply"]').attr('checked','checked');
  166. jQuery('#icl_translate_from_lang').show();
  167. }else{
  168. jQuery('input[name="icl_translate_reply"]').removeAttr('checked');
  169. jQuery('#icl_translate_from_lang').hide();
  170. }
  171. });
  172. jQuery('.vim-q').click(function(){
  173. jQuery('#icl_translate_from_lang').hide();
  174. })
  175. }
  176. addLoadEvent(icl_comment_reply_options);
  177. <?php endif; ?>
  178. </script>
  179. <?php endif;
  180. }
  181. function comment_row_actions($actions, $comment){
  182. global $sitepress, $wpdb;
  183. $ctrid = (int)$sitepress->get_element_trid($comment->comment_ID, 'comment');
  184. $original_comment_language = $wpdb->get_row("
  185. SELECT t.language_code, lt.name
  186. FROM {$wpdb->prefix}icl_translations t
  187. JOIN {$wpdb->prefix}icl_languages_translations lt ON t.language_code = lt.language_code
  188. WHERE trid={$ctrid} AND element_type='comment' AND element_id<>{$comment->comment_ID}
  189. AND lt.display_language_code='".$sitepress->get_current_language()."'
  190. ");
  191. if(empty($original_comment_language)){
  192. return $actions;
  193. }
  194. ?>
  195. <script type="text/javascript">
  196. 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 ?>'});
  197. </script>
  198. <div style="float:right;margin-top:4px;">
  199. <?php if($this->user_language == $original_comment_language->language_code): ?>
  200. <small><a href="#c<?php echo $comment->comment_ID ?>" class="icl_original_comment_link"><?php _e('Back to translated version', 'sitepress') ?></a></small>
  201. <?php else: ?>
  202. <small> <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>
  203. <?php endif; ?>
  204. </div>
  205. <?php
  206. return $actions;
  207. }
  208. /* //not using Google's Translate API (shutting down)
  209. function show_user_options(){
  210. global $sitepress;
  211. ?>
  212. <table class="form-table">
  213. <tbody>
  214. <tr>
  215. <th><?php _e('Comments Translation:', 'sitepress') ?></th>
  216. <td>
  217. <p><label><input type="checkbox" name="icl_enable_comments_translation" id="icl_enable_comments_translation" value="1"
  218. <?php if($this->enable_comments_translation): ?> checked="checked" <?php endif?> />
  219. <?php _e('Show translated comments.', 'sitepress') ?></label></p>
  220. <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>
  221. <?php /*
  222. <br />
  223. <p><label><input type="checkbox" name="icl_enable_replies_translation" id="icl_enable_replies_translation" value="1"
  224. <?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"' ?> />
  225. <?php _e('Translate my replies.', 'sitepress') ?></label>
  226. <?php if(!$sitepress->get_icl_translation_enabled() || !$sitepress->icl_account_configured()): ?>
  227. <?php printf(__('To translate your replies, you need to enable the <a href="%s">professional translation</a>.','sitepress'),'https://wpml.org/?page_id=1169'); ?>
  228. <?php endif; ?>
  229. </p>
  230. <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>
  231. \*\/ ?>
  232. </td>
  233. </tr>
  234. </tbody>
  235. </table>
  236. <?php
  237. }
  238. */
  239. /* //not using Google's Translate API (shutting down)
  240. function save_user_options(){
  241. $user_id = $_POST['user_id'];
  242. if($user_id){
  243. update_user_meta($user_id,'icl_enable_comments_translation',$_POST['icl_enable_comments_translation']);
  244. update_user_meta($user_id,'icl_enable_replies_translation',$_POST['icl_enable_replies_translation']);
  245. }
  246. }
  247. */
  248. public function machine_translate($from_language, $to_language, $text){
  249. //not using Google's Translate API (shutting down)
  250. return $text;
  251. /*
  252. global $ican_google_translation_request_fail_flag;
  253. if($ican_google_translation_request_fail_flag) return '';
  254. $url = sprintf(MACHINE_TRANSLATE_API_URL, urlencode($text), $from_language, $to_language);
  255. $url = str_replace('|','%7C',$url);
  256. $client = new WP_Http();
  257. $response = $client->request($url);
  258. if(!is_wp_error($response) && ($response['response']['code']=='200')){
  259. $translation = json_decode($response['body']);
  260. $translation = $translation->responseData->translatedText;
  261. }else{
  262. $ican_google_translation_request_fail_flag = 1;
  263. $translation ='';
  264. }
  265. return $translation;
  266. */
  267. }
  268. function delete_comment_actions( $comment_id ) {
  269. global $sitepress;
  270. $lang_details = $sitepress->get_element_language_details( $comment_id, 'comment' );
  271. if ( $lang_details && isset( $lang_details->trid ) && isset( $lang_details->source_language_code ) ) {
  272. $trid = $lang_details->trid;
  273. if ( $lang_details->source_language_code ) {
  274. $sitepress->delete_element_translation( $trid, 'comment', $lang_details->language_code );
  275. } else {
  276. $translations = $sitepress->get_element_translations( $lang_details->trid, 'comment' );
  277. $sitepress->delete_element_translation( $trid, 'comment' );
  278. foreach ( $translations as $t ) {
  279. if ( isset( $t->element_id ) && $t->element_id != $comment_id ) {
  280. remove_action( 'delete_comment', array( $this, 'delete_comment_actions' ) );
  281. wp_delete_comment( $t->element_id );
  282. add_action( 'delete_comment', array( $this, 'delete_comment_actions' ) );
  283. }
  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(isset($_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. $current_language = $sitepress->get_current_language();
  337. if(empty($post)){ //edit comment
  338. global $comment;
  339. $ctrid = $sitepress->get_element_trid($comment->comment_ID, 'comment');
  340. // original comment language
  341. $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");
  342. $post_type = $wpdb->get_var("SELECT post_type FROM {$wpdb->posts} WHERE ID='" . intval($comment->comment_post_ID) . "'");
  343. $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}'");
  344. }else{ // add new comment on front end
  345. $cur_lang = $current_language;
  346. $comment_language = $current_language;
  347. }
  348. $page_lang_info = $wpdb->get_var("
  349. SELECT name
  350. FROM {$wpdb->prefix}icl_languages_translations
  351. WHERE language_code='".$cur_lang."' AND display_language_code='".$this->user_language."'
  352. ");
  353. if($sitepress->have_icl_translator($this->user_language, $cur_lang)){
  354. $disabled = '';
  355. }else{
  356. $disabled = ' disabled="disabled"';
  357. }
  358. ?>
  359. <input type="hidden" name="icl_comment_language" value="<?php echo $comment_language ?>" />
  360. <?php if($this->enable_replies_translation && $userdata->user_level > 7 && $user_lang_info['code'] != $cur_lang): ?>
  361. <label style="cursor:pointer">
  362. <input type="hidden" name="icl_user_language" value="<?php echo $this->user_language ?>" />
  363. <input style="width:15px;" type="checkbox" name="icl_translate_reply" checked="checked"<?php echo $disabled ?> />
  364. <span><?php echo sprintf(__('Translate from %s into %s', 'sitepress'),$user_lang_info['display_name'], $page_lang_info); ?>
  365. <?php if($disabled): ?>
  366. <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>
  367. <?php endif; ?>
  368. </span>
  369. </label>
  370. <?php endif; ?>
  371. <?php
  372. }
  373. function comments_array_filter($comments){
  374. if(defined('__comments_array_filter_runonce')){
  375. return $comments;
  376. }
  377. global $wpdb, $sitepress;
  378. define('__comments_array_filter_runonce', true);
  379. if(empty($comments)){
  380. return $comments;
  381. }
  382. foreach($comments as $c){
  383. $cids[] = $c->comment_ID;
  384. }
  385. if($this->is_visitor){
  386. // get comments in visitor's language
  387. if(!empty($cids)){
  388. $comment_ids = $wpdb->get_col("
  389. SELECT element_id
  390. FROM {$wpdb->prefix}icl_translations
  391. WHERE element_type='comment' AND element_id IN(".join(',', $cids).")
  392. AND language_code = '{$this->user_language}'
  393. ");
  394. foreach($comments as $k=>$c){
  395. if(!in_array($c->comment_ID , (array)$comment_ids)){
  396. unset($comments[$k]);
  397. }
  398. }
  399. }
  400. }elseif(!$this->enable_comments_translation){
  401. // show only original comments regardless of the user language
  402. $comment_ids = array();
  403. if(!empty($cids)){
  404. $comment_ids = $wpdb->get_col("
  405. SELECT element_id
  406. FROM {$wpdb->prefix}icl_translations
  407. WHERE element_type='comment' AND element_id IN(".join(',', $cids).")
  408. AND source_language_code IS NULL
  409. ");
  410. }
  411. foreach($comments as $k=>$c){
  412. if(!in_array($c->comment_ID , $comment_ids)){
  413. unset($comments[$k]);
  414. }
  415. }
  416. //filter for this language
  417. /*
  418. if(!empty($cids)){
  419. $comment_ids = $wpdb->get_col("
  420. SELECT element_id
  421. FROM {$wpdb->prefix}icl_translations
  422. WHERE element_type='comment' AND element_id IN(".join(',', $cids).")
  423. AND language_code='{$this->user_language}'
  424. ");
  425. }
  426. if($comments){
  427. $_trids = $wpdb->get_col("SELECT DISTINCT trid FROM {$wpdb->prefix}icl_translations WHERE element_type='comment' AND element_id IN(".join(",",$cids).")");
  428. $_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}'");
  429. $_utrids = array_diff($_trids, $_ttrids);
  430. if(!empty($_utrids)){
  431. $_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");
  432. }
  433. }
  434. foreach($comments as $k=>$c){
  435. if(!in_array($c->comment_ID , (array)$comment_ids) && !in_array($c->comment_ID, (array)$_untranslated_elids)){
  436. unset($comments[$k]);
  437. }
  438. }
  439. */
  440. }else{
  441. $comment_ids = array();
  442. $comments_by_id = array();
  443. foreach($comments as $c){
  444. $comment_ids[] = $c->comment_ID;
  445. $comments_by_id[$c->comment_ID] = $c;
  446. }
  447. $trids = $wpdb->get_col("
  448. SELECT DISTINCT trid
  449. FROM {$wpdb->prefix}icl_translations
  450. WHERE element_type='comment' AND element_id IN (".join(',',$comment_ids).")
  451. ");
  452. // filter comments in the user's language
  453. $comments_in_the_users_language = array();
  454. $translated_comments_trids = array();
  455. if(!empty($trids)){
  456. $res = $wpdb->get_results( $wpdb->prepare("
  457. SELECT element_id, trid
  458. FROM {$wpdb->prefix}icl_translations
  459. WHERE element_type='comment' AND trid IN (".join(',',$trids).") AND language_code = %s
  460. ", $this->user_language));
  461. foreach($res as $row){
  462. $comments_in_the_users_language[] = $row->element_id;
  463. $translated_comments_trids[] = $row->trid;
  464. }
  465. }
  466. $comments_not_translated_trids = array_diff($trids, $translated_comments_trids);
  467. if($comments_not_translated_trids){
  468. $comments_not_translated = $wpdb->get_col("
  469. SELECT element_id
  470. FROM {$wpdb->prefix}icl_translations
  471. WHERE element_type='comment' AND trid IN (".join(',',$comments_not_translated_trids).") AND language_code <> '{$this->user_language}'
  472. ");
  473. }
  474. if(!empty($comments_not_translated)){
  475. $res = $wpdb->get_results("
  476. SELECT element_id, trid, language_code
  477. FROM {$wpdb->prefix}icl_translations
  478. WHERE element_type='comment' AND element_id IN (".join(',',$comments_not_translated).")
  479. ");
  480. $wp_comments_cols = array_keys($wpdb->get_row("SELECT * FROM {$wpdb->comments} LIMIT 1", ARRAY_A));
  481. foreach($res as $original_comment){
  482. $comment_content = $comments_by_id[$original_comment->element_id]->comment_content;
  483. $machine_translation = $this->machine_translate($original_comment->language_code, $this->user_language, $comment_content);
  484. $comment_new = clone $comments_by_id[$original_comment->element_id];
  485. $comment_new->comment_content = $machine_translation;
  486. unset($comment_new->comment_ID);
  487. $wpdb->insert($wpdb->comments, array_intersect_key((array)$comment_new, array_flip($wp_comments_cols)));
  488. $new_comment_id = $wpdb->insert_id;
  489. $sitepress->set_element_language_details($new_comment_id, 'comment', $original_comment->trid, $this->user_language);
  490. $comment_new->comment_ID = $new_comment_id;
  491. if($original_comment_parent = $comments_by_id[$original_comment->element_id]->comment_parent){
  492. // check for the comment parent in the user language
  493. $cptrid = $sitepress->get_element_trid($original_comment_parent, 'comment');
  494. $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}'");
  495. $wpdb->update($wpdb->comments, array('comment_parent'=>$comment_new->comment_parent), array('comment_ID'=>$new_comment_id));
  496. }
  497. if(!$machine_translation){
  498. $nonce = wp_create_nonce('machine-translation-failed'.$new_comment_id);
  499. $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>';
  500. $wpdb->update($wpdb->comments, array('comment_content'=>$comment_new->comment_content), array('comment_ID'=>$new_comment_id));
  501. }
  502. $comments_in_the_users_language[] = $new_comment_id;
  503. $comments[] = $comment_new;
  504. }
  505. }
  506. //filter out comments in other languages than the user's
  507. foreach((array)$comments as $k=>$c){
  508. if(!in_array($c->comment_ID , (array)$comments_in_the_users_language)){
  509. unset($comments[$k]);
  510. }
  511. }
  512. }
  513. return array_values($comments);
  514. }
  515. function use_comments_array_filter(){
  516. global $comments;
  517. $comments = $this->comments_array_filter($comments);
  518. }
  519. function comment_feed_join($join){
  520. global $wpdb, $sitepress;
  521. $lang = $this->enable_comments_translation ? $this->user_language : $sitepress->get_current_language();
  522. $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}'";
  523. return $join;
  524. }
  525. function filter_queries($sql){
  526. global $pagenow, $wpdb;
  527. if($pagenow == 'index.php'){
  528. if(preg_match('#SELECT \* FROM (.+)comments ORDER BY comment_date_gmt DESC LIMIT ([0-9]+), ([0-9]+)#i',$sql,$matches)){
  529. $res = $wpdb->get_results($sql);
  530. $comments = array();
  531. foreach( $res as $row){
  532. $comments[] = $row;
  533. }
  534. if(!$this->enable_comments_translation && $comments){
  535. // show only original comments regardless of the user language
  536. foreach($comments as $c){
  537. $element_comment_ids[] = $c->comment_ID;
  538. }
  539. if(!empty($element_comment_ids)){
  540. $comment_ids = $wpdb->get_col("
  541. SELECT element_id
  542. FROM {$wpdb->prefix}icl_translations
  543. WHERE element_type='comment' AND element_id IN(".join(',', $element_comment_ids).")
  544. AND source_language_code IS NULL
  545. ");
  546. if(!empty($comment_ids)){
  547. $sql = "SELECT * FROM {$matches[1]}comments c
  548. WHERE c.comment_ID IN (".join(',',$comment_ids).")
  549. ORDER BY c.comment_date_gmt DESC LIMIT {$matches[2]}, {$matches[3]}";
  550. }
  551. }
  552. }else{
  553. $this->comments_array_filter($comments);
  554. unset($comments);
  555. $sql = "
  556. SELECT * FROM {$matches[1]}comments c
  557. LEFT JOIN {$matches[1]}icl_translations t ON t.element_id=c.comment_ID
  558. WHERE
  559. (t.element_type='comment' AND t.language_code='{$this->user_language}')
  560. ORDER BY c.comment_date_gmt DESC LIMIT {$matches[2]}, {$matches[3]}";
  561. }
  562. }
  563. }elseif( isset($_POST['action']) && $_POST['action']=='get-comments' && isset($_POST['mode']) && $_POST['mode']=='single'){
  564. global $sitepress;
  565. 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)){
  566. $post_id = (int) $_POST['post_ID'];
  567. $post_type = get_post_type($post_id);
  568. $post_type_prefixed = "post_" . $post_type;
  569. $q = "SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_id=%s AND element_type=%s";
  570. $q_prepared = $wpdb->prepare($q, $post_id, $post_type_prefixed);
  571. $row = $wpdb->get_row($q_prepared, ARRAY_N);
  572. $c_language = $row[0];
  573. if($this->enable_comments_translation){
  574. $tr_ids_orig = array();
  575. $post_id = (int) $_POST['post_ID'];
  576. $q = "SELECT element_id, trid FROM {$wpdb->prefix}icl_translations t
  577. JOIN {$wpdb->comments} c ON t.element_id = c.comment_ID AND comment_POST_ID = %d
  578. WHERE t.element_type=%s AND language_code=%s";
  579. $q_prepared = $wpdb->prepare($q, $post_id, 'comment', $c_language);
  580. $res = $wpdb->get_results($q_prepared);
  581. foreach($res as $r){
  582. $tr_ids_orig[$r->trid] = $r->element_id;
  583. }
  584. $post_id = (int) $_POST['post_ID'];
  585. $q = "SELECT element_id, trid FROM {$wpdb->prefix}icl_translations t
  586. JOIN {$wpdb->comments} c ON t.element_id = c.comment_ID AND comment_POST_ID = %d
  587. WHERE t.element_type=%s AND language_code=%s";
  588. $q_prepared = $wpdb->prepare($q, $post_id, 'comment', $this->user_language);
  589. $res = $wpdb->get_results($q_prepared);
  590. foreach($res as $r){
  591. $tr_ids_tr[$r->trid] = $r->element_id;
  592. }
  593. $wp_comments_cols = array_keys($wpdb->get_row("SELECT * FROM {$wpdb->comments} LIMIT 1", ARRAY_A));
  594. foreach($tr_ids_orig as $o_trid=>$o_cid){
  595. if(!isset($tr_ids_tr[$o_trid])){
  596. $original_comment = get_comment($tr_ids_orig[$o_trid]);
  597. $machine_translation = $this->machine_translate($c_language, $this->user_language, $original_comment->comment_content);
  598. $comment_new = clone $original_comment;
  599. $comment_new->comment_content = $machine_translation;
  600. unset($comment_new->comment_ID);
  601. $wpdb->insert($wpdb->comments, array_intersect_key((array)$comment_new, array_flip($wp_comments_cols)));
  602. $new_comment_id = $wpdb->insert_id;
  603. $sitepress->set_element_language_details($new_comment_id, 'comment', $o_trid, $this->user_language);
  604. if($original_comment->comment_parent){
  605. $cptrid = $sitepress->get_element_trid($original_comment->comment_parent, 'comment');
  606. $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}'");
  607. $wpdb->update($wpdb->comments, array('comment_parent'=>$comment_new->comment_parent), array('comment_ID'=>$new_comment_id));
  608. }
  609. if(!$machine_translation){
  610. $nonce = wp_create_nonce('machine-translation-failed'.$new_comment_id);
  611. $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>';
  612. $wpdb->update($wpdb->comments, array('comment_content'=>$comment_new->comment_content), array('comment_ID'=>$new_comment_id));
  613. }
  614. }
  615. }
  616. $c_language = $this->user_language;
  617. }
  618. $sql = "
  619. SELECT *
  620. FROM {$matches[1]}comments c USE INDEX (comment_date_gmt)
  621. JOIN {$matches[1]}icl_translations t ON t.element_id=c.comment_ID
  622. WHERE
  623. t.element_type='comment' AND t.language_code='{$c_language}' AND
  624. ( comment_approved = '0' OR comment_approved = '1' ) AND
  625. comment_post_ID = '{$matches[2]}'
  626. ORDER BY comment_date_gmt ASC
  627. LIMIT {$matches[3]}, {$matches[4]}";
  628. }
  629. }
  630. return $sql;
  631. }
  632. /*
  633. function comment_feed_where($where){
  634. return $where;
  635. }
  636. */
  637. function wp_insert_comment($comment_id, $comment_data){
  638. global $sitepress, $wpdb;
  639. if(isset($comment_data->comment_post_ID)){
  640. $comment_post_ID = $comment_data->comment_post_ID;
  641. }elseif(isset($_POST['comment_post_ID'])){
  642. $comment_post_ID = $_POST['comment_post_ID'];
  643. }else{
  644. return false;
  645. }
  646. $post_type = $wpdb->get_var($wpdb->prepare("SELECT post_type FROM {$wpdb->posts} WHERE ID=%d", $comment_post_ID));
  647. if(isset($_POST['icl_user_language'])){
  648. if($_POST['icl_translate_reply']){
  649. $lang = $_POST['icl_user_language'];
  650. // send the comment to translation
  651. $this->send_comment_to_translation($comment_id, $wpdb->get_var($wpdb->prepare("
  652. SELECT language_code FROM {$wpdb->prefix}icl_translations
  653. WHERE element_type='post_{$post_type}' AND element_id=%d", $comment_post_ID)));
  654. }else{
  655. //$lang = $_POST['icl_comment_language'];
  656. $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));
  657. // sync comment parent
  658. // look for comment parent in original language and set for the comment that's been added
  659. if(isset($_POST['comment_parent'])){
  660. $comment_parent = $_POST['comment_parent'];
  661. }else{
  662. $comment_parent = $_POST['comment_ID'];
  663. }
  664. if($comment_parent){
  665. $ctrid = (int)$sitepress->get_element_trid($comment_parent,'comment');
  666. $original_comment_parent = $wpdb->get_var("
  667. SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid={$ctrid}
  668. AND element_type='comment' AND language_code='{$lang}'");
  669. $wpdb->update($wpdb->comments, array('comment_parent'=>$original_comment_parent), array('comment_ID'=>$comment_id));
  670. }
  671. }
  672. }else{
  673. $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));
  674. if(!$lang){
  675. $lang = $this->user_language; // just in case
  676. }
  677. }
  678. if(!$lang){
  679. $lang = $this->user_language;
  680. }
  681. $translation_id = $sitepress->set_element_language_details($comment_id, 'comment', null, $lang);
  682. }
  683. function send_comment_to_translation($comment_id, $to_language){
  684. global $wpdb, $sitepress_settings, $sitepress;
  685. $iclq = new ICanLocalizeQuery($sitepress_settings['site_id'], $sitepress_settings['access_key']);
  686. $from_lang = $sitepress->get_language_details($this->user_language);
  687. $to_lang = $sitepress->get_language_details($to_language);
  688. $from_lang_server = ICL_Pro_Translation::server_languages_map($from_lang['english_name']);
  689. $to_lang_server = ICL_Pro_Translation::server_languages_map($to_lang['english_name']);
  690. $body = $wpdb->get_var("SELECT comment_content FROM {$wpdb->comments} WHERE comment_ID={$comment_id}");
  691. $rid = $iclq->cms_create_message($body, $from_lang_server, $to_lang_server);
  692. if($rid > 0){
  693. // does this comment already exist in the messages status queue?
  694. $msid = $wpdb->get_var("SELECT id FROM {$wpdb->prefix}icl_message_status WHERE object_type='comment' AND object_id={$comment_id}");
  695. if($msid){
  696. $wpdb->update($wpdb->prefix.'icl_message_status',
  697. array('rid'=>$rid, 'md5' => md5($body), 'status' => MESSAGE_TRANSLATION_IN_PROGRESS),
  698. array('id' => $msid)
  699. );
  700. }else{
  701. $wpdb->insert($wpdb->prefix.'icl_message_status', array(
  702. 'rid' => $rid,
  703. 'object_id' => $comment_id,
  704. 'from_language' => $this->user_language,
  705. 'to_language' => $to_language,
  706. 'md5' => md5($body),
  707. 'object_type' => 'comment',
  708. 'status' => MESSAGE_TRANSLATION_IN_PROGRESS
  709. ));
  710. }
  711. }
  712. }
  713. function add_comment_translation($object_id, $to_language, $translation){
  714. global $wpdb, $sitepress;
  715. $original_comment_prepared = $wpdb->prepare( "
  716. SELECT *
  717. FROM {$wpdb->comments} WHERE comment_ID = %d
  718. ", array( $object_id ) );
  719. $original_comment = $wpdb->get_row( $original_comment_prepared, ARRAY_A);
  720. $new_comment = $original_comment;
  721. //sync comment parent
  722. if($original_comment['comment_parent']){
  723. $comment_trid = (int)$sitepress->get_element_trid($original_comment['comment_parent'],'comment');
  724. $comment_parent_prepared = $wpdb->prepare("
  725. SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid=%d
  726. AND element_type='comment' AND language_code=%s", array($comment_trid, $to_language));
  727. $new_comment['comment_parent'] = (int) $wpdb->get_var( $comment_parent_prepared );
  728. }
  729. $original_comment_id = $original_comment['comment_ID'];
  730. unset($original_comment);
  731. $new_comment['comment_content'] = $translation;
  732. unset($new_comment['comment_ID']);
  733. //remove_action('wp_insert_comment', array($this, 'wp_insert_comment'));
  734. //check whether a translation for this comment in this language already exists
  735. $trid = $sitepress->get_element_trid($original_comment_id, 'comment');
  736. $existing_comment_id_prepared = $wpdb->prepare( "
  737. SELECT element_id FROM {$wpdb->prefix}icl_translations
  738. WHERE trid=%d AND element_type='comment' AND language_code=%s", array( $trid, $to_language ) );
  739. $existing_comment_id = $wpdb->get_var( $existing_comment_id_prepared );
  740. if(!$existing_comment_id){
  741. remove_action('wp_insert_comment', array($this, 'wp_insert_comment'));
  742. $new_id = wp_insert_comment($new_comment);
  743. add_action('wp_insert_comment', array($this, 'wp_insert_comment'), 10, 2);
  744. }else{
  745. $new_id = $new_comment['comment_ID'] = $existing_comment_id;
  746. remove_action('transition_comment_status', array($this, 'transition_comment_status_actions'));
  747. wp_update_comment($new_comment);
  748. }
  749. $sitepress->set_element_language_details($new_id, 'comment', $trid, $to_language);
  750. return 1; //success
  751. }
  752. function add_custom_xmlrpc_methods($methods){
  753. //$methods['icanlocalize.notify_comment_translation'] = array($this, 'add_comment_translation');
  754. return $methods;
  755. }
  756. function comment_text_filter($comment_text){
  757. global $sitepress, $comment, $wp_query, $wpdb;
  758. static $comment_ids, $comment_originals, $page_language, $translation_status;
  759. //if($this->enable_comments_translation && $this->user_language != $sitepress->get_current_language() ){
  760. if($this->enable_comments_translation){
  761. // run this block once
  762. if(empty($comment_ids)){
  763. $current_language = $sitepress->get_current_language();
  764. $page_language = $wpdb->get_var("
  765. SELECT name
  766. FROM {$wpdb->prefix}icl_languages_translations
  767. WHERE language_code='". $current_language ."' AND display_language_code='".$this->user_language."'
  768. ");
  769. foreach($wp_query->comments as $c){
  770. $comment_ids[] = (int)$c->comment_ID;
  771. }
  772. $res_query = "
  773. SELECT t.element_id, t.trid, ms.status
  774. FROM {$wpdb->prefix}icl_translations t
  775. LEFT JOIN {$wpdb->prefix}icl_message_status ms ON t.element_id=ms.object_id
  776. WHERE t.element_id IN(".join(',',$comment_ids).") AND t.element_type='comment' AND t.language_code <> %s AND (ms.object_type='comment' OR ms.object_type IS NULL)";
  777. $res_query_prepared = $wpdb->prepare($res_query, $current_language);
  778. $res = $wpdb->get_results($res_query_prepared);
  779. foreach($res as $row){
  780. $tridsmap[$row->trid] = $row->element_id;
  781. if($row->status){
  782. $translation_status[$row->element_id] = $row->status;
  783. }
  784. }
  785. if(!empty($tridsmap)){
  786. $res_query = "SELECT t.trid, c.comment_content
  787. FROM {$wpdb->prefix}icl_translations t
  788. JOIN {$wpdb->comments} c ON t.element_id = c.comment_ID
  789. WHERE t.trid IN(".join(',',array_keys($tridsmap)).") AND t.element_type='comment' AND t.language_code=%s";
  790. $res_query_prepared = $wpdb->prepare($res_query, $current_language);
  791. $res = $wpdb->get_results($res_query_prepared);
  792. foreach($res as $row){
  793. $comment_originals[$tridsmap[$row->trid]] = $row->comment_content;
  794. }
  795. }
  796. }
  797. $str = '';
  798. if(isset($comment_originals[$comment->comment_ID])){
  799. $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.'">' . esc_html($comment_originals[$comment->comment_ID]) . '</div>';
  800. }
  801. if($translation_status[$comment->comment_ID] || isset($comment_originals[$comment->comment_ID])){
  802. $str .= '<p style="margin-top:1px;">';
  803. }
  804. if(isset($translation_status[$comment->comment_ID])){
  805. if($translation_status[$comment->comment_ID] == MESSAGE_TRANSLATION_IN_PROGRESS){
  806. $str .= '<i>' . __('Translation in progress', 'sitepress' ) . '</i> ';
  807. }elseif($translation_status[$comment->comment_ID] == MESSAGE_TRANSLATION_COMPLETE){
  808. $str .= '<i>' . __('Translation complete', 'sitepress' ) . '</i> ';
  809. }
  810. }
  811. if(isset($comment_originals[$comment->comment_ID])){
  812. $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>';
  813. }
  814. if($translation_status[$comment->comment_ID] || isset($comment_originals[$comment->comment_ID])){
  815. $str .= '</p>';
  816. }
  817. if($str){
  818. $comment_text .= $str;
  819. }
  820. }
  821. return $comment_text;
  822. }
  823. func

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