1. ### Function: Process Ratings
  2. process_ratings();
  3. function process_ratings() {
  4. global $wpdb, $user_identity, $user_ID;
  5. $rate = intval($_GET['rate']);
  6. $post_id = intval($_GET['pid']);
  7. if($rate > 0 && $post_id > 0 && check_allowtorate()) {
  8. // Check For Bot
  9. $bots_useragent = array('googlebot', 'google', 'msnbot', 'ia_archiver', 'lycos', 'jeeves', 'scooter', 'fast-webcrawler', 'slurp@inktomi', 'turnitinbot', 'technorati', 'yahoo', 'findexa', 'findlinks', 'gaisbo', 'zyborg', 'surveybot', 'bloglines', 'blogsearch', 'ubsub', 'syndic8', 'userland', 'gigabot', 'become.com');
  10. $useragent = $_SERVER['HTTP_USER_AGENT'];
  11. foreach ($bots_useragent as $bot) {
  12. if (stristr($useragent, $bot) !== false) {
  13. return;
  14. }
  15. }
  16. header('Content-Type: text/html; charset='.get_option('blog_charset').'');
  17. postratings_textdomain();
  18. $rated = check_rated($post_id);
  19. // Check Whether Post Has Been Rated By User
  20. if(!$rated) {
  21. // Check Whether Is There A Valid Post
  22. $post = get_post($post_id);
  23. // If Valid Post Then We Rate It
  24. if($post && !wp_is_post_revision($post)) {
  25. $ratings_max = intval(get_option('postratings_max'));
  26. $ratings_custom = intval(get_option('postratings_customrating'));
  27. $ratings_value = get_option('postratings_ratingsvalue');
  28. $post_title = addslashes($post->post_title);
  29. $post_ratings = get_post_custom($post_id);
  30. $post_ratings_users = intval($post_ratings['ratings_users'][0]);
  31. $post_ratings_score = intval($post_ratings['ratings_score'][0]);
  32. // Check For Ratings Lesser Than 1 And Greater Than $ratings_max
  33. if($rate < 1 || $rate > $ratings_max) {
  34. $rate = 0;
  35. }
  36. $post_ratings_users = ($post_ratings_users+1);
  37. $post_ratings_score = ($post_ratings_score+intval($ratings_value[$rate-1]));
  38. $post_ratings_average = round($post_ratings_score/$post_ratings_users, 2);
  39. if (!update_post_meta($post_id, 'ratings_users', $post_ratings_users)) {
  40. add_post_meta($post_id, 'ratings_users', $post_ratings_users, true);
  41. }
  42. if(!update_post_meta($post_id, 'ratings_score', $post_ratings_score)) {
  43. add_post_meta($post_id, 'ratings_score', $post_ratings_score, true);
  44. }
  45. if(!update_post_meta($post_id, 'ratings_average', $post_ratings_average)) {
  46. add_post_meta($post_id, 'ratings_average', $post_ratings_average, true);
  47. }
  48. // Clear cache
  49. wp_cache_post_change($post_id);
  50. // Add Log
  51. if(!empty($user_identity)) {
  52. $rate_user = addslashes($user_identity);
  53. } elseif(!empty($_COOKIE['comment_author_'.COOKIEHASH])) {
  54. $rate_user = addslashes($_COOKIE['comment_author_'.COOKIEHASH]);
  55. } else {
  56. $rate_user = __('Guest', 'wp-postratings');
  57. }
  58. $rate_userid = intval($user_ID);
  59. // Only Create Cookie If User Choose Logging Method 1 Or 3
  60. $postratings_logging_method = intval(get_option('postratings_logging_method'));
  61. if($postratings_logging_method == 1 || $postratings_logging_method == 3) {
  62. $rate_cookie = setcookie("rated_".$post_id, $ratings_value[$rate-1], time() + 30000000, COOKIEPATH);
  63. }
  64. // Log Ratings No Matter What
  65. $rate_log = $wpdb->query("INSERT INTO $wpdb->ratings VALUES (0, $post_id, '$post_title', ".$ratings_value[$rate-1].",'".current_time('timestamp')."', '".get_ipaddress()."', '".esc_attr(@gethostbyaddr(get_ipaddress()))."' ,'$rate_user', $rate_userid)");
  66. // Output AJAX Result
  67. echo the_ratings_results($post_id, $post_ratings_users, $post_ratings_score, $post_ratings_average);
  68. exit();
  69. } else {
  70. printf(__('Invalid Post ID. Post ID #%s.', 'wp-postratings'), $post_id);
  71. exit();
  72. } // End if($post)
  73. } else {
  74. printf(__('You Had Already Rated This Post. Post ID #%s.', 'wp-postratings'), $post_id);
  75. exit();
  76. }// End if(!$rated)
  77. } // End if($rate && $post_id && check_allowtorate())
  78. }
  79.