'recent-comments', 'description' => _a('The most recent comments')); $control_ops = array('width' => 500); $this->WP_Widget('atom-recent-comments', _a('Recent Comments'), $widget_ops, $control_ops); atom_add('ajax_requests', array(&$this, 'ajax')); // 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); // include in jQuery(document).ready() atom_add('jquery_init', array(&$this, 'js')); // 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(!defined('COUNTRY_FLAGS') && isset($options['country_flag']) && $options['country_flag'] && is_active_widget(false, $id, $this->id_base)) define('COUNTRY_FLAGS', TRUE); endforeach; } function flush_cache(){ wp_cache_delete('widget_recent_comments', 'widget'); } function js(){ // 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}"; if (isset($options['more']) && $options['more'] && !$options['site_wide'] && is_active_widget(false, $id, $this->id_base)): ?> $("# a.more").click(function(event){ event.preventDefault(); $offset = parseInt($(this).attr('rel')); $.ajax({ type: "GET", url: "", data: { instance: , offset: $offset, _ajax_nonce: "", atom: 'get_recent_comments' }, dataType: 'json', context: this, beforeSend: function() { $(this).addClass("loading"); }, complete: function() { $(this).removeClass("loading"); }, success: function(response){ var link = $(this); // append to list & update tab container height (if we're inside a tabbed widget) if(response.output != '') $(response.output).appendTo("# ul").hide().each(function(i){ $(this).delay(333*i).animate( { "opacity": "show", "height": "show", "marginTop": "show", "marginBottom": "show", "paddingTop": "show", "paddingBottom": "show" }, { duration: 333, step: function(now, fx){ link.parents('li.block .sections').height((link.parents('#').height()) + 5); } }); }); link.attr('rel', response.offset); // no more data? if(!response.more) link.hide(); } }); }); option_name); $instance = intval($_GET['instance']); $settings = $settings[$instance]; $offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0; $output = $this->get_comments($settings, $next, $offset); $offset = $offset + $settings['number']; //$next = ($this->get_comments($settings, $offset)) ? true : false; echo json_encode(array('output' => $output, 'more' => $next, 'offset' => $offset)); exit(); endif; } function template(){ return "\n" ." {AVATAR}\n" ." \n" ." {FLAG}{AUTHOR}\n" ." {CONTENT}\n" ." {DATE}\n" ." \n" ."\n"; } function get_comments($args, &$more, $offset = 0){ extract($args); if($site_wide): global $wpdb; $selects = array(); foreach (atom_get_sites() as $blog) $selects[] = "(SELECT comment_ID, comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date_gmt, comment_content, post_title, {$blog['blog_id']} as blog_id FROM ".$wpdb->get_blog_prefix($blog['blog_id'])."comments LEFT JOIN ".$wpdb->get_blog_prefix($blog['blog_id'])."posts ON comment_post_id = id WHERE post_status = 'publish' AND post_password = '' AND comment_approved = '1' AND comment_type = '' ORDER BY comment_date_gmt DESC LIMIT {$number})"; // real number is (number * # of blogs) $comments = $wpdb->get_results(implode(" UNION ALL ", $selects)." ORDER BY comment_date_gmt DESC", OBJECT); else: $comments = get_comments(array('number' => $number+1, 'status' => 'approve', 'offset' => intval($offset))); endif; $more = count($comments) > $number ? true : false; $count = 1; $output = ''; foreach((array)$comments as $comment): if($count++ == $number+1) break; $output .= ''; // output template $output .= atom_render_template($args['template'], array( 'TITLE' => $site_wide ? strip_tags($comment->post_title) : get_the_title($comment->comment_post_ID), 'URL' => $site_wide ? get_blog_permalink($comment->blog_id, $comment->comment_post_ID) : esc_url(get_comment_link($comment->comment_ID)), // @todo: get comment page for MU urls 'AVATAR' => atom_get_avatar($comment->comment_author_email, $avatar_size, '', $comment->comment_author), 'FLAG' => (($country_flag && $country = atom_get_country($comment->comment_author_IP)) ? " " : null), 'AUTHOR' => $comment->comment_author, 'CONTENT' => convert_smilies(atom_filter_content($comment->comment_content, array( 'limit' => $word_count, 'allowed_tags' => explode(',', $allowed_tags), 'more' => $content_filter_more ))), 'DATE' => atom_time_since(abs(strtotime($comment->comment_date_gmt))), 'EMAIL' => esc_url($comment->comment_author_email), // should not be used 'ID' => $comment->comment_ID, )); $output .= ''; endforeach; return $output; } function widget($args, $instance){ $cache = wp_cache_get('widget_recent_comments', 'widget'); if (!is_array($cache)) $cache = array(); if (isset($cache[$args['widget_id']])): echo $cache[$args['widget_id']]; return; endif; extract($args, EXTR_SKIP); $instance = wp_parse_args($instance, $this->defaults()); $comments = $this->get_comments($instance, $next); if(!$comments) return atom_add_debug_message("No comments were found in {$args['widget_id']} ({$args['widget_name']}). Widget marked as inactive");;; $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base); $output = $before_widget; if ($title) $output .= $before_title.$title.$after_title; $output .= ""; if($instance['more'] && !$instance['site_wide'] && $next && atom_get_options('jquery')) $output .= ""._a("Show More").""; $output .= $after_widget; echo $output; $cache[$args['widget_id']] = $output; wp_cache_set('widget_recent_comments', $cache, 'widget'); } function update($new_instance, $old_instance){ $instance = $old_instance; $instance['title'] = esc_attr($new_instance['title']); $instance['number'] = min(max(intval($new_instance['number']), 1), 20); $instance['word_count'] = intval($new_instance['word_count']); $instance['avatar_size'] = intval($new_instance['avatar_size']); $instance['country_flag'] = (bool)$new_instance['country_flag']; $instance['more'] = (bool)$new_instance['more']; // html template if(current_user_can('edit_themes')) $instance['template'] = $new_instance['template']; // mu option if(atom_mu_network()) $instance['site_wide'] = (bool)$new_instance['site_wide']; $this->flush_cache(); return wp_parse_args($instance, $this->defaults()); } function defaults(){ // default settings return apply_filters('atom_widget_recent_comments_defaults', array( 'title' => _a("Recent Comments"), 'number' => 5, 'word_count' => 20, 'avatar_size' => 48, 'country_flag' => false, 'more' => true, 'template' => $this->template(), 'site_wide' => false, // internal settings 'allowed_tags' => 'abbr,acronym,b,cite,code,del,dfn,em,i,ins,q,strong,sub,sup', 'content_filter_more' => '[…]', ), $this); } function form($instance){ $instance = wp_parse_args($instance, $this->defaults()); ?>
>

followRules />

get_field_id('word_count').'" name="'.$this->get_field_name('word_count').'" type="text" value="'.(isset($instance['word_count']) ? intval($instance['word_count']) : null).'" size="3" />'; ?>

/>

type="checkbox" id="get_field_id('more'); ?>" name="get_field_name('more'); ?>" followRules rules="CONFLICTS WITH get_field_name('site_wide'); ?>" />