Advertisement
Guest User

Untitled

a guest
Oct 17th, 2012
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.38 KB | None | 0 0
  1. class SPG_Widget_Recent_Comments extends WP_Widget {
  2.  
  3.     function __construct() {
  4.         $widget_ops = array('classname' => 'spg_widget_recent_comments', 'description' => __( 'The most recent comments' ) );
  5.         parent::__construct('spg-recent-comments', __('Recent Comments - Categorized'), $widget_ops);
  6.         $this->alt_option_name = 'spg_widget_recent_comments';
  7.  
  8.         if ( is_active_widget(false, false, $this->id_base) )
  9.             add_action( 'wp_head', array(&$this, 'recent_comments_style') );
  10.  
  11.         add_action( 'comment_post', array(&$this, 'flush_widget_cache') );
  12.         add_action( 'transition_comment_status', array(&$this, 'flush_widget_cache') );
  13.     }
  14.  
  15.     function recent_comments_style() {
  16.         if ( ! current_theme_supports( 'widgets' ) // Temp hack #14876
  17.             || ! apply_filters( 'show_recent_comments_widget_style', true, $this->id_base ) )
  18.             return;
  19.         ?>
  20.     <style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>
  21. <?php
  22.     }
  23.  
  24.     function flush_widget_cache() {
  25.         wp_cache_delete('spg_widget_recent_comments', 'widget');
  26.     }
  27.  
  28.     function widget( $args, $instance ) {
  29.         global $comments, $comment;
  30.  
  31.         $cache = wp_cache_get('spg_widget_recent_comments', 'widget');
  32.  
  33.         if ( ! is_array( $cache ) )
  34.             $cache = array();
  35.  
  36.         if ( ! isset( $args['widget_id'] ) )
  37.             $args['widget_id'] = $this->id;
  38.  
  39.         if ( isset( $cache[ $args['widget_id'] ] ) ) {
  40.             echo $cache[ $args['widget_id'] ];
  41.             return;
  42.         }
  43.  
  44.         extract($args, EXTR_SKIP);
  45.         $output = '';
  46.         $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Recent Comments' ) : $instance['title'], $instance, $this->id_base );
  47.  
  48.         if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
  49.             $number = 5;
  50.        
  51.         $category_name = empty( $instance['category_name'] ) ? '' : $instance['category_name'];
  52.        
  53.         $comments = get_comments( apply_filters( 'widget_comments_args', array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish' ) ) );
  54.         $output .= $before_widget;
  55.         if ( $title )
  56.             $output .= $before_title . $title . $after_title;
  57.            
  58.         $output .= '<ul id="recentcomments">';
  59.         if ( $comments ) {
  60.             foreach ( (array) $comments as $comment) {
  61.                 $output .=  '<li class="recentcomments">' . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(_x('%1$s on %2$s', 'widgets'), get_comment_author_link(), '<a href="' . esc_url( get_comment_link($comment->comment_ID) ) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
  62.             }
  63.         }
  64.         $output .= '</ul>';
  65.         $output .= $after_widget;
  66.  
  67.         echo $output;
  68.         $cache[$args['widget_id']] = $output;
  69.         wp_cache_set('widget_recent_comments', $cache, 'widget');
  70.     }
  71.  
  72.     function update( $new_instance, $old_instance ) {
  73.         $instance = $old_instance;
  74.         $instance['title'] = strip_tags($new_instance['title']);
  75.         $instance['number'] = absint( $new_instance['number'] );
  76.         $instance['category_name'] = strip_tags( $new_instance['category_name'] );
  77.         $this->flush_widget_cache();
  78.  
  79.         $alloptions = wp_cache_get( 'alloptions', 'options' );
  80.         if ( isset($alloptions['widget_recent_comments']) )
  81.             delete_option('widget_recent_comments');
  82.  
  83.         return $instance;
  84.     }
  85.  
  86.     function form( $instance ) {
  87.         $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
  88.         $number = isset($instance['number']) ? absint($instance['number']) : 5;
  89.         $category_name = esc_attr( $instance['category_name'] );
  90. ?>
  91.         <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  92.         <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
  93.  
  94.         <p>
  95.             <label for="<?php echo $this->get_field_id('category_name'); ?>"><?php _e( 'Only from category:' ); ?></label> <input type="text" value="<?php echo $category_name; ?>" name="<?php echo $this->get_field_name('category_name'); ?>" id="<?php echo $this->get_field_id('category_name'); ?>" class="widefat" />
  96.             <br />
  97.             <small><?php _e( 'Category slug, separated by commas.' ); ?></small>
  98.         </p>
  99.        
  100.         <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of comments to show:'); ?></label>
  101.         <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
  102. <?php
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement