Created
February 7, 2014 03:59
-
-
Save cassler/8857191 to your computer and use it in GitHub Desktop.
More powerful recent posts widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Widget_Recent_Posts_Xtra extends WP_Widget { | |
// name this widget. | |
// naming this widget allows us to use filters for unique widget areas. | |
var $namespace = "xtra-recent-posts-widget"; | |
function __construct() { | |
$widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your site") ); | |
parent::__construct('recent-posts', __('Custom Recent Posts'), $widget_ops); | |
$this->alt_option_name = 'widget_recent_entries'; | |
add_action( 'save_post', array(&$this, 'flush_widget_cache') ); | |
add_action( 'deleted_post', array(&$this, 'flush_widget_cache') ); | |
add_action( 'switch_theme', array(&$this, 'flush_widget_cache') ); | |
} | |
function widget($args, $instance) { | |
$cache = wp_cache_get('widget_recent_posts', 'widget'); | |
if ( !is_array($cache) ) | |
$cache = array(); | |
if ( ! isset( $args['widget_id'] ) ) | |
$args['widget_id'] = $this->id; | |
if ( isset( $cache[ $args['widget_id'] ] ) ) { | |
echo $cache[ $args['widget_id'] ]; | |
return; | |
} | |
ob_start(); | |
extract($args); | |
// namespace of widget | |
// wp_die(print_r($this, true)); | |
$this->namespace = ($instance['namespace'] ? sanitize_title($instance['namespace']) : $this->namespace); | |
// title of widget | |
$title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base); | |
// number of posts | |
if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) ) | |
$number = 10; | |
// show the featured image checkbox | |
$show_featured_image = absint($instance['show_featured_image']); | |
// show the post date? | |
$show_post_title = absint($instance['show_post_title']); | |
// show the post date? | |
$show_post_date = absint($instance['show_post_date']); | |
// show the post content? | |
$show_post_excerpt = absint($instance['show_post_excerpt']); | |
// set up the arguments to retrieve recent posts | |
$query_args = array( | |
'posts_per_page' => $number, | |
'no_found_rows' => true, | |
'post_status' => 'publish', | |
'ignore_sticky_posts' => true ); | |
// exclude any categories? | |
$exclude = empty( $instance['exclude'] ) ? '' : $query_args['category__not_in'] = explode(',', $instance['exclude']); | |
// include any categories? | |
$include = empty( $instance['include'] ) ? '' : $query_args['category__in'] = explode(',', $instance['include']); | |
$r = new WP_Query($query_args); | |
if ($r->have_posts()) : | |
?> | |
<?php echo $before_widget; ?> | |
<?php echo apply_filters('recent_post_list_title_' . $this->namespace, $before_title . $title . $after_title); ?> | |
<ul class="list-widget-recent-post list-<?php echo $this->namespace; ?>"> | |
<?php while ($r->have_posts()) : $r->the_post(); ?> | |
<li> | |
<?php | |
$anchor_link = '<a href="' . get_permalink(get_the_ID()) . '" title="' . esc_attr(get_the_title() ? get_the_title() : get_the_ID()) . '">%s</a>'; | |
if ( $show_featured_image ){ | |
$featured_image = '<div class="recent-post-featured-image">'; | |
if ( has_post_thumbnail() ){ | |
$post_thumbnail = get_the_post_thumbnail(get_the_ID(), 'thumbnail'); // for usage in filter below | |
$featured_image .= $post_thumbnail; | |
} | |
$featured_image .= '</div>'; | |
} | |
$list_item = ( $featured_image ? sprintf( $anchor_link, $featured_image) : NULL ); | |
if ( get_the_title() && $show_post_title ){ | |
$post_title = get_the_title(); | |
$list_item .= sprintf( $anchor_link, '<h4>' . $post_title . '</h4>'); | |
}; | |
if ( $show_post_date ){ | |
$list_item .= '<div class="recent-post-date">'; | |
$date = get_the_date(apply_filters('recent_post_date_format_' . $this->namespace, '') ); | |
$list_item .= $date; | |
$list_item .= '</div>'; | |
} | |
if ( $show_post_excerpt ){ | |
$list_item .= '<div class="recent-post-excert">'; | |
$excerpt = apply_filters('the_content', get_the_excerpt()); | |
$list_item .= $excerpt; | |
$list_item .= '</div>'; | |
} | |
echo apply_filters('recent_post_list_item_' . $this->namespace, $list_item, $post_title, $date, $post_thumbnail, $excerpt, $anchor_link); | |
?> | |
</li> | |
<?php endwhile; ?> | |
</ul> | |
<?php echo $after_widget; ?> | |
<?php | |
// Reset the global $the_post as this query will have stomped on it | |
wp_reset_postdata(); | |
endif; | |
$cache[$args['widget_id']] = ob_get_flush(); | |
wp_cache_set('widget_recent_posts', $cache, 'widget'); | |
} | |
function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
$instance['namespace'] = sanitize_title($new_instance['namespace']); | |
$instance['title'] = strip_tags($new_instance['title']); | |
$instance['number'] = (int) $new_instance['number']; | |
$instance['exclude'] = strip_tags( $new_instance['exclude'] ); | |
$instance['include'] = strip_tags( $new_instance['include'] ); | |
$instance['show_featured_image'] = absint( $new_instance['show_featured_image'] ); | |
$instance['show_post_title'] = absint( $new_instance['show_post_title'] ); | |
$instance['show_post_date'] = absint( $new_instance['show_post_date'] ); | |
$instance['show_post_excerpt'] = absint( $new_instance['show_post_excerpt'] ); | |
$this->flush_widget_cache(); | |
$alloptions = wp_cache_get( 'alloptions', 'options' ); | |
if ( isset($alloptions['widget_recent_entries']) ) | |
delete_option('widget_recent_entries'); | |
return $instance; | |
} | |
function flush_widget_cache() { | |
wp_cache_delete('widget_recent_posts', 'widget'); | |
} | |
function form( $instance ) { | |
$namespace = isset($instance['namespace']) ? sanitize_title($instance['namespace']) : $this->namespace; | |
$title = isset($instance['title']) ? esc_attr($instance['title']) : ''; | |
$number = isset($instance['number']) ? absint($instance['number']) : 5; | |
$exclude = esc_attr( $instance['exclude'] ); | |
$include = esc_attr( $instance['include'] ); | |
$show_featured_image = absint($instance['show_featured_image']); | |
$show_post_title = absint($instance['show_post_title']); | |
$show_post_date = absint($instance['show_post_date']); | |
$show_post_excerpt = absint( $instance['show_post_excerpt'] ); | |
?> | |
<p><label for="<?php echo $this->get_field_id('namespace'); ?>"><?php _e('Namespace:'); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id('namespace'); ?>" name="<?php echo $this->get_field_name('namespace'); ?>" type="text" value="<?php echo $namespace; ?>" /></p> | |
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p> | |
<p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label> | |
<input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p> | |
<p><input id="<?php echo $this->get_field_id( 'show_post_title' ); ?>" name="<?php echo $this->get_field_name( 'show_post_title' ); ?>" type="checkbox" value="1" <?php checked( $show_post_title, true, true ); ?>/> | |
<label for="<?php echo $this->get_field_id( 'show_post_title' ); ?>"><?php _e('Show Post Title?'); ?></label></p> | |
<p><input id="<?php echo $this->get_field_id( 'show_featured_image' ); ?>" name="<?php echo $this->get_field_name( 'show_featured_image' ); ?>" type="checkbox" value="1" <?php checked( $show_featured_image, true, true ); ?>/> | |
<label for="<?php echo $this->get_field_id( 'show_featured_image' ); ?>"><?php _e('Show Featured Image?'); ?></label></p> | |
<p><input id="<?php echo $this->get_field_id( 'show_post_date' ); ?>" name="<?php echo $this->get_field_name( 'show_post_date' ); ?>" type="checkbox" value="1" <?php checked( $show_post_date, true, true ); ?>/> | |
<label for="<?php echo $this->get_field_id( 'show_post_date' ); ?>"><?php _e('Show Post Date?'); ?></label></p> | |
<p><input id="<?php echo $this->get_field_id( 'show_post_excerpt' ); ?>" name="<?php echo $this->get_field_name( 'show_post_excerpt' ); ?>" type="checkbox" value="1" <?php checked( $show_post_excerpt, true, true ); ?>/> | |
<label for="<?php echo $this->get_field_id( 'show_post_excerpt' ); ?>"><?php _e('Show Post Excerpt?'); ?></label></p> | |
<p> | |
<label for="<?php echo $this->get_field_id('exclude'); ?>"><?php _e( 'Exclude Category(s):' ); ?></label> <input type="text" value="<?php echo $exclude; ?>" name="<?php echo $this->get_field_name('exclude'); ?>" id="<?php echo $this->get_field_id('exclude'); ?>" class="widefat" /> | |
<br /> | |
<small><?php _e( 'Category IDs, separated by commas.' ); ?></small> | |
</p> | |
<p>- or -</p> | |
<p> | |
<label for="<?php echo $this->get_field_id('include'); ?>"><?php _e( 'Include Category(s):' ); ?></label> <input type="text" value="<?php echo $include; ?>" name="<?php echo $this->get_field_name('include'); ?>" id="<?php echo $this->get_field_id('include'); ?>" class="widefat" /> | |
<br /> | |
<small><?php _e( 'Category IDs, separated by commas.' ); ?></small> | |
</p> | |
<?php | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment