'top-commenters', 'description' => _a("People who frequently comment the blog")); $control_ops = array('width' => 500); $this->WP_Widget('atom-top-commenters', _a('Top Commenters'), $widget_ops, $control_ops); // run after the widget instance is properly registered (we only need to define the COUNTRY_FLAGS constant) add_action('widgets_init', array(&$this, 'set_country_flags'), 99); // flush cache when comments are changed add_action('comment_post', array(&$this, 'flush_cache')); add_action('transition_comment_status', array(&$this, 'flush_cache')); } function set_country_flags(){ // we need to process all instances because this function gets to run only once $widget_settings = atom_get_options($this->option_name); foreach((array)$widget_settings as $instance => $options): // identify instance $id = "{$this->id_base}-{$instance}"; $block_id = "instance-{$id}"; // we need to set a custom margin value to the list text, if the widget shows avatars if((isset($options['country_flag'])) && is_active_widget(false, $id, $this->id_base) && !defined('COUNTRY_FLAGS')) define('COUNTRY_FLAGS', TRUE); endforeach; } function flush_cache(){ wp_cache_delete('widget_top_commenters', 'widget'); } function template($mode = 'full'){ switch($mode): case 'full': return "\n" ." {AVATAR}\n" ." \n" ." {FLAG}{NAME}\n" ." {COMMENT_COUNT}\n" ." \n" ."\n"; break; case 'images': return "\n" ." {AVATAR}\n" ."\n"; break; case 'brief': return "\n" ." \n" ." {NAME}\n" ." \n" ."\n"; break; case 'detailed': return "\n" ." \n" ." {NAME}\n" ." {COMMENT_COUNT}\n" ." \n" ."\n"; break; endswitch; } function widget($args, $instance){ global $wpdb; $cache = wp_cache_get('widget_top_commenters', 'widget'); if (!is_array($cache)) $cache = array(); if (isset($cache[$args['widget_id']])): echo $cache[$args['widget_id']]; return; endif; extract($args); $instance = wp_parse_args($instance, $this->defaults()); $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base); $output = $before_widget; $sql = "SELECT comment_author, comment_author_email, comment_author_url, comment_author_IP, user_id, count(1) as counter from {$wpdb->comments} WHERE comment_approved = '1' AND comment_type != 'pingback'"; // group by (only the first two are used): // - comment_author (name, default) // - comment_author_email (same users may have different emails) // - comment_author_url (people can have multiple sites) // - comment_author_IP (really useless, lots of people have dynamic IPs) $sql .= " GROUP BY comment_author, comment_author_email ORDER BY counter DESC, comment_date DESC LIMIT ".intval($instance['number']); $commenters = $wpdb->get_results($wpdb->prepare($sql)); if(!$commenters) return atom_add_debug_message("No relevant entries found in {$args['widget_id']} ({$args['widget_name']}). Widget marked as inactive");; if($title) $output .= $before_title.$title.$after_title; $output .= "'; $output .= $after_widget; echo $output; $cache[$args['widget_id']] = $output; wp_cache_set('widget_top_commenters', $cache, 'widget'); } function update($new_instance, $old_instance){ $instance['title'] = esc_attr($new_instance['title']); $instance['number'] = min(max(intval($new_instance['number']), 1), 50); $instance['mode'] = esc_attr($new_instance['mode']); $instance['avatar_size'] = intval($new_instance['avatar_size']); $instance['country_flag'] = (bool)$new_instance['country_flag']; // html template if(isset($new_instance['template']) && current_user_can('edit_themes')) $instance['template'] = $new_instance['template']; $this->flush_cache(); return wp_parse_args($instance, $this->defaults()); } function defaults(){ return apply_filters('atom_widget_top_commenters_defaults', array( 'title' => _a("Top Commenters"), 'number' => 12, 'mode' => 'avatars', 'avatar_size' => 72, 'country_flag' => false, 'template' => $this->template(), ), $this); } function form($instance){ $instance = wp_parse_args($instance, $this->defaults()); ?>
>