Skip to content

Instantly share code, notes, and snippets.

@middlesister
Last active August 29, 2015 13:56
Show Gist options
  • Save middlesister/9143560 to your computer and use it in GitHub Desktop.
Save middlesister/9143560 to your computer and use it in GitHub Desktop.
A widget for displaying latest podcasts, created for the plugin seriously-simple-podcasting.
<?php
/**
* Display recent podcasts widget
*
* Displays a list of recent podcast episodes.
* Based on WP_Recent_Posts widget
*
* @author Karin Taliga
*/
class IVST_Widget_Recent_Podcasts extends WP_Widget {
function __construct() {
$widget_ops = array(
'classname' => 'ivst_widget_recent_podcasts',
'description' => __( "Your site&#8217;s most recent Podcasts.", 'text-domain')
);
parent::__construct( 'recent-podcasts', __( 'Recent Podcasts', 'text-domain' ), $widget_ops );
$this->alt_option_name = 'ivst_widget_recent_podcasts';
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_podcasts', '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 );
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Podcasts', 'text-domain' );
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 10;
if ( ! $number )
$number = 10;
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
$r = new WP_Query( apply_filters( 'ivst_widget_podcasts_args', array( 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true, 'post_type' => 'podcast' ) ) );
if ( $r->have_posts() ) :
?>
<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<ul>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
<?php if ( $show_date ) : ?>
<span class="post-date"> - <?php echo get_the_date(); ?></span>
<?php endif; ?>
</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_podcasts', $cache, 'widget' );
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['number'] = (int) $new_instance['number'];
$instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset( $alloptions['ivst_widget_recent_podcasts'] ) )
delete_option( 'ivst_widget_recent_podcasts' );
return $instance;
}
function flush_widget_cache() {
wp_cache_delete( 'widget_recent_podcasts', 'widget' );
}
function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
$show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'text-domain' ); ?></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 podcasts to show:', 'text-domain' ); ?></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 class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?', 'text-domain' ); ?></label></p>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment