/includes/lp-widgets.php

  1. <?php 
  2.  
  3.  
  4. /****************************************************************REPLY WIDGET************************************** 
  5. ****** Amends display to show date not freshness ***** 
  6. ******************************************************************************************************************* 
  7. /** 
  8. * bbPress Replies Widget 
  9. * 
  10. * Adds a widget which displays the replies list 
  11. * 
  12. * @since bbPress (r2653) 
  13. * 
  14. * @uses WP_Widget 
  15. */ 
  16. class Last_Post_Replies_Widget extends WP_Widget { 
  17.  
  18. /** 
  19. * Last Post Replies Widget ------ Amends display to show date not freshness 
  20. * 
  21. * Registers the replies widget 
  22. * 
  23. * @since bbPress (r2653) 
  24. * 
  25. * @uses apply_filters() Calls 'bbp_replies_widget_options' with the 
  26. * widget options 
  27. */ 
  28. public function __construct() { 
  29. $widget_ops = apply_filters( 'last_post_replies_widget_options', array( 
  30. 'classname' => 'widget_display_replies',  
  31. 'description' => __( 'A list of the most recent replies.', 'bbp-last-post' ) 
  32. ) ); 
  33.  
  34. parent::__construct( false, __( '(Last Post) Recent Replies', 'bbp-last-post' ), $widget_ops ); 
  35.  
  36. /** 
  37. * Register the widget 
  38. * 
  39. * @since bbPress (r3389) 
  40. * 
  41. * @uses register_widget() 
  42. */ 
  43. public static function register_widget() { 
  44. register_widget( 'Last_Post_Replies_Widget' ); 
  45.  
  46. /** 
  47. * Displays the output, the replies list 
  48. * 
  49. * @since bbPress (r2653) 
  50. * 
  51. * @param mixed $args 
  52. * @param array $instance 
  53. * @uses apply_filters() Calls 'bbp_reply_widget_title' with the title 
  54. * @uses bbp_get_reply_author_link() To get the reply author link 
  55. * @uses bbp_get_reply_id() To get the reply id 
  56. * @uses bbp_get_reply_url() To get the reply url 
  57. * @uses bbp_get_reply_excerpt() To get the reply excerpt 
  58. * @uses bbp_get_reply_topic_title() To get the reply topic title 
  59. * @uses get_the_date() To get the date of the reply 
  60. * @uses get_the_time() To get the time of the reply 
  61. */ 
  62. public function widget( $args, $instance ) { 
  63.  
  64. // Get widget settings 
  65. $settings = $this->parse_settings( $instance ); 
  66.  
  67. // Typical WordPress filter 
  68. $settings['title'] = apply_filters( 'widget_title', $settings['title'], $instance, $this->id_base ); 
  69.  
  70. // bbPress filter 
  71. $settings['title'] = apply_filters( 'bbp_replies_widget_title', $settings['title'], $instance, $this->id_base ); 
  72.  
  73. // Note: private and hidden forums will be excluded via the 
  74. // bbp_pre_get_posts_normalize_forum_visibility action and function. 
  75. $widget_query = new WP_Query( array( 
  76. 'post_type' => bbp_get_reply_post_type(),  
  77. 'post_status' => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),  
  78. 'posts_per_page' => (int) $settings['max_shown'],  
  79. 'ignore_sticky_posts' => true,  
  80. 'no_found_rows' => true,  
  81. ) ); 
  82.  
  83. // Bail if no replies 
  84. if ( ! $widget_query->have_posts() ) { 
  85. return; 
  86.  
  87. echo $args['before_widget']; 
  88.  
  89. if ( !empty( $settings['title'] ) ) { 
  90. echo $args['before_title'] . $settings['title'] . $args['after_title']; 
  91. } ?> 
  92.  
  93. <ul> 
  94.  
  95. <?php while ( $widget_query->have_posts() ) : $widget_query->the_post(); ?> 
  96.  
  97. <li> 
  98.  
  99. <?php 
  100.  
  101. // Verify the reply ID 
  102. $reply_id = bbp_get_reply_id( $widget_query->post->ID ); 
  103. $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>'; 
  104.  
  105. // Only query user if showing them 
  106. if ( ! empty( $settings['show_user'] ) ) : 
  107. $author_link = bbp_get_reply_author_link( array( 'post_id' => $reply_id, 'type' => 'both', 'size' => 14 ) ); 
  108. else : 
  109. $author_link = false; 
  110. endif; 
  111.  
  112. // Reply author, link, and timestamp 
  113. if ( ! empty( $settings['show_date'] ) && !empty( $author_link ) ) : 
  114.  
  115. // translators: 1: reply author, 2: reply link, 3: reply timestamp 
  116. $date_format = get_option( 'date_format' ); 
  117. $time_format = get_option( 'time_format' ); 
  118. $date= date( "{$date_format}", get_the_time( 'U' ) ); 
  119. $time=date( "{$time_format}", get_the_time( 'U' ) ); 
  120. $active_time = sprintf( _x( '%1$s at %2$s', 'date at time', 'bbp-last-post' ), $date, $time );  
  121. printf( _x( '%1$s on %2$s %3$s', 'widgets', 'bbp-last-post' ), $author_link, $reply_link, '<div>' . $active_time . '</div>' ); 
  122.  
  123. // Reply link and timestamp 
  124. elseif ( ! empty( $settings['show_date'] ) ) : 
  125.  
  126. // translators: 1: reply link, 2: reply timestamp 
  127. $date_format = get_option( 'date_format' ); 
  128. $time_format = get_option( 'time_format' ); 
  129. $date= date( "{$date_format}", get_the_time( 'U' ) ); 
  130. $time=date( "{$time_format}", get_the_time( 'U' ) ); 
  131. $active_time = sprintf( _x( '%1$s at %2$s', 'date at time', 'bbp-last-post' ), $date, $time );  
  132. printf( _x( '%1$s %2$s', 'widgets', 'bbp-last-post' ), $reply_link, '<div>' . $active_time . '</div>' ); 
  133.  
  134. // Reply author and title 
  135. elseif ( !empty( $author_link ) ) : 
  136.  
  137. // translators: 1: reply author, 2: reply link 
  138. printf( _x( '%1$s on %2$s', 'widgets', 'bbp-last-post' ), $author_link, $reply_link ); 
  139.  
  140. // Only the reply title 
  141. else : 
  142.  
  143. // translators: 1: reply link 
  144. printf( _x( '%1$s', 'widgets', 'bbp-last-post' ), $reply_link ); 
  145.  
  146. endif; 
  147.  
  148. ?> 
  149.  
  150. </li> 
  151.  
  152. <?php endwhile; ?> 
  153.  
  154. </ul> 
  155.  
  156. <?php echo $args['after_widget']; 
  157.  
  158. // Reset the $post global 
  159. wp_reset_postdata(); 
  160.  
  161. /** 
  162. * Update the reply widget options 
  163. * 
  164. * @since bbPress (r2653) 
  165. * 
  166. * @param array $new_instance The new instance options 
  167. * @param array $old_instance The old instance options 
  168. */ 
  169. public function update( $new_instance = array(), $old_instance = array() ) { 
  170. $instance = $old_instance; 
  171. $instance['title'] = strip_tags( $new_instance['title'] ); 
  172. $instance['show_date'] = (bool) $new_instance['show_date']; 
  173. $instance['show_user'] = (bool) $new_instance['show_user']; 
  174. $instance['max_shown'] = (int) $new_instance['max_shown']; 
  175.  
  176. return $instance; 
  177.  
  178. /** 
  179. * Output the reply widget options form 
  180. * 
  181. * @since bbPress (r2653) 
  182. * 
  183. * @param $instance Instance 
  184. * @uses BBP_Replies_Widget::get_field_id() To output the field id 
  185. * @uses BBP_Replies_Widget::get_field_name() To output the field name 
  186. */ 
  187. public function form( $instance = array() ) { 
  188.  
  189. // Get widget settings 
  190. $settings = $this->parse_settings( $instance ); ?> 
  191.  
  192. <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> 
  193. <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> 
  194. <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> 
  195. <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> 
  196.  
  197. <?php 
  198.  
  199. /** 
  200. * Merge the widget settings into defaults array. 
  201. * 
  202. * @since bbPress (r4802) 
  203. * 
  204. * @param $instance Instance 
  205. * @uses bbp_parse_args() To merge widget settings into defaults 
  206. */ 
  207. public function parse_settings( $instance = array() ) { 
  208. return bbp_parse_args( $instance, array( 
  209. 'title' => __( 'Recent Replies', 'bbp-last-post' ),  
  210. 'max_shown' => 5,  
  211. 'show_date' => false,  
  212. 'show_user' => false 
  213. ),  
  214. 'replies_widget_settings' ); 
  215.  
  216.  
  217.  
  218. function register_last_post_widget() { 
  219. register_widget('Last_Post_Replies_Widget'); 
  220.  
  221.  
  222. add_action('widgets_init', 'register_last_post_widget'); 
.