ID, '_review_post_pros_text', true);
if(empty($prosvalues)){
$prosvalues = (!empty($review_post['review_post_pros_text'])) ? $review_post['review_post_pros_text'] : '';
}
$consvalues = get_post_meta($post->ID, '_review_post_cons_text', true);
if(empty($consvalues)){
$consvalues = (!empty($review_post['review_post_cons_text'])) ? $review_post['review_post_cons_text'] : '';
}
?>
>
class="wpsm-one-half wpsm-column-first">
total_comments !='') :?>
|
4)
wp_die(sha1(microtime())); // return some random trash :)
$comments_count = wp_count_comments($post_id);
if (empty($comments_count->approved))
wp_die(esc_html__('No comments on this post', 'rehub-theme'));
unset($comments_count);
$comments_v = get_comments(array(
'post_id' => $post_id,
'status' => 'approve',
'orderby' => 'comment_date',
'order' => 'DESC',
));
foreach($comments_v as $key=>$comment) {
$meta = get_comment_meta($comment->comment_ID);
$comment->user_average = isset($meta['user_average'][0]) ? $meta['user_average'][0] : 0;
if(isset($meta['user_average'][0])){
$comment->user_average = $meta['user_average'][0];
}
elseif(isset($meta['rating'][0])){
$comment->user_average = $meta['rating'][0];
}else{
$comment->user_average = 0;
}
$comment->recomm_plus = isset($meta['recomm_plus'][0]) ? $meta['recomm_plus'][0] : 0;
//$comment->recomm_minus = isset($meta['recomm_minus'][0]) ? $meta['recomm_minus'][0] : 0;
$comments_and_meta_v[$key] = $comment;
}
unset($comments_v);
switch ($tab_number) {
case 1 : $sorted_comments_v = show_tab_get_newest($comments_and_meta_v); break;
case 2 : $sorted_comments_v = show_tab_get_most_helpful($comments_and_meta_v); break;
case 3 : $sorted_comments_v = show_tab_get_highest_rating($comments_and_meta_v); break;
case 4 : $sorted_comments_v = show_tab_get_lowest_rating($comments_and_meta_v); break;
default: die(sha1(microtime())); // not needed, but...
}
unset($comments_and_meta_v);
show_tab_print_comments($sorted_comments_v, $posttype);
exit;
}
// ----------------------------------------------
function show_tab_get_newest($comments_v) {
return $comments_v; // it already sorted as we need
}
// ----------------------------------------------
function show_tab_get_most_helpful_sort ($a, $b) {
if ($a->recomm_plus > $b->recomm_plus)
return -1;
elseif ($a->recomm_plus < $b->recomm_plus)
return 1;
elseif ($a->comment_ID > $b->comment_ID)
return -1;
else
return 1;
}
function show_tab_get_most_helpful($comments_v) {
$comments_v = show_tab_delete_unlikes_comments($comments_v);
usort($comments_v, 'show_tab_get_most_helpful_sort');
return $comments_v;
}
// ----------------------------------------------
function show_tab_get_highest_rating_sort ($a, $b) {
if ($a->user_average > $b->user_average)
return -1;
elseif ($a->user_average < $b->user_average)
return 1;
elseif ($a->comment_ID > $b->comment_ID)
return -1;
else
return 1;
}
function show_tab_get_highest_rating($comments_v) {
$comments_v = show_tab_delete_unrated_comments($comments_v);
usort($comments_v, 'show_tab_get_highest_rating_sort');
return $comments_v;
}
// ----------------------------------------------
function show_tab_get_lowest_rating_sort ($a, $b) {
if ($a->user_average > $b->user_average)
return 1;
elseif ($a->user_average < $b->user_average)
return -1;
elseif ($a->comment_ID > $b->comment_ID)
return 1;
else
return -1;
}
function show_tab_get_lowest_rating($comments_v) {
$comments_v = show_tab_delete_unrated_comments($comments_v);
usort($comments_v, 'show_tab_get_lowest_rating_sort');
return $comments_v;
}
// ----------------------------------------------
function show_tab_delete_unrated_comments($comments_v) {
$result_v = array();
foreach($comments_v as $comment) {
if (empty($comment->user_average)) continue;
$result_v[] = $comment;
}
return $result_v;
}
// ----------------------------------------------
function show_tab_delete_unlikes_comments($comments_v) {
$result_v = array();
foreach($comments_v as $comment) {
if (empty($comment->recomm_plus)) continue;
$result_v[] = $comment;
}
return $result_v;
}
// ----------------------------------------------
function show_tab_print_comments($sorted_comments_v, $posttype) {
$callback = ($posttype == 'product') ? 'woocommerce_comments' : 'rehub_framework_comments';
wp_list_comments(array(
'avatar_size' => 50,
'max_depth' => 4,
'style' => 'ul',
'reverse_top_level' => 0,
'callback' => $callback,
'echo' => 'true'
), $sorted_comments_v);
}
//////////////////////////////////////////////////////////////////
// Helpful or NOT
//////////////////////////////////////////////////////////////////
add_action( 'wp_ajax_nopriv_commentplus', 'commentplus_re' );
add_action( 'wp_ajax_commentplus', 'commentplus_re' );
function commentplus_re() {
$nonce = sanitize_text_field($_POST['cplusnonce']);
if ( ! wp_verify_nonce( $nonce, 'commre-nonce' ) )
die ( 'Nope!' );
if ( isset( $_POST['comm_help'] ) ) {
$post_id = intval($_POST['post_id']); // post id
$comm_plus = get_comment_meta( $post_id, "recomm_plus", true ); // get helpful comment count
$comm_minus = get_comment_meta( $post_id, "recomm_minus", true ); // get unhelpful comment count
if ( is_user_logged_in() ) { // user is logged in
global $current_user;
$user_id = $current_user->ID; // current user
$meta_POSTS = get_user_meta( $user_id, "_comm_help_posts" ); // post ids from user meta
$meta_USERS = get_comment_meta( $post_id, "_user_comm_help" ); // user ids from post meta
$liked_POSTS = ""; // setup array variable
$liked_USERS = ""; // setup array variable
if ( count( $meta_POSTS ) != 0 ) { // meta exists, set up values
$liked_POSTS = $meta_POSTS[0];
}
if ( !is_array( $liked_POSTS ) ) // make array just in case
$liked_POSTS = array();
if ( count( $meta_USERS ) != 0 ) { // meta exists, set up values
$liked_USERS = $meta_USERS[0];
}
if ( !is_array( $liked_USERS ) ) // make array just in case
$liked_USERS = array();
$liked_POSTS['post-'.$post_id] = $post_id; // Add post id to user meta array
$liked_USERS['user-'.$user_id] = $user_id; // add user id to post meta array
$user_likes = count( $liked_POSTS ); // count user likes
if ($_POST['comm_help'] =='plus') {
if ( !AlreadyCommentplus( $post_id ) ) {
update_comment_meta( $post_id, "recomm_plus", ++$comm_plus ); // +1 count to helpful
update_comment_meta( $post_id, "_user_comm_help", $liked_USERS ); // Add user ID to post meta
update_user_meta( $user_id, "_comm_help_posts", $liked_POSTS ); // Add post ID to user meta
update_user_meta( $user_id, "_comm_help_count", $user_likes ); // +1 count user meta
}
}
if ($_POST['comm_help'] =='minus') {
if ( !AlreadyCommentplus( $post_id ) ) {
update_comment_meta( $post_id, "recomm_minus", ++$comm_minus ); // +1 count to unhelpful
update_comment_meta( $post_id, "_user_comm_help", $liked_USERS ); // Add user ID to post meta
update_user_meta( $user_id, "_comm_help_posts", $liked_POSTS ); // Add post ID to user meta
update_user_meta( $user_id, "_comm_help_count", $user_likes ); // +1 count user meta
}
}
} else { // user is not logged in (anonymous)
$ip = rehub_get_ip(); // user IP address
$meta_IPS = get_comment_meta( $post_id, "_user_IP_comm_help" ); // stored IP addresses
$liked_IPS = ""; // set up array variable
if ( count( $meta_IPS ) != 0 ) { // meta exists, set up values
$liked_IPS = $meta_IPS[0];
}
if ( !is_array( $liked_IPS ) ) // make array just in case
$liked_IPS = array();
if ( !in_array( $ip, $liked_IPS ) ) // if IP not in array
$liked_IPS['ip-'.$ip] = $ip; // add IP to array
if ($_POST['comm_help'] =='plus') {
if ( !AlreadyCommentplus( $post_id ) ) {
update_comment_meta( $post_id, "recomm_plus", ++$comm_plus ); // +1 count post meta
update_comment_meta( $post_id, "_user_IP_comm_help", $liked_IPS ); // Add user IP to post meta
}
}
if ($_POST['comm_help'] =='minus') {
if ( !AlreadyCommentplus( $post_id ) ) {
update_comment_meta( $post_id, "recomm_minus", ++$comm_minus ); // +1 count to unhelpful
update_comment_meta( $post_id, "_user_IP_comm_help", $liked_IPS ); // Add user IP to post meta
}
}
}
}
exit;
}
function AlreadyCommentplus( $post_id ) { // test if user liked before
if ( is_user_logged_in() ) { // user is logged in
global $current_user;
$user_id = $current_user->ID; // current user
$meta_USERS = get_comment_meta( $post_id, "_user_comm_help" ); // user ids from post meta
$liked_USERS = ""; // set up array variable
if ( is_array($meta_USERS) && count( $meta_USERS ) != 0 ) { // meta exists, set up values
$liked_USERS = $meta_USERS[0];
}
if( !is_array( $liked_USERS ) ) // make array just in case
$liked_USERS = array();
if ( in_array( $user_id, $liked_USERS ) ) { // True if User ID in array
return true;
}
return false;
}
else { // user is anonymous, use IP address for voting
$meta_IPS = get_comment_meta($post_id, "_user_IP_comm_help"); // get previously voted IP address
$ip = rehub_get_ip(); // Retrieve current user IP
$liked_IPS = ""; // set up array variable
if ( count( $meta_IPS ) != 0 ) { // meta exists, set up values
$liked_IPS = $meta_IPS[0];
}
if ( !is_array( $liked_IPS ) ) // make array just in case
$liked_IPS = array();
if ( in_array( $ip, $liked_IPS ) ) { // True is IP in array
return true;
}
return false;
}
}
function getCommentLike_re( $comment_text ) {
wp_enqueue_script('rhcommentvote');
$post_id = get_comment_ID();
$comm_plus = get_comment_meta( $post_id, "recomm_plus", true ); // get helpful comment count
$comm_minus = get_comment_meta( $post_id, "recomm_minus", true ); // get unhelpful comment count
if ( ( !$comm_plus ) || ( $comm_plus && $comm_plus == "0" ) ) { // no votes, set up empty variable
$comm_plus_count = '0';
} elseif ( $comm_plus && $comm_plus != "0" ) { // there are votes!
$comm_plus_count = esc_attr( $comm_plus );
}
if ( ( !$comm_minus ) || ( $comm_minus && $comm_minus == "0" ) ) { // no votes, set up empty variable
$comm_minus_count = '0';
} elseif ( $comm_minus && $comm_minus != "0" ) { // there are votes!
$comm_minus_count = esc_attr( $comm_minus );
}
$already = (AlreadyCommentplus( $post_id )) ? ' alreadycomment' : '';
$output = '