BBP_Replies_Widget

BbPress Replies Widget.

Defined (1)

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

/includes/common/widgets.php  
  1. class BBP_Replies_Widget extends WP_Widget { 
  2.  
  3. /** 
  4. * bbPress Replies Widget 
  5. * Registers the replies widget 
  6. * @since bbPress (r2653) 
  7. * @uses apply_filters() Calls 'bbp_replies_widget_options' with the 
  8. * widget options 
  9. */ 
  10. public function __construct() { 
  11. $widget_ops = apply_filters( 'bbp_replies_widget_options', array( 
  12. 'classname' => 'widget_display_replies',  
  13. 'description' => __( 'A list of the most recent replies.', 'bbpress' ) 
  14. ) ); 
  15.  
  16. parent::__construct( false, __( '(bbPress) Recent Replies', '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_Replies_Widget' ); 
  25.  
  26. /** 
  27. * Displays the output, the replies list 
  28. * @since bbPress (r2653) 
  29. * @param mixed $args 
  30. * @param array $instance 
  31. * @uses apply_filters() Calls 'bbp_reply_widget_title' with the title 
  32. * @uses bbp_get_reply_author_link() To get the reply author link 
  33. * @uses bbp_get_reply_id() To get the reply id 
  34. * @uses bbp_get_reply_url() To get the reply url 
  35. * @uses bbp_get_reply_excerpt() To get the reply excerpt 
  36. * @uses bbp_get_reply_topic_title() To get the reply topic title 
  37. * @uses get_the_date() To get the date of the reply 
  38. * @uses get_the_time() To get the time of the reply 
  39. */ 
  40. public function widget( $args, $instance ) { 
  41.  
  42. // Get widget settings 
  43. $settings = $this->parse_settings( $instance ); 
  44.  
  45. // Typical WordPress filter 
  46. $settings['title'] = apply_filters( 'widget_title', $settings['title'], $instance, $this->id_base ); 
  47.  
  48. // bbPress filter 
  49. $settings['title'] = apply_filters( 'bbp_replies_widget_title', $settings['title'], $instance, $this->id_base ); 
  50.  
  51. // Note: private and hidden forums will be excluded via the 
  52. // bbp_pre_get_posts_normalize_forum_visibility action and function. 
  53. $widget_query = new WP_Query( array( 
  54. 'post_type' => bbp_get_reply_post_type(),  
  55. 'post_status' => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),  
  56. 'posts_per_page' => (int) $settings['max_shown'],  
  57. 'ignore_sticky_posts' => true,  
  58. 'no_found_rows' => true,  
  59. ) ); 
  60.  
  61. // Bail if no replies 
  62. if ( ! $widget_query->have_posts() ) { 
  63. return; 
  64.  
  65. echo $args['before_widget']; 
  66.  
  67. if ( !empty( $settings['title'] ) ) { 
  68. echo $args['before_title'] . $settings['title'] . $args['after_title']; 
  69. } ?> 
  70.  
  71. <ul> 
  72.  
  73. <?php while ( $widget_query->have_posts() ) : $widget_query->the_post(); ?> 
  74.  
  75. <li> 
  76.  
  77. <?php 
  78.  
  79. // Verify the reply ID 
  80. $reply_id = bbp_get_reply_id( $widget_query->post->ID ); 
  81. $reply_link = '<a class="bbp-reply-topic-title" href="' . esc_url( bbp_get_reply_url( $reply_id ) ) . '" title="' . esc_attr( bbp_get_reply_excerpt( $reply_id, 50 ) ) . '">' . bbp_get_reply_topic_title( $reply_id ) . '</a>'; 
  82.  
  83. // Only query user if showing them 
  84. if ( ! empty( $settings['show_user'] ) ) : 
  85. $author_link = bbp_get_reply_author_link( array( 'post_id' => $reply_id, 'type' => 'both', 'size' => 14 ) ); 
  86. else : 
  87. $author_link = false; 
  88. endif; 
  89.  
  90. // Reply author, link, and timestamp 
  91. if ( ! empty( $settings['show_date'] ) && !empty( $author_link ) ) : 
  92.  
  93. // translators: 1: reply author, 2: reply link, 3: reply timestamp 
  94. printf( _x( '%1$s on %2$s %3$s', 'widgets', 'bbpress' ), $author_link, $reply_link, '<div>' . bbp_get_time_since( get_the_time( 'U' ) ) . '</div>' ); 
  95.  
  96. // Reply link and timestamp 
  97. elseif ( ! empty( $settings['show_date'] ) ) : 
  98.  
  99. // translators: 1: reply link, 2: reply timestamp 
  100. printf( _x( '%1$s %2$s', 'widgets', 'bbpress' ), $reply_link, '<div>' . bbp_get_time_since( get_the_time( 'U' ) ) . '</div>' ); 
  101.  
  102. // Reply author and title 
  103. elseif ( !empty( $author_link ) ) : 
  104.  
  105. // translators: 1: reply author, 2: reply link 
  106. printf( _x( '%1$s on %2$s', 'widgets', 'bbpress' ), $author_link, $reply_link ); 
  107.  
  108. // Only the reply title 
  109. else : 
  110.  
  111. // translators: 1: reply link 
  112. printf( _x( '%1$s', 'widgets', 'bbpress' ), $reply_link ); 
  113.  
  114. endif; 
  115.  
  116. ?> 
  117.  
  118. </li> 
  119.  
  120. <?php endwhile; ?> 
  121.  
  122. </ul> 
  123.  
  124. <?php echo $args['after_widget']; 
  125.  
  126. // Reset the $post global 
  127. wp_reset_postdata(); 
  128.  
  129. /** 
  130. * Update the reply widget options 
  131. * @since bbPress (r2653) 
  132. * @param array $new_instance The new instance options 
  133. * @param array $old_instance The old instance options 
  134. */ 
  135. public function update( $new_instance = array(), $old_instance = array() ) { 
  136. $instance = $old_instance; 
  137. $instance['title'] = strip_tags( $new_instance['title'] ); 
  138. $instance['show_date'] = (bool) $new_instance['show_date']; 
  139. $instance['show_user'] = (bool) $new_instance['show_user']; 
  140. $instance['max_shown'] = (int) $new_instance['max_shown']; 
  141.  
  142. return $instance; 
  143.  
  144. /** 
  145. * Output the reply widget options form 
  146. * @since bbPress (r2653) 
  147. * @param $instance Instance 
  148. * @uses BBP_Replies_Widget::get_field_id() To output the field id 
  149. * @uses BBP_Replies_Widget::get_field_name() To output the field name 
  150. */ 
  151. public function form( $instance = array() ) { 
  152.  
  153. // Get widget settings 
  154. $settings = $this->parse_settings( $instance ); ?> 
  155.  
  156. <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> 
  157. <p><label for="<?php echo $this->get_field_id( 'max_shown' ); ?>"><?php _e( 'Maximum replies 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> 
  158. <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> 
  159. <p><label for="<?php echo $this->get_field_id( 'show_user' ); ?>"><?php _e( 'Show reply 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> 
  160.  
  161. <?php 
  162.  
  163. /** 
  164. * Merge the widget settings into defaults array. 
  165. * @since bbPress (r4802) 
  166. * @param $instance Instance 
  167. * @uses bbp_parse_args() To merge widget settings into defaults 
  168. */ 
  169. public function parse_settings( $instance = array() ) { 
  170. return bbp_parse_args( $instance, array( 
  171. 'title' => __( 'Recent Replies', 'bbpress' ),  
  172. 'max_shown' => 5,  
  173. 'show_date' => false,  
  174. 'show_user' => false 
  175. ),  
  176. 'replies_widget_settings' );