$len) {
$str = substr($str, 0, $len-3) . "...";
}
return $str;
}
// This is the function that queries whether the commenters have entered a URL
function ns_get_user_url($user) {
global $wpdb;
$url = $wpdb->get_var("
SELECT comment_author_url
FROM $wpdb->comments
WHERE comment_author = '".addslashes($user)."'
AND comment_author_url != 'http://'
AND comment_approved = 1
ORDER BY comment_date DESC LIMIT 1
");
return $url;
}
// This is the function that writes out the top commentators list
function ns_show_top_commentators() {
// Each widget can store its own options. We keep strings here.
$options = get_option('widget_topcomm');
// This prepares URLs for filtering
if($options['filterUrl'] != "") {
$filterUrl = trim($options['filterUrl']);
$filterUrl = explode(",", $filterUrl);
for($i=0; $i ";
$listEnd = "
";
}
// This is the function that prepare time period limitation for filtering
if($listPeriod == "h") {
$listPeriod = "DATE_FORMAT(comment_date, '$%Y-%m-%d %H') = DATE_FORMAT(CURDATE(), '%Y-%m-%d %H')";
} elseif($listPeriod == "d") {
$listPeriod = "DATE_FORMAT(comment_date, '%Y-%m-%d') = DATE_FORMAT(CURDATE(), '%Y-%m-%d')";
} elseif($listPeriod == "w") {
$listPeriod = "DATE_FORMAT(comment_date, '%Y-%v') = DATE_FORMAT(CURDATE(), '%Y-%v')";
} elseif($listPeriod == "m") {
$listPeriod = "DATE_FORMAT(comment_date, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m')";
} elseif($listPeriod == "y") {
$listPeriod = "DATE_FORMAT(comment_date, '%Y') = DATE_FORMAT(CURDATE(), '%Y')";
} elseif($listPeriod == "a") {
$listPeriod = "1=1";
} elseif(is_numeric($listPeriod)) {
$listPeriod = "comment_date >= CURDATE() - INTERVAL $listPeriod DAY";
} else {
$listPeriod = "DATE_FORMAT(comment_date, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m')";
}
// This writes out the proper SQL whether to group commenters
// by e-mail or user name
if($options['groupBy'] == "0") {
$groupBy = "GROUP BY comment_author";
} else {
$groupBy = "GROUP BY comment_author_email";
}
// These lines generate our output. Widgets can be very complex
// but as you can see here, they can also be very, very simple.
global $wpdb;
$commenters = $wpdb->get_results("
SELECT COUNT(comment_author) AS comment_comments, comment_author, comment_author_url, comment_author_email
FROM $wpdb->comments
WHERE comment_type != 'pingback'
AND comment_author != ''
AND comment_approved = '1'
AND $listPeriod
$excludeNames
$filterUrl
$filterEmail
$groupBy
ORDER BY comment_comments DESC, comment_author ASC
$limitList
");
if(is_array($commenters)) {
$countList = 0;
echo $listDesc . "\n";
echo $listStart . "\n";
foreach ($commenters as $k) {
$url = ns_get_user_url($k->comment_author);
// This writes out the list of commentors and checks for 1) Hyperlink each name,
// 2) NoFollow each name, 3) Limit characters in names, 4) Remarks for blank list, and
// 5) Display only users with URLs variables
// check if onlyWithUrl = 1
if($options['onlyWithUrl'] == '1') {
// if onlyWithUrl = 1, check if trimurl != ''
// write
if(trim($url) != '') {
echo " ';
}
echo ns_substr_ellipse($k->comment_author, $limitChar);
if(trim($url) != '') {
if($makeLink == 1)
echo "";
if($showCount == 1) echo " (" . $k->comment_comments . ")";
}
echo "
';
}
echo ns_substr_ellipse($k->comment_author, $limitChar);
if(trim($url) != '') {
if($makeLink == 1)
echo "";
if($showCount == 1) echo " (" . $k->comment_comments . ")";
}
echo "
" . "\n";
}
}
// This is the function that outputs our top commentators list
function widget_topcomm($args) {
// $args is an array of strings that help widgets to conform to
// the active theme: before_widget, before_title, after_widget,
// and after_title are the array keys. Default tags: li and h2.
extract($args);
// This one string determines whether you want it to appear in the main page or everywhere
$options = get_option('widget_topcomm');
$showInHome = $options['showInHome'];
$title = htmlspecialchars(stripcslashes($options['title']), ENT_QUOTES);
global $wpdb;
if($showInHome == 1) {
if(is_home())
echo $before_widget . $before_title . $title . $after_title;
} else {
echo $before_widget . $before_title . $title . $after_title;
}
if($showInHome == 1) {
if(is_home())
echo ns_show_top_commentators();
} else {
echo ns_show_top_commentators();
}
if($showInHome == 1) {
if(is_home())
echo $after_widget . "";
} else {
echo $after_widget . "";
}
}
function widget_topcomm_control() {
// Get our options and see if we're handling a form submission.
$options = get_option('widget_topcomm');
if (!is_array($options) )
$options = array('title'=>'Top Commentators', 'listDesc'=>'You commented; therefore you are loved:', 'excludeNames'=>'\'Lorna\', \'Administrator\'','limitList'=>'10', 'limitChar'=>'25', 'listNull'=>'Be the first to comment in my site.', 'filterUrl'=>'', 'filterEmail'=>'', 'listType'=>'bul', 'listPeriod'=>'m', 'makeLink'=>'1', 'noFollow'=>'1', 'showCount'=>'1', 'groupBy'=>'1', 'showInHome'=>'0', 'onlyWithUrl'=>'0', 'displayGravatar'=>'0', 'avatarSize'=>'20');
if ( $_POST['topcomm-submit'] ) {
// Remember to sanitize and format use input appropriately.
$options['title'] = $_POST['topcomm-title'];
$options['excludeNames'] = $_POST['topcomm-excludeNames'];
$options['limitList'] = $_POST['topcomm-limitList'];
$options['limitChar'] = $_POST['topcomm-limitChar'];
$options['listDesc'] = $_POST['topcomm-listDesc'];
$options['listType'] = $_POST['topcomm-listType'];
if($_POST['topcomm-listPeriodnum'] == '') {
$options['listPeriod'] = $_POST['topcomm-listPeriod'];
} else {
$options['listPeriod'] = $_POST['topcomm-listPeriodnum'];
}
$options['listNull'] = $_POST['topcomm-listNull'];
$options['filterUrl'] = $_POST['topcomm-filterUrl'];
$options['filterEmail'] = $_POST['topcomm-filterEmail'];
$options['makeLink'] = $_POST['topcomm-makeLink'];
$options['noFollow'] = $_POST['topcomm-noFollow'];
$options['showInHome'] = $_POST['topcomm-showInHome'];
$options['onlyWithUrl'] = $_POST['topcomm-onlyWithUrl'];
$options['showCount'] = $_POST['topcomm-showCount'];
$options['groupBy'] = $_POST['topcomm-groupBy'];
// Gravatar options
$options['displayGravatar'] = $_POST['topcomm-displayGravatar'];
$options['avatarSize'] = ($_POST['topcomm-avatarSize'] ? $_POST['topcomm-avatarSize'] : 20) ;
update_option('widget_topcomm', $options);
}
// Be sure you format your options to be valid HTML attributes.
// Here is our little form segment. Notice that we don't need a
// complete form. This will be embedded into the existing form.
$options = get_option('widget_topcomm');
$title = htmlspecialchars(stripcslashes($options['title']), ENT_QUOTES);
$excludeNames = htmlspecialchars(stripcslashes($options['excludeNames']), ENT_QUOTES);
$limitList = $options['limitList'];
$limitChar = $options['limitChar'];
$listDesc = $options['listDesc'];
$listType = $options['listType'];
$listPeriod = $options['listPeriod'];
$listNull = $options['listNull'];
$filterUrl = $options['filterUrl'];
$filterEmail = $options['filterEmail'];
$makeLink = $options['makeLink'];
$noFollow = $options['noFollow'];
$showInHome = $options['showInHome'];
$onlyWithUrl = $options['onlyWithUrl'];
$showCount = $options['showCount'];
$groupBy = $options['groupBy'];
$displayGravatar = $options['displayGravatar'];
$avatarSize = $options['avatarSize'];
?>
Leave blank to exclude description.
Separate each name with a comma (,)
Or specify number of days:
Enter numbers only
Enter numbers only
Separate each URl with a comma (,)
Separate each e-mail with a comma (,)
'; } // This registers our widget so it appears with the other available // widgets and can be dragged and dropped into any active sidebars. register_sidebar_widget(array('Top Commentators', 'widgets'), 'widget_topcomm'); // This registers our optional widget control form. Because of this // our widget will have a button that reveals a 300x100 pixel form. register_widget_control(array('Top Commentators', 'widgets'), 'widget_topcomm_control', 410, 500); } // Run our code later in case this loads prior to any required plugins. add_action('widgets_init', 'widget_topcomm_init'); ?>