Twenty_Eleven_Ephemera_Widget

Widget For displaying post format posts.

Defined (1)

The class is defined in the following location(s).

/inc/widgets.php  
  1. class Twenty_Eleven_Ephemera_Widget extends WP_Widget { 
  2.  
  3. /** 
  4. * PHP5 constructor. 
  5. * @since Twenty Eleven 2.2 
  6. */ 
  7. function __construct() { 
  8. parent::__construct( 'widget_twentyeleven_ephemera', __( 'Twenty Eleven Ephemera', 'twentyeleven' ), array( 
  9. 'classname' => 'widget_twentyeleven_ephemera',  
  10. 'description' => __( 'Use this widget to list your recent Aside, Status, Quote, and Link posts', 'twentyeleven' ),  
  11. 'customize_selective_refresh' => true,  
  12. ) ); 
  13. $this->alt_option_name = 'widget_twentyeleven_ephemera'; 
  14.  
  15. add_action( 'save_post', array( &$this, 'flush_widget_cache' ) ); 
  16. add_action( 'deleted_post', array( &$this, 'flush_widget_cache' ) ); 
  17. add_action( 'switch_theme', array( &$this, 'flush_widget_cache' ) ); 
  18.  
  19. /** 
  20. * PHP4 constructor. 
  21. * @since Twenty Eleven 1.0 
  22. */ 
  23. function Twenty_Eleven_Ephemera_Widget() { 
  24. self::__construct(); 
  25.  
  26. /** 
  27. * Outputs the HTML for this widget. 
  28. * @since Twenty Eleven 1.0 
  29. * @param array $args An array of standard parameters for widgets in this theme. 
  30. * @param array $instance An array of settings for this widget instance. 
  31. **/ 
  32. function widget( $args, $instance ) { 
  33. $cache = wp_cache_get( 'widget_twentyeleven_ephemera', 'widget' ); 
  34.  
  35. if ( ! is_array( $cache ) ) 
  36. $cache = array(); 
  37.  
  38. if ( ! isset( $args['widget_id'] ) ) 
  39. $args['widget_id'] = null; 
  40.  
  41. if ( ! is_customize_preview() && isset( $cache[ $args['widget_id'] ] ) ) { 
  42. echo $cache[ $args['widget_id'] ]; 
  43. return; 
  44.  
  45. ob_start(); 
  46. extract( $args, EXTR_SKIP ); 
  47.  
  48. /** This filter is documented in wp-includes/default-widgets.php */ 
  49. $args['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Ephemera', 'twentyeleven' ) : $instance['title'], $instance, $this->id_base ); 
  50.  
  51. if ( ! isset( $instance['number'] ) ) 
  52. $instance['number'] = '10'; 
  53.  
  54. if ( ! $args['number'] = absint( $instance['number'] ) ) 
  55. $args['number'] = 10; 
  56.  
  57. $ephemera_args = array( 
  58. 'order' => 'DESC',  
  59. 'posts_per_page' => $args['number'],  
  60. 'no_found_rows' => true,  
  61. 'post_status' => 'publish',  
  62. 'post__not_in' => get_option( 'sticky_posts' ),  
  63. 'tax_query' => array( 
  64. array( 
  65. 'taxonomy' => 'post_format',  
  66. 'terms' => array( 'post-format-aside', 'post-format-link', 'post-format-status', 'post-format-quote' ),  
  67. 'field' => 'slug',  
  68. 'operator' => 'IN',  
  69. ),  
  70. ),  
  71. ); 
  72. $ephemera = new WP_Query( $ephemera_args ); 
  73.  
  74. if ( $ephemera->have_posts() ) : 
  75. echo $args['before_widget']; 
  76. echo $args['before_title']; 
  77. echo $args['title']; 
  78. echo $args['after_title']; 
  79. ?> 
  80. <ol> 
  81. <?php while ( $ephemera->have_posts() ) : $ephemera->the_post(); ?> 
  82.  
  83. <?php if ( 'link' != get_post_format() ) : ?> 
  84.  
  85. <li class="widget-entry-title"> 
  86. <a href="<?php echo esc_url( get_permalink() ); ?>" rel="bookmark"><?php the_title(); ?></a> 
  87. <span class="comments-link"> 
  88. <?php comments_popup_link( __( '0 <span class="reply">comments →</span>', 'twentyeleven' ), __( '1 <span class="reply">comment →</span>', 'twentyeleven' ), __( '% <span class="reply">comments →</span>', 'twentyeleven' ) ); ?> 
  89. </span> 
  90. </li> 
  91.  
  92. <?php else : ?> 
  93.  
  94. <li class="widget-entry-title"> 
  95. <a href="<?php echo esc_url( twentyeleven_get_first_url() ); ?>" rel="bookmark"><?php the_title(); ?> <span>→</span></a> 
  96. <span class="comments-link"> 
  97. <?php comments_popup_link( __( '0 <span class="reply">comments →</span>', 'twentyeleven' ), __( '1 <span class="reply">comment →</span>', 'twentyeleven' ), __( '% <span class="reply">comments →</span>', 'twentyeleven' ) ); ?> 
  98. </span> 
  99. </li> 
  100.  
  101. <?php endif; ?> 
  102.  
  103. <?php endwhile; ?> 
  104. </ol> 
  105. <?php 
  106.  
  107. echo $args['after_widget']; 
  108.  
  109. // Reset the post globals as this query will have stomped on it 
  110. wp_reset_postdata(); 
  111.  
  112. // end check for ephemeral posts 
  113. endif; 
  114.  
  115. $cache[ $args['widget_id'] ] = ob_get_flush(); 
  116. if ( ! is_customize_preview() ) { 
  117. wp_cache_set( 'widget_twentyeleven_ephemera', $cache, 'widget' ); 
  118.  
  119.  
  120. /** 
  121. * Update widget settings. 
  122. * Deals with the settings when they are saved by the admin. Here is 
  123. * where any validation should be dealt with. 
  124. * @since Twenty Eleven 1.0 
  125. **/ 
  126. function update( $new_instance, $old_instance ) { 
  127. $instance = $old_instance; 
  128. $instance['title'] = strip_tags( $new_instance['title'] ); 
  129. $instance['number'] = (int) $new_instance['number']; 
  130. $this->flush_widget_cache(); 
  131.  
  132. $alloptions = wp_cache_get( 'alloptions', 'options' ); 
  133. if ( isset( $alloptions['widget_twentyeleven_ephemera'] ) ) 
  134. delete_option( 'widget_twentyeleven_ephemera' ); 
  135.  
  136. return $instance; 
  137.  
  138. /** 
  139. * Flush widget cache. 
  140. * @since Twenty Eleven 1.0 
  141. */ 
  142. function flush_widget_cache() { 
  143. wp_cache_delete( 'widget_twentyeleven_ephemera', 'widget' ); 
  144.  
  145. /** 
  146. * Set up the widget form. 
  147. * Displays the form for this widget on the Widgets page of the WP Admin area. 
  148. * @since Twenty Eleven 1.0 
  149. **/ 
  150. function form( $instance ) { 
  151. $title = isset( $instance['title']) ? esc_attr( $instance['title'] ) : ''; 
  152. $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 10; 
  153. ?> 
  154. <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'twentyeleven' ); ?></label> 
  155. <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> 
  156.  
  157. <p><label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of posts to show:', 'twentyeleven' ); ?></label> 
  158. <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> 
  159. <?php