Last_Post_Replies_Widget

The bbP last post Last Post Replies Widget class.

Defined (1)

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

/includes/lp-widgets.php  
  1. class Last_Post_Replies_Widget extends WP_Widget { 
  2.  
  3. /** 
  4. * Last Post Replies Widget ------ Amends display to show date not freshness 
  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( 'last_post_replies_widget_options', array( 
  12. 'classname' => 'widget_display_replies',  
  13. 'description' => __( 'A list of the most recent replies.', 'bbp-last-post' ) 
  14. ) ); 
  15.  
  16. parent::__construct( false, __( '(Last Post) Recent Replies', 'bbp-last-post' ), $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( 'Last_Post_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. $date_format = get_option( 'date_format' ); 
  95. $time_format = get_option( 'time_format' ); 
  96. $date= date( "{$date_format}", get_the_time( 'U' ) ); 
  97. $time=date( "{$time_format}", get_the_time( 'U' ) ); 
  98. $active_time = sprintf( _x( '%1$s at %2$s', 'date at time', 'bbp-last-post' ), $date, $time );  
  99. printf( _x( '%1$s on %2$s %3$s', 'widgets', 'bbp-last-post' ), $author_link, $reply_link, '<div>' . $active_time . '</div>' ); 
  100.  
  101. // Reply link and timestamp 
  102. elseif ( ! empty( $settings['show_date'] ) ) : 
  103.  
  104. // translators: 1: reply link, 2: reply timestamp 
  105. $date_format = get_option( 'date_format' ); 
  106. $time_format = get_option( 'time_format' ); 
  107. $date= date( "{$date_format}", get_the_time( 'U' ) ); 
  108. $time=date( "{$time_format}", get_the_time( 'U' ) ); 
  109. $active_time = sprintf( _x( '%1$s at %2$s', 'date at time', 'bbp-last-post' ), $date, $time );  
  110. printf( _x( '%1$s %2$s', 'widgets', 'bbp-last-post' ), $reply_link, '<div>' . $active_time . '</div>' ); 
  111.  
  112. // Reply author and title 
  113. elseif ( !empty( $author_link ) ) : 
  114.  
  115. // translators: 1: reply author, 2: reply link 
  116. printf( _x( '%1$s on %2$s', 'widgets', 'bbp-last-post' ), $author_link, $reply_link ); 
  117.  
  118. // Only the reply title 
  119. else : 
  120.  
  121. // translators: 1: reply link 
  122. printf( _x( '%1$s', 'widgets', 'bbp-last-post' ), $reply_link ); 
  123.  
  124. endif; 
  125.  
  126. ?> 
  127.  
  128. </li> 
  129.  
  130. <?php endwhile; ?> 
  131.  
  132. </ul> 
  133.  
  134. <?php echo $args['after_widget']; 
  135.  
  136. // Reset the $post global 
  137. wp_reset_postdata(); 
  138.  
  139. /** 
  140. * Update the reply widget options 
  141. * @since bbPress (r2653) 
  142. * @param array $new_instance The new instance options 
  143. * @param array $old_instance The old instance options 
  144. */ 
  145. public function update( $new_instance = array(), $old_instance = array() ) { 
  146. $instance = $old_instance; 
  147. $instance['title'] = strip_tags( $new_instance['title'] ); 
  148. $instance['show_date'] = (bool) $new_instance['show_date']; 
  149. $instance['show_user'] = (bool) $new_instance['show_user']; 
  150. $instance['max_shown'] = (int) $new_instance['max_shown']; 
  151.  
  152. return $instance; 
  153.  
  154. /** 
  155. * Output the reply widget options form 
  156. * @since bbPress (r2653) 
  157. * @param $instance Instance 
  158. * @uses BBP_Replies_Widget::get_field_id() To output the field id 
  159. * @uses BBP_Replies_Widget::get_field_name() To output the field name 
  160. */ 
  161. public function form( $instance = array() ) { 
  162.  
  163. // Get widget settings 
  164. $settings = $this->parse_settings( $instance ); ?> 
  165.  
  166. <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'bbp-last-post' ); ?> <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> 
  167. <p><label for="<?php echo $this->get_field_id( 'max_shown' ); ?>"><?php _e( 'Maximum replies to show:', 'bbp-last-post' ); ?> <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> 
  168. <p><label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Show post date:', 'bbp-last-post' ); ?> <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> 
  169. <p><label for="<?php echo $this->get_field_id( 'show_user' ); ?>"><?php _e( 'Show reply author:', 'bbp-last-post' ); ?> <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> 
  170.  
  171. <?php 
  172.  
  173. /** 
  174. * Merge the widget settings into defaults array. 
  175. * @since bbPress (r4802) 
  176. * @param $instance Instance 
  177. * @uses bbp_parse_args() To merge widget settings into defaults 
  178. */ 
  179. public function parse_settings( $instance = array() ) { 
  180. return bbp_parse_args( $instance, array( 
  181. 'title' => __( 'Recent Replies', 'bbp-last-post' ),  
  182. 'max_shown' => 5,  
  183. 'show_date' => false,  
  184. 'show_user' => false 
  185. ),  
  186. 'replies_widget_settings' );