home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Plugins / show_top_commentators / show_top_commentators.php
PHP Script  |  2007-06-12  |  6KB  |  154 lines

  1. <?php
  2. /*
  3. Plugin Name: Show Top Commentators
  4. Plugin URI: http://www.pfadvice.com/wordpress-plugins/show-top-commentators/
  5. Description: Encourage more feedback and discussion from readers, by rewarding them every time they post a comment! Readers with the most comments are displayed on your Wordpress blog, with their names (linked to their website if they provided one).
  6. Version: 1.05
  7. Author: Nate Sanden
  8. Author URI: http://www.savingadvice.com
  9.  
  10. Installation Instructions:
  11. http://www.pfadvice.com/wordpress-plugins/show-top-commentators/#install
  12.  
  13. Shameless Begging: While this plugin is completely free to use, we would greatly appreciate a post telling your readers that you are using this new plugin. By doing so you will encourage us to make other great plugins for WordPress that you can also use in the future!
  14. */
  15.  
  16. $ns_options = array(
  17.                     "reset" => "monthly", //reset hourly, daily, weekly, monthly, yearly, all OR # (eg 30 days)
  18.                     "limit"  => 10, //maximum number of commentator's to show
  19.                     "filter_users" => "Administrator,admin", //commma seperated list of users ("nate,jeff,etc").
  20.                     "filter_user_ids" => "1,2", //comma sperated list of user_ids ("1,2")
  21.                     "filter_urls" => "", //commma seperated list of full or partial URL's (www.badsite.com,etc)
  22.                     "none_text" => "None yet!", //if there are no commentators, what should we display?
  23.                     "make_links" => 1, //link the commentator's name to thier website?
  24.                     "number_of_comments" => "y", //show number of comments next to their name? y=yes n=no
  25.                     "name_limit" => 28, //maximum number of characters a commentator's name can be, before we cut it off with an ellipse
  26.                     "start_html" => "<li>",
  27.                     "end_html"   => "</li>",
  28.                    );
  29.  
  30. //first we need to format options so they are useable
  31. $ns_options = ns_format_options($ns_options);
  32.  
  33. function ns_substr_ellipse($str, $len) {
  34.    if(strlen($str) > $len) {
  35.       $str = substr($str, 0, $len-3) . "...";
  36.    }
  37.    return $str;
  38. }
  39.  
  40. //temporary until i can condense this into one query in $commenters
  41. function ns_get_user_url($user) {
  42.    global $wpdb, $ns_options;
  43.     $url = $wpdb->get_var("
  44.        SELECT comment_author_url
  45.        FROM $wpdb->comments
  46.        WHERE comment_author = '".addslashes($user)."'
  47.        AND comment_author_url != 'http://'
  48.        $ns_options[filter_urls]
  49.        ORDER BY comment_date DESC LIMIT 1
  50.    ");
  51.    return $url;
  52. }
  53.  
  54. function ns_show_top_commentators() {
  55.  
  56.    global $wpdb, $ns_options;
  57.  
  58.    if($ns_options["reset"] != '') {
  59.       if(!is_numeric($ns_options["reset"])) {
  60.          $reset_sql = "DATE_FORMAT(comment_date, '$ns_options[reset]') = DATE_FORMAT(CURDATE(), '$ns_options[reset]')";
  61.       } else {
  62.          $reset_sql = "comment_date >= CURDATE() - INTERVAL $ns_options[reset] DAY"; 
  63.       }
  64.    } else {
  65.       $reset_sql = "1=1";
  66.    }
  67.  
  68.     $commenters = $wpdb->get_results("
  69.        SELECT COUNT(comment_author) AS comment_comments, comment_author
  70.        FROM $wpdb->comments o
  71.        WHERE $reset_sql
  72.           AND comment_author NOT IN($ns_options[filter_users])
  73.           AND user_id NOT IN($ns_options[filter_user_ids])
  74.           AND comment_author != ''
  75.           AND comment_type != 'pingback'
  76.           AND comment_approved = '1'
  77.        GROUP BY comment_author
  78.        ORDER BY comment_comments DESC LIMIT $ns_options[limit]
  79.    ");
  80.  
  81.    if(is_array($commenters)) {
  82.        foreach ($commenters as $k) {
  83.           if($ns_options["make_links"] == 1) {
  84.             $url = ns_get_user_url($k->comment_author);
  85.           }
  86.           echo $ns_options["start_html"];
  87.           if(trim($url) != '' && $ns_options["make_links"] == 1) {
  88.              echo "<a href='" . $url . "'>";
  89.           }
  90.           echo ns_substr_ellipse($k->comment_author, $ns_options["name_limit"]);
  91.           if(trim($url) != '' && $ns_options["make_links"] == 1) {
  92.              echo "</a>";
  93.           }
  94.           if($ns_options["number_of_comments"] == 'y') {
  95.                  echo " (" . $k->comment_comments . ")\n";
  96.           }
  97.           echo $ns_options["end_html"] . "\n";
  98.           unset($url);
  99.        }
  100.     } else {
  101.       echo $ns_options["start_html"] . $ns_options["none_text"] . $ns_options["end_html"];;
  102.     }
  103.  
  104. }
  105.  
  106. function ns_format_options($options) {
  107.    //$reset needs to turn into %sql format
  108.     if($options["reset"] == "hourly") {
  109.       $options["reset"] = "%Y-%m-%d %H";
  110.    } elseif($options["reset"] == "daily") {
  111.       $options["reset"] = "%Y-%m-%d";
  112.    } elseif($options["reset"] == "weekly") {
  113.       $options["reset"] = "%Y-%v";
  114.    } elseif($options["reset"] == "monthly") {
  115.       $options["reset"] = "%Y-%m";
  116.    } elseif($options["reset"] == "yearly") {
  117.       $options["reset"] = "%Y";
  118.    } elseif($options["reset"] == "all") {
  119.       $options["reset"] = "";
  120.    } elseif(is_numeric($options["reset"])) {
  121.        $options["reset"] = $options["reset"]; //last x days
  122.    } else {
  123.       $options["reset"] = "%Y-%m"; //just use monthly
  124.    }
  125.    //$filter urls needs to be comma seperated with single quotes
  126.    $filter_urls = trim($options["filter_urls"]);
  127.    if($filter_urls) { $filter_urls = explode(",", $filter_urls); } else { $filter_urls = array(); }
  128.    for($i=0; $i<count($filter_urls); $i++) {
  129.       $new_urls .= " AND comment_author_url NOT LIKE '%" . trim($filter_urls[$i]) . "%'";
  130.    }
  131.    //echo $new_urls;
  132.    $options["filter_urls"] = $new_urls;
  133.    //lets trim $limit just for the hell of it. (you never know)
  134.    $options["limit"] = trim($options["limit"]);
  135.    $options["number_of_comments"] = trim($options["number_of_comments"]);
  136.    $options["name_limit"] = trim($options["name_limit"]);
  137.    //$filter_users needs to be comma seperated with single quotes
  138.    $filter_users = trim($options["filter_users"]);
  139.    $filter_users = explode(",", $filter_users);
  140.    for($i=0; $i<count($filter_users); $i++) {
  141.       $new_users[] = "'" . trim($filter_users[$i]) . "'";
  142.    }
  143.    $options["filter_users"] = implode(",", $new_users);
  144.    //filter userids
  145.    $filter_user_ids = trim($options["filter_user_ids"]);
  146.    $filter_user_ids = explode(",", $filter_user_ids);
  147.    for($i=0; $i<count($filter_user_ids); $i++) {
  148.       $new_user_ids[] = "'" . trim($filter_user_ids[$i]) . "'";
  149.    }
  150.    $options["filter_user_ids"] = implode(",", $new_user_ids);
  151.    return $options;
  152. }
  153.  
  154. ?>