forked from JulioPotier/wpml-comments-merging
-
Notifications
You must be signed in to change notification settings - Fork 2
/
wpml-comments-merging.php
74 lines (68 loc) · 2.58 KB
/
wpml-comments-merging.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/*
Plugin Name: WPML comments merging
Plugin URI: https://github.com/jgalea/wpml-comments-merging
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.
Version: 2.0
Author: Jean Galea
Author URI: http://www.jeangalea.com
License: GPL
This is a fixed version of the no longer maintained WPML Comment Merging plugin:
http://wordpress.org/extend/plugins/wpml-comment-merging/
Thanks to Simon Wheatley for contributing the fix.
*/
function sort_merged_comments($a, $b) {
return $a->comment_ID - $b->comment_ID;
}
function merge_comments($comments, $post_ID) {
global $sitepress;
remove_filter( 'comments_clauses', array( $sitepress, 'comments_clauses' ) );
// get all the languages for which this post exists
$languages = apply_filters( 'wpml_active_languages', NULL, 'skip_missing=1' );
$post = get_post( $post_ID );
$type = $post->post_type;
foreach($languages as $code => $l) {
// in $comments are already the comments from the current language
if(!$l['active']) {
$otherID = apply_filters( 'wpml_object_id', $post_ID, $type, false, $l['language_code'] );
if ($otherID === null)
continue;
$othercomments = get_comments( array('post_id' => $otherID, 'status' => 'approve', 'order' => 'ASC') );
$comments = array_merge($comments, $othercomments);
}
}
if ($languages) {
// if we merged some comments in we need to reestablish an order
usort($comments, 'sort_merged_comments');
}
//
add_filter( 'comments_clauses', array( $sitepress, 'comments_clauses' ), 10, 2 );
return $comments;
}
function merge_comment_count($count, $post_ID) {
// get all the languages for which this post exists
$languages = apply_filters( 'wpml_active_languages', NULL, 'skip_missing=1' );
$post = get_post( $post_ID );
$type = $post->post_type;
foreach($languages as $l) {
// in $count is already the count from the current language
if(!$l['active']) {
$otherID = apply_filters( 'wpml_object_id', $post_ID, $type, false, $l['language_code'] );
if($otherID) {
// cannot use call_user_func due to php regressions
if ($type == 'page') {
$otherpost = get_page($otherID);
} else {
$otherpost = get_post($otherID);
}
if ($otherpost) {
// increment comment count using translation post comment count.
$count = $count + $otherpost->comment_count;
}
}
}
}
return $count;
}
add_filter('comments_array', 'merge_comments', 100, 2);
add_filter('get_comments_number', 'merge_comment_count', 100, 2);