kristopherlouie

bbPress Widget, Show Author / Author Link

Apr 26th, 2012
157
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * bbPress Topic Widget
  3.  *
  4.  * Adds a widget which displays the topic list
  5.  *
  6.  * @since bbPress (r2653)
  7.  *
  8.  * @uses WP_Widget
  9.  */
  10. class BAM_Topics_Widget extends WP_Widget {
  11.  
  12.     /**
  13.      * Register the widget
  14.      *
  15.      * @since bbPress (r3389)
  16.      *
  17.      * @uses register_widget()
  18.      */
  19.     function register_widget() {
  20.         register_widget( 'BAM_Topics_Widget' );
  21.     }
  22.  
  23.     /**
  24.      * bbPress Topic Widget
  25.      *
  26.      * Registers the topic widget
  27.      *
  28.      * @since bbPress (r2653)
  29.      *
  30.      * @uses apply_filters() Calls 'BAM_Topics_Widget_options' with the
  31.      *                        widget options
  32.      */
  33.     function BAM_Topics_Widget() {
  34.         $widget_ops = apply_filters( 'BAM_Topics_Widget_options', array(
  35.             'classname'   => 'widget_display_topics',
  36.             'description' => __( 'A list of recent topics, sorted by popularity or freshness.', 'bbpress' )
  37.         ) );
  38.  
  39.         parent::WP_Widget( false, __( 'BAM bbPress Topics List', 'bbpress' ), $widget_ops );
  40.     }
  41.  
  42.     /**
  43.      * Displays the output, the topic list
  44.      *
  45.      * @since bbPress (r2653)
  46.      *
  47.      * @param mixed $args
  48.      * @param array $instance
  49.      * @uses apply_filters() Calls 'bbp_topic_widget_title' with the title
  50.      * @uses bbp_set_query_name() To set the query name to 'bbp_widget'
  51.      * @uses bbp_reset_query_name() To reset the query name
  52.      * @uses bbp_has_topics() The main topic loop
  53.      * @uses bbp_topics() To check whether there are more topics available
  54.      *                     in the loop
  55.      * @uses bbp_the_topic() Loads up the current topic in the loop
  56.      * @uses bbp_topic_permalink() To display the topic permalink
  57.      * @uses bbp_topic_title() To display the topic title
  58.      * @uses bbp_get_topic_last_active_time() To get the topic last active
  59.      *                                         time
  60.      * @uses bbp_get_reply_author_link() To get the reply author link
  61.      * @uses bbp_get_reply_author() To get the reply author name
  62.      * @uses bbp_get_author_link() To get the author link
  63.      * @uses bbp_get_topic_id() To get the topic id
  64.      * @uses bbp_get_topic_reply_count() To get the topic reply count
  65.      */
  66.     function widget( $args, $instance ) {
  67.  
  68.         extract( $args );
  69.  
  70.         $title        = apply_filters( 'bbp_topic_widget_title', $instance['title'] );
  71.         $max_shown    = !empty( $instance['max_shown']    ) ? (int) $instance['max_shown'] : 5;
  72.         $show_date    = !empty( $instance['show_date']    ) ? 'on'                         : false;
  73.         $parent_forum = !empty( $instance['parent_forum'] ) ? $instance['parent_forum']    : 'any';
  74.         $pop_check    = ( $instance['pop_check'] < $max_shown || empty( $instance['pop_check'] ) ) ? -1 : $instance['pop_check'];
  75.  
  76.         // Query defaults
  77.         $topics_query = array(
  78.             'author'         => 1,
  79.             'post_parent'    => $parent_forum,
  80.             'posts_per_page' => $max_shown > $pop_check ? $max_shown : $pop_check,
  81.             'posts_per_page' => $max_shown,
  82.             'show_stickies'  => false,
  83.             'order'          => 'DESC',
  84.         );
  85.  
  86.         bbp_set_query_name( 'bbp_widget' );
  87.  
  88.         // Topics exist
  89.         if ( bbp_has_topics( $topics_query ) ) :
  90.            
  91.             // Sort by time
  92.             if ( $pop_check < $max_shown ) :
  93.  
  94.                 echo $before_widget;
  95.                 echo $before_title . $title . $after_title; ?>
  96.  
  97.                 <ul>
  98.                    
  99.                     <?php while ( bbp_topics() ) : bbp_the_topic(); ?>
  100.                     <?php $author_link = bbp_get_user_profile_link( $author_id ); ?>
  101.                         <li>
  102.                             <a class="bbp-forum-title" href="<?php bbp_topic_permalink(); ?>" title="<?php bbp_topic_title(); ?>"><?php bbp_topic_title(); ?></a>
  103.                             <span>Posted <?php if ( $show_date == 'on' ) _e( '' . bbp_get_topic_last_active_time() . ' ago' ); ?> by <a href="<?php bbp_get_author_link(); ?>" title="<?php bbp_get_reply_author(); ?>"><?php _e( '' . bbp_get_reply_author() . ''); ?></a></span> <!-- author url <?php bbp_get_reply_author_link(); ?> -->
  104.                         </li>
  105.  
  106.                     <?php endwhile; ?>
  107.  
  108.                 </ul>
  109.  
  110.                 <?php echo $after_widget;
  111.  
  112.             // Sort by popularity
  113.             elseif ( $pop_check >= $max_shown ) :
  114.  
  115.                 echo $before_widget;
  116.                 echo $before_title . $title . $after_title;
  117.  
  118.                 while ( bbp_topics() ) {
  119.                     bbp_the_topic();
  120.                     $topics[bbp_get_topic_id()] = bbp_get_topic_reply_count();
  121.                 }
  122.  
  123.                 arsort( $topics );
  124.                 $topic_count = 1;
  125.  
  126.                 ?>
  127.  
  128.                 <ul>
  129.  
  130.                     <?php foreach ( $topics as $topic_id => $topic_reply_count ) : ?>
  131.  
  132.                         <li><a class="bbp-topic-title" href="<?php bbp_topic_permalink( $topic_id ); ?>" title="<?php bbp_topic_title( $topic_id ); ?>"><?php bbp_topic_title( $topic_id ); ?></a><?php if ( $show_date == 'on' ) _e( ', ' . bbp_get_topic_last_active_time( $topic_id ) . ' ago' ); ?></li>
  133.  
  134.                     <?php
  135.  
  136.                         $topic_count++;
  137.  
  138.                         if ( $topic_count > $max_shown )
  139.                             break;
  140.  
  141.                     endforeach; ?>
  142.  
  143.                 </ul>
  144.  
  145.                 <?php echo $after_widget;
  146.  
  147.             endif;
  148.         endif;
  149.  
  150.         bbp_reset_query_name();
  151.  
  152.     }
  153.  
  154.     /**
  155.      * Update the forum widget options
  156.      *
  157.      * @since bbPress (r2653)
  158.      *
  159.      * @param array $new_instance The new instance options
  160.      * @param array $old_instance The old instance options
  161.      */
  162.     function update( $new_instance, $old_instance ) {
  163.         $instance              = $old_instance;
  164.         $instance['title']     = strip_tags( $new_instance['title']     );
  165.         $instance['max_shown'] = strip_tags( $new_instance['max_shown'] );
  166.         $instance['show_date'] = strip_tags( $new_instance['show_date'] );
  167.         $instance['pop_check'] = strip_tags( $new_instance['pop_check'] );
  168.  
  169.         return $instance;
  170.     }
  171.  
  172.     /**
  173.      * Output the topic widget options form
  174.      *
  175.      * @since bbPress (r2653)
  176.      *
  177.      * @param $instance Instance
  178.      * @uses BAM_Topics_Widget::get_field_id() To output the field id
  179.      * @uses BAM_Topics_Widget::get_field_name() To output the field name
  180.      */
  181.     function form( $instance ) {
  182.         $title     = !empty( $instance['title']     ) ? esc_attr( $instance['title']     ) : '';
  183.         $max_shown = !empty( $instance['max_shown'] ) ? esc_attr( $instance['max_shown'] ) : '';
  184.         $show_date = !empty( $instance['show_date'] ) ? esc_attr( $instance['show_date'] ) : '';
  185.         $pop_check = !empty( $instance['pop_check'] ) ? esc_attr( $instance['pop_check'] ) : ''; ?>
  186.  
  187.         <p><label for="<?php echo $this->get_field_id( 'title'     ); ?>"><?php _e( 'Title:',                  'bbpress' ); ?> <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; ?>" /></label></p>
  188.         <p><label for="<?php echo $this->get_field_id( 'max_shown' ); ?>"><?php _e( 'Maximum topics to show:', 'bbpress' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_shown' ); ?>" name="<?php echo $this->get_field_name( 'max_shown' ); ?>" type="text" value="<?php echo $max_shown; ?>" /></label></p>
  189.         <p><label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Show post date:',         'bbpress' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" <?php checked( 'on', $show_date ); ?> /></label></p>
  190.         <p>
  191.             <label for="<?php echo $this->get_field_id( 'pop_check' ); ?>"><?php _e( 'Popularity check:',  'bbpress' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'pop_check' ); ?>" name="<?php echo $this->get_field_name( 'pop_check' ); ?>" type="text" value="<?php echo $pop_check; ?>" /></label>
  192.             <br /><small><?php _e( 'Number of topics back to check reply count to determine popularity. A number less than the maximum number of topics to show disables the check.', 'bbpress' ); ?></small>
  193.         </p>
  194.  
  195.         <?php
  196.     }
  197. }
RAW Paste Data