/includes/widget.php

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