BBP_Forums_Widget

BbPress Forum Widget.

Defined (1)

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

/includes/common/widgets.php  
  1. class BBP_Forums_Widget extends WP_Widget { 
  2.  
  3. /** 
  4. * bbPress Forum Widget 
  5. * Registers the forum widget 
  6. * @since bbPress (r2653) 
  7. * @uses apply_filters() Calls 'bbp_forums_widget_options' with the 
  8. * widget options 
  9. */ 
  10. public function __construct() { 
  11. $widget_ops = apply_filters( 'bbp_forums_widget_options', array( 
  12. 'classname' => 'widget_display_forums',  
  13. 'description' => __( 'A list of forums with an option to set the parent.', 'bbpress' ) 
  14. ) ); 
  15.  
  16. parent::__construct( false, __( '(bbPress) Forums List', '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_Forums_Widget' ); 
  25.  
  26. /** 
  27. * Displays the output, the forum list 
  28. * @since bbPress (r2653) 
  29. * @param mixed $args Arguments 
  30. * @param array $instance Instance 
  31. * @uses apply_filters() Calls 'bbp_forum_widget_title' with the title 
  32. * @uses get_option() To get the forums per page option 
  33. * @uses current_user_can() To check if the current user can read 
  34. * private() To resety name 
  35. * @uses bbp_has_forums() The main forum loop 
  36. * @uses bbp_forums() To check whether there are more forums available 
  37. * in the loop 
  38. * @uses bbp_the_forum() Loads up the current forum in the loop 
  39. * @uses bbp_forum_permalink() To display the forum permalink 
  40. * @uses bbp_forum_title() To display the forum title 
  41. */ 
  42. public function widget( $args, $instance ) { 
  43.  
  44. // Get widget settings 
  45. $settings = $this->parse_settings( $instance ); 
  46.  
  47. // Typical WordPress filter 
  48. $settings['title'] = apply_filters( 'widget_title', $settings['title'], $instance, $this->id_base ); 
  49.  
  50. // bbPress filter 
  51. $settings['title'] = apply_filters( 'bbp_forum_widget_title', $settings['title'], $instance, $this->id_base ); 
  52.  
  53. // Note: private and hidden forums will be excluded via the 
  54. // bbp_pre_get_posts_normalize_forum_visibility action and function. 
  55. $widget_query = new WP_Query( array( 
  56. 'post_type' => bbp_get_forum_post_type(),  
  57. 'post_parent' => $settings['parent_forum'],  
  58. 'post_status' => bbp_get_public_status_id(),  
  59. 'posts_per_page' => get_option( '_bbp_forums_per_page', 50 ),  
  60. 'ignore_sticky_posts' => true,  
  61. 'no_found_rows' => true,  
  62. 'orderby' => 'menu_order title',  
  63. 'order' => 'ASC' 
  64. ) ); 
  65.  
  66. // Bail if no posts 
  67. if ( ! $widget_query->have_posts() ) { 
  68. return; 
  69.  
  70. echo $args['before_widget']; 
  71.  
  72. if ( !empty( $settings['title'] ) ) { 
  73. echo $args['before_title'] . $settings['title'] . $args['after_title']; 
  74. } ?> 
  75.  
  76. <ul> 
  77.  
  78. <?php while ( $widget_query->have_posts() ) : $widget_query->the_post(); ?> 
  79.  
  80. <li><a class="bbp-forum-title" href="<?php bbp_forum_permalink( $widget_query->post->ID ); ?>"><?php bbp_forum_title( $widget_query->post->ID ); ?></a></li> 
  81.  
  82. <?php endwhile; ?> 
  83.  
  84. </ul> 
  85.  
  86. <?php echo $args['after_widget']; 
  87.  
  88. // Reset the $post global 
  89. wp_reset_postdata(); 
  90.  
  91. /** 
  92. * Update the forum widget options 
  93. * @since bbPress (r2653) 
  94. * @param array $new_instance The new instance options 
  95. * @param array $old_instance The old instance options 
  96. */ 
  97. public function update( $new_instance, $old_instance ) { 
  98. $instance = $old_instance; 
  99. $instance['title'] = strip_tags( $new_instance['title'] ); 
  100. $instance['parent_forum'] = sanitize_text_field( $new_instance['parent_forum'] ); 
  101.  
  102. // Force to any 
  103. if ( !empty( $instance['parent_forum'] ) && !is_numeric( $instance['parent_forum'] ) ) { 
  104. $instance['parent_forum'] = 'any'; 
  105.  
  106. return $instance; 
  107.  
  108. /** 
  109. * Output the forum widget options form 
  110. * @since bbPress (r2653) 
  111. * @param $instance Instance 
  112. * @uses BBP_Forums_Widget::get_field_id() To output the field id 
  113. * @uses BBP_Forums_Widget::get_field_name() To output the field name 
  114. */ 
  115. public function form( $instance ) { 
  116.  
  117. // Get widget settings 
  118. $settings = $this->parse_settings( $instance ); ?> 
  119.  
  120. <p> 
  121. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'bbpress' ); ?> 
  122. <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'] ); ?>" /> 
  123. </label> 
  124. </p> 
  125.  
  126. <p> 
  127. <label for="<?php echo $this->get_field_id( 'parent_forum' ); ?>"><?php _e( 'Parent Forum ID:', 'bbpress' ); ?> 
  128. <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'] ); ?>" /> 
  129. </label> 
  130.  
  131. <br /> 
  132.  
  133. <small><?php _e( '"0" to show only root - "any" to show all', 'bbpress' ); ?></small> 
  134. </p> 
  135.  
  136. <?php 
  137.  
  138. /** 
  139. * Merge the widget settings into defaults array. 
  140. * @since bbPress (r4802) 
  141. * @param $instance Instance 
  142. * @uses bbp_parse_args() To merge widget settings into defaults 
  143. */ 
  144. public function parse_settings( $instance = array() ) { 
  145. return bbp_parse_args( $instance, array( 
  146. 'title' => __( 'Forums', 'bbpress' ),  
  147. 'parent_forum' => 0 
  148. ), 'forum_widget_settings' );