Skip to content

Instantly share code, notes, and snippets.

@Dudo1985
Created October 19, 2021 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dudo1985/9b74dfcbfe712af8f0bacada4d6a0af1 to your computer and use it in GitHub Desktop.
Save Dudo1985/9b74dfcbfe712af8f0bacada4d6a0af1 to your computer and use it in GitHub Desktop.
This will replace the rating value of yasr_visitor_votes showing the rating of the current user if logged in
<?php
add_filter('yasr_vv_shortcode', static function ($shortcode_html, $stored_votes) {
//if user is not logged, return shortcode
if(!is_user_logged_in()) {
return $shortcode_html;
}
$post_id = get_the_ID();
$current_user_rating = YasrDatabaseRatings::visitorVotesHasUserVoted($post_id);
//if current user has not rated, return shortcode
if($current_user_rating === false) {
return $shortcode_html;
}
$number_of_votes = $stored_votes['number_of_votes'];
if ($number_of_votes > 0) {
$average_rating = $stored_votes['sum_votes']/$number_of_votes;
} else {
$average_rating = 0;
}
$average_rating=round($average_rating, 1);
$search = 'data-rating=\''.$average_rating.'\'';
$replace = 'data-rating=\''.$current_user_rating.'\'';
//ovewrite and return
return str_replace($search, $replace, $shortcode_html);
}, 10, 2);
@nansouille
Copy link

nansouille commented Dec 10, 2021

thanks

another version if you want that connected users only vote without seeing the average

/* YAR */

add_filter('yasr_vv_shortcode', static function ($shortcode_html, $stored_votes) {
  
    if(!is_user_logged_in()) {
		return 'Vous devez vous connecter pour voter !';

    } 

    $post_id = get_the_ID();
    $current_user_rating = YasrDatabaseRatings::visitorVotesHasUserVoted($post_id);
	
	 $number_of_votes = $stored_votes['number_of_votes'];

    if ($number_of_votes > 0) {
        $average_rating = $stored_votes['sum_votes']/$number_of_votes;
    } else {
        $average_rating = 0;
    }

    $average_rating=round($average_rating, 1);

    $search  = "data-rating='".$average_rating."'";
    $replace = "data-rating='".$current_user_rating."'";

    //if current user has not rated, return shortcode
    if($current_user_rating === false) {
		
		$replace = str_replace(trim($search), "data-rating=''", $shortcode_html);
		
		return $replace;
   
    } else {

	
    return str_replace($search, $replace, $shortcode_html);
	}

 

}, 10, 2);

@Dudo1985
Copy link
Author

Thank you for sharing this!

@Dudo1985
Copy link
Author

I've made some minor changes to your code:

add_filter('yasr_vv_shortcode', static function ($shortcode_html, $stored_votes) {

    if(!is_user_logged_in()) {
        //this will overwrite stars!
        return 'YOUR TEXT HERE';
    }

    $post_id = get_the_ID();
    $current_user_rating = YasrDatabaseRatings::visitorVotesHasUserVoted($post_id);

    $number_of_votes = $stored_votes['number_of_votes'];

    if ($number_of_votes > 0) {
        $average_rating = $stored_votes['sum_votes']/$number_of_votes;
    } else {
        $average_rating = 0;
    }

    $average_rating=round($average_rating, 1);

    $search  = "data-rating='".$average_rating."'";
    $replace = "data-rating='".$current_user_rating."'";
    
    //no need if-else here, since $current_user_rating has the proper value
    return str_replace($search, $replace, $shortcode_html);

}, 10, 2);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment