3 * Makes a custom Widget for displaying Aside, Link, Status, and Quote Posts available with Twenty Eleven
5 * Learn more: http://codex.wordpress.org/Widgets_API#Developing_Widgets
8 * @subpackage Twenty_Eleven
9 * @since Twenty Eleven 1.0
11 class Twenty_Eleven_Ephemera_Widget extends WP_Widget {
18 function Twenty_Eleven_Ephemera_Widget() {
19 $widget_ops = array( 'classname' => 'widget_twentyeleven_ephemera', 'description' => __( 'Use this widget to list your recent Aside, Status, Quote, and Link posts', 'twentyeleven' ) );
20 $this->WP_Widget( 'widget_twentyeleven_ephemera', __( 'Twenty Eleven Ephemera', 'twentyeleven' ), $widget_ops );
21 $this->alt_option_name = 'widget_twentyeleven_ephemera';
23 add_action( 'save_post', array(&$this, 'flush_widget_cache' ) );
24 add_action( 'deleted_post', array(&$this, 'flush_widget_cache' ) );
25 add_action( 'switch_theme', array(&$this, 'flush_widget_cache' ) );
29 * Outputs the HTML for this widget.
31 * @param array An array of standard parameters for widgets in this theme
32 * @param array An array of settings for this widget instance
33 * @return void Echoes it's output
35 function widget( $args, $instance ) {
36 $cache = wp_cache_get( 'widget_twentyeleven_ephemera', 'widget' );
38 if ( !is_array( $cache ) )
41 if ( ! isset( $args['widget_id'] ) )
42 $args['widget_id'] = null;
44 if ( isset( $cache[$args['widget_id']] ) ) {
45 echo $cache[$args['widget_id']];
50 extract( $args, EXTR_SKIP );
52 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Ephemera', 'twentyeleven' ) : $instance['title'], $instance, $this->id_base);
54 if ( ! isset( $instance['number'] ) )
55 $instance['number'] = '10';
57 if ( ! $number = absint( $instance['number'] ) )
60 $ephemera_args = array(
62 'posts_per_page' => $number,
63 'no_found_rows' => true,
64 'post_status' => 'publish',
65 'post__not_in' => get_option( 'sticky_posts' ),
68 'taxonomy' => 'post_format',
69 'terms' => array( 'post-format-aside', 'post-format-link', 'post-format-status', 'post-format-quote' ),
75 $ephemera = new WP_Query( $ephemera_args );
77 if ( $ephemera->have_posts() ) :
80 echo $title; // Can set this with a widget option, or omit altogether
84 <?php while ( $ephemera->have_posts() ) : $ephemera->the_post(); ?>
86 <?php if ( 'link' != get_post_format() ) : ?>
88 <li class="widget-entry-title">
89 <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>
90 <span class="comments-link">
91 <?php comments_popup_link( __( '0 <span class="reply">comments →</span>', 'twentyeleven' ), __( '1 <span class="reply">comment →</span>', 'twentyeleven' ), __( '% <span class="reply">comments →</span>', 'twentyeleven' ) ); ?>
97 <li class="widget-entry-title">
99 // Grab first link from the post content. If none found, use the post permalink as fallback.
100 $link_url = twentyeleven_url_grabber();
102 if ( empty( $link_url ) )
103 $link_url = get_permalink();
105 <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>
106 <span class="comments-link">
107 <?php comments_popup_link( __( '0 <span class="reply">comments →</span>', 'twentyeleven' ), __( '1 <span class="reply">comment →</span>', 'twentyeleven' ), __( '% <span class="reply">comments →</span>', 'twentyeleven' ) ); ?>
119 // Reset the post globals as this query will have stomped on it
122 // end check for ephemeral posts
125 $cache[$args['widget_id']] = ob_get_flush();
126 wp_cache_set( 'widget_twentyeleven_ephemera', $cache, 'widget' );
130 * Deals with the settings when they are saved by the admin. Here is
131 * where any validation should be dealt with.
133 function update( $new_instance, $old_instance ) {
134 $instance = $old_instance;
135 $instance['title'] = strip_tags( $new_instance['title'] );
136 $instance['number'] = (int) $new_instance['number'];
137 $this->flush_widget_cache();
139 $alloptions = wp_cache_get( 'alloptions', 'options' );
140 if ( isset( $alloptions['widget_twentyeleven_ephemera'] ) )
141 delete_option( 'widget_twentyeleven_ephemera' );
146 function flush_widget_cache() {
147 wp_cache_delete( 'widget_twentyeleven_ephemera', 'widget' );
151 * Displays the form for this widget on the Widgets page of the WP Admin area.
153 function form( $instance ) {
154 $title = isset( $instance['title']) ? esc_attr( $instance['title'] ) : '';
155 $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 10;
157 <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'twentyeleven' ); ?></label>
158 <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>
160 <p><label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of posts to show:', 'twentyeleven' ); ?></label>
161 <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>