Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * bbPress Topic Widget
- *
- * Adds a widget which displays the topic list
- *
- * @since bbPress (r2653)
- *
- * @uses WP_Widget
- */
- class BAM_Topics_Widget extends WP_Widget {
- /**
- * Register the widget
- *
- * @since bbPress (r3389)
- *
- * @uses register_widget()
- */
- function register_widget() {
- register_widget( 'BAM_Topics_Widget' );
- }
- /**
- * bbPress Topic Widget
- *
- * Registers the topic widget
- *
- * @since bbPress (r2653)
- *
- * @uses apply_filters() Calls 'BAM_Topics_Widget_options' with the
- * widget options
- */
- function BAM_Topics_Widget() {
- $widget_ops = apply_filters( 'BAM_Topics_Widget_options', array(
- 'classname' => 'widget_display_topics',
- 'description' => __( 'A list of recent topics, sorted by popularity or freshness.', 'bbpress' )
- ) );
- parent::WP_Widget( false, __( 'BAM bbPress Topics List', 'bbpress' ), $widget_ops );
- }
- /**
- * Displays the output, the topic list
- *
- * @since bbPress (r2653)
- *
- * @param mixed $args
- * @param array $instance
- * @uses apply_filters() Calls 'bbp_topic_widget_title' with the title
- * @uses bbp_set_query_name() To set the query name to 'bbp_widget'
- * @uses bbp_reset_query_name() To reset the query name
- * @uses bbp_has_topics() The main topic loop
- * @uses bbp_topics() To check whether there are more topics available
- * in the loop
- * @uses bbp_the_topic() Loads up the current topic in the loop
- * @uses bbp_topic_permalink() To display the topic permalink
- * @uses bbp_topic_title() To display the topic title
- * @uses bbp_get_topic_last_active_time() To get the topic last active
- * time
- * @uses bbp_get_reply_author_link() To get the reply author link
- * @uses bbp_get_reply_author() To get the reply author name
- * @uses bbp_get_author_link() To get the author link
- * @uses bbp_get_topic_id() To get the topic id
- * @uses bbp_get_topic_reply_count() To get the topic reply count
- */
- function widget( $args, $instance ) {
- extract( $args );
- $title = apply_filters( 'bbp_topic_widget_title', $instance['title'] );
- $max_shown = !empty( $instance['max_shown'] ) ? (int) $instance['max_shown'] : 5;
- $show_date = !empty( $instance['show_date'] ) ? 'on' : false;
- $parent_forum = !empty( $instance['parent_forum'] ) ? $instance['parent_forum'] : 'any';
- $pop_check = ( $instance['pop_check'] < $max_shown || empty( $instance['pop_check'] ) ) ? -1 : $instance['pop_check'];
- // Query defaults
- $topics_query = array(
- 'author' => 1,
- 'post_parent' => $parent_forum,
- 'posts_per_page' => $max_shown > $pop_check ? $max_shown : $pop_check,
- 'posts_per_page' => $max_shown,
- 'show_stickies' => false,
- 'order' => 'DESC',
- );
- bbp_set_query_name( 'bbp_widget' );
- // Topics exist
- if ( bbp_has_topics( $topics_query ) ) :
- // Sort by time
- if ( $pop_check < $max_shown ) :
- echo $before_widget;
- echo $before_title . $title . $after_title; ?>
- <ul>
- <?php while ( bbp_topics() ) : bbp_the_topic(); ?>
- <?php $author_link = bbp_get_user_profile_link( $author_id ); ?>
- <li>
- <a class="bbp-forum-title" href="<?php bbp_topic_permalink(); ?>" title="<?php bbp_topic_title(); ?>"><?php bbp_topic_title(); ?></a>
- <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(); ?> -->
- </li>
- <?php endwhile; ?>
- </ul>
- <?php echo $after_widget;
- // Sort by popularity
- elseif ( $pop_check >= $max_shown ) :
- echo $before_widget;
- echo $before_title . $title . $after_title;
- while ( bbp_topics() ) {
- bbp_the_topic();
- $topics[bbp_get_topic_id()] = bbp_get_topic_reply_count();
- }
- arsort( $topics );
- $topic_count = 1;
- ?>
- <ul>
- <?php foreach ( $topics as $topic_id => $topic_reply_count ) : ?>
- <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>
- <?php
- $topic_count++;
- if ( $topic_count > $max_shown )
- break;
- endforeach; ?>
- </ul>
- <?php echo $after_widget;
- endif;
- endif;
- bbp_reset_query_name();
- }
- /**
- * Update the forum widget options
- *
- * @since bbPress (r2653)
- *
- * @param array $new_instance The new instance options
- * @param array $old_instance The old instance options
- */
- function update( $new_instance, $old_instance ) {
- $instance = $old_instance;
- $instance['title'] = strip_tags( $new_instance['title'] );
- $instance['max_shown'] = strip_tags( $new_instance['max_shown'] );
- $instance['show_date'] = strip_tags( $new_instance['show_date'] );
- $instance['pop_check'] = strip_tags( $new_instance['pop_check'] );
- return $instance;
- }
- /**
- * Output the topic widget options form
- *
- * @since bbPress (r2653)
- *
- * @param $instance Instance
- * @uses BAM_Topics_Widget::get_field_id() To output the field id
- * @uses BAM_Topics_Widget::get_field_name() To output the field name
- */
- function form( $instance ) {
- $title = !empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
- $max_shown = !empty( $instance['max_shown'] ) ? esc_attr( $instance['max_shown'] ) : '';
- $show_date = !empty( $instance['show_date'] ) ? esc_attr( $instance['show_date'] ) : '';
- $pop_check = !empty( $instance['pop_check'] ) ? esc_attr( $instance['pop_check'] ) : ''; ?>
- <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>
- <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>
- <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>
- <p>
- <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>
- <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>
- </p>
- <?php
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment