Featured_Replies_Widget

The bbPress - Featured Replies Featured Replies Widget class.

Defined (1)

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

/includes/widget.php  
  1. class Featured_Replies_Widget extends WP_Widget { 
  2.  
  3. public function __construct() { 
  4. parent::__construct( 
  5. 'widget-name-id',  
  6. __( '(bbPress) Featured Replies', 'bbp-featured-replies' ),  
  7. array( 
  8. 'classname' => 'bbp-featured-replies-widget',  
  9. 'description' => __( 'Display bbPress replies marked as "featured".', 'bbp-featured-replies' ) 
  10. ); 
  11.  
  12. } // end constructor 
  13.  
  14.  
  15. /** 
  16. * Outputs the content of the widget. 
  17. * @param array args The array of form elements 
  18. * @param array instance The current instance of the widget 
  19. */ 
  20. public function widget( $args, $instance ) { 
  21.  
  22. $cache = wp_cache_get( 'featured_replies_widget', 'widget' ); 
  23.  
  24. if ( ! is_array( $cache ) ) 
  25. $cache = array(); 
  26.  
  27. if ( ! isset( $args['widget_id'] ) ) 
  28. $args['widget_id'] = $this->id; 
  29.  
  30. if ( isset( $cache[ $args['widget_id'] ] ) ) { 
  31. echo $cache[ $args['widget_id'] ]; 
  32. return; 
  33.  
  34. extract( $args, EXTR_SKIP ); 
  35.  
  36. $title = apply_filters( 'widget_title', $instance['title'] ); 
  37.  
  38. if( empty( $number ) || ! $number = absint( $number ) ) 
  39. $number = 5; 
  40.  
  41. $query_args = apply_filters( 'featured_replies_query', array( 
  42. 'number' => $number,  
  43. 'status' => 'approve',  
  44. 'post_status' => 'publish',  
  45. 'post_type' => bbp_get_reply_post_type(),  
  46. 'meta_query' => array( 
  47. array( 
  48. 'key' => 'featured',  
  49. 'value' => '1' 
  50. ) ); 
  51.  
  52. $query = new WP_Query; 
  53.  
  54. $replies = $query->query( $query_args ); 
  55.  
  56. if( $replies ) : 
  57.  
  58. $output = $before_widget; 
  59. if ( $title ) 
  60. $output .= $before_title . $title . $after_title; 
  61.  
  62. $output .= '<ul id="bbp-featured-replies">'; 
  63.  
  64. if ( $replies ) { 
  65.  
  66. foreach ( (array) $replies as $reply) { 
  67. $topic_id = bbp_get_reply_topic_id( $reply->ID ); 
  68. $output .= '<li class="bbp-featured-replies">'; 
  69. $output .= sprintf( 
  70. _x( '%1$s on %2$s', 'widgets' ),  
  71. bbp_get_reply_author_link( array( 
  72. 'post_id' => $reply->ID,  
  73. 'type' => 'name',  
  74. ) ),  
  75. '<a href="' . esc_url( bbp_get_topic_permalink( $topic_id ) . '#post-' . $reply->ID ) . '">' . bbp_get_topic_title( $topic_id ) . '</a>' 
  76. ); 
  77. $output .= '</li>'; 
  78.  
  79. $output .= '</ul>'; 
  80.  
  81. endif; 
  82.  
  83. $output .= $after_widget; 
  84.  
  85. echo $output; 
  86.  
  87. $cache[$args['widget_id']] = $output; 
  88. wp_cache_set( 'featured_replies_widget', $cache, 'widget' ); 
  89.  
  90. } // end widget 
  91.  
  92.  
  93. /** 
  94. * Processes the widget's options to be saved. 
  95. * @param array new_instance The previous instance of values before the update. 
  96. * @param array old_instance The new instance of values to be generated via the update. 
  97. */ 
  98. public function update( $new_instance, $old_instance ) { 
  99.  
  100. $instance = $old_instance; 
  101.  
  102. $instance['title'] = sanitize_text_field( $new_instance['title'] ); 
  103. $instance['number'] = absint( $new_instance['number'] ); 
  104.  
  105. $this->flush_widget_cache(); 
  106.  
  107. return $instance; 
  108.  
  109. } // end widget 
  110.  
  111. function flush_widget_cache() { 
  112. wp_cache_delete( 'featured_replies_widget', 'widget' ); 
  113.  
  114. /** 
  115. * Generates the administration form for the widget. 
  116. * @param array instance The array of keys and values for the widget. 
  117. */ 
  118. public function form( $instance ) { 
  119.  
  120. $defaults = array( 
  121. 'title' => __( 'Featured Replies', 'bbp-featured-replies' ),  
  122. 'number' => 5 
  123. ); 
  124.  
  125. $args = wp_parse_args( $instance, $defaults ); 
  126.  
  127. $title = esc_attr( $args['title'] ); 
  128. $number = esc_attr( $args['number'] ); 
  129.  
  130. ?> 
  131. <p> 
  132. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Widget Title:', 'bbp-featured-replies' ); ?></label> 
  133. <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( $title ); ?>" /> 
  134. </p> 
  135. <p> 
  136. <input class="widefat small-text" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="number" value="<?php echo esc_attr( $number ); ?>" /> 
  137. <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e( 'Number to show', 'bbp-featured-replies' ); ?></label> 
  138. </p> 
  139.  
  140. <?php 
  141.  
  142. } // end form 
  143.  
  144.  
  145. } // end class