2011-04-20 23:46:33 +02:00
< ? php
/**
2011-05-02 23:04:44 +02:00
* Makes a custom Widget for displaying Aside , Link , Status , and Quote Posts available with Twenty Eleven
*
* Learn more : http :// codex . wordpress . org / Widgets_API #Developing_Widgets
*
* @ package WordPress
2011-05-18 21:06:09 +02:00
* @ subpackage Twenty_Eleven
2011-05-02 23:04:44 +02:00
* @ since Twenty Eleven 1.0
2011-04-20 23:46:33 +02:00
*/
class Twenty_Eleven_Ephemera_Widget extends WP_Widget {
2011-04-26 20:34:52 +02:00
2011-04-20 23:46:33 +02:00
/**
* Constructor
*
* @ return void
**/
function Twenty_Eleven_Ephemera_Widget () {
2011-04-26 20:46:31 +02:00
$widget_ops = array ( 'classname' => 'widget_twentyeleven_ephemera' , 'description' => __ ( 'Use this widget to list your recent Aside, Status, Quote, and Link posts' , 'twentyeleven' ) );
2011-04-20 23:46:33 +02:00
$this -> WP_Widget ( 'widget_twentyeleven_ephemera' , __ ( 'Twenty Eleven Ephemera' , 'twentyeleven' ), $widget_ops );
$this -> alt_option_name = 'widget_twentyeleven_ephemera' ;
2011-04-26 20:34:52 +02:00
2011-04-20 23:46:33 +02:00
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' ) );
}
/**
* Outputs the HTML for this widget .
*
2011-04-26 20:34:52 +02:00
* @ param array An array of standard parameters for widgets in this theme
* @ param array An array of settings for this widget instance
2011-04-20 23:46:33 +02:00
* @ return void Echoes it ' s output
**/
function widget ( $args , $instance ) {
$cache = wp_cache_get ( 'widget_twentyeleven_ephemera' , 'widget' );
2011-04-27 06:45:11 +02:00
if ( ! is_array ( $cache ) )
2011-04-20 23:46:33 +02:00
$cache = array ();
if ( ! isset ( $args [ 'widget_id' ] ) )
$args [ 'widget_id' ] = null ;
2011-04-27 06:45:11 +02:00
if ( isset ( $cache [ $args [ 'widget_id' ]] ) ) {
2011-04-20 23:46:33 +02:00
echo $cache [ $args [ 'widget_id' ]];
return ;
}
2011-04-26 20:34:52 +02:00
ob_start ();
2011-04-20 23:46:33 +02:00
extract ( $args , EXTR_SKIP );
2011-04-26 20:34:52 +02:00
2011-05-26 22:50:53 +02:00
$title = apply_filters ( 'widget_title' , empty ( $instance [ 'title' ] ) ? __ ( 'Ephemera' , 'twentyeleven' ) : $instance [ 'title' ], $instance , $this -> id_base );
2011-04-26 20:34:52 +02:00
2011-04-20 23:46:33 +02:00
if ( ! isset ( $instance [ 'number' ] ) )
$instance [ 'number' ] = '10' ;
2011-04-26 20:34:52 +02:00
2011-04-20 23:46:33 +02:00
if ( ! $number = absint ( $instance [ 'number' ] ) )
$number = 10 ;
$ephemera_args = array (
'order' => 'DESC' ,
'posts_per_page' => $number ,
2011-06-10 22:14:59 +02:00
'no_found_rows' => true ,
2011-04-20 23:46:33 +02:00
'post_status' => 'publish' ,
'post__not_in' => get_option ( 'sticky_posts' ),
'tax_query' => array (
array (
'taxonomy' => 'post_format' ,
2011-04-26 20:46:31 +02:00
'terms' => array ( 'post-format-aside' , 'post-format-link' , 'post-format-status' , 'post-format-quote' ),
2011-04-20 23:46:33 +02:00
'field' => 'slug' ,
'operator' => 'IN' ,
),
),
);
2011-06-11 05:44:42 +02:00
$ephemera = new WP_Query ( $ephemera_args );
2011-04-20 23:46:33 +02:00
if ( $ephemera -> have_posts () ) :
2011-04-26 20:34:52 +02:00
2011-04-20 23:46:33 +02:00
echo $before_widget ;
echo $before_title ;
echo $title ; // Can set this with a widget option, or omit altogether
echo $after_title ;
?>
< ol >
< ? php while ( $ephemera -> have_posts () ) : $ephemera -> the_post (); ?>
2011-04-26 20:34:52 +02:00
2011-04-28 11:47:30 +02:00
< ? php if ( 'link' != get_post_format () ) : ?>
2011-04-26 20:34:52 +02:00
2011-04-29 00:04:39 +02:00
< li class = " widget-entry-title " >
2011-04-29 01:59:08 +02:00
< a href = " <?php echo esc_url( get_permalink() ); ?> " title = " <?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?> " rel = " bookmark " >< ? php the_title (); ?> </a>
2011-04-20 23:46:33 +02:00
< span class = " comments-link " >
< ? php comments_popup_link ( __ ( '0 <span class="reply">comments →</span>' , 'twentyeleven' ), __ ( '1 <span class="reply">comment →</span>' , 'twentyeleven' ), __ ( '% <span class="reply">comments →</span>' , 'twentyeleven' ) ); ?>
</ span >
</ li >
2011-04-26 20:34:52 +02:00
2011-04-26 20:46:31 +02:00
< ? php else : ?>
2011-04-20 23:46:33 +02:00
2011-04-29 00:04:39 +02:00
< li class = " widget-entry-title " >
2011-04-20 23:46:33 +02:00
< ? php
2011-05-13 18:25:53 +02:00
// Grab first link from the post content. If none found, use the post permalink as fallback.
$link_url = twentyeleven_url_grabber ();
2011-04-26 20:34:52 +02:00
2011-05-13 18:25:53 +02:00
if ( empty ( $link_url ) )
$link_url = get_permalink ();
2011-04-20 23:46:33 +02:00
?>
2011-04-29 01:59:08 +02:00
< a href = " <?php echo esc_url( $link_url ); ?> " title = " <?php printf( esc_attr__( 'Link to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?> " rel = " bookmark " >< ? php the_title (); ?> <span>→</span></a>
2011-04-20 23:46:33 +02:00
< span class = " comments-link " >
< ? php comments_popup_link ( __ ( '0 <span class="reply">comments →</span>' , 'twentyeleven' ), __ ( '1 <span class="reply">comment →</span>' , 'twentyeleven' ), __ ( '% <span class="reply">comments →</span>' , 'twentyeleven' ) ); ?>
</ span >
</ li >
2011-04-26 20:34:52 +02:00
2011-04-20 23:46:33 +02:00
< ? php endif ; ?>
2011-04-26 20:34:52 +02:00
2011-04-20 23:46:33 +02:00
< ? php endwhile ; ?>
</ ol >
< ? php
echo $after_widget ;
2011-04-26 20:34:52 +02:00
2011-04-28 11:47:30 +02:00
// Reset the post globals as this query will have stomped on it
2011-04-20 23:46:33 +02:00
wp_reset_postdata ();
// end check for ephemeral posts
endif ;
$cache [ $args [ 'widget_id' ]] = ob_get_flush ();
wp_cache_set ( 'widget_twentyeleven_ephemera' , $cache , 'widget' );
}
/**
* Deals with the settings when they are saved by the admin . Here is
* where any validation should be dealt with .
**/
function update ( $new_instance , $old_instance ) {
$instance = $old_instance ;
2011-04-27 06:45:11 +02:00
$instance [ 'title' ] = strip_tags ( $new_instance [ 'title' ] );
2011-04-20 23:46:33 +02:00
$instance [ 'number' ] = ( int ) $new_instance [ 'number' ];
$this -> flush_widget_cache ();
$alloptions = wp_cache_get ( 'alloptions' , 'options' );
2011-04-27 06:45:11 +02:00
if ( isset ( $alloptions [ 'widget_twentyeleven_ephemera' ] ) )
2011-04-20 23:46:33 +02:00
delete_option ( 'widget_twentyeleven_ephemera' );
return $instance ;
}
2011-04-26 20:34:52 +02:00
2011-04-20 23:46:33 +02:00
function flush_widget_cache () {
wp_cache_delete ( 'widget_twentyeleven_ephemera' , 'widget' );
2011-04-26 20:34:52 +02:00
}
2011-04-20 23:46:33 +02:00
/**
* Displays the form for this widget on the Widgets page of the WP Admin area .
**/
function form ( $instance ) {
2011-04-27 06:45:11 +02:00
$title = isset ( $instance [ 'title' ]) ? esc_attr ( $instance [ 'title' ] ) : '' ;
$number = isset ( $instance [ 'number' ] ) ? absint ( $instance [ 'number' ] ) : 10 ;
?>
< p >< label for = " <?php echo esc_attr( $this->get_field_id ( 'title' ) ); ?> " >< ? php _e ( 'Title:' , 'twentyeleven' ); ?> </label>
< input class = " widefat " id = " <?php echo esc_attr( $this->get_field_id ( 'title' ) ); ?> " name = " <?php echo esc_attr( $this->get_field_name ( 'title' ) ); ?> " type = " text " value = " <?php echo esc_attr( $title ); ?> " /></ p >
< p >< label for = " <?php echo esc_attr( $this->get_field_id ( 'number' ) ); ?> " >< ? php _e ( 'Number of posts to show:' , 'twentyeleven' ); ?> </label>
< input id = " <?php echo esc_attr( $this->get_field_id ( 'number' ) ); ?> " name = " <?php echo esc_attr( $this->get_field_name ( 'number' ) ); ?> " type = " text " value = " <?php echo esc_attr( $number ); ?> " size = " 3 " /></ p >
2011-04-20 23:46:33 +02:00
< ? php
}
2011-04-28 11:47:30 +02:00
}