Advertisement
Guest User

WPML comment merging

a guest
Apr 18th, 2014
1,652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.26 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WPML comment merging
  4. Plugin URI: http://wordpress.org/extend/plugins/wpml-comment-merging/
  5. Description: This plugin merges comments from all translations of the posts and pages, so that they all are displayed on each other. Comments are internally still attached to the post or page they were made on.
  6. Version: 1.3
  7. Author: Fabian Lange
  8. Author URI: http://blog.codecentric.de/en/2010/06/wordpress-wpml-comments-filter-plugin/
  9. License: MIT
  10. */
  11.  
  12. function sort_merged_comments($a, $b) {
  13.     return $a->comment_ID - $b->comment_ID;
  14. }
  15.  
  16. function merge_comments($comments, $post_ID) {
  17.     global $sitepress;
  18.    
  19.     remove_filter('comments_clauses', array($sitepress, 'comments_clauses'), 10, 2);
  20.     // get all the languages for which this post exists
  21.     $languages = icl_get_languages('skip_missing=1');
  22.     $type = is_page($post_ID) ? 'page' : 'post';
  23.     foreach($languages as $l) {
  24.         // in $comments are already the comments from the current language
  25.         if(!$l['active']) {
  26.             $otherID = icl_object_id($post_ID, $type, false, $l['language_code']);
  27.             $othercomments = get_comments( array('post_id' => $otherID, 'status' => 'approve', 'order' => 'ASC') );
  28.             $comments = array_merge($comments, $othercomments);
  29.         }
  30.     }
  31.     if ($languages) {
  32.         // if we merged some comments in we need to reestablish an order
  33.         usort($comments, 'sort_merged_comments');
  34.     }
  35.    
  36.     return $comments;
  37. }
  38.  
  39. function merge_comment_count($count, $post_ID) {
  40.     // get all the languages for which this post exists
  41.     $languages = icl_get_languages('skip_missing=1');
  42.     $type = is_page($post_ID) ? 'page' : 'post';
  43.  
  44.     foreach($languages as $l) {
  45.         // in $count is already the count from the current language
  46.         if(!$l['active']) {
  47.             $otherID = icl_object_id($post_ID, $type, false, $l['language_code']);
  48.             if($otherID) {
  49.                 // cannot use call_user_func due to php regressions
  50.                 if ($type == 'page') {
  51.                     $otherpost = get_page($otherID);
  52.                 } else {
  53.                     $otherpost = get_post($otherID);
  54.                 }
  55.                 if ($otherpost) {
  56.                     // increment comment count using translation post comment count.
  57.                     $count = $count + $otherpost->comment_count;
  58.                 }
  59.             }
  60.         }
  61.     }
  62.     return $count;
  63. }
  64.  
  65. add_filter('comments_array', 'merge_comments', 100, 2);
  66. add_filter('get_comments_number', 'merge_comment_count', 100, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement