Quantcast
Channel: polylang – WordPress.org Forums
Viewing all articles
Browse latest Browse all 11471

Iznogood1 on "[Plugin: Polylang] Multilang comments + hightlight post"

$
0
0

Let me sum up your need :
You want to display all the comments for one post whatever their language (ie merge the comments made for the same post in different languages).

This can be achieved in 3 steps :
1. Remove the filter on comments that loads only those in the current language
2. Modify the function that built the list of comments (comments_array)
3. Modify the function that count the comments (get_comments_number)

Theses 3 steps can be programmed in your functions.php
You'll find here after the total code to be included in your functions.php
It is the consolidation of the code written at http://wordpress.org/support/topic/multilang-comment (that use modified fucntions of the WPML comment merging plugin.
Note that the functions that modify comments_array and get_comments_number are using WPML functions (icl_...) instead of Polylang ones (pll_...) as polylang contains a "compatibility" module (polylang/include/wpml-compat.php) that allow us to use WPML functions.

Here is the code to put in functions.php :

//Remove the filter
function polylang_remove_comments_filter() {
global $wp_filter;
global $polylang;
remove_filter('comments_clauses', array(&$polylang, 'comments_clauses'));
}

function sort_merged_comments($a, $b) {
return $a->comment_ID - $b->comment_ID;
}

//Get the comments from the same posts in different languages
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 = icl_get_languages('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 = icl_object_id($post_ID, $type, false, $l['language_code']);
$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' ) );

return $comments;
}

//Add comments for the same posts in different languages
function merge_comment_count($count, $post_ID) {
// get all the languages for which this post exists
$languages = icl_get_languages('skip_missing=1');
$languages = pll_the_languages(array('raw'=>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 = icl_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;
}

//Trigger the functions
add_action('wp','polylang_remove_comments_filter');
add_filter('comments_array', 'merge_comments', 100, 2);
add_filter('get_comments_number', 'merge_comment_count', 100, 2);

Tell us if it is working as you want.
If yes, do not forget to tag this thread as [Resolved]


Viewing all articles
Browse latest Browse all 11471

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>