Advertisement
Guest User

Untitled

a guest
Jul 6th, 2011
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 66.93 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WP-PostRatings
  4. Plugin URI: http://lesterchan.net/portfolio/programming/php/
  5. Description: Adds an AJAX rating system for your WordPress blog's post/page.
  6. Version: 1.61
  7. Author: Lester 'GaMerZ' Chan
  8. Author URI: http://lesterchan.net
  9. */
  10.  
  11.  
  12. /*
  13.     Copyright 2009  Lester Chan  (email : lesterchan@gmail.com)
  14.  
  15.     This program is free software; you can redistribute it and/or modify
  16.     it under the terms of the GNU General Public License as published by
  17.     the Free Software Foundation; either version 2 of the License, or
  18.     (at your option) any later version.
  19.  
  20.     This program is distributed in the hope that it will be useful,
  21.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.     GNU General Public License for more details.
  24.  
  25.     You should have received a copy of the GNU General Public License
  26.     along with this program; if not, write to the Free Software
  27.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  28. */
  29.  
  30.  
  31. ### Define Image Extension
  32. define('RATINGS_IMG_EXT', 'gif');
  33. //define('RATINGS_IMG_EXT', 'png');
  34.  
  35.  
  36. ### Load WP-Config File If This File Is Called Directly
  37. if (!function_exists('add_action')) {
  38.     $wp_root = '../../..';
  39.     if (file_exists($wp_root.'/wp-load.php')) {
  40.         require_once($wp_root.'/wp-load.php');
  41.     } else {
  42.         require_once($wp_root.'/wp-config.php');
  43.     }
  44. }
  45.  
  46.  
  47. ### Create Text Domain For Translations
  48. add_action('init', 'postratings_textdomain');
  49. function postratings_textdomain() {
  50.     load_plugin_textdomain('wp-postratings', false, 'wp-postratings');
  51. }
  52.  
  53.  
  54. ### Rating Logs Table Name
  55. global $wpdb;
  56. $wpdb->ratings = $wpdb->prefix.'ratings';
  57.  
  58.  
  59. ### Function: Ratings Administration Menu
  60. add_action('admin_menu', 'ratings_menu');
  61. function ratings_menu() {
  62.     if (function_exists('add_menu_page')) {
  63.         add_menu_page(__('Ratings', 'wp-postratings'), __('Ratings', 'wp-postratings'), 'manage_ratings', 'wp-postratings/postratings-manager.php', '', plugins_url('wp-postratings/images/stars(png)/rating_on.png'));
  64.     }
  65.     if (function_exists('add_submenu_page')) {
  66.         add_submenu_page('wp-postratings/postratings-manager.php', __('Manage Ratings', 'wp-postratings'), __('Manage Ratings', 'wp-postratings'), 'manage_ratings', 'wp-postratings/postratings-manager.php');
  67.         add_submenu_page('wp-postratings/postratings-manager.php', __('Ratings Options', 'wp-postratings'), __('Ratings Options', 'wp-postratings'),  'manage_ratings', 'wp-postratings/postratings-options.php');
  68.         add_submenu_page('wp-postratings/postratings-manager.php', __('Ratings Templates', 'wp-postratings'), __('Ratings Templates', 'wp-postratings'),  'manage_ratings', 'wp-postratings/postratings-templates.php');
  69.         add_submenu_page('wp-postratings/postratings-manager.php', __('Uninstall WP-PostRatings', 'wp-postratings'), __('Uninstall WP-PostRatings', 'wp-postratings'), 'manage_ratings', 'wp-postratings/postratings-uninstall.php');
  70.     }
  71. }
  72.  
  73.  
  74. ### Function: Display The Rating For The Post
  75. function the_ratings($start_tag = 'div', $custom_id = 0, $display = true) {
  76.     global $id;
  77.     // Allow Custom ID
  78.     if(intval($custom_id) > 0) {
  79.         $ratings_id = $custom_id;
  80.     } else {
  81.         $ratings_id = $id;
  82.     }
  83.     // Loading Style
  84.     $postratings_ajax_style = get_option('postratings_ajax_style');
  85.     if(intval($postratings_ajax_style['loading']) == 1) {
  86.         $loading = "\n<$start_tag id=\"post-ratings-$ratings_id-loading\"  class=\"post-ratings-loading\"><img src=\"".plugins_url('wp-postratings/images/loading.gif')."\" width=\"16\" height=\"16\" alt=\"".__('Loading', 'wp-postratings')." ...\" title=\"".__('Loading', 'wp-postratings')." ...\" class=\"post-ratings-image\" />&nbsp;".__('Loading', 'wp-postratings')." ...</".$start_tag.">\n";
  87.     } else {
  88.         $loading = '';
  89.     }
  90.     // Check To See Whether User Has Voted
  91.     $user_voted = check_rated($ratings_id);
  92.     // If User Voted Or Is Not Allowed To Rate
  93.     if($user_voted) {
  94.         if(!$display) {
  95.             return "<$start_tag id=\"post-ratings-$ratings_id\" class=\"post-ratings\">".the_ratings_results($ratings_id).'</'.$start_tag.'>'.$loading;
  96.         } else {
  97.             echo "<$start_tag id=\"post-ratings-$ratings_id\" class=\"post-ratings\">".the_ratings_results($ratings_id).'</'.$start_tag.'>'.$loading;
  98.             return;
  99.         }
  100.     // If User Is Not Allowed To Rate
  101.     } else if(!check_allowtorate()) {
  102.         if(!$display) {
  103.             return "<$start_tag id=\"post-ratings-$ratings_id\" class=\"post-ratings\">".the_ratings_results($ratings_id, 0, 0, 0, 1).'</'.$start_tag.'>'.$loading;
  104.         } else {
  105.             echo "<$start_tag id=\"post-ratings-$ratings_id\" class=\"post-ratings\">".the_ratings_results($ratings_id, 0, 0, 0, 1).'</'.$start_tag.'>'.$loading;
  106.             return;
  107.         }
  108.     // If User Has Not Voted
  109.     } else {
  110.         if(!$display) {
  111.             return "<$start_tag id=\"post-ratings-$ratings_id\" class=\"post-ratings\">".the_ratings_vote($ratings_id).'</'.$start_tag.'>'.$loading;
  112.         } else {
  113.             echo "<$start_tag id=\"post-ratings-$ratings_id\" class=\"post-ratings\">".the_ratings_vote($ratings_id).'</'.$start_tag.'>'.$loading;
  114.             return;
  115.         }
  116.     }
  117. }
  118.  
  119.  
  120. ### Function: Print Out jQuery Script At The Top
  121. add_action('wp_head', 'ratings_javascripts_header');
  122. function ratings_javascripts_header() {
  123.     wp_print_scripts('jquery');
  124. }
  125.  
  126.  
  127. ### Function: Enqueue Ratings JavaScripts/CSS
  128. add_action('wp_enqueue_scripts', 'ratings_scripts');
  129. function ratings_scripts() {
  130.     global $text_direction;
  131.     if(@file_exists(TEMPLATEPATH.'/postratings-css.css')) {
  132.         wp_enqueue_style('wp-postratings', get_stylesheet_directory_uri().'/postratings-css.css', false, '1.50', 'all');
  133.     } else {
  134.         wp_enqueue_style('wp-postratings', plugins_url('wp-postratings/postratings-css.css'), false, '1.50', 'all');
  135.     }  
  136.     if('rtl' == $text_direction) {
  137.         if(@file_exists(TEMPLATEPATH.'/postratings-css-rtl.css')) {
  138.             wp_enqueue_style('wp-postratings-rtl', get_stylesheet_directory_uri().'/postratings-css-rtl.css', false, '1.50', 'all');
  139.         } else {
  140.             wp_enqueue_style('wp-postratings-rtl', plugins_url('wp-postratings/postratings-css-rtl.css'), false, '1.50', 'all');
  141.         }
  142.     }
  143.     $postratings_max = intval(get_option('postratings_max'));
  144.     $postratings_custom = intval(get_option('postratings_customrating'));
  145.     $postratings_ajax_style = get_option('postratings_ajax_style');
  146.     $postratings_javascript = '';
  147.     if($postratings_custom) {
  148.         for($i = 1; $i <= $postratings_max; $i++) {
  149.             $postratings_javascript .= 'var ratings_'.$i.'_mouseover_image=new Image();ratings_'.$i.'_mouseover_image.src=ratingsL10n.plugin_url+"/images/"+ratingsL10n.image+"/rating_'.$i.'_over."+ratingsL10n.image_ext;';
  150.         }
  151.     } else {
  152.         $postratings_javascript = 'var ratings_mouseover_image=new Image();ratings_mouseover_image.src=ratingsL10n.plugin_url+"/images/"+ratingsL10n.image+"/rating_over."+ratingsL10n.image_ext;';
  153.     }
  154.     wp_enqueue_script('wp-postratings', plugins_url('wp-postratings/postratings-js.js'), array('jquery'), '1.50', true);
  155.     wp_localize_script('wp-postratings', 'ratingsL10n', array(
  156.         'plugin_url' => plugins_url('wp-postratings'),
  157.         'ajax_url' => plugins_url('wp-postratings/wp-postratings.php'),
  158.         'text_wait' => __('Please rate only 1 post at a time.', 'wp-postratings'),
  159.         'image' => get_option('postratings_image'),
  160.         'image_ext' => RATINGS_IMG_EXT,
  161.         'max' => $postratings_max,
  162.         'show_loading' => intval($postratings_ajax_style['loading']),
  163.         'show_fading' => intval($postratings_ajax_style['fading']),
  164.         'custom' => $postratings_custom,
  165.         'l10n_print_after' => $postratings_javascript
  166.     ));
  167. }
  168.  
  169.  
  170. ### Function: Enqueue Ratings Stylesheets/JavaScripts In WP-Admin
  171. add_action('admin_enqueue_scripts', 'ratings_scripts_admin');
  172. function ratings_scripts_admin($hook_suffix) {
  173.     $postratings_admin_pages = array('wp-postratings/postratings-manager.php', 'wp-postratings/postratings-options.php', 'wp-postratings/postratings-templates.php', 'wp-postratings/postratings-uninstall.php');
  174.     if(in_array($hook_suffix, $postratings_admin_pages)) {
  175.         wp_enqueue_style('wp-postratings-admin', plugins_url('wp-postratings/postratings-admin-css.css'), false, '1.50', 'all');
  176.         wp_enqueue_script('wp-postratings-admin', plugins_url('wp-postratings/postratings-admin-js.js'), array('jquery'), '1.50', true);
  177.         wp_localize_script('wp-postratings-admin', 'ratingsAdminL10n', array(
  178.             'admin_ajax_url' => plugins_url('wp-postratings/postratings-admin-ajax.php')
  179.         ));
  180.     }
  181. }
  182.  
  183.  
  184. ### Function: Display Ratings Results
  185. function the_ratings_results($post_id, $new_user = 0, $new_score = 0, $new_average = 0, $type = 0) {
  186.     if($new_user == 0 && $new_score == 0 && $new_average == 0) {
  187.         $post_ratings_data = null;
  188.     } else {
  189.         $post_ratings_data->ratings_users = $new_user;
  190.         $post_ratings_data->ratings_score = $new_score;
  191.         $post_ratings_data->ratings_average = $new_average;
  192.     }
  193.     // Display The Contents
  194.     if($type == 1) {
  195.         $template_postratings_text = stripslashes(get_option('postratings_template_permission'));
  196.     } else {
  197.         $template_postratings_text = stripslashes(get_option('postratings_template_text'));
  198.     }
  199.     // Return Post Ratings Template
  200.     return expand_ratings_template($template_postratings_text, $post_id, $post_ratings_data);
  201. }
  202.  
  203.  
  204. ### Function: Display Ratings Vote
  205. function the_ratings_vote($post_id, $new_user = 0, $new_score = 0, $new_average = 0) {
  206.   if($new_user == 0 && $new_score == 0 && $new_average == 0) {
  207.     $post_ratings_data = null;
  208.   } else {
  209.     $post_ratings_data->ratings_users = $new_user;
  210.     $post_ratings_data->ratings_score = $new_score;
  211.     $post_ratings_data->ratings_average = $new_average;
  212.   }
  213.     // If No Ratings, Return No Ratings templae
  214.     if(get_post_meta($post_id, 'ratings_users', true) == 0) {
  215.         $template_postratings_none = stripslashes(get_option('postratings_template_none'));
  216.         // Return Post Ratings Template
  217.         return expand_ratings_template($template_postratings_none, $post_id, $post_ratings_data);
  218.     } else {
  219.         // Display The Contents
  220.         $template_postratings_vote = stripslashes(get_option('postratings_template_vote'));
  221.         // Return Post Ratings Voting Template
  222.         return expand_ratings_template($template_postratings_vote, $post_id, $post_ratings_data);
  223.     }
  224. }
  225.  
  226.  
  227. ### Function: Check Who Is Allow To Rate
  228. function check_allowtorate() {
  229.     global $user_ID;
  230.     $user_ID = intval($user_ID);
  231.     $allow_to_vote = intval(get_option('postratings_allowtorate'));
  232.     switch($allow_to_vote) {
  233.         // Guests Only
  234.         case 0:
  235.             if($user_ID > 0) {
  236.                 return false;
  237.             }
  238.             return true;
  239.             break;
  240.         // Registered Users Only
  241.         case 1:
  242.             if($user_ID == 0) {
  243.                 return false;
  244.             }
  245.             return true;
  246.             break;
  247.         // Registered Users And Guests
  248.         case 2:
  249.         default:
  250.             return true;
  251.     }
  252. }
  253.  
  254.  
  255. ### Function: Check Whether User Have Rated For The Post
  256. function check_rated($post_id) {
  257.     global $user_ID;
  258.     $postratings_logging_method = intval(get_option('postratings_logging_method'));
  259.     switch($postratings_logging_method) {
  260.         // Do Not Log
  261.         case 0:
  262.             return false;
  263.             break;
  264.         // Logged By Cookie
  265.         case 1:
  266.             return check_rated_cookie($post_id);
  267.             break;
  268.         // Logged By IP
  269.         case 2:
  270.             return check_rated_ip($post_id);
  271.             break;
  272.         // Logged By Cookie And IP
  273.         case 3:
  274.             $rated_cookie = check_rated_cookie($post_id);
  275.             if($rated_cookie > 0) {
  276.                 return true;
  277.             } else {
  278.                 return check_rated_ip($post_id);
  279.             }
  280.             break;
  281.         // Logged By Username
  282.         case 4:
  283.             return check_rated_username($post_id);
  284.             break;
  285.     }
  286.     return false;  
  287. }
  288.  
  289.  
  290. ### Function: Check Rated By Cookie
  291. function check_rated_cookie($post_id) {
  292.     if(isset($_COOKIE["rated_$post_id"])) {
  293.         return true;
  294.     } else {
  295.         return false;
  296.     }
  297. }
  298.  
  299.  
  300. ### Function: Check Rated By IP
  301. function check_rated_ip($post_id) {
  302.     global $wpdb;
  303.     // Check IP From IP Logging Database
  304.     $get_rated = $wpdb->get_var("SELECT rating_ip FROM $wpdb->ratings WHERE rating_postid = $post_id AND rating_ip = '".get_ipaddress()."'");
  305.     // 0: False | > 0: True
  306.     return intval($get_rated);
  307. }
  308.  
  309.  
  310. ### Function: Check Rated By Username
  311. function check_rated_username($post_id) {
  312.     global $wpdb, $user_ID;
  313.     if(!is_user_logged_in()) {
  314.         return 0;
  315.     }
  316.     $rating_userid = intval($user_ID);
  317.     // Check User ID From IP Logging Database
  318.     $get_rated = $wpdb->get_var("SELECT rating_userid FROM $wpdb->ratings WHERE rating_postid = $post_id AND rating_userid = $rating_userid");
  319.     // 0: False | > 0: True
  320.     return intval($get_rated);
  321. }
  322.  
  323.  
  324. ### Function: Get Comment Authors Ratings
  325. add_action('loop_start', 'get_comment_authors_ratings');
  326. function get_comment_authors_ratings() {
  327.     global $wpdb, $post, $comment_authors_ratings;
  328.     if(!is_feed() && !is_admin()) {
  329.         $comment_authors_ratings = array();
  330.         if($post->ID) {
  331.             $comment_authors_ratings_results = $wpdb->get_results("SELECT rating_username, rating_rating, rating_ip FROM $wpdb->ratings WHERE rating_postid = ".$post->ID);
  332.         }
  333.         if($comment_authors_ratings_results) {
  334.             foreach($comment_authors_ratings_results as $comment_authors_ratings_result) {
  335.                 $comment_author = stripslashes($comment_authors_ratings_result->rating_username);
  336.                 $comment_authors_ratings[$comment_author] = $comment_authors_ratings_result->rating_rating;
  337.                 $comment_authors_ratings[$comment_authors_ratings_result->rating_ip] = $comment_authors_ratings_result->rating_rating;
  338.             }
  339.         }
  340.     }
  341. }
  342.  
  343.  
  344. ### Function: Comment Author Ratings
  345. function comment_author_ratings($comment_author_specific = '', $display = true) {
  346.     global $comment_authors_ratings;
  347.     if(get_comment_type() == 'comment') {
  348.         $post_ratings_images = '';
  349.         $ratings_image = get_option('postratings_image');
  350.         $ratings_max = intval(get_option('postratings_max'));
  351.         $ratings_custom = intval(get_option('postratings_customrating'));
  352.         if(empty($comment_author_specific)) {
  353.             $comment_author = get_comment_author();
  354.         } else {
  355.             $comment_author = $comment_author_specific;
  356.         }
  357.         $comment_author_rating = intval($comment_authors_ratings[$comment_author]);
  358.         if($comment_author_rating == 0) {
  359.             $comment_author_rating = intval($comment_authors_ratings[get_comment_author_IP()]);
  360.         }
  361.         if($comment_author_rating != 0) {
  362.             // Display Rated Images
  363.             if($ratings_custom && $ratings_max == 2) {
  364.                 if($comment_author_rating > 0) {
  365.                     $comment_author_rating = '+'.$comment_author_rating;
  366.                 }      
  367.             }
  368.             $image_alt = sprintf(__('%s gives a rating of %s', 'wp-postratings'), $comment_author, $comment_author_rating);
  369.             $post_ratings_images = get_ratings_images_comment_author($ratings_custom, $ratings_max, $comment_author_rating, $ratings_image, $image_alt);
  370.         }
  371.         if($display) {
  372.             return $post_ratings_images;
  373.         } else {
  374.             return $post_ratings_images;
  375.         }
  376.     }
  377. }
  378.  
  379.  
  380. ### Function:  Display Comment Author Ratings
  381. //add_filter('comment_text', 'comment_author_ratings_filter');
  382. function comment_author_ratings_filter($comment_text) {
  383.     global $comment, $comment_authors_ratings;
  384.     $output = '';
  385.     if(!is_feed() && !is_admin()) {
  386.         if(get_comment_type() == 'comment') {
  387.             $post_ratings_images = '';
  388.             $ratings_image = get_option('postratings_image');
  389.             $ratings_max = intval(get_option('postratings_max'));
  390.             $ratings_custom = intval(get_option('postratings_customrating'));
  391.             $comment_author = get_comment_author();
  392.             $comment_author_rating = intval($comment_authors_ratings[$comment_author]);
  393.             if($comment_author_rating == 0) {
  394.                 $comment_author_rating = intval($comment_authors_ratings[get_comment_author_IP()]);
  395.             }
  396.             if($comment_author_rating != 0) {
  397.                 // Display Rated Images
  398.                 if($ratings_custom && $ratings_max == 2) {
  399.                     if($comment_author_rating > 0) {
  400.                         $comment_author_rating = '+'.$comment_author_rating;
  401.                     }      
  402.                 }
  403.                 $image_alt = sprintf(__('%s gives a rating of %s', 'wp-postratings'), $comment_author, $comment_author_rating);
  404.                 $post_ratings_images = get_ratings_images_comment_author($ratings_custom, $ratings_max, $comment_author_rating, $ratings_image, $image_alt);
  405.             }
  406.             $output .= '<div class="post-ratings-comment-author">';
  407.             if($post_ratings_images != '') {
  408.                 $output .= get_comment_author().' ratings for this post: '.$post_ratings_images;
  409.             } else {
  410.                 $output .= get_comment_author().' did not rate this post.';
  411.             }
  412.             $output .= '</div>';
  413.         }
  414.     }
  415.     return $comment_text.$output;
  416. }
  417.  
  418.  
  419. ### Function: Get IP Address
  420. if(!function_exists('get_ipaddress')) {
  421.     function get_ipaddress() {
  422.         if (empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
  423.             $ip_address = $_SERVER["REMOTE_ADDR"];
  424.         } else {
  425.             $ip_address = $_SERVER["HTTP_X_FORWARDED_FOR"];
  426.         }
  427.         if(strpos($ip_address, ',') !== false) {
  428.             $ip_address = explode(',', $ip_address);
  429.             $ip_address = $ip_address[0];
  430.         }
  431.         return esc_attr($ip_address);
  432.     }
  433. }
  434.  
  435.  
  436. ### Function: Return All Images From A Rating Image Folder
  437. function ratings_images_folder($folder_name) {
  438.     $normal_images = array('rating_over.'.RATINGS_IMG_EXT, 'rating_on.'.RATINGS_IMG_EXT, 'rating_half.'.RATINGS_IMG_EXT, 'rating_off.'.RATINGS_IMG_EXT);
  439.     $postratings_path = WP_PLUGIN_DIR.'/wp-postratings/images/'.$folder_name;
  440.     $images_count_temp = 1;
  441.     $images_count = 1;
  442.     $count = 0;
  443.     $rating['max'] = 0;
  444.     $rating['custom'] = 0;
  445.     $rating['images'] = array();
  446.     if(is_dir($postratings_path)) {
  447.         if($handle = @opendir($postratings_path)) {
  448.             while (false !== ($filename = readdir($handle))) {  
  449.                 if ($filename != '.' && $filename != '..' && substr($filename, -8) != '-rtl.'.RATINGS_IMG_EXT && strpos($filename, '.') !== 0) {
  450.                     if(in_array($filename, $normal_images)) {
  451.                         $count++;
  452.                     } elseif(intval(substr($filename,7, -7)) > $rating['max']) {
  453.                         $rating['max'] = intval(substr($filename,7, -7));
  454.                     }
  455.                     $rating['images'][] = $filename;
  456.                     $images_count++;
  457.                 }
  458.             }
  459.             closedir($handle);
  460.         }
  461.     }
  462.     if($count != sizeof($normal_images)) {
  463.         $rating['custom'] = 1;
  464.     }
  465.     if($rating['max'] == 0) {
  466.         $rating['max'] = intval(get_option('postratings_max'));
  467.     }
  468.     return $rating;
  469. }
  470.  
  471.  
  472. ### Function: Add PostRatings To Post/Page Automatically
  473. //add_action('the_content', 'add_ratings_to_content');
  474. function add_ratings_to_content($content) {
  475.     if (!is_feed()) {
  476.         $content .= the_ratings('div', 0, false);
  477.     }
  478.     return $content;
  479. }
  480.  
  481.  
  482. ### Function: Short Code For Inserting Ratings Into Posts
  483. add_shortcode('ratings', 'ratings_shortcode');
  484. function ratings_shortcode($atts) {
  485.     extract(shortcode_atts(array('id' => '0', 'results' => false), $atts));
  486.     if(!is_feed()) {
  487.         if($results) {
  488.             return the_ratings_results($id);
  489.         } else {
  490.             return the_ratings('span', $id, false);
  491.         }
  492.     } else {
  493.         return __('Note: There is a rating embedded within this post, please visit this post to rate it.', 'wp-postratings');
  494.     }
  495. }
  496.  
  497.  
  498. ### Function: Snippet Text
  499. if(!function_exists('snippet_text')) {
  500.     function snippet_text($text, $length = 0) {
  501.         if (defined('MB_OVERLOAD_STRING')) {
  502.           $text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset'));
  503.             if (mb_strlen($text) > $length) {
  504.                 return htmlentities(mb_substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...';
  505.             } else {
  506.                 return htmlentities($text, ENT_COMPAT, get_option('blog_charset'));
  507.             }
  508.         } else {
  509.             $text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset'));
  510.             if (strlen($text) > $length) {
  511.                 return htmlentities(substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...';
  512.             } else {
  513.                 return htmlentities($text, ENT_COMPAT, get_option('blog_charset'));
  514.             }
  515.         }
  516.     }
  517. }
  518.  
  519.  
  520. ### Function: Process Post Excerpt, For Some Reasons, The Default get_post_excerpt() Does Not Work As Expected
  521. function ratings_post_excerpt($post_id, $post_excerpt, $post_content) {
  522.     if(post_password_required($post_id)) {
  523.         return __('There is no excerpt because this is a protected post.', 'wp-postratings');
  524.     }
  525.     if(empty($post_excerpt)) {
  526.         return snippet_text(strip_tags($post_content), 200);
  527.     } else {
  528.         return $post_excerpt;
  529.     }
  530. }
  531.  
  532.  
  533. ### Function: Add Rating Custom Fields
  534. add_action('publish_post', 'add_ratings_fields');
  535. add_action('publish_page', 'add_ratings_fields');
  536. function add_ratings_fields($post_ID) {
  537.     global $wpdb;
  538.     if(!wp_is_post_revision($post_ID)) {
  539.         add_post_meta($post_ID, 'ratings_users', 0, true);
  540.         add_post_meta($post_ID, 'ratings_score', 0, true);
  541.         add_post_meta($post_ID, 'ratings_average', 0, true);
  542.     }
  543. }
  544.  
  545.  
  546. ### Function:Delete Rating Custom Fields
  547. add_action('delete_post', 'delete_ratings_fields');
  548. function delete_ratings_fields($post_ID) {
  549.     global $wpdb;
  550.     if(!wp_is_post_revision($post_ID)) {
  551.         delete_post_meta($post_ID, 'ratings_users');
  552.         delete_post_meta($post_ID, 'ratings_score');
  553.         delete_post_meta($post_ID, 'ratings_average'); 
  554.     }
  555. }
  556.  
  557.  
  558. ### Function: Process Ratings
  559. process_ratings();
  560. function process_ratings() {
  561.     global $wpdb, $user_identity, $user_ID;
  562.     $rate = intval($_GET['rate']);
  563.     $post_id = intval($_GET['pid']);
  564.     if($rate > 0 && $post_id > 0 && check_allowtorate()) {     
  565.         // Check For Bot
  566.         $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');
  567.         $useragent = $_SERVER['HTTP_USER_AGENT'];
  568.         foreach ($bots_useragent as $bot) {
  569.             if (stristr($useragent, $bot) !== false) {
  570.                 return;
  571.             }
  572.         }
  573.         header('Content-Type: text/html; charset='.get_option('blog_charset').'');
  574.         postratings_textdomain();
  575.         $rated = check_rated($post_id);
  576.         // Check Whether Post Has Been Rated By User
  577.         if(!$rated) {
  578.             // Check Whether Is There A Valid Post
  579.             $post = get_post($post_id);
  580.             // If Valid Post Then We Rate It
  581.             if($post && !wp_is_post_revision($post)) {
  582.                 $ratings_max = intval(get_option('postratings_max'));
  583.                 $ratings_custom = intval(get_option('postratings_customrating'));
  584.                 $ratings_value = get_option('postratings_ratingsvalue');
  585.                 $post_title = addslashes($post->post_title);
  586.                 $post_ratings = get_post_custom($post_id);
  587.                 $post_ratings_users = intval($post_ratings['ratings_users'][0]);
  588.                 $post_ratings_score = intval($post_ratings['ratings_score'][0]);   
  589.                 // Check For Ratings Lesser Than 1 And Greater Than $ratings_max
  590.                 if($rate < 1 || $rate > $ratings_max) {
  591.                     $rate = 0;
  592.                 }
  593.                 $post_ratings_users = ($post_ratings_users+1);
  594.                 $post_ratings_score = ($post_ratings_score+intval($ratings_value[$rate-1]));
  595.                 $post_ratings_average = round($post_ratings_score/$post_ratings_users, 2);
  596.                 if (!update_post_meta($post_id, 'ratings_users', $post_ratings_users)) {
  597.                     add_post_meta($post_id, 'ratings_users', $post_ratings_users, true);
  598.                 }
  599.                 if(!update_post_meta($post_id, 'ratings_score', $post_ratings_score)) {
  600.                     add_post_meta($post_id, 'ratings_score', $post_ratings_score, true);
  601.                 }
  602.                 if(!update_post_meta($post_id, 'ratings_average', $post_ratings_average)) {
  603.                     add_post_meta($post_id, 'ratings_average', $post_ratings_average, true);   
  604.                 }
  605.                 // Add Log
  606.                 if(!empty($user_identity)) {
  607.                     $rate_user = addslashes($user_identity);
  608.                 } elseif(!empty($_COOKIE['comment_author_'.COOKIEHASH])) {
  609.                     $rate_user = addslashes($_COOKIE['comment_author_'.COOKIEHASH]);
  610.                 } else {
  611.                     $rate_user = __('Guest', 'wp-postratings');
  612.                 }
  613.                 $rate_userid = intval($user_ID);
  614.                 // Only Create Cookie If User Choose Logging Method 1 Or 3
  615.                 $postratings_logging_method = intval(get_option('postratings_logging_method'));
  616.                 if($postratings_logging_method == 1 || $postratings_logging_method == 3) {
  617.                     $rate_cookie = setcookie("rated_".$post_id, $ratings_value[$rate-1], time() + 30000000, COOKIEPATH);
  618.                 }
  619.                 // Log Ratings No Matter What
  620.                 $rate_log = $wpdb->query("INSERT INTO $wpdb->ratings VALUES (0, $post_id, '$post_title', ".$ratings_value[$rate-1].",'".current_time('timestamp')."', '".get_ipaddress()."', '".@gethostbyaddr(get_ipaddress())."' ,'$rate_user', $rate_userid)");
  621.                 // Output AJAX Result
  622.                 echo the_ratings_results($post_id, $post_ratings_users, $post_ratings_score, $post_ratings_average);
  623.                 exit();
  624.             } else {
  625.                 printf(__('Invalid Post ID. Post ID #%s.', 'wp-postratings'), $post_id);
  626.                 exit();
  627.             } // End if($post)
  628.         } else {
  629.             printf(__('You Had Already Rated This Post. Post ID #%s.', 'wp-postratings'), $post_id);
  630.             exit();
  631.         }// End if(!$rated)
  632.     } // End if($rate && $post_id && check_allowtorate())
  633. }
  634.  
  635.  
  636. ### Function: Modify Default WordPress Listing To Make It Sorted By Most Rated
  637. function ratings_most_fields($content) {
  638.     global $wpdb;
  639.     $content .= ", ($wpdb->postmeta.meta_value+0) AS ratings_votes";
  640.     return $content;
  641. }
  642. function ratings_most_join($content) {
  643.     global $wpdb;
  644.     $content .= " LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID";
  645.     return $content;
  646. }
  647. function ratings_most_where($content) {
  648.     global $wpdb;
  649.     $content .= " AND $wpdb->postmeta.meta_key = 'ratings_users'";
  650.     return $content;
  651. }
  652. function ratings_most_orderby($content) {
  653.     $orderby = trim(addslashes(get_query_var('r_orderby')));
  654.     if(empty($orderby) && ($orderby != 'asc' || $orderby != 'desc')) {
  655.         $orderby = 'desc';
  656.     }
  657.     $content = " ratings_votes $orderby";
  658.     return $content;
  659. }
  660.  
  661.  
  662. ### Function: Modify Default WordPress Listing To Make It Sorted By Highest Rated
  663. function ratings_highest_fields($content) {
  664.     $content .= ", (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users";
  665.     return $content;
  666. }
  667. function ratings_highest_join($content) {
  668.     global $wpdb;
  669.     $content .= " LEFT JOIN $wpdb->postmeta AS t1 ON t1.post_id = $wpdb->posts.ID LEFT JOIN $wpdb->postmeta As t2 ON t1.post_id = t2.post_id";
  670.     return $content;
  671. }
  672. function ratings_highest_where($content) {
  673.     $ratings_max = intval(get_option('postratings_max'));
  674.     $ratings_custom = intval(get_option('postratings_customrating'));
  675.     if($ratings_custom && $ratings_max == 2) {
  676.         $content .= " AND t1.meta_key = 'ratings_score' AND t2.meta_key = 'ratings_users'";
  677.     } else {
  678.         $content .= " AND t1.meta_key = 'ratings_average' AND t2.meta_key = 'ratings_users'";
  679.     }  
  680.     return $content;
  681. }
  682. function ratings_highest_orderby($content) {
  683.     $orderby = trim(addslashes(get_query_var('r_orderby')));
  684.     if(empty($orderby) || ($orderby != 'asc' && $orderby != 'desc')) {
  685.         $orderby = 'desc';
  686.     }
  687.     $content = " ratings_average $orderby, ratings_users $orderby";
  688.     return $content;
  689. }
  690.  
  691.  
  692. ### Function: Ratings Public Variables
  693. add_filter('query_vars', 'ratings_variables');
  694. function ratings_variables($public_query_vars) {
  695.     $public_query_vars[] = 'r_sortby';
  696.     $public_query_vars[] = 'r_orderby';
  697.     return $public_query_vars;
  698. }
  699.  
  700.  
  701. ### Function: Sort Ratings Posts
  702. add_action('pre_get_posts', 'ratings_sorting');
  703. function ratings_sorting($local_wp_query) {
  704.     if($local_wp_query->get('r_sortby') == 'most_rated') {
  705.         add_filter('posts_fields', 'ratings_most_fields');
  706.         add_filter('posts_join', 'ratings_most_join');
  707.         add_filter('posts_where', 'ratings_most_where');
  708.         add_filter('posts_orderby', 'ratings_most_orderby');
  709.         remove_filter('posts_fields', 'ratings_highest_fields');
  710.         remove_filter('posts_join', 'ratings_highest_join');
  711.         remove_filter('posts_where', 'ratings_highest_where');
  712.         remove_filter('posts_orderby', 'ratings_highest_orderby');
  713.     } elseif($local_wp_query->get('r_sortby') == 'highest_rated') {
  714.         add_filter('posts_fields', 'ratings_highest_fields');
  715.         add_filter('posts_join', 'ratings_highest_join');
  716.         add_filter('posts_where', 'ratings_highest_where');
  717.         add_filter('posts_orderby', 'ratings_highest_orderby');
  718.         remove_filter('posts_fields', 'ratings_most_fields');
  719.         remove_filter('posts_join', 'ratings_most_join');
  720.         remove_filter('posts_where', 'ratings_most_where');
  721.         remove_filter('posts_orderby', 'ratings_most_orderby');
  722.     } else {
  723.         remove_filter('posts_fields', 'ratings_highest_fields');
  724.         remove_filter('posts_join', 'ratings_highest_join');
  725.         remove_filter('posts_where', 'ratings_highest_where');
  726.         remove_filter('posts_orderby', 'ratings_highest_orderby');
  727.         remove_filter('posts_fields', 'ratings_most_fields');
  728.         remove_filter('posts_join', 'ratings_most_join');
  729.         remove_filter('posts_where', 'ratings_most_where');
  730.         remove_filter('posts_orderby', 'ratings_most_orderby');
  731.     }
  732. }
  733.  
  734.  
  735. ### Function: Plug Into WP-Stats
  736. add_action('wp','postratings_wp_stats');
  737. function postratings_wp_stats() {
  738.     if(function_exists('stats_page')) {
  739.         if(strpos(get_option('stats_url'), $_SERVER['REQUEST_URI']) || strpos($_SERVER['REQUEST_URI'], 'stats-options.php') || strpos($_SERVER['REQUEST_URI'], 'wp-stats/wp-stats.php')) {
  740.             add_filter('wp_stats_page_admin_plugins', 'postratings_page_admin_general_stats');
  741.             add_filter('wp_stats_page_admin_most', 'postratings_page_admin_most_stats');
  742.             add_filter('wp_stats_page_plugins', 'postratings_page_general_stats');
  743.             add_filter('wp_stats_page_most', 'postratings_page_most_stats');
  744.         }
  745.     }
  746. }
  747.  
  748.  
  749. ### Function: Add WP-PostRatings General Stats To WP-Stats Page Options
  750. function postratings_page_admin_general_stats($content) {
  751.     $stats_display = get_option('stats_display');
  752.     if($stats_display['ratings'] == 1) {
  753.         $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_ratings" value="ratings" checked="checked" />&nbsp;&nbsp;<label for="wpstats_ratings">'.__('WP-PostRatings', 'wp-postratings').'</label><br />'."\n";
  754.     } else {
  755.         $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_ratings" value="ratings" />&nbsp;&nbsp;<label for="wpstats_ratings">'.__('WP-PostRatings', 'wp-postratings').'</label><br />'."\n";
  756.     }
  757.     return $content;
  758. }
  759.  
  760.  
  761. ### Function: Add WP-PostRatings Top Most/Highest Stats To WP-Stats Page Options
  762. function postratings_page_admin_most_stats($content) {
  763.     $stats_display = get_option('stats_display');
  764.     $stats_mostlimit = intval(get_option('stats_mostlimit'));
  765.     if($stats_display['rated_highest_post'] == 1) {
  766.         $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_highest_post" value="rated_highest_post" checked="checked" />&nbsp;&nbsp;<label for="wpstats_rated_highest_post">'.sprintf(_n('%s Highest Rated Post', '%s Highest Rated Posts', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
  767.     } else {
  768.         $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_highest_post" value="rated_highest_post" />&nbsp;&nbsp;<label for="wpstats_rated_highest_post">'.sprintf(_n('%s Highest Rated Post', '%s Highest Rated Posts', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
  769.     }
  770.     if($stats_display['rated_highest_page'] == 1) {
  771.         $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_highest_page" value="rated_highest_page" checked="checked" />&nbsp;&nbsp;<label for="wpstats_rated_highest_page">'.sprintf(_n('%s Highest Rated Page', '%s Highest Rated Pages', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
  772.     } else {
  773.         $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_highest_page" value="rated_highest_page" />&nbsp;&nbsp;<label for="wpstats_rated_highest_page">'.sprintf(_n('%s Highest Rated Page', '%s Highest Rated Pages', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
  774.     }
  775.     if($stats_display['rated_most_post'] == 1) {
  776.         $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_most_post" value="rated_most_post" checked="checked" />&nbsp;&nbsp;<label for="wpstats_rated_most_post">'.sprintf(_n('%s Most Rated Post', '%s Most Rated Posts', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
  777.     } else {
  778.         $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_most_post" value="rated_most_post" />&nbsp;&nbsp;<label for="wpstats_rated_most_post">'.sprintf(_n('%s Most Rated Post', '%s Most Rated Posts', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
  779.     }
  780.     if($stats_display['rated_most_page'] == 1) {
  781.         $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_most_page" value="rated_most_page" checked="checked" />&nbsp;&nbsp;<label for="wpstats_rated_most_page">'.sprintf(_n('%s Most Rated Page', '%s Most Rated Pages', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
  782.     } else {
  783.         $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_most_page" value="rated_most_page" />&nbsp;&nbsp;<label for="wpstats_rated_most_page">'.sprintf(_n('%s Most Rated Page', '%s Most Rated Pages', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
  784.     }
  785.     return $content;
  786. }
  787.  
  788.  
  789. ### Function: Add WP-PostRatings General Stats To WP-Stats Page
  790. function postratings_page_general_stats($content) {
  791.     $stats_display = get_option('stats_display');
  792.     if($stats_display['ratings'] == 1) {
  793.         $content .= '<p><strong>'.__('WP-PostRatings', 'wp-postratings').'</strong></p>'."\n";
  794.         $content .= '<ul>'."\n";
  795.         $content .= '<li>'.sprintf(_n('<strong>%s</strong> user casted his vote.', '<strong>%s</strong> users casted their vote.', get_ratings_users(false), 'wp-postratings'), number_format_i18n(get_ratings_users(false))).'</li>'."\n";
  796.         $content .= '</ul>'."\n";
  797.     }
  798.     return $content;
  799. }
  800.  
  801.  
  802. ### Function: Add WP-PostRatings Top Most/Highest Stats To WP-Stats Page
  803. function postratings_page_most_stats($content) {
  804.     $stats_display = get_option('stats_display');
  805.     $stats_mostlimit = intval(get_option('stats_mostlimit'));
  806.     if($stats_display['rated_highest_post'] == 1) {
  807.         $content .= '<p><strong>'.sprintf(_n('%s Highest Rated Post', '%s Highest Rated Posts', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</strong></p>'."\n";
  808.         $content .= '<ul>'."\n";
  809.         $content .= get_highest_rated('post', 0, $stats_mostlimit, 0, false);
  810.         $content .= '</ul>'."\n";
  811.     }
  812.     if($stats_display['rated_highest_page'] == 1) {
  813.         $content .= '<p><strong>'.sprintf(_n('%s Highest Rated Page', '%s Highest Rated Pages', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</strong></p>'."\n";
  814.         $content .= '<ul>'."\n";
  815.         $content .= get_highest_rated('page', 0, $stats_mostlimit, 0, false);
  816.         $content .= '</ul>'."\n";
  817.     }
  818.     if($stats_display['rated_most_post'] == 1) {
  819.         $content .= '<p><strong>'.sprintf(_n('%s Most Rated Post', '%s Most Rated Posts', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</strong></p>'."\n";
  820.         $content .= '<ul>'."\n";
  821.         $content .= get_most_rated('post', 0, $stats_mostlimit, 0, false);
  822.         $content .= '</ul>'."\n";
  823.     }
  824.     if($stats_display['rated_most_page'] == 1) {
  825.         $content .= '<p><strong>'.sprintf(_n('%s Most Rated Page', '%s Most Rated Pages', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</strong></p>'."\n";
  826.         $content .= '<ul>'."\n";
  827.         $content .= get_most_rated('page', 0, $stats_mostlimit, 0, false);
  828.         $content .= '</ul>'."\n";
  829.     }
  830.     return $content;
  831. }
  832.  
  833.  
  834. ### Function: Gets HTML of rating images
  835. function get_ratings_images($ratings_custom, $ratings_max, $post_rating, $ratings_image, $image_alt, $insert_half) {
  836.     global $text_direction;
  837.     $ratings_images = '';
  838.     if('rtl' == $text_direction && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT)) {
  839.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  840.     } elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start.'.RATINGS_IMG_EXT)) {
  841.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_start.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  842.     }
  843.     if($ratings_custom) {
  844.         for($i=1; $i <= $ratings_max; $i++) {
  845.             if($i <= $post_rating) {
  846.                 $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_on.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  847.             } elseif($i == $insert_half) {            
  848.                 if ('rtl' == $text_direction && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_half-rtl.'.RATINGS_IMG_EXT)) {
  849.                     $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_half-rtl.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  850.                 } else {
  851.                     $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_half.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  852.                 }
  853.             } else {
  854.                 $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_off.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  855.             }
  856.         }
  857.     } else {
  858.         for($i=1; $i <= $ratings_max; $i++) {
  859.             if($i <= $post_rating) {
  860.                 $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_on.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  861.             } elseif($i == $insert_half) {
  862.                 if ('rtl' == $text_direction && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_half-rtl.'.RATINGS_IMG_EXT)) {
  863.                     $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_half-rtl.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  864.                 } else {
  865.                     $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_half.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  866.                 }
  867.             } else {
  868.                 $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_off.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  869.             }
  870.         }
  871.     }
  872.     if('rtl' == $text_direction && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT)) {
  873.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  874.     } elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_end.'.RATINGS_IMG_EXT)) {
  875.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_end.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  876.     }
  877.     return $ratings_images;
  878. }
  879.  
  880.  
  881. ### Function: Gets HTML of rating images for voting
  882. function get_ratings_images_vote($post_id, $ratings_custom, $ratings_max, $post_rating, $ratings_image, $image_alt, $insert_half, $ratings_texts) {
  883.     global $text_direction;
  884.     $ratings_images = '';
  885.     if('rtl' == $text_direction && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT)) {
  886.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  887.     } elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start.'.RATINGS_IMG_EXT)) {
  888.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_start.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  889.     }
  890.     if($ratings_custom) {
  891.         for($i=1; $i <= $ratings_max; $i++) {
  892.             if ('rtl' == $text_direction && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'half-rtl.'.RATINGS_IMG_EXT)) {
  893.                 $use_half_rtl = 1;
  894.             } else {
  895.                 $use_half_rtl = 0;
  896.             }
  897.             $ratings_text = stripslashes($ratings_texts[$i-1]);
  898.             if($i <= $post_rating) {
  899.                 $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_on.'.RATINGS_IMG_EXT).'" alt="'.$ratings_text.'" title="'.$ratings_text.'" onmouseover="current_rating('.$post_id.', '.$i.', \''.$ratings_text.'\');" onmouseout="ratings_off('.$post_rating.', '.$insert_half.', '.$use_half_rtl.');" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />';    
  900.             } elseif($i == $insert_half) {
  901.                 if ($use_half_rtl) {
  902.                     $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_half-rtl.'.RATINGS_IMG_EXT).'" alt="'.$ratings_text.'" title="'.$ratings_text.'" onmouseover="current_rating('.$post_id.', '.$i.', \''.$ratings_text.'\');" onmouseout="ratings_off('.$post_rating.', '.$insert_half.', '.$use_half_rtl.');" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />';
  903.                 } else {
  904.                     $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_half.'.RATINGS_IMG_EXT).'" alt="'.$ratings_text.'" title="'.$ratings_text.'" onmouseover="current_rating('.$post_id.', '.$i.', \''.$ratings_text.'\');" onmouseout="ratings_off('.$post_rating.', '.$insert_half.', '.$use_half_rtl.');" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />';
  905.                 }
  906.             } else {
  907.                 $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_off.'.RATINGS_IMG_EXT).'" alt="'.$ratings_text.'" title="'.$ratings_text.'" onmouseover="current_rating('.$post_id.', '.$i.', \''.$ratings_text.'\');" onmouseout="ratings_off('.$post_rating.', '.$insert_half.', '.$use_half_rtl.');" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />';
  908.             }
  909.         }
  910.     } else {
  911.         if ('rtl' == $text_direction && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_half-rtl.'.RATINGS_IMG_EXT)) {
  912.             $use_half_rtl = 1;
  913.         } else {
  914.             $use_half_rtl = 0;
  915.         }
  916.         for($i=1; $i <= $ratings_max; $i++) {
  917.             $ratings_text = stripslashes($ratings_texts[$i-1]);
  918.             if($i <= $post_rating) {
  919.                 $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_on.'.RATINGS_IMG_EXT).'" alt="'.$ratings_text.'" title="'.$ratings_text.'" onmouseover="current_rating('.$post_id.', '.$i.', \''.$ratings_text.'\');" onmouseout="ratings_off('.$post_rating.', '.$insert_half.', '.$use_half_rtl.');" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />';    
  920.             } elseif($i == $insert_half) {
  921.                 if ($use_half_rtl) {
  922.                     $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_half-rtl.'.RATINGS_IMG_EXT).'" alt="'.$ratings_text.'" title="'.$ratings_text.'" onmouseover="current_rating('.$post_id.', '.$i.', \''.$ratings_text.'\');" onmouseout="ratings_off('.$post_rating.', '.$insert_half.', '.$use_half_rtl.');" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />';
  923.                 } else {
  924.                     $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_half.'.RATINGS_IMG_EXT).'" alt="'.$ratings_text.'" title="'.$ratings_text.'" onmouseover="current_rating('.$post_id.', '.$i.', \''.$ratings_text.'\');" onmouseout="ratings_off('.$post_rating.', '.$insert_half.', '.$use_half_rtl.');" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />';
  925.                 }
  926.             } else {
  927.                 $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_off.'.RATINGS_IMG_EXT).'" alt="'.$ratings_text.'" title="'.$ratings_text.'" onmouseover="current_rating('.$post_id.', '.$i.', \''.$ratings_text.'\');" onmouseout="ratings_off('.$post_rating.', '.$insert_half.', '.$use_half_rtl.');" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />';
  928.             }
  929.         }
  930.     }
  931.     if('rtl' == $text_direction && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT)) {
  932.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  933.     } elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_end.'.RATINGS_IMG_EXT)) {
  934.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_end.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  935.     }
  936.     return $ratings_images;
  937. }
  938.  
  939.  
  940. ### Function: Gets HTML of rating images for comment author
  941. function get_ratings_images_comment_author($ratings_custom, $ratings_max, $comment_author_rating, $ratings_image, $image_alt) {
  942.     global $text_direction;
  943.     $ratings_images = '';
  944.     if('rtl' == $text_direction && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT)) {
  945.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  946.     } elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start.'.RATINGS_IMG_EXT)) {
  947.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_start.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  948.     }
  949.     if($ratings_custom && $ratings_max == 2) {
  950.         if($comment_author_rating > 0) {
  951.             $ratings_images .= '<img src="'.plugins_url('wp-postratings/images/'.$ratings_image.'/rating_2_on.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  952.         } else {
  953.             $ratings_images .= '<img src="'.plugins_url('wp-postratings/images/'.$ratings_image.'/rating_1_on.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  954.         }
  955.     } elseif($ratings_custom) {
  956.         for($i=1; $i <= $ratings_max; $i++) {
  957.             if($i <= $comment_author_rating) {
  958.                 $ratings_images .= '<img src="'.plugins_url('wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_on.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  959.             } else {
  960.                 $ratings_images .= '<img src="'.plugins_url('wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_off.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  961.             }
  962.         }
  963.     } else {
  964.         for($i=1; $i <= $ratings_max; $i++) {
  965.             if($i <= $comment_author_rating) {
  966.                 $ratings_images .= '<img src="'.plugins_url('wp-postratings/images/'.$ratings_image.'/rating_on.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';    
  967.             } else {
  968.                 $ratings_images .= '<img src="'.plugins_url('wp-postratings/images/'.$ratings_image.'/rating_off.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  969.             }
  970.         }
  971.     }
  972.     if('rtl' == $text_direction && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT)) {
  973.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  974.     } elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_end.'.RATINGS_IMG_EXT)) {
  975.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_end.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  976.     }
  977.     return $ratings_images;
  978. }
  979.  
  980. ### Function: Replaces the template's variables with appropriate values
  981. function expand_ratings_template($template, $post_id, $post_ratings_data = null, $max_post_title_chars = 0) {
  982.     global $post;
  983.     $temp_post = $post;
  984.     // Get global variables
  985.     $ratings_image = get_option('postratings_image');
  986.     $ratings_max = intval(get_option('postratings_max'));
  987.     $ratings_custom = intval(get_option('postratings_customrating'));
  988.     // Get post related variables
  989.     if(is_null($post_ratings_data)) {
  990.         $post_ratings_data = get_post_custom($post_id);
  991.         $post_ratings_users = intval($post_ratings_data['ratings_users'][0]);
  992.         $post_ratings_score = intval($post_ratings_data['ratings_score'][0]);
  993.         $post_ratings_average = floatval($post_ratings_data['ratings_average'][0]);
  994.     } else {
  995.         $post_ratings_users = intval($post_ratings_data->ratings_users);
  996.         $post_ratings_score = intval($post_ratings_data->ratings_score);
  997.         $post_ratings_average = floatval($post_ratings_data->ratings_average);
  998.     }
  999.     if($post_ratings_score == 0 || $post_ratings_users == 0) {
  1000.         $post_ratings = 0;
  1001.         $post_ratings_average = 0;
  1002.         $post_ratings_percentage = 0;
  1003.     } else {
  1004.         $post_ratings = round($post_ratings_average, 1);
  1005.         $post_ratings_percentage = round((($post_ratings_score/$post_ratings_users)/$ratings_max) * 100, 2);    
  1006.     }
  1007.     $post_ratings_text = '<span class="post-ratings-text" id="ratings_'.$post_id.'_text"></span>';
  1008.     // Get the image's alt text
  1009.     if($ratings_custom && $ratings_max == 2) {
  1010.         if($post_ratings_score > 0) {
  1011.             $post_ratings_score = '+'.$post_ratings_score;
  1012.         }
  1013.         $post_ratings_alt_text = sprintf(_n('%s rating', '%s rating', $post_ratings_score, 'wp-postratings'), number_format_i18n($post_ratings_score)).__(',', 'wp-postratings').' '.sprintf(_n('%s vote', '%s votes', $post_ratings_users, 'wp-postratings'), number_format_i18n($post_ratings_users));
  1014.     } else {
  1015.         $post_ratings_score = number_format_i18n($post_ratings_score);
  1016.         $post_ratings_alt_text = sprintf(_n('%s vote', '%s votes', $post_ratings_users, 'wp-postratings'), number_format_i18n($post_ratings_users)).__(',', 'wp-postratings').' '.__('average', 'wp-postratings').': '.number_format_i18n($post_ratings_average, 2).' '.__('out of', 'wp-postratings').' '.number_format_i18n($ratings_max);
  1017.     }
  1018.     // Check for half star
  1019.     $insert_half = 0;
  1020.     $average_diff = abs(floor($post_ratings_average)-$post_ratings);
  1021.     if($average_diff >= 0.25 && $average_diff <= 0.75) {
  1022.         $insert_half = ceil($post_ratings_average);
  1023.     } elseif($average_diff > 0.75) {
  1024.         $insert_half = ceil($post_ratings);
  1025.     }  
  1026.     // Replace the variables
  1027.     $value = $template;
  1028.     if (strpos($template, '%RATINGS_IMAGES%') !== false) {
  1029.         $post_ratings_images = get_ratings_images($ratings_custom, $ratings_max, $post_ratings, $ratings_image, $post_ratings_alt_text, $insert_half);
  1030.         $value = str_replace("%RATINGS_IMAGES%", $post_ratings_images, $value);
  1031.     }
  1032.     if (strpos($template, '%RATINGS_IMAGES_VOTE%') !== false) {
  1033.         $ratings_texts = get_option('postratings_ratingstext');
  1034.         $post_ratings_images = get_ratings_images_vote($post_id, $ratings_custom, $ratings_max, $post_ratings, $ratings_image, $post_ratings_alt_text, $insert_half, $ratings_texts);
  1035.         $value = str_replace("%RATINGS_IMAGES_VOTE%", $post_ratings_images, $value);
  1036.     }
  1037.     $value = str_replace("%RATINGS_ALT_TEXT%", $post_ratings_alt_text, $value);
  1038.     $value = str_replace("%RATINGS_TEXT%", $post_ratings_text, $value);
  1039.     $value = str_replace("%RATINGS_MAX%", number_format_i18n($ratings_max), $value);
  1040.     $value = str_replace("%RATINGS_SCORE%", $post_ratings_score, $value);
  1041.     $value = str_replace("%RATINGS_AVERAGE%", number_format_i18n($post_ratings_average, 2), $value);
  1042.     $value = str_replace("%RATINGS_PERCENTAGE%", number_format_i18n($post_ratings_percentage, 2), $value);
  1043.     $value = str_replace("%RATINGS_USERS%", number_format_i18n($post_ratings_users), $value);
  1044.     if (strpos($template, '%POST_URL%') !== false) {
  1045.         $post_link = get_permalink($post_id);
  1046.         $value = str_replace("%POST_URL%", $post_link, $value);
  1047.     }
  1048.     if (strpos($template, '%POST_TITLE%') !== false) {
  1049.         $post_title = get_the_title($post_id);
  1050.         if ($max_post_title_chars > 0) {
  1051.             $post_title = snippet_text($post_title, $max_post_title_chars);
  1052.         }
  1053.         $value = str_replace("%POST_TITLE%", $post_title, $value);
  1054.     }
  1055.     if (strpos($template, '%POST_EXCERPT%') !== false) {
  1056.         if ($post->ID != $post_id) {
  1057.             $post = &get_post($post_id);
  1058.         }
  1059.         $post_excerpt = ratings_post_excerpt($post_id, $post->post_excerpt, $post->post_content, $post->post_password);
  1060.         $value = str_replace("%POST_EXCERPT%", $post_excerpt, $value);
  1061.     }
  1062.     if (strpos($template, '%POST_CONTENT%') !== false) {
  1063.         if ($post->ID != $post_id) {
  1064.             $post = &get_post($post_id);
  1065.         }
  1066.         $value = str_replace("%POST_CONTENT%", get_the_content(), $value);
  1067.     }
  1068.     // Return value
  1069.     $post = $temp_post;
  1070.     return apply_filters('expand_ratings_template', htmlspecialchars_decode($value));
  1071. }
  1072.  
  1073.  
  1074. ### Class: WP-PostRatings Widget
  1075. class WP_Widget_PostRatings extends WP_Widget {
  1076.     // Constructor
  1077.     function WP_Widget_PostRatings() {
  1078.         $widget_ops = array('description' => __('WP-PostRatings ratings statistics', 'wp-postratings'));
  1079.         $this->WP_Widget('ratings-widget', __('Ratings', 'wp-postratings'), $widget_ops);
  1080.     }
  1081.  
  1082.     // Display Widget
  1083.     function widget($args, $instance) {
  1084.         extract($args);
  1085.         $title = apply_filters('widget_title', esc_attr($instance['title']));
  1086.         $type = esc_attr($instance['type']);
  1087.         $mode = esc_attr($instance['mode']);
  1088.         $limit = intval($instance['limit']);
  1089.         $min_votes = intval($instance['min_votes']);
  1090.         $chars = intval($instance['chars']);
  1091.         $cat_ids = explode(',', esc_attr($instance['cat_ids']));
  1092.         $time_range = esc_attr($instance['time_range']);
  1093.         echo $before_widget.$before_title.$title.$after_title;
  1094.         echo '<ul>'."\n";
  1095.         switch($type) {
  1096.             case 'most_rated':
  1097.                 get_most_rated($mode, $min_votes, $limit, $chars);
  1098.                 break;
  1099.             case 'most_rated_category':
  1100.                 get_most_rated($cat_ids, $mode, $min_votes, $limit, $chars);
  1101.                 break;
  1102.             case 'most_rated_range':
  1103.                 get_most_rated_range($time_range, $mode, $limit, $chars);
  1104.                 break;
  1105.             case 'most_rated_range_category':
  1106.                 get_most_rated_range_category($time_range, $cat_ids, $mode, $limit, $chars);
  1107.                 break;
  1108.             case 'highest_rated':
  1109.                 get_highest_rated($mode, $min_votes, $limit, $chars);
  1110.                 break;
  1111.             case 'highest_rated_category':
  1112.                 get_highest_rated_category($cat_ids, $mode, $min_votes, $limit, $chars);
  1113.                 break;
  1114.             case 'highest_rated_range':
  1115.                 get_highest_rated_range($time_range, $mode, $limit, $chars);
  1116.                 break;
  1117.             case 'highest_rated_range_category':
  1118.                 get_highest_rated_range_category($time_range, $cat_ids, $mode, $limit, $chars);
  1119.                 break;
  1120.             case 'lowest_rated':
  1121.                 get_lowest_rated($mode, $min_votes, $limit, $chars);
  1122.                 break;
  1123.             case 'lowest_rated_category':
  1124.                 get_lowest_rated_category($cat_ids, $mode, $min_votes, $limit, $chars);
  1125.                 break;
  1126.             case 'lowest_rated_range':
  1127.                 get_lowest_rated_range($time_range, $mode, $limit, $chars);
  1128.                 break;
  1129.             case 'highest_score':
  1130.                 get_highest_score($mode, $min_votes, $limit, $chars);
  1131.                 break;
  1132.             case 'highest_score_category':
  1133.                 get_highest_score_category($cat_ids, $mode, $min_votes, $limit, $chars);
  1134.                 break;
  1135.             case 'highest_score_range':
  1136.                 get_highest_score_range($time_range, $mode, $limit, $chars);
  1137.                 break;
  1138.             case 'highest_score_range_category':
  1139.                 get_highest_score_range_category($time_range, $cat_ids, $mode, $limit, $chars);
  1140.                 break;
  1141.         }
  1142.         echo '</ul>'."\n";
  1143.         echo $after_widget;
  1144.     }
  1145.  
  1146.     // When Widget Control Form Is Posted
  1147.     function update($new_instance, $old_instance) {
  1148.         if (!isset($new_instance['submit'])) {
  1149.             return false;
  1150.         }
  1151.         $instance = $old_instance;
  1152.         $instance['title'] = strip_tags($new_instance['title']);
  1153.         $instance['type'] = strip_tags($new_instance['type']);
  1154.         $instance['mode'] = strip_tags($new_instance['mode']);
  1155.         $instance['limit'] = intval($new_instance['limit']);
  1156.         $instance['min_votes'] = intval($new_instance['min_votes']);
  1157.         $instance['chars'] = intval($new_instance['chars']);
  1158.         $instance['cat_ids'] = strip_tags($new_instance['cat_ids']);
  1159.         $instance['time_range'] = strip_tags($new_instance['time_range']);
  1160.         return $instance;
  1161.     }
  1162.  
  1163.     // DIsplay Widget Control Form
  1164.     function form($instance) {
  1165.         global $wpdb;
  1166.         $instance = wp_parse_args((array) $instance, array('title' => __('Ratings', 'wp-postratings'), 'type' => 'highest_rated', 'mode' => 'both', 'limit' => 10, 'min_votes' => 0, 'chars' => 200, 'cat_ids' => '0', 'time_range' => '1 day'));
  1167.         $title = esc_attr($instance['title']);
  1168.         $type = esc_attr($instance['type']);
  1169.         $mode = esc_attr($instance['mode']);
  1170.         $limit = intval($instance['limit']);
  1171.         $min_votes = intval($instance['min_votes']);
  1172.         $chars = intval($instance['chars']);
  1173.         $cat_ids = esc_attr($instance['cat_ids']);
  1174.         $time_range = esc_attr($instance['time_range']);
  1175. ?>
  1176.         <p>
  1177.             <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'wp-postratings'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label>
  1178.         </p>
  1179.         <p>
  1180.             <label for="<?php echo $this->get_field_id('type'); ?>"><?php _e('Statistics Type:', 'wp-postratings'); ?>
  1181.                 <select name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>" class="widefat">
  1182.                     <option value="most_rated"<?php selected('most_rated', $type); ?>><?php _e('Most Rated', 'wp-postratings'); ?></option>
  1183.                     <option value="most_rated_category"<?php selected('most_rated_category', $type); ?>><?php _e('Most Rated By Category', 'wp-postratings'); ?></option>
  1184.                     <option value="most_rated_range"<?php selected('most_rated_range', $type); ?>><?php _e('Most Rated By Time Range', 'wp-postratings'); ?></option>
  1185.                     <option value="most_rated_range_category"<?php selected('most_rated_range_category', $type); ?>><?php _e('Most Rated By Time Range And Category', 'wp-postratings'); ?></option>
  1186.                     <optgroup>&nbsp;</optgroup>
  1187.                     <option value="highest_rated"<?php selected('highest_rated', $type); ?>><?php _e('Highest Rated', 'wp-postratings'); ?></option>
  1188.                     <option value="highest_rated_category"<?php selected('highest_rated_category', $type); ?>><?php _e('Highest Rated By Category', 'wp-postratings'); ?></option>
  1189.                     <option value="highest_rated_range"<?php selected('highest_rated_range', $type); ?>><?php _e('Highest Rated By Time Range', 'wp-postratings'); ?></option>
  1190.                     <option value="highest_rated_range_category"<?php selected('highest_rated_range_category', $type); ?>><?php _e('Highest Rated By Time Range And Category', 'wp-postratings'); ?></option>
  1191.                     <optgroup>&nbsp;</optgroup>
  1192.                     <option value="lowest_rated"<?php selected('lowest_rated', $type); ?>><?php _e('Lowest Rated', 'wp-postratings'); ?></option>
  1193.                     <option value="lowest_rated_category"<?php selected('lowest_rated_category', $type); ?>><?php _e('Lowest Rated By Category', 'wp-postratings'); ?></option>
  1194.                     <option value="lowest_rated_range"<?php selected('lowest_rated_range', $type); ?>><?php _e('Lowest Rated By Time Range', 'wp-postratings'); ?></option>
  1195.                     <optgroup>&nbsp;</optgroup>
  1196.                     <option value="highest_score"<?php selected('highest_score', $type); ?>><?php _e('Highest Score', 'wp-postratings'); ?></option>
  1197.                     <option value="highest_score_category"<?php selected('highest_score_category', $type); ?>><?php _e('Highest Score By Category', 'wp-postratings'); ?></option>
  1198.                     <option value="highest_score_range"<?php selected('highest_score_range', $type); ?>><?php _e('Highest Score By Time Range', 'wp-postratings'); ?></option>
  1199.                     <option value="highest_score_range_category"<?php selected('highest_score_range_category', $type); ?>><?php _e('Highest Score By Time Range And Category', 'wp-postratings'); ?></option>
  1200.                 </select>
  1201.             </label>
  1202.         </p>
  1203.         <p>
  1204.             <label for="<?php echo $this->get_field_id('mode'); ?>"><?php _e('Include Ratings From:', 'wp-postratings'); ?>
  1205.                 <select name="<?php echo $this->get_field_name('mode'); ?>" id="<?php echo $this->get_field_id('mode'); ?>" class="widefat">
  1206.                     <option value="both"<?php selected('both', $mode); ?>><?php _e('Posts &amp; Pages', 'wp-postratings'); ?></option>
  1207.                     <option value="post"<?php selected('post', $mode); ?>><?php _e('Posts Only', 'wp-postratings'); ?></option>
  1208.                     <option value="page"<?php selected('page', $mode); ?>><?php _e('Pages Only', 'wp-postratings'); ?></option>
  1209.                 </select>
  1210.             </label>
  1211.         </p>
  1212.         <p>
  1213.             <label for="<?php echo $this->get_field_id('limit'); ?>"><?php _e('No. Of Records To Show:', 'wp-postratings'); ?> <input class="widefat" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="<?php echo $limit; ?>" /></label>
  1214.         </p>
  1215.         <p>
  1216.             <label for="<?php echo $this->get_field_id('min_votes'); ?>"><?php _e('Minimum Votes:', 'wp-postratings'); ?> <span style="color: red;">*</span> <input class="widefat" id="<?php echo $this->get_field_id('min_votes'); ?>" name="<?php echo $this->get_field_name('min_votes'); ?>" type="text" value="<?php echo $min_votes; ?>" size="4" /></label><br />
  1217.             <small><?php _e('You can set the minimum votes that a post or page must have before it gets displayed.', 'wp-postratings'); ?></small>
  1218.         </p>
  1219.         <p>
  1220.             <label for="<?php echo $this->get_field_id('chars'); ?>"><?php _e('Maximum Post Title Length (Characters):', 'wp-postratings'); ?> <input class="widefat" id="<?php echo $this->get_field_id('chars'); ?>" name="<?php echo $this->get_field_name('chars'); ?>" type="text" value="<?php echo $chars; ?>" /></label><br />
  1221.             <small><?php _e('<strong>0</strong> to disable.', 'wp-postratings'); ?></small>
  1222.         </p>
  1223.         <p>
  1224.             <label for="<?php echo $this->get_field_id('cat_ids'); ?>"><?php _e('Category IDs:', 'wp-postratings'); ?> <span style="color: red;">**</span> <input class="widefat" id="<?php echo $this->get_field_id('cat_ids'); ?>" name="<?php echo $this->get_field_name('cat_ids'); ?>" type="text" value="<?php echo $cat_ids; ?>" /></label><br />
  1225.             <small><?php _e('Seperate mutiple categories with commas.', 'wp-postratings'); ?></small>
  1226.         </p>
  1227.         <p>
  1228.             <label for="<?php echo $this->get_field_id('time_range'); ?>"><?php _e('Time Range:', 'wp-postratings'); ?> <span style="color: red;">**</span> <input class="widefat" id="<?php echo $this->get_field_id('time_range'); ?>" name="<?php echo $this->get_field_name('time_range'); ?>" type="text" value="<?php echo $time_range; ?>" /></label><br />
  1229.             <small><?php _e('Use values like <strong>1 day</strong>, <strong>2 weeks</strong>, <strong>1 month</strong>.', 'wp-postratings'); ?></small>
  1230.         </p>
  1231.         <p style="color: red;">
  1232.             <small><?php _e('* Time range statistics does not support Minimum Votes field, you can ignore that it.', 'wp-postratings'); ?></small><br />
  1233.             <small><?php _e('** If you are not using any category or time range statistics, you can ignore it.', 'wp-postratings'); ?></small>
  1234.         <p>
  1235.         <input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
  1236. <?php
  1237.     }
  1238. }
  1239.  
  1240.  
  1241. ### Function: Init WP-PostRatings Widget
  1242. add_action('widgets_init', 'widget_ratings_init');
  1243. function widget_ratings_init() {
  1244.     register_widget('WP_Widget_PostRatings');
  1245. }
  1246.  
  1247.  
  1248. ### Function: Create Rating Logs Table
  1249. add_action('activate_wp-postratings/wp-postratings.php', 'create_ratinglogs_table');
  1250. function create_ratinglogs_table() {
  1251.     global $wpdb;
  1252.     postratings_textdomain();
  1253.     if(@is_file(ABSPATH.'/wp-admin/upgrade-functions.php')) {
  1254.         include_once(ABSPATH.'/wp-admin/upgrade-functions.php');
  1255.     } elseif(@is_file(ABSPATH.'/wp-admin/includes/upgrade.php')) {
  1256.         include_once(ABSPATH.'/wp-admin/includes/upgrade.php');
  1257.     } else {
  1258.         die('We have problem finding your \'/wp-admin/upgrade-functions.php\' and \'/wp-admin/includes/upgrade.php\'');
  1259.     }
  1260.     $charset_collate = '';
  1261.     if($wpdb->supports_collation()) {
  1262.         if(!empty($wpdb->charset)) {
  1263.             $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
  1264.         }
  1265.         if(!empty($wpdb->collate)) {
  1266.             $charset_collate .= " COLLATE $wpdb->collate";
  1267.         }
  1268.     }
  1269.     // Create Post Ratings Table
  1270.     $create_ratinglogs_sql = "CREATE TABLE $wpdb->ratings (".
  1271.             "rating_id INT(11) NOT NULL auto_increment,".
  1272.             "rating_postid INT(11) NOT NULL ,".
  1273.             "rating_posttitle TEXT NOT NULL,".
  1274.             "rating_rating INT(2) NOT NULL ,".
  1275.             "rating_timestamp VARCHAR(15) NOT NULL ,".
  1276.             "rating_ip VARCHAR(40) NOT NULL ,".
  1277.             "rating_host VARCHAR(200) NOT NULL,".
  1278.             "rating_username VARCHAR(50) NOT NULL,".
  1279.             "rating_userid int(10) NOT NULL default '0',".
  1280.             "PRIMARY KEY (rating_id)) $charset_collate;";
  1281.     maybe_create_table($wpdb->ratings, $create_ratinglogs_sql);
  1282.     // Add In Options (4 Records)
  1283.     add_option('postratings_image', 'stars', 'Your Ratings Image');
  1284.     add_option('postratings_max', '5', 'Your Max Ratings');
  1285.     add_option('postratings_template_vote', '%RATINGS_IMAGES_VOTE% (<strong>%RATINGS_USERS%</strong> '.__('votes', 'wp-postratings').__(',', 'wp-postratings').' '.__('average', 'wp-postratings').': <strong>%RATINGS_AVERAGE%</strong> '.__('out of', 'wp-postratings').' %RATINGS_MAX%)<br />%RATINGS_TEXT%', 'Ratings Vote Template Text');
  1286.     add_option('postratings_template_text', '%RATINGS_IMAGES% (<em><strong>%RATINGS_USERS%</strong> '.__('votes', 'wp-postratings').__(',', 'wp-postratings').' '.__('average', 'wp-postratings').': <strong>%RATINGS_AVERAGE%</strong> '.__('out of', 'wp-postratings').' %RATINGS_MAX%'.__(',', 'wp-postratings').' <strong>'.__('rated', 'wp-postratings').'</strong></em>)', 'Ratings Template Text');
  1287.     add_option('postratings_template_none', '%RATINGS_IMAGES_VOTE% ('.__('No Ratings Yet', 'wp-postratings').')<br />%RATINGS_TEXT%', 'Ratings Template For No Ratings');
  1288.     // Database Upgrade For WP-PostRatings 1.02
  1289.     add_option('postratings_logging_method', '3', 'Logging Method Of User Rated\'s Answer');
  1290.     add_option('postratings_allowtorate', '2', 'Who Is Allowed To Rate');
  1291.     // Database Uprade For WP-PostRatings 1.04 
  1292.     maybe_add_column($wpdb->ratings, 'rating_userid', "ALTER TABLE $wpdb->ratings ADD rating_userid INT( 10 ) NOT NULL DEFAULT '0';");
  1293.     // Database Uprade For WP-PostRatings 1.05
  1294.     add_option('postratings_ratingstext', array(__('1 Star', 'wp-postratings'), __('2 Stars', 'wp-postratings'), __('3 Stars', 'wp-postratings'), __('4 Stars', 'wp-postratings'), __('5 Stars', 'wp-postratings')), 'Individual Post Rating Text');
  1295.     add_option('postratings_template_highestrated', '<li><a href="%POST_URL%" title="%POST_TITLE%">%POST_TITLE%</a> %RATINGS_IMAGES% (%RATINGS_AVERAGE% '.__('out of', 'wp-postratings').' %RATINGS_MAX%)</li>', 'Template For Highest Rated');
  1296.     // Database Upgrade For WP-PostRatings 1.11
  1297.     add_option('postratings_ajax_style', array('loading' => 1, 'fading' => 1), 'Ratings AJAX Style');
  1298.     // Database Upgrade For WP-PostRatings 1.20
  1299.     add_option('postratings_ratingsvalue', array(1,2,3,4,5), 'Individual Post Rating Value');
  1300.     add_option('postratings_customrating', 0, 'Use Custom Ratings');
  1301.     add_option('postratings_template_permission', '%RATINGS_IMAGES% (<em><strong>%RATINGS_USERS%</strong> '.__('votes', 'wp-postratings').__(',', 'wp-postratings').' '.__('average', 'wp-postratings').': <strong>%RATINGS_AVERAGE%</strong> '.__('out of', 'wp-postratings').' %RATINGS_MAX%</em>)<br /><em>'.__('You need to be a registered member to rate this post.', 'wp-postratings').'</em>', 'Ratings Template Text');
  1302.     // Database Upgrade For WP-PostRatings 1.30
  1303.     add_option('postratings_template_mostrated', '<li><a href="%POST_URL%"  title="%POST_TITLE%">%POST_TITLE%</a> - %RATINGS_USERS% '.__('votes', 'wp-postratings').'</li>', 'Most Rated Template Text');
  1304.     // Database Upgrade For WP-PostRatings 1.50
  1305.     delete_option('widget_ratings_highest_rated');
  1306.     delete_option('widget_ratings_most_rated');
  1307.     // Set 'manage_ratings' Capabilities To Administrator  
  1308.     $role = get_role('administrator');
  1309.     if(!$role->has_cap('manage_ratings')) {
  1310.         $role->add_cap('manage_ratings');
  1311.     }
  1312. }
  1313.  
  1314.  
  1315. ### Seperate PostRatings Stats For Readability
  1316. require_once('postratings-stats.php');
  1317. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement