PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/themes/_elise/includes/custom-widgets/widget-recent-posts-tab.php

https://gitlab.com/mattswann/launch-housing
PHP | 249 lines | 163 code | 62 blank | 24 comment | 18 complexity | 3ebfa842e044eefae37bd36f4478c4db MD5 | raw file
  1. <?php
  2. /**
  3. * Plugin Name: Recent Posts Tab Widget
  4. * Description: A widget that displays Tab with recent/popular posts and recent comments.
  5. * Version: 1.0
  6. * Author: LeopardThemes
  7. */
  8. add_action( 'widgets_init', 'elise_recent_posts_tab' );
  9. function elise_recent_posts_tab() {
  10. register_widget( 'Elise_Recent_Posts_Tab' );
  11. }
  12. class Elise_Recent_Posts_Tab extends WP_Widget {
  13. function __construct() {
  14. $widget_ops = array('classname' => 'widget_recent_posts_tab', 'description' => __('A widget that displays Tabs with recent/popular posts and recent comments. ', 'Elise') );
  15. parent::__construct('recent-posts-tab', __('Elise - Recent Posts Tabs', 'Elise'), $widget_ops);
  16. $this->alt_option_name = 'recent_posts_tab';
  17. add_action( 'save_post', array($this, 'flush_widget_cache') );
  18. add_action( 'deleted_post', array($this, 'flush_widget_cache') );
  19. add_action( 'switch_theme', array($this, 'flush_widget_cache') );
  20. }
  21. function widget($args, $instance) {
  22. $cache = array();
  23. if ( ! $this->is_preview() ) {
  24. $cache = wp_cache_get( 'widget_recent_posts_tab', 'widget' );
  25. }
  26. if ( ! is_array( $cache ) ) {
  27. $cache = array();
  28. }
  29. if ( ! isset( $args['widget_id'] ) ) {
  30. $args['widget_id'] = $this->id;
  31. }
  32. if ( isset( $cache[ $args['widget_id'] ] ) ) {
  33. echo $cache[ $args['widget_id'] ];
  34. return;
  35. }
  36. ob_start();
  37. extract($args);
  38. // $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts Tab' );
  39. /** This filter is documented in wp-includes/default-widgets.php */
  40. // $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  41. $number_recent = ( ! empty( $instance['number_recent'] ) ) ? absint( $instance['number_recent'] ) : 3;
  42. if ( ! $number_recent )
  43. $number_recent = 3;
  44. $number_comments = ( ! empty( $instance['number_comments'] ) ) ? absint( $instance['number_comments'] ) : 3;
  45. if ( ! $number_comments )
  46. $number_comments = 3;
  47. $show_popular_posts = isset( $instance['show_popular_posts'] ) ? $instance['show_popular_posts'] : true;
  48. $show_recent_comments = isset( $instance['show_recent_comments'] ) ? $instance['show_recent_comments'] : true;
  49. /**
  50. * Filter the arguments for the Recent Posts widget.
  51. *
  52. * @since 3.4.0
  53. *
  54. * @see WP_Query::get_posts()
  55. *
  56. * @param array $args An array of arguments used to retrieve the recent posts.
  57. */
  58. ?>
  59. <?php echo $before_widget; ?>
  60. <?php // if ( $title ) echo $before_title . $title . $after_title; ?>
  61. <?php $random_tab_id = rand(1, 9999) ?>
  62. <!-- Nav tabs -->
  63. <ul class="nav nav-tabs">
  64. <li class="recent-posts-tab active"><a href="#recent-posts-tab-<?php echo intval($random_tab_id) ?>" data-toggle="tab"><?php _e('Recent', 'Elise') ?></a></li>
  65. <?php if ($show_popular_posts) { ?><li class="popular-posts-tab"><a href="#popular-posts-tab-<?php echo intval($random_tab_id) ?>" data-toggle="tab"><?php _e('Popular', 'Elise') ?></a></li><?php } ?>
  66. <?php if ($show_recent_comments) { ?><li class="recent-comments-tab"><a href="#recent-comments-tab-<?php echo intval($random_tab_id) ?>" data-toggle="tab"><i class="fa fa-comments"></i></a></li><?php } ?>
  67. </ul>
  68. <!-- Tab panes -->
  69. <div class="tab-content">
  70. <div class="tab-pane fade in active" id="recent-posts-tab-<?php echo intval($random_tab_id) ?>">
  71. <?php
  72. $r = new WP_Query( apply_filters( 'widget_posts_args', array(
  73. 'posts_per_page' => intval($number_recent),
  74. 'no_found_rows' => true,
  75. 'post_status' => 'publish',
  76. 'ignore_sticky_posts' => true
  77. ) ) );
  78. if ($r->have_posts()) :
  79. ?>
  80. <ul class="recent_posts">
  81. <?php while ( $r->have_posts() ) : $r->the_post(); ?>
  82. <li>
  83. <span class="post-date"><?php echo get_the_date(); ?></span>
  84. <a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
  85. </li>
  86. <?php endwhile; endif; ?>
  87. </ul>
  88. <?php wp_reset_postdata(); ?>
  89. </div>
  90. <?php if ($show_popular_posts) { ?>
  91. <div class="tab-pane fade" id="popular-posts-tab-<?php echo intval($random_tab_id) ?>">
  92. <?php
  93. $r = new WP_Query( apply_filters( 'widget_posts_args', array(
  94. 'posts_per_page' => intval($number_recent),
  95. 'no_found_rows' => true,
  96. 'meta_key' => 'post_views_count',
  97. 'orderby' => 'meta_value_num',
  98. 'order' => 'DESC',
  99. 'post_status' => 'publish',
  100. 'ignore_sticky_posts' => true
  101. ) ) );
  102. if ($r->have_posts()) :
  103. ?>
  104. <ul class="popular_posts">
  105. <?php while ( $r->have_posts() ) : $r->the_post(); ?>
  106. <li>
  107. <span class="post-date"><?php echo get_the_date(); ?></span>
  108. <span class="post-views"><?php echo elise_get_post_count(get_the_ID()); ?></span>
  109. <a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
  110. </li>
  111. <?php endwhile; endif; ?>
  112. </ul>
  113. <?php wp_reset_postdata(); ?>
  114. </div>
  115. <?php } ?>
  116. <?php if ($show_recent_comments) { ?>
  117. <div class="tab-pane fade" id="recent-comments-tab-<?php echo intval($random_tab_id) ?>">
  118. <?php
  119. global $comment;
  120. $output = '';
  121. $comments = get_comments( apply_filters( 'widget_comments_args', array(
  122. 'number' => intval($number_comments),
  123. 'status' => 'approve',
  124. 'post_status' => 'publish'
  125. ) ) );
  126. $output .= '<ul class="recent_comments">';
  127. if ( $comments ) {
  128. // Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)
  129. $post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );
  130. _prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false );
  131. foreach ( (array) $comments as $comment) {
  132. $output .= '<li><span class="comment-author-tab">' . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(__('%1$s says %2$s', 'Elise'), get_comment_author_link(), '</span><a class="recent_comments_excerpt" href="' . esc_url( get_comment_link($comment->comment_ID) ) . '">' . wp_html_excerpt( $comment->comment_content, 60, ' [...]' ) . '</a>') . '</li>';
  133. }
  134. }
  135. $output .= '</ul>';
  136. echo $output;
  137. ?>
  138. </div>
  139. <?php } ?>
  140. </div>
  141. <?php echo $after_widget; ?>
  142. <?php
  143. // Reset the global $the_post as this query will have stomped on it
  144. //wp_reset_postdata();
  145. // endif;
  146. if ( ! $this->is_preview() ) {
  147. $cache[ $args['widget_id'] ] = ob_get_flush();
  148. wp_cache_set( 'widget_recent_posts_tab', $cache, 'widget' );
  149. } else {
  150. ob_end_flush();
  151. }
  152. }
  153. function update( $new_instance, $old_instance ) {
  154. $instance = $old_instance;
  155. //$instance['title'] = strip_tags($new_instance['title']);
  156. $instance['number_recent'] = (int) $new_instance['number_recent'];
  157. $instance['number_comments'] = (int) $new_instance['number_comments'];
  158. $instance['show_popular_posts'] = isset( $new_instance['show_popular_posts'] ) ? (bool) $new_instance['show_popular_posts'] : false;
  159. $instance['show_recent_comments'] = isset( $new_instance['show_recent_comments'] ) ? (bool) $new_instance['show_recent_comments'] : false;
  160. $this->flush_widget_cache();
  161. $alloptions = wp_cache_get( 'alloptions', 'options' );
  162. if ( isset($alloptions['widget_recent_posts_tab']) )
  163. delete_option('widget_recent_posts_tab');
  164. return $instance;
  165. }
  166. function flush_widget_cache() {
  167. wp_cache_delete('widget_recent_posts_tab', 'widget');
  168. }
  169. function form( $instance ) {
  170. //$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
  171. $number_recent = isset( $instance['number_recent'] ) ? absint( $instance['number_recent'] ) : 3;
  172. $number_comments = isset( $instance['number_comments'] ) ? absint( $instance['number_comments'] ) : 3;
  173. $show_popular_posts = isset( $instance['show_popular_posts'] ) ? (bool) $instance['show_popular_posts'] : true;
  174. $show_recent_comments = isset( $instance['show_recent_comments'] ) ? (bool) $instance['show_recent_comments'] : true;
  175. ?>
  176. <p><label for="<?php echo $this->get_field_id( 'number_recent' ); ?>"><?php _e( 'Number of recent/popular posts to show:', 'Elise' ); ?></label>
  177. <input id="<?php echo $this->get_field_id( 'number_recent' ); ?>" name="<?php echo $this->get_field_name( 'number_recent' ); ?>" type="text" value="<?php echo $number_recent; ?>" size="3" /></p>
  178. <p><input class="checkbox" type="checkbox" <?php checked( $show_popular_posts ); ?> id="<?php echo $this->get_field_id( 'show_popular_posts' ); ?>" name="<?php echo $this->get_field_name( 'show_popular_posts' ); ?>" />
  179. <label for="<?php echo $this->get_field_id( 'show_popular_posts' ); ?>"><?php _e( 'Display Popular Posts Tab', 'Elise' ); ?></label></p>
  180. <p><input class="checkbox" type="checkbox" <?php checked( $show_recent_comments ); ?> id="<?php echo $this->get_field_id( 'show_recent_comments' ); ?>" name="<?php echo $this->get_field_name( 'show_recent_comments' ); ?>" />
  181. <label for="<?php echo $this->get_field_id( 'show_recent_comments' ); ?>"><?php _e( 'Display Recent Comments Tab', 'Elise' ); ?></label></p>
  182. <p><label for="<?php echo $this->get_field_id( 'number_comments' ); ?>"><?php _e( 'Number of recent comments to show:', 'Elise' ); ?></label>
  183. <input id="<?php echo $this->get_field_id( 'number_comments' ); ?>" name="<?php echo $this->get_field_name( 'number_comments' ); ?>" type="text" value="<?php echo $number_comments; ?>" size="3" /></p>
  184. <?php
  185. }
  186. }
  187. ?>