ratings = $table_prefix . 'ratings';
### Function: Ratings Administration Menu
add_action('admin_menu', 'ratings_menu');
function ratings_menu() {
if (function_exists('add_menu_page')) {
add_menu_page(__('Ratings', 'wp-postratings'), __('Ratings', 'wp-postratings'), 'manage_ratings', 'postratings/postratings-manager.php');
}
if (function_exists('add_submenu_page')) {
add_submenu_page('postratings/postratings-manager.php', __('Manage Ratings', 'wp-postratings'), __('Manage Ratings', 'wp-postratings'), 'manage_ratings', 'postratings/postratings-manager.php');
add_submenu_page('postratings/postratings-manager.php', __('Ratings Options', 'wp-postratings'), __('Ratings Options', 'wp-postratings'), 'manage_ratings', 'postratings/postratings-options.php');
add_submenu_page('postratings/postratings-manager.php', __('Ratings Usage', 'wp-postratings'), __('Ratings Usage', 'wp-postratings'), 'manage_ratings', 'postratings/postratings-usage.php');
}
}
### Function: Display The Rating For The Post
function the_ratings($start_tag = 'div', $display = true) {
global $id;
// Loading Style
$loading = "<$start_tag id=\"post-ratings-$id-loading\" class=\"post-ratings-loading\">
".__('Loading', 'wp-postratings')." ...".$start_tag.">\n";
// Check To See Whether User Has Voted
$user_voted = check_rated($id);
// If User Voted Or Is Not Allowed To Rate
if($user_voted || !check_allowtorate()) {
if(!$display) {
return "<$start_tag id=\"post-ratings-$id\" class=\"post-ratings\">".the_ratings_results($id).''.$start_tag.'>'."\n$loading";
} else {
echo "<$start_tag id=\"post-ratings-$id\" class=\"post-ratings\">".the_ratings_results($id).''.$start_tag.'>'."\n$loading";
return;
}
// If User Has Not Voted
} else {
if(!$display) {
return "<$start_tag id=\"post-ratings-$id\" class=\"post-ratings\">".the_ratings_vote($id).''.$start_tag.'>'."\n$loading";
} else {
echo "<$start_tag id=\"post-ratings-$id\" class=\"post-ratings\">".the_ratings_vote($id).''.$start_tag.'>'."\n$loading\n";
return;
}
}
}
### Function: Displays Rating Header
add_action('wp_head', 'the_ratings_header');
function the_ratings_header() {
if(strpos($_SERVER['SCRIPT_NAME'], 'cgi') === false) {
$ratings_ajax_url = dirname($_SERVER['SCRIPT_NAME']);
} else {
$ratings_ajax_url = dirname($_SERVER['PHP_SELF']);
}
if(substr($ratings_ajax_url, -1) == '/') {
$ratings_ajax_url = substr($ratings_ajax_url, 0, -1);
}
echo "\n".''."\n";
echo ''."\n";
echo ''."\n";
echo ''."\n";
echo ''."\n";
echo ''."\n";
}
### Function: Displays Ratings Header In WP-Admin
add_action('admin_head', 'ratings_header_admin');
function ratings_header_admin() {
echo ''."\n";
}
### Function: Display Ratings Results
function the_ratings_results($post_id, $new_user = 0, $new_score = 0, $new_average = 0) {
$ratings_image = get_settings('postratings_image');
$ratings_max = intval(get_settings('postratings_max'));
if($new_user == 0 && $new_score == 0 && $new_average == 0) {
$post_ratings = get_post_custom($post_id);
$post_ratings_users = $post_ratings['ratings_users'][0];
$post_ratings_score = $post_ratings['ratings_score'][0];
$post_ratings_average = $post_ratings['ratings_average'][0];
} else {
$post_ratings_users = $new_user;
$post_ratings_score = $new_score;
$post_ratings_average = $new_average;
}
$post_ratings_images = '';
if($post_ratings_score == 0 || $post_ratings_users == 0) {
$post_ratings = 0;
$post_ratings_average = 0;
$post_ratings_percentage = 0;
} else {
$post_ratings = round($post_ratings_average, 1);
$post_ratings_percentage = round((($post_ratings_score/$post_ratings_users)/$ratings_max) * 100, 2);
}
// Check For Half Star
$insert_half = 0;
$average_diff = abs(floor($post_ratings_average)-$post_ratings);
if($average_diff >= 0.25 && $average_diff <= 0.75) {
$insert_half = ceil($post_ratings_average);
} elseif($average_diff > 0.75) {
$insert_half = ceil($post_ratings);
}
$post_ratings = intval($post_ratings);
// Display Start Of Rating Image
if(file_exists(ABSPATH.'/wp-content/plugins/postratings/images/'.$ratings_image.'/rating_start.gif')) {
$post_ratings_images .= '
';
}
// Display Rated Images
$image_alt = $post_ratings_users.' '.__('votes', 'wp-postratings').', '.__('average', 'wp-postratings').': '.$post_ratings_average.' '.__('out of', 'wp-postratings').' '.$ratings_max;
for($i=1; $i <= $ratings_max; $i++) {
if($i <= $post_ratings) {
$post_ratings_images .= '
';
} elseif($i == $insert_half) {
$post_ratings_images .= '
';
} else {
$post_ratings_images .= '
';
}
}
// Display End Of Rating Image
if(file_exists(ABSPATH.'/wp-content/plugins/postratings/images/'.$ratings_image.'/rating_end.gif')) {
$post_ratings_images .= '
';
}
// Display User Rated Text
$post_ratings_user_rated = '';
// Display The Contents
$template_postratings_text = stripslashes(get_settings('postratings_template_text'));
$template_postratings_text = str_replace("%RATINGS_IMAGES%", $post_ratings_images, $template_postratings_text);
$template_postratings_text = str_replace("%RATINGS_MAX%", $ratings_max, $template_postratings_text);
$template_postratings_text = str_replace("%RATINGS_SCORE%", $post_ratings_score, $template_postratings_text);
$template_postratings_text = str_replace("%RATINGS_USER_RATED%", $post_ratings_user_rated, $template_postratings_text);
$template_postratings_text = str_replace("%RATINGS_AVERAGE%", $post_ratings_average, $template_postratings_text);
$template_postratings_text = str_replace("%RATINGS_PERCENTAGE%", $post_ratings_percentage, $template_postratings_text);
$template_postratings_text = str_replace("%RATINGS_USERS%", number_format($post_ratings_users), $template_postratings_text);
// Return Post Ratings Template
return $template_postratings_text;
}
### Function: Display Ratings Vote
function the_ratings_vote($post_id, $new_user = 0, $new_score = 0, $new_average = 0) {
$ratings_image = get_settings('postratings_image');
$ratings_max = intval(get_settings('postratings_max'));
if($new_user == 0 && $new_score == 0 && $new_average == 0) {
$post_ratings = get_post_custom($post_id);
$post_ratings_users = $post_ratings['ratings_users'][0];
$post_ratings_score = $post_ratings['ratings_score'][0];
$post_ratings_average = $post_ratings['ratings_average'][0];
} else {
$post_ratings_users = $new_user;
$post_ratings_score = $new_score;
$post_ratings_average = $new_average;
}
$post_ratings_images = '';
if($post_ratings_score == 0 || $post_ratings_users == 0) {
$post_ratings = 0;
$post_ratings_average = 0;
$post_ratings_percentage = 0;
} else {
$post_ratings = round($post_ratings_average, 1);
$post_ratings_percentage = round((($post_ratings_score/$post_ratings_users)/$ratings_max) * 100, 2);
}
// Check For Half Star
$insert_half = 0;
$average_diff = abs(floor($post_ratings_average)-$post_ratings);
if($average_diff >= 0.25 && $average_diff <= 0.75) {
$insert_half = ceil($post_ratings_average);
} elseif($average_diff > 0.75) {
$insert_half = ceil($post_ratings);
}
$post_ratings = intval($post_ratings);
$postratings_ratingstext = get_settings('postratings_ratingstext');
// Display Start Of Rating Image
if(file_exists(ABSPATH.'/wp-content/plugins/postratings/images/'.$ratings_image.'/rating_start.gif')) {
$post_ratings_images .= '
';
}
// Display Rated Images
for($i=1; $i <= $ratings_max; $i++) {
$ratings_text = stripslashes($postratings_ratingstext[$i-1]);
if($i <= $post_ratings) {
$post_ratings_images .= '
';
} elseif($i == $insert_half) {
$post_ratings_images .= '
';
} else {
$post_ratings_images .= '
';
}
}
// Display End Of Rating Image
if(file_exists(ABSPATH.'/wp-content/plugins/postratings/images/'.$ratings_image.'/rating_end.gif')) {
$post_ratings_images .= '
';
}
// Individual Post Ratings Text
$post_ratings_text = '';
// If No Ratings, Return No Ratings templae
if($post_ratings == 0) {
$template_postratings_none = stripslashes(get_settings('postratings_template_none'));
$template_postratings_none = str_replace("%RATINGS_IMAGES_VOTE%", $post_ratings_images, $template_postratings_none);
$template_postratings_none = str_replace("%RATINGS_MAX%", $ratings_max, $template_postratings_none);
$template_postratings_none = str_replace("%RATINGS_SCORE%", $post_ratings_score, $template_postratings_none);
$template_postratings_none = str_replace("%RATINGS_TEXT%", $post_ratings_text, $template_postratings_none);
$template_postratings_none = str_replace("%RATINGS_AVERAGE%", $post_ratings_average, $template_postratings_none);
$template_postratings_none = str_replace("%RATINGS_PERCENTAGE%", $post_ratings_percentage, $template_postratings_none);
$template_postratings_none = str_replace("%RATINGS_USERS%", $post_ratings_users, $template_postratings_none);
// Return Post Ratings Template
return $template_postratings_none;
} else {
// Display The Contents
$template_postratings_vote = stripslashes(get_settings('postratings_template_vote'));
$template_postratings_vote = str_replace("%RATINGS_IMAGES_VOTE%", $post_ratings_images, $template_postratings_vote);
$template_postratings_vote = str_replace("%RATINGS_MAX%", $ratings_max, $template_postratings_vote);
$template_postratings_vote = str_replace("%RATINGS_SCORE%", $post_ratings_score, $template_postratings_vote);
$template_postratings_vote = str_replace("%RATINGS_TEXT%", $post_ratings_text, $template_postratings_vote);
$template_postratings_vote = str_replace("%RATINGS_AVERAGE%", $post_ratings_average, $template_postratings_vote);
$template_postratings_vote = str_replace("%RATINGS_PERCENTAGE%", $post_ratings_percentage, $template_postratings_vote);
$template_postratings_vote = str_replace("%RATINGS_USERS%", number_format($post_ratings_users), $template_postratings_vote);
// Return Post Ratings Voting Template
return $template_postratings_vote;
}
}
### Function: Check Who Is Allow To Rate
function check_allowtorate() {
global $user_ID;
$user_ID = intval($user_ID);
$allow_to_vote = intval(get_settings('postratings_allowtorate'));
switch($allow_to_vote) {
// Guests Only
case 0:
if($user_ID > 0) {
return false;
}
return true;
break;
// Registered Users Only
case 1:
if($user_ID == 0) {
return false;
}
return true;
break;
// Registered Users And Guests
case 2:
default:
return true;
}
}
### Function: Check Whether User Have Rated For The Post
function check_rated($post_id) {
global $user_ID;
$postratings_logging_method = intval(get_settings('postratings_logging_method'));
switch($postratings_logging_method) {
// Do Not Log
case 0:
return false;
break;
// Logged By Cookie
case 1:
return check_rated_cookie($post_id);
break;
// Logged By IP
case 2:
return check_rated_ip($post_id);
break;
// Logged By Cookie And IP
case 3:
$rated_cookie = check_rated_cookie($post_id);
if($rated_cookie > 0) {
return true;
} else {
return check_rated_ip($post_id);
}
break;
// Logged By Username
case 4:
return check_rated_username($post_id);
break;
}
return false;
}
### Function: Check Rated By Cookie
function check_rated_cookie($post_id) {
// 0: False | > 0: True
return intval($_COOKIE["rated_$post_id"]);
}
### Function: Check Rated By IP
function check_rated_ip($post_id) {
global $wpdb;
// Check IP From IP Logging Database
$get_rated = $wpdb->get_var("SELECT rating_ip FROM $wpdb->ratings WHERE rating_postid = $post_id AND rating_ip = '".get_ipaddress()."'");
// 0: False | > 0: True
return intval($get_rated);
}
### Function: Check Rated By Username
function check_rated_username($post_id) {
global $wpdb, $user_ID;
$rating_userid = intval($user_ID);
// Check User ID From IP Logging Database
$get_rated = $wpdb->get_var("SELECT rating_userid FROM $wpdb->ratings WHERE rating_postid = $post_id AND rating_userid = $rating_userid");
// 0: False | > 0: True
return intval($get_rated);
}
### Function: Get IP Address
if(!function_exists('get_ipaddress')) {
function get_ipaddress() {
if (empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
$ip_address = $_SERVER["REMOTE_ADDR"];
} else {
$ip_address = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
if(strpos($ip_address, ',') !== false) {
$ip_address = explode(',', $ip_address);
$ip_address = $ip_address[0];
}
return $ip_address;
}
}
### Function: Place Rating In Content
add_filter('the_content', 'place_ratings', 7);
function place_ratings($content){
$content = preg_replace( "/\[ratings\]/ise", "the_ratings('div', false)", $content);
return $content;
}
### Function: Display Most Rated Page/Post
if(!function_exists('get_most_rated')) {
function get_most_rated($mode = '', $limit = 10, $chars = 0, $display = true) {
global $wpdb, $post;
$where = '';
$temp = '';
if($mode == 'post') {
$where = "$wpdb->posts.post_status = 'publish'";
} elseif($mode == 'page') {
$where = "$wpdb->posts.post_status = 'static'";
} else {
$where = "($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'static')";
}
$most_rated = $wpdb->get_results("SELECT $wpdb->posts.ID, post_title, post_name, post_status, post_date, (meta_value+0.00) AS ratings_votes FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_date < '".current_time('mysql')."' AND $where AND meta_key = 'ratings_users' AND post_password = '' ORDER BY ratings_votes DESC LIMIT $limit");
if($most_rated) {
if($chars > 0) {
foreach ($most_rated as $post) {
$post_title = htmlspecialchars(stripslashes($post->post_title));
$post_votes = intval($post->ratings_votes);
$temp .= "
';
}
// Display Rated Images
for($i=1; $i <= $ratings_max; $i++) {
if($i <= $post_ratings) {
$post_ratings_images .= '
';
} elseif($i == $insert_half) {
$post_ratings_images .= '
';
} else {
$post_ratings_images .= '
';
}
}
// Display End Of Rating Image
if(file_exists(ABSPATH.'/wp-content/plugins/postratings/images/'.$ratings_image.'/rating_end.gif')) {
$post_ratings_images .= '
';
}
if($chars > 0) {
$temp = "