Everbox_Popular_Widget

The EverBox Everbox Popular Widget class.

Defined (1)

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

/inc/widgets/class-popular-widget.php  
  1. class Everbox_Popular_Widget extends WP_Widget { 
  2.  
  3. /** 
  4. * Sets up the widget configures 
  5. */ 
  6. function __construct() { 
  7. parent::__construct( 
  8. 'everbox_popular', // Base ID 
  9. __('EverBox - Tab Posts', 'everbox'), // Name 
  10. array( 'description' => __( 'A tab widget that contain popular posts list and latest posts list.', 'everbox' ), 'classname' => 'widget_stream-list' ) // Args 
  11. ); 
  12.  
  13. /** 
  14. * Outputs the content of the widget 
  15. * @see WP_Widget::widget() 
  16. * @param array $args Widget arguments. 
  17. * @param array $instance Saved values from database. 
  18. */ 
  19. function widget( $args, $instance ) { 
  20.  
  21. extract( $args ); 
  22. $class = ''; 
  23.  
  24. $popular_title = ( ! empty( $instance['popular_title'] ) ) ? $instance['popular_title'] : __( 'Popular', 'everbox' ); 
  25. $latest_title = ( ! empty( $instance['latest_title'] ) ) ? $instance['latest_title'] : __( 'Latest', 'everbox' ); 
  26. $range = ( ! empty( $instance['range'] ) ) ? intval( $instance['range'] ) : '7'; 
  27. $posts_num = ( ! empty( $instance['posts_num'] ) ) ? intval( $instance['posts_num'] ) : 8; 
  28.  
  29. echo $args['before_widget']; 
  30.  
  31. ?> 
  32. <div class="widget-content"> 
  33. <ul id="filter-toggle-buttons"> 
  34. <li class="filter-button"><a href="#popular_posts-<?php echo $this->number; ?>"><?php echo $popular_title; ?></a></li> 
  35. <li class="filter-button"><a href="#latest_posts-<?php echo $this->number; ?>"><?php echo $latest_title; ?></a></li> 
  36. </ul> 
  37. <!-- END #filter-toggle-buttons --> 
  38. <div id="popular_posts-<?php echo $this->number; ?>" class="posts-list tab_content"> 
  39. <?php  
  40. $popular_args = array( 
  41. 'posts_per_page' => $posts_num,  
  42. 'orderby' => 'comment_count',  
  43. 'order' => 'DESC' 
  44. ); 
  45. $popular_query = null; 
  46. switch ($range) { 
  47. case '7': 
  48. add_filter( 'posts_where', array( $this, 'filter_where_7days' ) ); 
  49. $popular_query = new WP_Query( $popular_args ); 
  50. remove_filter( 'posts_where', array( $this, 'filter_where_7days' ) ); 
  51. break; 
  52. case '30': 
  53. add_filter( 'posts_where', array( $this, 'filter_where_30days' ) ); 
  54. $popular_query = new WP_Query( $popular_args ); 
  55. remove_filter( 'posts_where', array( $this, 'filter_where_30days' ) ); 
  56. break; 
  57. case '1': 
  58. default: 
  59. add_filter( 'posts_where', array( $this, 'filter_where_1days' ) ); 
  60. $popular_query = new WP_Query( $popular_args ); 
  61. remove_filter( 'posts_where', array( $this, 'filter_where_1days' ) ); 
  62. break; 
  63.  
  64. $output = ''; 
  65. if( $popular_query->have_posts() ) : 
  66. $output .= "<ul>"; 
  67. while( $popular_query->have_posts() ) : $popular_query->the_post(); 
  68. $output .= "<li>"; 
  69. if( has_post_thumbnail() ) { 
  70. $output .= sprintf( '<div class="thumb">%s</div>', get_the_post_thumbnail( get_the_id(), '120x100' ) ); 
  71. $output .= '<div class="right-body">'; 
  72. $output .= sprintf( '<a href="%1$s" class="title" title="%2$s" role="bookmark">%3$s</a>',  
  73. get_permalink( get_the_id() ),  
  74. esc_attr( get_the_title() ),  
  75. get_the_title() 
  76. ); 
  77. $output .= sprintf( '<time class="entry-date published" datetime="%1$s">%2$s</time>',  
  78. esc_attr( get_the_date( 'c' ) ),  
  79. esc_html( human_time_diff( get_the_time('U'), current_time('timestamp') ) . __(' ago', 'everbox') ) 
  80. ); 
  81. $output .= '<span class="divider">•</span>'; 
  82. $output .= '<span class="comments-count">' . sprintf( __('%s Comments', 'everbox'), get_comments_number() ) . '<span>'; 
  83. $output .= '</div>'; 
  84. $output .= '</li>'; 
  85. endwhile; 
  86. $output .= "</ul>"; 
  87. echo $output; 
  88. else : 
  89. _e('No Posts', 'everbox'); 
  90. endif; 
  91. wp_reset_postdata(); 
  92. ?> 
  93. </div> 
  94. <!-- END #popular_posts --> 
  95. <div id="latest_posts-<?php echo $this->number; ?>" class="posts-list tab_content"> 
  96. <?php 
  97. $latest_query = new WP_Query( array( 
  98. 'posts_per_page' => $posts_num 
  99. ) ); 
  100. $output = ''; 
  101. if( $latest_query->have_posts() ) : 
  102. $output .= "<ul>"; 
  103. while( $latest_query->have_posts() ) : $latest_query->the_post(); 
  104. $output .= "<li>"; 
  105. if( has_post_thumbnail() ) { 
  106. $output .= sprintf( '<div class="thumb">%s</div>', get_the_post_thumbnail( get_the_id(), 'eb-small-thumb' ) ); 
  107. $output .= '<div class="right-body">'; 
  108. $output .= sprintf( '<a href="%1$s" class="title" title="%2$s" role="bookmark">%3$s</a>',  
  109. get_permalink( get_the_id() ),  
  110. esc_attr( get_the_title() ),  
  111. get_the_title() 
  112. ); 
  113. $output .= sprintf( '<time class="entry-date published" datetime="%1$s">%2$s</time>',  
  114. esc_attr( get_the_date( 'c' ) ),  
  115. esc_html( human_time_diff( get_the_time('U'), current_time('timestamp') ) . __(' ago', 'everbox') ) 
  116. ); 
  117. $output .= '</div>'; 
  118. $output .= '</li>'; 
  119. endwhile; 
  120. $output .= "</ul>"; 
  121. echo $output; 
  122. else : 
  123. _e('No Posts', 'everbox'); 
  124. endif; 
  125. wp_reset_postdata(); 
  126. ?> 
  127. </div> 
  128. <!-- END #latest_posts --> 
  129. </div> 
  130. <!-- END .widget-content --> 
  131. <?php 
  132. echo $args['after_widget']; 
  133.  
  134. /** 
  135. * Outputs the options form on admin 
  136. * @see WP_Widget::form() 
  137. * @param array $instance Previously saved values from database. 
  138. */ 
  139. function form( $instance ) { 
  140.  
  141. $popular_title = ( ! empty( $instance['popular_title'] ) ) ? esc_attr( $instance['popular_title'] ) : __('Popular', 'everbox'); 
  142. $latest_title = ( ! empty( $instance['latest_title'] ) ) ? esc_attr( $instance['latest_title'] ) : __('Latest', 'everbox'); 
  143. $range = ( ! empty( $instance['range'] ) ) ? intval( $instance['range'] ) : 1; 
  144. $posts_num = ( ! empty( $instance['posts_num'] ) ) ? intval( $instance['posts_num'] ) : 8; 
  145.  
  146. ?> 
  147. <p> 
  148. <label for="<?php echo $this->get_field_id( 'popular_title' ); ?>"><?php _e( 'Popular tab title:', 'everbox' ); ?></label> 
  149. <input class="widefat" id="<?php echo $this->get_field_id( 'popular_title' ); ?>" name="<?php echo $this->get_field_name( 'popular_title' ); ?>" type="text" value="<?php echo $popular_title; ?>" /> 
  150. </p> 
  151. <p> 
  152. <label for="<?php echo $this->get_field_id( 'range' ); ?>"><?php _e( 'Range:', 'everbox' ); ?></label> 
  153. <br /> 
  154. <select class='widefat' name="<?php echo $this->get_field_name('range'); ?>" id="<?php echo $this->get_field_id('range'); ?>"> 
  155. <option value="1" <?php selected( '1', $range ); ?>><?php _e( 'Last 1 days', 'everbox' ); ?></option> 
  156. <option value="7" <?php selected( '7', $range ); ?>><?php _e( 'Last 7 days', 'everbox' ); ?></option> 
  157. <option value="30" <?php selected( '30', $range ); ?>><?php _e( 'Last 30 days', 'everbox' ); ?></option> 
  158. </select> 
  159. </p> 
  160. <hr> 
  161. <p> 
  162. <label for="<?php echo $this->get_field_id( 'latest_title' ); ?>"><?php _e( 'Latest tab title:', 'everbox' ); ?></label> 
  163. <input class="widefat" id="<?php echo $this->get_field_id( 'latest_title' ); ?>" name="<?php echo $this->get_field_name( 'latest_title' ); ?>" type="text" value="<?php echo $latest_title; ?>" /> 
  164. </p> 
  165. <p> 
  166. <label for="<?php echo $this->get_field_id( 'posts_num' ); ?>"><?php _e( 'Posts num:', 'everbox' ); ?></label> 
  167. <input class="widefat" id="<?php echo $this->get_field_id( 'posts_num' ); ?>" name="<?php echo $this->get_field_name( 'posts_num' ); ?>" type="number" value="<?php echo $posts_num; ?>" /> 
  168. <label for="<?php echo $this->get_field_id( 'posts_num' ); ?>"><?php _e('how many posts you want to show for both popular tab and latest tab.', 'everbox'); ?></label> 
  169. </p> 
  170. <?php 
  171.  
  172. /** 
  173. * Processing widget options on save 
  174. * @see WP_Widget::update() 
  175. * @param array $new_instance Values just sent to be saved. 
  176. * @param array $old_instance Previously saved values from database. 
  177. * @return array Updated safe values to be saved. 
  178. */ 
  179. function update( $new_instance, $old_instance ) { 
  180.  
  181. $instance = array(); 
  182.  
  183. $instance['popular_title'] = strip_tags( $new_instance['popular_title'] ); 
  184. $instance['latest_title'] = strip_tags( $new_instance['latest_title'] ); 
  185. $instance['range'] = absint( strip_tags( $new_instance['range'] ) ); 
  186. $instance['posts_num'] = absint( strip_tags( $new_instance['posts_num'] ) ); 
  187.  
  188. return $instance; 
  189.  
  190.  
  191. function filter_where_1days( $where ) { 
  192. //posts in the last 1 days 
  193. $where .= " AND post_date > '" . date('Y-m-d', strtotime('-1 days')) . "'"; 
  194. return $where; 
  195.  
  196. function filter_where_7days( $where ) { 
  197. //posts in the last 7 days 
  198. $where .= " AND post_date > '" . date('Y-m-d', strtotime('-7 days')) . "'"; 
  199. return $where; 
  200.  
  201. function filter_where_30days( $where ) { 
  202. //posts in the last 30 days 
  203. $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'"; 
  204. return $where; 
  205.