Welbim_Recent_Posts.php (4581B)
1 <?php 2 namespace Welbim\Helper\Widgets; 3 4 /* 5 ============================== 6 custom Recent Tour Type Widget 7 ============================== 8 */ 9 10 class Welbim_Recent_Posts extends \WP_Widget { 11 12 13 14 15 /** 16 * Register widget with WordPress. 17 */ 18 public function __construct() { 19 $widget_ops = array( 20 'classname' => 'sidebar-post sidebar-widget post-widget', 21 'description' => __( 'A welbim Posts Widget' ), 22 'customize_selective_refresh' => true, 23 ); 24 parent::__construct( 'recent_posts_sidebar', __( 'Welbim Recent Posts' ), $widget_ops ); 25 } 26 27 public function widget( $args, $instance ) { 28 $cache = array(); 29 if ( ! $this->is_preview() ) { 30 $cache = wp_cache_get( 'welbim_recent_blog_posts', 'widget' ); 31 } 32 33 if ( ! is_array( $cache ) ) { 34 $cache = array(); 35 } 36 37 if ( ! isset( $args['widget_id'] ) ) { 38 $args['widget_id'] = $this->id; 39 } 40 41 if ( isset( $cache[ $args['widget_id'] ] ) ) { 42 printf( $cache[ $args['widget_id'] ] ); 43 return; 44 } 45 46 ob_start(); 47 48 $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' ); 49 50 /** 51 * This filter is documented in wp-includes/default-widgets.php 52 */ 53 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); 54 55 $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5; 56 if ( ! $number ) { 57 $number = 5; 58 } 59 $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false; 60 61 $select = isset( $instance['select'] ) ? $instance['select'] : ''; 62 63 $r = new \WP_Query( 64 apply_filters( 65 'widget_posts_args', 66 array( 67 'posts_per_page' => $number, 68 'no_found_rows' => true, 69 'post_status' => 'publish', 70 'post_type' => array( 71 'Post', 72 'ignore_sticky_posts' => true, 73 ), 74 ) 75 ) 76 ); 77 if ( $r->have_posts() ) : 78 ?> 79 <?php printf( $args['before_widget'] ); ?> 80 <?php 81 if ( $title ) { 82 printf( $args['before_title'] . $title . $args['after_title'] ); 83 } 84 ?> 85 86 <div class="news-widget-two"> 87 88 <!-- Recent Post --> 89 <?php 90 while ( $r->have_posts() ) : 91 $r->the_post(); 92 ?> 93 <article class="post"> 94 <figure class="post-thumb"> 95 <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'welbim-recent-post-size' ); ?></a> 96 </figure> 97 <div class="comment"><?php welbim_comments_count(); ?></div> 98 <div class="text"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div> 99 </article> 100 <?php endwhile; ?> 101 <!-- Recent Post --> 102 </div> 103 <?php printf( $args['after_widget'] ); ?> 104 <?php 105 wp_reset_postdata(); 106 endif; 107 108 if ( ! $this->is_preview() ) { 109 $cache[ $args['widget_id'] ] = ob_get_flush(); 110 wp_cache_set( 'welbim_recent_blog_posts', $cache, 'widget' ); 111 } else { 112 ob_end_flush(); 113 } 114 } 115 116 public function update( $new_instance, $old_instance ) { 117 $instance = $old_instance; 118 $instance['title'] = strip_tags( $new_instance['title'] ); 119 $instance['number'] = (int) $new_instance['number']; 120 $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false; 121 $this->flush_widget_cache(); 122 123 return $instance; 124 } 125 126 public function flush_widget_cache() { 127 wp_cache_delete( 'welbim_recent_blog_posts', 'widget' ); 128 } 129 130 public function form( $instance ) { 131 $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; 132 $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5; 133 $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false; 134 ?> 135 <p><label for="<?php printf( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:' ); ?></label> 136 <input class="widefat" id="<?php printf( $this->get_field_id( 'title' ) ); ?>" name="<?php printf( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php printf( $title ); ?>" /> 137 </p> 138 139 <p><label for="<?php printf( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of posts to show:' ); ?></label> 140 <input id="<?php printf( $this->get_field_id( 'number' ) ); ?>" name="<?php printf( $this->get_field_name( 'number' ) ); ?>" type="text" value="<?php printf( $number ); ?>" size="3" /> 141 </p> 142 143 <p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php printf( $this->get_field_id( 'show_date' ) ); ?>" name="<?php printf( $this->get_field_name( 'show_date' ) ); ?>" /> 144 <label for="<?php printf( $this->get_field_id( 'show_date' ) ); ?>"><?php _e( 'Display post date?' ); ?></label> 145 </p> 146 <?php 147 } 148 }