BBP_Topics_Widget

BbPress Topic Widget.

Defined (1)

The class is defined in the following location(s).

/includes/common/widgets.php  
  1. class BBP_Topics_Widget extends WP_Widget { 
  2.  
  3. /** 
  4. * bbPress Topic Widget 
  5. * Registers the topic widget 
  6. * @since bbPress (r2653) 
  7. * @uses apply_filters() Calls 'bbp_topics_widget_options' with the 
  8. * widget options 
  9. */ 
  10. public function __construct() { 
  11. $widget_ops = apply_filters( 'bbp_topics_widget_options', array( 
  12. 'classname' => 'widget_display_topics',  
  13. 'description' => __( 'A list of recent topics, sorted by popularity or freshness.', 'bbpress' ) 
  14. ) ); 
  15.  
  16. parent::__construct( false, __( '(bbPress) Recent Topics', 'bbpress' ), $widget_ops ); 
  17.  
  18. /** 
  19. * Register the widget 
  20. * @since bbPress (r3389) 
  21. * @uses register_widget() 
  22. */ 
  23. public static function register_widget() { 
  24. register_widget( 'BBP_Topics_Widget' ); 
  25.  
  26. /** 
  27. * Displays the output, the topic list 
  28. * @since bbPress (r2653) 
  29. * @param mixed $args 
  30. * @param array $instance 
  31. * @uses apply_filters() Calls 'bbp_topic_widget_title' with the title 
  32. * @uses bbp_topic_permalink() To display the topic permalink 
  33. * @uses bbp_topic_title() To display the topic title 
  34. * @uses bbp_get_topic_last_active_time() To get the topic last active 
  35. * time 
  36. * @uses bbp_get_topic_id() To get the topic id 
  37. */ 
  38. public function widget( $args = array(), $instance = array() ) { 
  39.  
  40. // Get widget settings 
  41. $settings = $this->parse_settings( $instance ); 
  42.  
  43. // Typical WordPress filter 
  44. $settings['title'] = apply_filters( 'widget_title', $settings['title'], $instance, $this->id_base ); 
  45.  
  46. // bbPress filter 
  47. $settings['title'] = apply_filters( 'bbp_topic_widget_title', $settings['title'], $instance, $this->id_base ); 
  48.  
  49. // How do we want to order our results? 
  50. switch ( $settings['order_by'] ) { 
  51.  
  52. // Order by most recent replies 
  53. case 'freshness' : 
  54. $topics_query = array( 
  55. 'post_type' => bbp_get_topic_post_type(),  
  56. 'post_parent' => $settings['parent_forum'],  
  57. 'posts_per_page' => (int) $settings['max_shown'],  
  58. 'post_status' => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),  
  59. 'ignore_sticky_posts' => true,  
  60. 'no_found_rows' => true,  
  61. 'meta_key' => '_bbp_last_active_time',  
  62. 'orderby' => 'meta_value',  
  63. 'order' => 'DESC',  
  64. ); 
  65. break; 
  66.  
  67. // Order by total number of replies 
  68. case 'popular' : 
  69. $topics_query = array( 
  70. 'post_type' => bbp_get_topic_post_type(),  
  71. 'post_parent' => $settings['parent_forum'],  
  72. 'posts_per_page' => (int) $settings['max_shown'],  
  73. 'post_status' => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),  
  74. 'ignore_sticky_posts' => true,  
  75. 'no_found_rows' => true,  
  76. 'meta_key' => '_bbp_reply_count',  
  77. 'orderby' => 'meta_value',  
  78. 'order' => 'DESC' 
  79. ); 
  80. break; 
  81.  
  82. // Order by which topic was created most recently 
  83. case 'newness' : 
  84. default : 
  85. $topics_query = array( 
  86. 'post_type' => bbp_get_topic_post_type(),  
  87. 'post_parent' => $settings['parent_forum'],  
  88. 'posts_per_page' => (int) $settings['max_shown'],  
  89. 'post_status' => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),  
  90. 'ignore_sticky_posts' => true,  
  91. 'no_found_rows' => true,  
  92. 'order' => 'DESC' 
  93. ); 
  94. break; 
  95.  
  96. // Note: private and hidden forums will be excluded via the 
  97. // bbp_pre_get_posts_normalize_forum_visibility action and function. 
  98. $widget_query = new WP_Query( $topics_query ); 
  99.  
  100. // Bail if no topics are found 
  101. if ( ! $widget_query->have_posts() ) { 
  102. return; 
  103.  
  104. echo $args['before_widget']; 
  105.  
  106. if ( !empty( $settings['title'] ) ) { 
  107. echo $args['before_title'] . $settings['title'] . $args['after_title']; 
  108. } ?> 
  109.  
  110. <ul> 
  111.  
  112. <?php while ( $widget_query->have_posts() ) : 
  113.  
  114. $widget_query->the_post(); 
  115. $topic_id = bbp_get_topic_id( $widget_query->post->ID ); 
  116. $author_link = ''; 
  117.  
  118. // Maybe get the topic author 
  119. if ( ! empty( $settings['show_user'] ) ) : 
  120. $author_link = bbp_get_topic_author_link( array( 'post_id' => $topic_id, 'type' => 'both', 'size' => 14 ) ); 
  121. endif; ?> 
  122.  
  123. <li> 
  124. <a class="bbp-forum-title" href="<?php bbp_topic_permalink( $topic_id ); ?>"><?php bbp_topic_title( $topic_id ); ?></a> 
  125.  
  126. <?php if ( ! empty( $author_link ) ) : ?> 
  127.  
  128. <?php printf( _x( 'by %1$s', 'widgets', 'bbpress' ), '<span class="topic-author">' . $author_link . '</span>' ); ?> 
  129.  
  130. <?php endif; ?> 
  131.  
  132. <?php if ( ! empty( $settings['show_date'] ) ) : ?> 
  133.  
  134. <div><?php bbp_topic_last_active_time( $topic_id ); ?></div> 
  135.  
  136. <?php endif; ?> 
  137.  
  138. </li> 
  139.  
  140. <?php endwhile; ?> 
  141.  
  142. </ul> 
  143.  
  144. <?php echo $args['after_widget']; 
  145.  
  146. // Reset the $post global 
  147. wp_reset_postdata(); 
  148.  
  149. /** 
  150. * Update the topic widget options 
  151. * @since bbPress (r2653) 
  152. * @param array $new_instance The new instance options 
  153. * @param array $old_instance The old instance options 
  154. */ 
  155. public function update( $new_instance = array(), $old_instance = array() ) { 
  156. $instance = $old_instance; 
  157. $instance['title'] = strip_tags( $new_instance['title'] ); 
  158. $instance['order_by'] = strip_tags( $new_instance['order_by'] ); 
  159. $instance['parent_forum'] = sanitize_text_field( $new_instance['parent_forum'] ); 
  160. $instance['show_date'] = (bool) $new_instance['show_date']; 
  161. $instance['show_user'] = (bool) $new_instance['show_user']; 
  162. $instance['max_shown'] = (int) $new_instance['max_shown']; 
  163.  
  164. // Force to any 
  165. if ( !empty( $instance['parent_forum'] ) && !is_numeric( $instance['parent_forum'] ) ) { 
  166. $instance['parent_forum'] = 'any'; 
  167.  
  168. return $instance; 
  169.  
  170. /** 
  171. * Output the topic widget options form 
  172. * @since bbPress (r2653) 
  173. * @param $instance Instance 
  174. * @uses BBP_Topics_Widget::get_field_id() To output the field id 
  175. * @uses BBP_Topics_Widget::get_field_name() To output the field name 
  176. */ 
  177. public function form( $instance = array() ) { 
  178.  
  179. // Get widget settings 
  180. $settings = $this->parse_settings( $instance ); ?> 
  181.  
  182. <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 esc_attr( $settings['title'] ); ?>" /></label></p> 
  183. <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 esc_attr( $settings['max_shown'] ); ?>" /></label></p> 
  184.  
  185. <p> 
  186. <label for="<?php echo $this->get_field_id( 'parent_forum' ); ?>"><?php _e( 'Parent Forum ID:', 'bbpress' ); ?> 
  187. <input class="widefat" id="<?php echo $this->get_field_id( 'parent_forum' ); ?>" name="<?php echo $this->get_field_name( 'parent_forum' ); ?>" type="text" value="<?php echo esc_attr( $settings['parent_forum'] ); ?>" /> 
  188. </label> 
  189.  
  190. <br /> 
  191.  
  192. <small><?php _e( '"0" to show only root - "any" to show all', 'bbpress' ); ?></small> 
  193. </p> 
  194.  
  195. <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( true, $settings['show_date'] ); ?> value="1" /></label></p> 
  196. <p><label for="<?php echo $this->get_field_id( 'show_user' ); ?>"><?php _e( 'Show topic author:', 'bbpress' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'show_user' ); ?>" name="<?php echo $this->get_field_name( 'show_user' ); ?>" <?php checked( true, $settings['show_user'] ); ?> value="1" /></label></p> 
  197.  
  198. <p> 
  199. <label for="<?php echo $this->get_field_id( 'order_by' ); ?>"><?php _e( 'Order By:', 'bbpress' ); ?></label> 
  200. <select name="<?php echo $this->get_field_name( 'order_by' ); ?>" id="<?php echo $this->get_field_name( 'order_by' ); ?>"> 
  201. <option <?php selected( $settings['order_by'], 'newness' ); ?> value="newness"><?php _e( 'Newest Topics', 'bbpress' ); ?></option> 
  202. <option <?php selected( $settings['order_by'], 'popular' ); ?> value="popular"><?php _e( 'Popular Topics', 'bbpress' ); ?></option> 
  203. <option <?php selected( $settings['order_by'], 'freshness' ); ?> value="freshness"><?php _e( 'Topics With Recent Replies', 'bbpress' ); ?></option> 
  204. </select> 
  205. </p> 
  206.  
  207. <?php 
  208.  
  209. /** 
  210. * Merge the widget settings into defaults array. 
  211. * @since bbPress (r4802) 
  212. * @param $instance Instance 
  213. * @uses bbp_parse_args() To merge widget options into defaults 
  214. */ 
  215. public function parse_settings( $instance = array() ) { 
  216. return bbp_parse_args( $instance, array( 
  217. 'title' => __( 'Recent Topics', 'bbpress' ),  
  218. 'max_shown' => 5,  
  219. 'show_date' => false,  
  220. 'show_user' => false,  
  221. 'parent_forum' => 'any',  
  222. 'order_by' => false 
  223. ), 'topic_widget_settings' );