Guest User

Untitled

a guest
Jul 6th, 2011
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 33.71 KB | None | 0 0
  1. <?php
  2. /*
  3. +----------------------------------------------------------------+
  4. |                                                                                           |
  5. |   WordPress 2.8 Plugin: WP-PostRatings 1.61                               |
  6. |   Copyright (c) 2009 Lester "GaMerZ" Chan                                 |
  7. |                                                                                           |
  8. |   File Written By:                                                                    |
  9. |   - Lester "GaMerZ" Chan                                                          |
  10. |   - http://lesterchan.net                                                         |
  11. |                                                                                           |
  12. |   File Information:                                                                   |
  13. |   - Containts Post Rating Stats                                                   |
  14. |   - wp-content/plugins/wp-postratings/postratings-stats.php           |
  15. |                                                                                           |
  16. +----------------------------------------------------------------+
  17. */
  18.  
  19.  
  20. ### Function: Display Most Rated Page/Post
  21. if(!function_exists('get_most_rated')) {
  22.     function get_most_rated($mode = '', $min_votes = 0, $limit = 10, $chars = 0, $display = true) {
  23.         global $wpdb;
  24.         $ratings_max = intval(get_option('postratings_max'));
  25.         $ratings_custom = intval(get_option('postratings_customrating'));
  26.         $output = '';
  27.         if(!empty($mode) && $mode != 'both') {
  28.             $where = "$wpdb->posts.post_type = '$mode'";
  29.         } else {
  30.             $where = '1=1';
  31.         }
  32.         if($ratings_custom && $ratings_max == 2) {
  33.             $order_by = 'ratings_score';
  34.         } else {
  35.             $order_by = 'ratings_average';
  36.         }
  37.         $temp = stripslashes(get_option('postratings_template_mostrated'));
  38.         $most_rated = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score FROM $wpdb->posts 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 LEFT JOIN $wpdb->postmeta AS t3 ON t3.post_id = $wpdb->posts.ID WHERE t1.meta_key = 'ratings_average' AND t2.meta_key = 'ratings_users' AND t3.meta_key = 'ratings_score' AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."' AND $wpdb->posts.post_status = 'publish' AND t2.meta_value >= $min_votes AND $where ORDER BY ratings_users DESC, $order_by DESC LIMIT $limit");
  39.         if($most_rated) {
  40.             foreach ($most_rated as $post) {
  41.                 $output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
  42.             }
  43.         } else {
  44.             $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
  45.         }
  46.         if($display) {
  47.             echo $output;
  48.         } else {
  49.             return $output;
  50.         }
  51.     }
  52. }
  53.  
  54.  
  55. ### Function: Display Most Rated Page/Post By Category ID
  56. if(!function_exists('get_most_rated_category')) {
  57.     function get_most_rated_category($category_id = 0, $mode = '', $min_votes = 0, $limit = 10, $chars = 0, $display = true) {
  58.         global $wpdb;
  59.         $ratings_max = intval(get_option('postratings_max'));
  60.         $ratings_custom = intval(get_option('postratings_customrating'));
  61.         $output = '';
  62.         if(is_array($category_id)) {
  63.             $category_sql = "$wpdb->term_taxonomy.term_id IN (".join(',', $category_id).')';
  64.         } else {
  65.             $category_sql = "$wpdb->term_taxonomy.term_id = $category_id";
  66.         }
  67.         if(!empty($mode) && $mode != 'both') {
  68.             $where = "$wpdb->posts.post_type = '$mode'";
  69.         } else {
  70.             $where = '1=1';
  71.         }
  72.         if($ratings_custom && $ratings_max == 2) {
  73.             $order_by = 'ratings_score';
  74.         } else {
  75.             $order_by = 'ratings_average';
  76.         }
  77.         $temp = stripslashes(get_option('postratings_template_mostrated'));
  78.         $most_rated = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score FROM $wpdb->posts 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 LEFT JOIN $wpdb->postmeta AS t3 ON t3.post_id = $wpdb->posts.ID INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE t1.meta_key = 'ratings_average' AND t2.meta_key = 'ratings_users' AND t3.meta_key = 'ratings_score' AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."' AND $wpdb->posts.post_status = 'publish' AND $wpdb->term_taxonomy.taxonomy = 'category' AND $category_sql AND t2.meta_value >= $min_votes AND $where ORDER BY ratings_users DESC, $order_by DESC LIMIT $limit");
  79.         if($most_rated) {
  80.             foreach ($most_rated as $post) {
  81.                 $output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
  82.             }
  83.         } else {
  84.             $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
  85.         }
  86.         if($display) {
  87.             echo $output;
  88.         } else {
  89.             return $output;
  90.         }
  91.     }
  92. }
  93.  
  94.  
  95. ### Function: Display Most Rated Page/Post With Time Range
  96. if(!function_exists('get_most_rated_range')) {
  97.     function get_most_rated_range($time = '1 day', $mode = '', $limit = 10, $chars = 0, $display = true) {
  98.         global $wpdb;
  99.         $ratings_max = intval(get_option('postratings_max'));
  100.         $ratings_custom = intval(get_option('postratings_customrating'));
  101.         $min_time = strtotime('-'.$time, current_time('timestamp'));
  102.         $output = '';
  103.         if(!empty($mode) && $mode != 'both') {
  104.             $where = "$wpdb->posts.post_type = '$mode'";
  105.         } else {
  106.             $where = '1=1';
  107.         }
  108.         if($ratings_custom && $ratings_max == 2) {
  109.             $order_by = 'ratings_score';
  110.         } else {
  111.             $order_by = 'ratings_average';
  112.         }
  113.         $temp = stripslashes(get_option('postratings_template_mostrated'));
  114.         $most_rated = $wpdb->get_results("SELECT COUNT($wpdb->ratings.rating_postid) AS ratings_users, SUM($wpdb->ratings.rating_rating) AS ratings_score, ROUND(((SUM($wpdb->ratings.rating_rating)/COUNT($wpdb->ratings.rating_postid))), 2) AS ratings_average, $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->ratings ON $wpdb->ratings.rating_postid = $wpdb->posts.ID WHERE rating_timestamp >= $min_time AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."'  AND $wpdb->posts.post_status = 'publish' AND $where GROUP BY $wpdb->ratings.rating_postid ORDER BY ratings_users DESC, $order_by DESC LIMIT $limit");
  115.         if($most_rated) {
  116.             foreach ($most_rated as $post) {
  117.                 $output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
  118.             }
  119.         } else {
  120.             $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
  121.         }
  122.         if($display) {
  123.             echo $output;
  124.         } else {
  125.             return $output;
  126.         }
  127.     }
  128. }
  129.  
  130.  
  131. ### Function: Display Most Rated Page/Post With Time Range By Category ID
  132. if(!function_exists('get_most_rated_range_category')) {
  133.     function get_most_rated_range_category($time = '1 day', $category_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) {
  134.         global $wpdb;
  135.         $ratings_max = intval(get_option('postratings_max'));
  136.         $ratings_custom = intval(get_option('postratings_customrating'));
  137.         $min_time = strtotime('-'.$time, current_time('timestamp'));
  138.         $output = '';
  139.         if(is_array($category_id)) {
  140.             // There is a bug with multiple categoies. The number of votes will be multiplied by the number of categories passed in.
  141.             $category_sql = "$wpdb->term_taxonomy.term_id IN (".join(',', $category_id).')';
  142.         } else {
  143.             $category_sql = "$wpdb->term_taxonomy.term_id = $category_id";
  144.         }
  145.         if(!empty($mode) && $mode != 'both') {
  146.             $where = "$wpdb->posts.post_type = '$mode'";
  147.         } else {
  148.             $where = '1=1';
  149.         }
  150.         if($ratings_custom && $ratings_max == 2) {
  151.             $order_by = 'ratings_score';
  152.         } else {
  153.             $order_by = 'ratings_average';
  154.         }
  155.         $temp = stripslashes(get_option('postratings_template_mostrated'));
  156.         $most_rated = $wpdb->get_results("SELECT COUNT($wpdb->ratings.rating_postid) AS ratings_users, SUM($wpdb->ratings.rating_rating) AS ratings_score, ROUND(((SUM($wpdb->ratings.rating_rating)/COUNT($wpdb->ratings.rating_postid))), 2) AS ratings_average, $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->ratings ON $wpdb->ratings.rating_postid = $wpdb->posts.ID INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE rating_timestamp >= $min_time AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."'  AND $wpdb->posts.post_status = 'publish' AND $wpdb->term_taxonomy.taxonomy = 'category' AND $category_sql AND $where GROUP BY $wpdb->ratings.rating_postid ORDER BY ratings_users DESC, $order_by DESC LIMIT $limit");
  157.         if($most_rated) {
  158.             foreach ($most_rated as $post) {
  159.                 $output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
  160.             }
  161.         } else {
  162.             $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
  163.         }
  164.         if($display) {
  165.             echo $output;
  166.         } else {
  167.             return $output;
  168.         }
  169.     }
  170. }
  171.  
  172.  
  173. ### Function: Display Highest Rated Page/Post
  174. if(!function_exists('get_highest_rated')) {
  175.     function get_highest_rated($mode = '', $min_votes = 0, $limit = 10, $chars = 0, $display = true) {
  176.         global $wpdb;
  177.         $ratings_max = intval(get_option('postratings_max'));
  178.         $ratings_custom = intval(get_option('postratings_customrating'));
  179.         $output = '';
  180.         if(!empty($mode) && $mode != 'both') {
  181.             $where = "$wpdb->posts.post_type = '$mode'";
  182.         } else {
  183.             $where = '1=1';
  184.         }
  185.         if($ratings_custom && $ratings_max == 2) {
  186.             $order_by = 'ratings_score';
  187.         } else {
  188.             $order_by = 'ratings_average';
  189.         }
  190.         $temp = stripslashes(get_option('postratings_template_highestrated'));
  191.         $highest_rated = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score FROM $wpdb->posts 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 LEFT JOIN $wpdb->postmeta AS t3 ON t3.post_id = $wpdb->posts.ID WHERE t1.meta_key = 'ratings_average' AND t2.meta_key = 'ratings_users' AND t3.meta_key = 'ratings_score' AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."' AND $wpdb->posts.post_status = 'publish' AND t2.meta_value >= $min_votes AND $where ORDER BY $order_by DESC, ratings_users DESC LIMIT $limit");
  192.         if($highest_rated) {
  193.             foreach($highest_rated as $post) {
  194.                 $output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
  195.             }
  196.         } else {
  197.             $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
  198.         }
  199.         if($display) {
  200.             echo $output;
  201.         } else {
  202.             return $output;
  203.         }
  204.     }
  205. }
  206.  
  207.  
  208. ### Function: Display Highest Rated Page/Post By Category ID
  209. if(!function_exists('get_highest_rated_category')) {
  210.     function get_highest_rated_category($category_id = 0, $mode = '', $min_votes = 0, $limit = 10, $chars = 0, $display = true) {
  211.         global $wpdb;
  212.         $ratings_max = intval(get_option('postratings_max'));
  213.         $ratings_custom = intval(get_option('postratings_customrating'));
  214.         $output = '';
  215.         // Code By: Dirceu P. Junior (http://pomoti.com)
  216.         if(is_array($category_id)) {
  217.             $category_sql = "$wpdb->term_taxonomy.term_id IN (".join(',', $category_id).')';
  218.         } else {
  219.             $category_sql = "$wpdb->term_taxonomy.term_id = $category_id";
  220.         }
  221.         if(!empty($mode) && $mode != 'both') {
  222.             $where = "$wpdb->posts.post_type = '$mode'";
  223.         } else {
  224.             $where = '1=1';
  225.         }
  226.         if($ratings_custom && $ratings_max == 2) {
  227.             $order_by = 'ratings_score';
  228.         } else {
  229.             $order_by = 'ratings_average';
  230.         }
  231.         $temp = stripslashes(get_option('postratings_template_highestrated'));
  232.         $highest_rated = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score FROM $wpdb->posts 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 LEFT JOIN $wpdb->postmeta AS t3 ON t3.post_id = $wpdb->posts.ID INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE t1.meta_key = 'ratings_average' AND t2.meta_key = 'ratings_users' AND t3.meta_key = 'ratings_score' AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."'  AND $wpdb->posts.post_status = 'publish' AND $wpdb->term_taxonomy.taxonomy = 'category' AND $category_sql AND t2.meta_value >= $min_votes AND $where ORDER BY $order_by DESC, ratings_users DESC LIMIT $limit");
  233.         if($highest_rated) {
  234.             foreach($highest_rated as $post) {
  235.                 $output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
  236.             }
  237.         } else {
  238.             $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
  239.         }
  240.         if($display) {
  241.             echo $output;
  242.         } else {
  243.             return $output;
  244.         }
  245.     }
  246. }
  247.  
  248.  
  249. ### Function: Display Highest Rated Page/Post With Time Range
  250. if(!function_exists('get_highest_rated_range')) {
  251.     function get_highest_rated_range($time = '1 day', $mode = '', $limit = 10, $chars = 0, $display = true) {
  252.         global $wpdb;
  253.         $ratings_max = intval(get_option('postratings_max'));
  254.         $ratings_custom = intval(get_option('postratings_customrating'));
  255.         $min_time = strtotime('-'.$time, current_time('timestamp'));
  256.         $output = '';
  257.         if(!empty($mode) && $mode != 'both') {
  258.             $where = "$wpdb->posts.post_type = '$mode'";
  259.         } else {
  260.             $where = '1=1';
  261.         }
  262.         if($ratings_custom && $ratings_max == 2) {
  263.             $order_by = 'ratings_score';
  264.         } else {
  265.             $order_by = 'ratings_average';
  266.         }
  267.         $temp = stripslashes(get_option('postratings_template_highestrated'));
  268.         $highest_rated = $wpdb->get_results("SELECT COUNT($wpdb->ratings.rating_postid) AS ratings_users, SUM($wpdb->ratings.rating_rating) AS ratings_score, ROUND(((SUM($wpdb->ratings.rating_rating)/COUNT($wpdb->ratings.rating_postid))), 2) AS ratings_average, $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->ratings ON $wpdb->ratings.rating_postid = $wpdb->posts.ID WHERE rating_timestamp >= $min_time AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."'  AND $wpdb->posts.post_status = 'publish' AND $where GROUP BY $wpdb->ratings.rating_postid ORDER BY $order_by DESC, ratings_users DESC LIMIT $limit");
  269.         if($highest_rated) {
  270.             foreach($highest_rated as $post) {
  271.                 $output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
  272.             }
  273.         } else {
  274.             $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
  275.         }
  276.         if($display) {
  277.             echo $output;
  278.         } else {
  279.             return $output;
  280.         }
  281.     }
  282. }
  283.  
  284.  
  285. ### Function: Display Highest Rated Page/Post With Time Range By Category ID
  286. if(!function_exists('get_highest_rated_range_category')) {
  287.     function get_highest_rated_range_category($time = '1 day', $category_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) {
  288.         global $wpdb;
  289.         $ratings_max = intval(get_option('postratings_max'));
  290.         $ratings_custom = intval(get_option('postratings_customrating'));
  291.         $min_time = strtotime('-'.$time, current_time('timestamp'));
  292.         $output = '';
  293.         // Code By: Dirceu P. Junior (http://pomoti.com)
  294.         if(is_array($category_id)) {
  295.             $category_sql = "$wpdb->term_taxonomy.term_id IN (".join(',', $category_id).')';
  296.         } else {
  297.             $category_sql = "$wpdb->term_taxonomy.term_id = $category_id";
  298.         }
  299.         if(!empty($mode) && $mode != 'both') {
  300.             $where = "$wpdb->posts.post_type = '$mode'";
  301.         } else {
  302.             $where = '1=1';
  303.         }
  304.         if($ratings_custom && $ratings_max == 2) {
  305.             $order_by = 'ratings_score';
  306.         } else {
  307.             $order_by = 'ratings_average';
  308.         }
  309.         $temp = stripslashes(get_option('postratings_template_highestrated'));
  310.         $highest_rated = $wpdb->get_results("SELECT COUNT($wpdb->ratings.rating_postid) AS ratings_users, SUM($wpdb->ratings.rating_rating) AS ratings_score, ROUND(((SUM($wpdb->ratings.rating_rating)/COUNT($wpdb->ratings.rating_postid))), 2) AS ratings_average, $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->ratings ON $wpdb->ratings.rating_postid = $wpdb->posts.ID INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE rating_timestamp >= $min_time AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."'  AND $wpdb->posts.post_status = 'publish' AND $wpdb->term_taxonomy.taxonomy = 'category' AND $category_sql AND $where GROUP BY $wpdb->ratings.rating_postid ORDER BY $order_by DESC, ratings_users DESC LIMIT $limit");
  311.         if($highest_rated) {
  312.             foreach($highest_rated as $post) {
  313.                 $output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
  314.             }
  315.         } else {
  316.             $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
  317.         }
  318.         if($display) {
  319.             echo $output;
  320.         } else {
  321.             return $output;
  322.         }
  323.     }
  324. }
  325.  
  326.  
  327. ### Function: Display Lowest Rated Page/Post
  328. if(!function_exists('get_lowest_rated')) {
  329.     function get_lowest_rated($mode = '', $min_votes = 0, $limit = 10, $chars = 0, $display = true) {
  330.         global $wpdb;
  331.         $ratings_max = intval(get_option('postratings_max'));
  332.         $ratings_custom = intval(get_option('postratings_customrating'));
  333.         $output = '';
  334.         if(!empty($mode) && $mode != 'both') {
  335.             $where = "$wpdb->posts.post_type = '$mode'";
  336.         } else {
  337.             $where = '1=1';
  338.         }
  339.         if($ratings_custom && $ratings_max == 2) {
  340.             $order_by = 'ratings_score';
  341.         } else {
  342.             $order_by = 'ratings_average';
  343.         }
  344.         $temp = stripslashes(get_option('postratings_template_highestrated'));
  345.         $lowest_rated = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score FROM $wpdb->posts 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 LEFT JOIN $wpdb->postmeta AS t3 ON t3.post_id = $wpdb->posts.ID WHERE t1.meta_key = 'ratings_average' AND t2.meta_key = 'ratings_users' AND t3.meta_key = 'ratings_score' AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."' AND $wpdb->posts.post_status = 'publish' AND t2.meta_value >= $min_votes AND $where ORDER BY $order_by ASC, ratings_users DESC LIMIT $limit");
  346.         if($lowest_rated) {
  347.             foreach($lowest_rated as $post) {
  348.                 $output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
  349.             }
  350.         } else {
  351.             $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
  352.         }
  353.         if($display) {
  354.             echo $output;
  355.         } else {
  356.             return $output;
  357.         }
  358.     }
  359. }
  360.  
  361.  
  362. ### Function: Display Lowest Rated Page/Post By Category ID
  363. if(!function_exists('get_lowest_rated_category')) {
  364.     function get_lowest_rated_category($category_id = 0, $mode = '', $min_votes = 0, $limit = 10, $chars = 0, $display = true) {
  365.         global $wpdb;
  366.         $ratings_max = intval(get_option('postratings_max'));
  367.         $ratings_custom = intval(get_option('postratings_customrating'));
  368.         $output = '';
  369.         // Code By: Dirceu P. Junior (http://pomoti.com)
  370.         if(is_array($category_id)) {
  371.             $category_sql = "$wpdb->term_taxonomy.term_id IN (".join(',', $category_id).')';
  372.         } else {
  373.             $category_sql = "$wpdb->term_taxonomy.term_id = $category_id";
  374.         }
  375.         if(!empty($mode) && $mode != 'both') {
  376.             $where = "$wpdb->posts.post_type = '$mode'";
  377.         } else {
  378.             $where = '1=1';
  379.         }
  380.         if($ratings_custom && $ratings_max == 2) {
  381.             $order_by = 'ratings_score';
  382.         } else {
  383.             $order_by = 'ratings_average';
  384.         }
  385.         $temp = stripslashes(get_option('postratings_template_highestrated'));
  386.         $lowest_rated = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score FROM $wpdb->posts 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 LEFT JOIN $wpdb->postmeta AS t3 ON t3.post_id = $wpdb->posts.ID INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE t1.meta_key = 'ratings_average' AND t2.meta_key = 'ratings_users' AND t3.meta_key = 'ratings_score' AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."'  AND $wpdb->posts.post_status = 'publish' AND $wpdb->term_taxonomy.taxonomy = 'category' AND $category_sql AND t2.meta_value >= $min_votes AND $where ORDER BY $order_by ASC, ratings_users DESC LIMIT $limit");
  387.         if($lowest_rated) {
  388.             foreach($lowest_rated as $post) {
  389.                 $output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
  390.             }
  391.         } else {
  392.             $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
  393.         }
  394.         if($display) {
  395.             echo $output;
  396.         } else {
  397.             return $output;
  398.         }
  399.     }
  400. }
  401.  
  402.  
  403. ### Function: Display Lowest Rated Page/Post With Time Range
  404. if(!function_exists('get_lowest_rated_range')) {
  405.     function get_lowest_rated_range($time = '1 day', $mode = '', $limit = 10, $chars = 0, $display = true) {
  406.         global $wpdb;
  407.         $ratings_max = intval(get_option('postratings_max'));
  408.         $ratings_custom = intval(get_option('postratings_customrating'));
  409.         $min_time = strtotime('-'.$time, current_time('timestamp'));
  410.         $output = '';
  411.         if(!empty($mode) && $mode != 'both') {
  412.             $where = "$wpdb->posts.post_type = '$mode'";
  413.         } else {
  414.             $where = '1=1';
  415.         }
  416.         if($ratings_custom && $ratings_max == 2) {
  417.             $order_by = 'ratings_score';
  418.         } else {
  419.             $order_by = 'ratings_average';
  420.         }
  421.         $temp = stripslashes(get_option('postratings_template_highestrated'));
  422.         $highest_rated = $wpdb->get_results("SELECT COUNT($wpdb->ratings.rating_postid) AS ratings_users, SUM($wpdb->ratings.rating_rating) AS ratings_score, ROUND(((SUM($wpdb->ratings.rating_rating)/COUNT($wpdb->ratings.rating_postid))), 2) AS ratings_average, $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->ratings ON $wpdb->ratings.rating_postid = $wpdb->posts.ID WHERE rating_timestamp >= $min_time AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."'  AND $wpdb->posts.post_status = 'publish' AND $where GROUP BY $wpdb->ratings.rating_postid ORDER BY $order_by ASC, ratings_users DESC LIMIT $limit");
  423.         if($highest_rated) {
  424.             foreach($highest_rated as $post) {
  425.                 $output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
  426.             }
  427.         } else {
  428.             $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
  429.         }
  430.         if($display) {
  431.             echo $output;
  432.         } else {
  433.             return $output;
  434.         }
  435.     }
  436. }
  437.  
  438. ### Function: Display Highest Score Page/Post
  439. if(!function_exists('get_highest_score')) {
  440.     function get_highest_score($mode = '', $min_votes = 0, $limit = 10, $chars = 0, $display = true) {
  441.         global $wpdb;
  442.         $output = '';
  443.         if(!empty($mode) && $mode != 'both') {
  444.             $where = "$wpdb->posts.post_type = '$mode'";
  445.         } else {
  446.             $where = '1=1';
  447.         }
  448.         $temp = stripslashes(get_option('postratings_template_mostrated'));
  449.         $highest_score = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score FROM $wpdb->posts 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 LEFT JOIN $wpdb->postmeta AS t3 ON t3.post_id = $wpdb->posts.ID WHERE t1.meta_key = 'ratings_average' AND t2.meta_key = 'ratings_users' AND t3.meta_key = 'ratings_score' AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."' AND $wpdb->posts.post_status = 'publish' AND t2.meta_value >= $min_votes AND $where ORDER BY ratings_score DESC, ratings_average DESC LIMIT $limit");
  450.         if($highest_score) {
  451.             foreach ($highest_score as $post) {
  452.                 $output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
  453.             }
  454.         } else {
  455.             $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
  456.         }
  457.         if($display) {
  458.             echo $output;
  459.         } else {
  460.             return $output;
  461.         }
  462.     }
  463. }
  464.  
  465.  
  466. ### Function: Display Highest Score Page/Post By Category ID
  467. if(!function_exists('get_highest_score_category')) {
  468.     function get_highest_score_category($category_id = 0, $mode = '', $min_votes = 0, $limit = 10, $chars = 0, $display = true) {
  469.         global $wpdb;
  470.         $output = '';
  471.         if(is_array($category_id)) {
  472.             $category_sql = "$wpdb->term_taxonomy.term_id IN (".join(',', $category_id).')';
  473.         } else {
  474.             $category_sql = "$wpdb->term_taxonomy.term_id = $category_id";
  475.         }
  476.         if(!empty($mode) && $mode != 'both') {
  477.             $where = "$wpdb->posts.post_type = '$mode'";
  478.         } else {
  479.             $where = '1=1';
  480.         }
  481.         $temp = stripslashes(get_option('postratings_template_mostrated'));
  482.         $highest_score = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score FROM $wpdb->posts 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 LEFT JOIN $wpdb->postmeta AS t3 ON t3.post_id = $wpdb->posts.ID INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE t1.meta_key = 'ratings_average' AND t2.meta_key = 'ratings_users' AND t3.meta_key = 'ratings_score' AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."' AND $wpdb->posts.post_status = 'publish' AND $wpdb->term_taxonomy.taxonomy = 'category' AND $category_sql AND t2.meta_value >= $min_votes AND $where ORDER BY ratings_score DESC, ratings_average DESC LIMIT $limit");
  483.         if($highest_score) {
  484.             foreach ($highest_score as $post) {
  485.                 $output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
  486.             }
  487.         } else {
  488.             $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
  489.         }
  490.         if($display) {
  491.             echo $output;
  492.         } else {
  493.             return $output;
  494.         }
  495.     }
  496. }
  497.  
  498.  
  499. ### Function: Display Highest Score Page/Post With Time Range
  500. if(!function_exists('get_highest_score_range')) {
  501.     function get_highest_score_range($time = '1 day', $mode = '', $limit = 10, $chars = 0, $display = true) {
  502.         global $wpdb;
  503.         $min_time = strtotime('-'.$time, current_time('timestamp'));
  504.         $output = '';
  505.         if(!empty($mode) && $mode != 'both') {
  506.             $where = "$wpdb->posts.post_type = '$mode'";
  507.         } else {
  508.             $where = '1=1';
  509.         }
  510.         $temp = stripslashes(get_option('postratings_template_mostrated'));
  511.         $highest_score = $wpdb->get_results("SELECT COUNT($wpdb->ratings.rating_postid) AS ratings_users, SUM($wpdb->ratings.rating_rating) AS ratings_score, ROUND(((SUM($wpdb->ratings.rating_rating)/COUNT($wpdb->ratings.rating_postid))), 2) AS ratings_average, $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->ratings ON $wpdb->ratings.rating_postid = $wpdb->posts.ID WHERE rating_timestamp >= $min_time AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."'  AND $wpdb->posts.post_status = 'publish' AND $where GROUP BY $wpdb->ratings.rating_postid ORDER BY ratings_score DESC, ratings_average DESC LIMIT $limit");
  512.         if($highest_score) {
  513.             foreach ($highest_score as $post) {
  514.                 $output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
  515.             }
  516.         } else {
  517.             $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
  518.         }
  519.         if($display) {
  520.             echo $output;
  521.         } else {
  522.             return $output;
  523.         }
  524.     }
  525. }
  526.  
  527.  
  528. ### Function: Display Highest Score Page/Post With Time Range By Category ID
  529. if(!function_exists('get_highest_score_range_category')) {
  530.     function get_highest_score_range_category($time = '1 day', $category_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) {
  531.         global $wpdb;
  532.         $min_time = strtotime('-'.$time, current_time('timestamp'));
  533.         $output = '';
  534.         if(is_array($category_id)) {
  535.             // There is a bug with multiple categoies. The number of votes will be multiplied by the number of categories passed in.
  536.             $category_sql = "$wpdb->term_taxonomy.term_id IN (".join(',', $category_id).')';
  537.         } else {
  538.             $category_sql = "$wpdb->term_taxonomy.term_id = $category_id";
  539.         }
  540.         if(!empty($mode) && $mode != 'both') {
  541.             $where = "$wpdb->posts.post_type = '$mode'";
  542.         } else {
  543.             $where = '1=1';
  544.         }
  545.         $temp = stripslashes(get_option('postratings_template_mostrated'));
  546.         $highest_score = $wpdb->get_results("SELECT COUNT($wpdb->ratings.rating_postid) AS ratings_users, SUM($wpdb->ratings.rating_rating) AS ratings_score, ROUND(((SUM($wpdb->ratings.rating_rating)/COUNT($wpdb->ratings.rating_postid))), 2) AS ratings_average, $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->ratings ON $wpdb->ratings.rating_postid = $wpdb->posts.ID INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE rating_timestamp >= $min_time AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."'  AND $wpdb->posts.post_status = 'publish' AND $wpdb->term_taxonomy.taxonomy = 'category' AND $category_sql AND $where GROUP BY $wpdb->ratings.rating_postid ORDER BY ratings_score DESC, ratings_average DESC LIMIT $limit");
  547.         if($highest_score) {
  548.             foreach ($highest_score as $post) {
  549.                 $output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
  550.             }
  551.         } else {
  552.             $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
  553.         }
  554.         if($display) {
  555.             echo $output;
  556.         } else {
  557.             return $output;
  558.         }
  559.     }
  560. }
  561.  
  562.  
  563. ### Function: Display Highest Rated Page/Post By Tag ID
  564. if(!function_exists('get_highest_rated_tag')) {
  565.     function get_highest_rated_tag($tag_id = 0, $mode = '', $min_votes = 0, $limit = 10, $chars = 0, $display = true) {
  566.         global $wpdb;
  567.         $ratings_max = intval(get_option('postratings_max'));
  568.         $ratings_custom = intval(get_option('postratings_customrating'));
  569.         $output = '';
  570.         if(is_array($tag_id)) {
  571.             $tag_sql = "$wpdb->term_taxonomy.term_id IN (".join(',', $tag_id).')';
  572.         } else {
  573.             $tag_sql = "$wpdb->term_taxonomy.term_id = $tag_id";
  574.         }
  575.         if(!empty($mode) && $mode != 'both') {
  576.             $where = "$wpdb->posts.post_type = '$mode'";
  577.         } else {
  578.             $where = '1=1';
  579.         }
  580.         if($ratings_custom && $ratings_max == 2) {
  581.             $order_by = 'ratings_score';
  582.         } else {
  583.             $order_by = 'ratings_average';
  584.         }
  585.         $temp = stripslashes(get_option('postratings_template_highestrated'));
  586.         $highest_rated = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score FROM $wpdb->posts 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 LEFT JOIN $wpdb->postmeta AS t3 ON t3.post_id = $wpdb->posts.ID INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE t1.meta_key = 'ratings_average' AND t2.meta_key = 'ratings_users' AND t3.meta_key = 'ratings_score' AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."'  AND $wpdb->posts.post_status = 'publish' AND $wpdb->term_taxonomy.taxonomy = 'post_tag' AND $tag_sql AND t2.meta_value >= $min_votes AND $where ORDER BY $order_by DESC, ratings_users DESC LIMIT $limit");
  587.         if($highest_rated) {
  588.             foreach($highest_rated as $post) {
  589.                 $output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
  590.             }
  591.         } else {
  592.             $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
  593.         }
  594.         if($display) {
  595.             echo $output;
  596.         } else {
  597.             return $output;
  598.         }
  599.     }
  600. }
  601.  
  602.  
  603. ### Function: Display Lowest Rated Page/Post By Tag ID
  604. if(!function_exists('get_lowest_rated_tag')) {
  605.     function get_lowest_rated_tag($tag_id = 0, $mode = '', $min_votes = 0, $limit = 10, $chars = 0, $display = true) {
  606.         global $wpdb;
  607.         $ratings_max = intval(get_option('postratings_max'));
  608.         $ratings_custom = intval(get_option('postratings_customrating'));
  609.         $output = '';
  610.         if(is_array($tag_id)) {
  611.             $tag_sql = "$wpdb->term_taxonomy.term_id IN (".join(',', $tag_id).')';
  612.         } else {
  613.             $tag_sql = "$wpdb->term_taxonomy.term_id = $tag_id";
  614.         }
  615.         if(!empty($mode) && $mode != 'both') {
  616.             $where = "$wpdb->posts.post_type = '$mode'";
  617.         } else {
  618.             $where = '1=1';
  619.         }
  620.         if($ratings_custom && $ratings_max == 2) {
  621.             $order_by = 'ratings_score';
  622.         } else {
  623.             $order_by = 'ratings_average';
  624.         }
  625.         $temp = stripslashes(get_option('postratings_template_highestrated'));
  626.         $lowest_rated = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score FROM $wpdb->posts 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 LEFT JOIN $wpdb->postmeta AS t3 ON t3.post_id = $wpdb->posts.ID INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE t1.meta_key = 'ratings_average' AND t2.meta_key = 'ratings_users' AND t3.meta_key = 'ratings_score' AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."'  AND $wpdb->posts.post_status = 'publish' AND $wpdb->term_taxonomy.taxonomy = 'post_tag' AND $tag_sql AND t2.meta_value >= $min_votes AND $where ORDER BY $order_by ASC, ratings_users DESC LIMIT $limit");
  627.         if($lowest_rated) {
  628.             foreach($lowest_rated as $post) {
  629.                 $output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
  630.             }
  631.         } else {
  632.             $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
  633.         }
  634.         if($display) {
  635.             echo $output;
  636.         } else {
  637.             return $output;
  638.         }
  639.     }
  640. }
  641.  
  642.  
  643. ### Function: Display Total Rating Users
  644. if(!function_exists('get_ratings_users')) {
  645.     function get_ratings_users($display = true) {
  646.         global $wpdb;
  647.         $ratings_users = $wpdb->get_var("SELECT SUM((meta_value+0.00)) FROM $wpdb->postmeta WHERE meta_key = 'ratings_users'");
  648.         if($display) {
  649.             echo $ratings_users;
  650.         } else {
  651.             return $ratings_users;
  652.         }
  653.     }
  654. }
  655. ?>
Advertisement
Add Comment
Please, Sign In to add comment