function shortcode_visitor_votes_callback($atts) { $shortcode_html = NULL; //Avoid undefined variable outside is_singular && is_main_query extract(shortcode_atts(array('size' => 'large', 'postid' => FALSE), $atts)); //If it's not specified use get_the_id if (!$postid) { $post_id = get_the_ID(); } else { $post_id = $postid; } $ajax_nonce_visitor = wp_create_nonce("yasr_nonce_insert_visitor_rating"); $votes = yasr_get_visitor_votes($post_id); //always reference it $medium_rating = 0; //Avoid undefined variable if (!$votes) { $votes = 0; //Avoid undefined variable if there is not overall rating $votes_number = 0; //Avoid undefined variable } else { foreach ($votes as $user_votes) { $votes_number = $user_votes->number_of_votes; if ($votes_number != 0) { $medium_rating = $user_votes->sum_votes / $votes_number; } else { $medium_rating = 0; } } } $medium_rating = round($medium_rating, 1); $stars_attribute = yasr_stars_size($size); $yasr_cookiename = 'yasr_visitor_vote_' . $post_id; if (isset($_COOKIE[$yasr_cookiename])) { $cookie_value = $_COOKIE[$yasr_cookiename]; $cookie_value = (int) $cookie_value; if ($cookie_value > 5) { $cookie_value = 5; } elseif ($cookie_value < 1) { $cookie_value = 1; } } else { $cookie_value = FALSE; } $vote_if_user_already_rated = FALSE; $shortcode_html = "<div id=\"yasr_visitor_votes_{$post_id}\" class=\"yasr-visitor-votes\">"; $span_after_rate_it = ""; //if anonymous are allowed to vote if (YASR_ALLOWED_USER === 'allow_anonymous') { //I've to checl a logged in user that has already rated if (is_user_logged_in()) { $readonly = 'false'; //Chek if a logged in user has already rated for this post $vote_if_user_already_rated = yasr_check_if_user_already_voted($post_id); //If user has already rated if ($vote_if_user_already_rated) { $span_after_rate_it = "<span class=\"yasr-small-block-bold\" id=\"yasr-already-voted-text\">" . __("You've already voted this article with", 'yet-another-stars-rating') . " {$vote_if_user_already_rated} </span>"; } } else { //if cookie exists if ($cookie_value) { $readonly = 'true'; if (YASR_TEXT_BEFORE_STARS == 1 && YASR_CUSTOM_TEXT_USER_VOTED != '') { $span_after_rate_it = $span_after_rate_it = "<span class=\"yasr-small-block-bold\" id=\"yasr-already-voted-text\">" . YASR_CUSTOM_TEXT_USER_VOTED . " </span>"; } else { $span_after_rate_it = "<span class=\"yasr-small-block-bold\" id=\"yasr-already-voted-text\">" . __("You've already voted this article with", 'yet-another-stars-rating') . " {$cookie_value} </span>"; } } else { $readonly = 'false'; } } } elseif (YASR_ALLOWED_USER === 'logged_only') { //If user is logged in and can vote if (is_user_logged_in()) { $readonly = 'false'; //REadonly is false if user is logged //Chek if a logged in user has already rated for this post $vote_if_user_already_rated = yasr_check_if_user_already_voted($post_id); if ($vote_if_user_already_rated) { $span_after_rate_it = "<span class=\"yasr-small-block-bold\" id=\"yasr-already-voted-text\">" . __("You've already voted this article with", 'yet-another-stars-rating') . " {$vote_if_user_already_rated} </span>"; } } else { $readonly = 'true'; //readonly is true if user isn't logged $span_after_rate_it = __("You must sign in to vote", 'yet-another-stars-rating'); } } if (YASR_VISITORS_STATS === 'yes') { $span_dashicon = "<span class=\"dashicons dashicons-chart-bar yasr-dashicons-visitor-stats \" id=\"yasr-total-average-dashicon-{$post_id}\" title=\"yasr-stats-dashicon\"></span>"; } else { $span_dashicon = ""; } if (YASR_TEXT_BEFORE_STARS == 1 && YASR_TEXT_BEFORE_VISITOR_RATING != '') { $text_before_star = str_replace('%total_count%', $votes_number, YASR_TEXT_BEFORE_VISITOR_RATING); $text_before_star = str_replace('%average%', $medium_rating, $text_before_star); $shortcode_html .= "<div class=\"yasr-container-custom-text-and-visitor-rating\">\n <span id=\"yasr-custom-text-before-visitor-rating\">" . $text_before_star . "</span></div>"; } if (YASR_TEXT_BEFORE_STARS == 1 && YASR_TEXT_AFTER_VISITOR_RATING != '') { $text_after_star = str_replace('%total_count%', $votes_number, YASR_TEXT_AFTER_VISITOR_RATING); $text_after_star = str_replace('%average%', $medium_rating, $text_after_star); $span_text_after_star = "<span class=\"yasr-total-average-container\" id=\"yasr-total-average-text_{$post_id}\">" . $text_after_star . "</span>"; } else { $span_text_after_star = "<span class=\"yasr-total-average-container\" id=\"yasr-total-average-text_{$post_id}\">\n [" . __("Total: ", 'yet-another-stars-rating') . "{$votes_number} " . __("Average: ", 'yet-another-stars-rating') . "{$medium_rating}/5]\n </span>"; } $shortcode_html .= "<div class=\"{$stars_attribute['class']}\" id=\"yasr_rateit_visitor_votes_{$post_id}\" data-postid=\"{$post_id}\" data-rateit-starwidth=\"{$stars_attribute['px_size']}\" data-rateit-starheight=\"{$stars_attribute['px_size']}\" data-rateit-value=\"{$medium_rating}\" data-rateit-step=\"1\" data-rateit-resetable=\"false\" data-rateit-readonly=\"{$readonly}\"></div>"; $shortcode_html .= $span_dashicon; $shortcode_html .= $span_text_after_star; $shortcode_html .= $span_after_rate_it; $shortcode_html .= "</div>"; wp_localize_script('yasrfront', "yasrVisitorsVotesData", array('voteIfUserAlredyRated' => $vote_if_user_already_rated, 'nonceVisitor' => $ajax_nonce_visitor)); //IF show visitor votes in loop is disabled use is_singular && is_main query if (YASR_SHOW_VISITOR_VOTES_IN_LOOP === 'disabled') { if (is_singular() && is_main_query()) { return $shortcode_html; } } elseif (YASR_SHOW_VISITOR_VOTES_IN_LOOP === 'enabled') { return $shortcode_html; } }
function shortcode_visitor_votes_callback($atts) { $shortcode_html = NULL; //Avoid undefined variable outside is_singular && is_main_query extract(shortcode_atts(array('size' => 'large', 'postid' => FALSE), $atts)); //If it's not specified use get_the_id if (!$postid) { $post_id = get_the_ID(); } else { $post_id = $postid; } $ajax_nonce_visitor = wp_create_nonce("yasr_nonce_insert_visitor_rating"); $votes = yasr_get_visitor_votes($post_id); //always reference it $medium_rating = 0; //Avoid undefined variable if (!$votes) { $votes = 0; //Avoid undefined variable if there is not overall rating $votes_number = 0; //Avoid undefined variable } else { foreach ($votes as $user_votes) { $votes_number = $user_votes->number_of_votes; if ($votes_number != 0) { $medium_rating = $user_votes->sum_votes / $votes_number; } else { $medium_rating = 0; } } } $image = YASR_IMG_DIR . "/loader.gif"; $loader_html = '<div id="loader-visitor-rating" > ' . __("Loading, please wait", "yasr") . ' <img src=' . "{$image}" . ' title="yasr-loader" alt="yasr-loader"></div>'; $medium_rating = round($medium_rating, 1); $stars_attribute = yasr_stars_size($size); $yasr_cookiename = 'yasr_visitor_vote_' . $post_id; if (isset($_COOKIE[$yasr_cookiename])) { $cookie_value = $_COOKIE[$yasr_cookiename]; $cookie_value = (int) $cookie_value; if ($cookie_value > 5) { $cookie_value = 5; } elseif ($cookie_value < 1) { $cookie_value = 1; } } else { $cookie_value = FALSE; } $vote_if_user_already_rated = FALSE; $shortcode_html = "<div id=\"yasr_visitor_votes_{$post_id}\" class=\"yasr-visitor-votes\">"; $span_after_rate_it = ""; //if anonymous are allowed to vote if (YASR_ALLOWED_USER === 'allow_anonymous') { //I've to checl a logged in user that has already rated if (is_user_logged_in()) { $readonly = 'false'; //Chek if a logged in user has already rated for this post $vote_if_user_already_rated = yasr_check_if_user_already_voted(); //If user has already rated if ($vote_if_user_already_rated) { $span_after_rate_it = "<span class=\"yasr-small-block-bold\" id=\"yasr-already-voted-text\">" . __("You've already voted this article with", "yasr") . " {$vote_if_user_already_rated} </span>"; } } else { //if cookie exists if ($cookie_value) { $readonly = 'true'; if (YASR_TEXT_BEFORE_STARS == 1 && YASR_CUSTOM_TEXT_USER_VOTED != '') { $span_after_rate_it = $span_after_rate_it = "<span class=\"yasr-small-block-bold\" id=\"yasr-already-voted-text\">" . YASR_CUSTOM_TEXT_USER_VOTED . " </span>"; } else { $span_after_rate_it = "<span class=\"yasr-small-block-bold\" id=\"yasr-already-voted-text\">" . __("You've already voted this article with", "yasr") . " {$cookie_value} </span>"; } } else { $readonly = 'false'; } } } elseif (YASR_ALLOWED_USER === 'logged_only') { //If user is logged in and can vote if (is_user_logged_in()) { $readonly = 'false'; //REadonly is false if user is logged //Chek if a logged in user has already rated for this post $vote_if_user_already_rated = yasr_check_if_user_already_voted(); if ($vote_if_user_already_rated) { $span_after_rate_it = "<span class=\"yasr-small-block-bold\" id=\"yasr-already-voted-text\">" . __("You've already voted this article with", "yasr") . " {$vote_if_user_already_rated} </span>"; } } else { $readonly = 'true'; //readonly is true if user isn't logged $span_after_rate_it = __("You must sign to vote", "yasr"); } } if (YASR_VISITORS_STATS === 'yes') { $span_dashicon = "<span class=\"dashicons dashicons-chart-bar yasr-dashicons-visitor-stats \" id=\"yasr-total-average-dashicon-{$post_id}\" title=\"yasr-stats-dashicon\"></span>"; } else { $span_dashicon = ""; } if (YASR_TEXT_BEFORE_STARS == 1 && YASR_TEXT_BEFORE_VISITOR_RATING != '') { $text_before_star = str_replace('%total_count%', $votes_number, YASR_TEXT_BEFORE_VISITOR_RATING); $text_before_star = str_replace('%average%', $medium_rating, $text_before_star); $shortcode_html .= "<div class=\"yasr-container-custom-text-and-visitor-rating\">\n <span id=\"yasr-custom-text-before-visitor-rating\">" . $text_before_star . "</span></div>"; } if (YASR_TEXT_BEFORE_STARS == 1 && YASR_TEXT_AFTER_VISITOR_RATING != '') { $text_after_star = str_replace('%total_count%', $votes_number, YASR_TEXT_AFTER_VISITOR_RATING); $text_after_star = str_replace('%average%', $medium_rating, $text_after_star); $span_text_after_star = "<span class=\"yasr-total-average-container\" id=\"yasr-total-average-text_{$post_id}\">" . $text_after_star . "</span>"; } else { $span_text_after_star = "<span class=\"yasr-total-average-container\" id=\"yasr-total-average-text_{$post_id}\">\n [" . __("Total: ", "yasr") . "{$votes_number} " . __("Average: ", "yasr") . "{$medium_rating}/5]\n </span>"; } $shortcode_html .= "<div class=\"{$stars_attribute['class']}\" id=\"yasr_rateit_visitor_votes_{$post_id}\" data-rateit-starwidth=\"{$stars_attribute['px_size']}\" data-rateit-starheight=\"{$stars_attribute['px_size']}\" data-rateit-value=\"{$medium_rating}\" data-rateit-step=\"1\" data-rateit-resetable=\"false\" data-rateit-readonly=\"{$readonly}\"></div>"; $shortcode_html .= $span_dashicon; $shortcode_html .= $span_text_after_star; $shortcode_html .= $span_after_rate_it; $shortcode_html .= "</div>"; $var_tooltip_values = __("bad, poor, ok, good, super", "yasr"); $var_tooltip_values = json_encode($var_tooltip_values); $var_post_id = json_encode($post_id); $var_ajax_url = json_encode(admin_url('admin-ajax.php')); $var_size = json_encode($size); $var_logged_user = json_encode(is_user_logged_in()); $var_vote_if_user_already_rated = json_encode($vote_if_user_already_rated); $var_loader_html = json_encode("{$loader_html}"); $var_nonce_visitor = json_encode("{$ajax_nonce_visitor}"); $var_visitor_stats_enabled = json_encode(YASR_VISITORS_STATS); $javascript = "\n\n <script type=\"text/javascript\">\n\n jQuery(document).ready(function() {\n\n var stringTooltipValues = {$var_tooltip_values};\n var arrayTooltipValues = stringTooltipValues.split(', ');\n var postid = {$var_post_id};\n var ajaxurl = {$var_ajax_url};\n var size = {$var_size};\n var loggedUser = {$var_logged_user};\n var voteIfUserAlredyRated = {$var_vote_if_user_already_rated};\n var loaderHtml = {$var_loader_html};\n var nonceVisitor = {$var_nonce_visitor};\n \n yasrVisitorsVotes(arrayTooltipValues, postid, ajaxurl, size, loggedUser, voteIfUserAlredyRated, loaderHtml, nonceVisitor);\n\n var visitorStatsEnabled = {$var_visitor_stats_enabled};\n\n //If stats are enabled call the function \n if (visitorStatsEnabled == 'yes') {\n yasrDrawTipsProgress (postid, ajaxurl); \n }\n\n });\n\n </script>\n\n "; //IF show visitor votes in loop is disabled use is_singular && is_main query if (YASR_SHOW_VISITOR_VOTES_IN_LOOP === 'disabled') { if (is_singular() && is_main_query()) { return $shortcode_html . $javascript; } } elseif (YASR_SHOW_VISITOR_VOTES_IN_LOOP === 'enabled') { return $shortcode_html . $javascript; } }