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