3 * Custom Widget for displaying specific post formats
5 * Displays posts from Aside, Quote, Video, Audio, Image, Gallery, and Link formats.
7 * @link http://codex.wordpress.org/Widgets_API#Developing_Widgets
10 * @subpackage Twenty_Fourteen
11 * @since Twenty Fourteen 1.0
14 class Twenty_Fourteen_Ephemera_Widget extends WP_Widget {
17 * The supported post formats.
20 * @since Twenty Fourteen 1.0
24 private $formats = array( 'aside', 'image', 'video', 'audio', 'quote', 'link', 'gallery' );
27 * Pluralized post format strings.
30 * @since Twenty Fourteen 1.0
34 private $format_strings;
39 * @since Twenty Fourteen 1.0
41 * @return Twenty_Fourteen_Ephemera_Widget
43 public function __construct() {
44 parent::__construct( 'widget_twentyfourteen_ephemera', __( 'Twenty Fourteen Ephemera', 'twentyfourteen' ), array(
45 'classname' => 'widget_twentyfourteen_ephemera',
46 'description' => __( 'Use this widget to list your recent Aside, Quote, Video, Audio, Image, Gallery, and Link posts', 'twentyfourteen' ),
50 * @todo http://core.trac.wordpress.org/ticket/23257: Add plural versions of Post Format strings
52 $this->format_strings = array(
53 'aside' => __( 'Asides', 'twentyfourteen' ),
54 'image' => __( 'Images', 'twentyfourteen' ),
55 'video' => __( 'Videos', 'twentyfourteen' ),
56 'audio' => __( 'Audio', 'twentyfourteen' ),
57 'quote' => __( 'Quotes', 'twentyfourteen' ),
58 'link' => __( 'Links', 'twentyfourteen' ),
59 'gallery' => __( 'Galleries', 'twentyfourteen' ),
64 * Output the HTML for this widget.
67 * @since Twenty Fourteen 1.0
69 * @param array $args An array of standard parameters for widgets in this theme.
70 * @param array $instance An array of settings for this widget instance.
71 * @return void Echoes its output.
73 public function widget( $args, $instance ) {
74 $format = $instance['format'];
75 $number = empty( $instance['number'] ) ? 2 : absint( $instance['number'] );
76 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? $this->format_strings[ $format ] : $instance['title'], $instance, $this->id_base );
78 $ephemera = new WP_Query( array(
80 'posts_per_page' => $number,
81 'no_found_rows' => true,
82 'post_status' => 'publish',
83 'post__not_in' => get_option( 'sticky_posts' ),
86 'taxonomy' => 'post_format',
87 'terms' => array( "post-format-$format" ),
94 if ( $ephemera->have_posts() ) :
95 $tmp_content_width = $GLOBALS['content_width'];
96 $GLOBALS['content_width'] = 306;
98 echo $args['before_widget'];
100 <h1 class="widget-title <?php echo esc_attr( $format ); ?>">
101 <a class="entry-format" href="<?php echo esc_url( get_post_format_link( $format ) ); ?>"><?php echo $title; ?></a>
105 <?php while ( $ephemera->have_posts() ) : $ephemera->the_post(); ?>
107 <article <?php post_class(); ?>>
108 <div class="entry-content">
110 if ( has_post_format( 'gallery' ) ) :
112 if ( post_password_required() ) :
113 the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyfourteen' ) );
117 $galleries = get_post_galleries( get_the_ID(), false );
118 if ( isset( $galleries[0]['ids'] ) )
119 $images = explode( ',', $galleries[0]['ids'] );
122 $images = get_posts( array(
126 'orderby' => 'menu_order',
127 'post_mime_type' => 'image',
128 'post_parent' => get_the_ID(),
129 'post_type' => 'attachment',
133 $total_images = count( $images );
135 if ( has_post_thumbnail() ) :
136 $post_thumbnail = get_the_post_thumbnail();
137 elseif ( $total_images > 0 ) :
138 $image = array_shift( $images );
139 $post_thumbnail = wp_get_attachment_image( $image, 'post-thumbnail' );
142 if ( ! empty ( $post_thumbnail ) ) :
144 <a href="<?php the_permalink(); ?>"><?php echo $post_thumbnail; ?></a>
146 <p class="wp-caption-text">
148 printf( _n( 'This gallery contains <a href="%1$s" rel="bookmark">%2$s photo</a>.', 'This gallery contains <a href="%1$s" rel="bookmark">%2$s photos</a>.', $total_images, 'twentyfourteen' ),
149 esc_url( get_permalink() ),
150 number_format_i18n( $total_images )
158 the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyfourteen' ) );
161 </div><!-- .entry-content -->
163 <header class="entry-header">
164 <div class="entry-meta">
166 if ( ! has_post_format( 'link' ) ) :
167 the_title( '<h1 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h1>' );
170 printf( '<span class="entry-date"><a href="%1$s" rel="bookmark"><time class="entry-date" datetime="%2$s">%3$s</time></a></span> <span class="byline"><span class="author vcard"><a class="url fn n" href="%4$s" rel="author">%5$s</a></span></span>',
171 esc_url( get_permalink() ),
172 esc_attr( get_the_date( 'c' ) ),
173 esc_html( get_the_date() ),
174 esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
178 if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) :
180 <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyfourteen' ), __( '1 Comment', 'twentyfourteen' ), __( '% Comments', 'twentyfourteen' ) ); ?></span>
182 </div><!-- .entry-meta -->
183 </header><!-- .entry-header -->
184 </article><!-- #post-## -->
189 <a class="post-format-archive-link" href="<?php echo esc_url( get_post_format_link( $format ) ); ?>"><?php printf( __( 'More %s <span class="meta-nav">→</span>', 'twentyfourteen' ), $this->format_strings[ $format ] ); ?></a>
192 echo $args['after_widget'];
194 // Reset the post globals as this query will have stomped on it.
197 $GLOBALS['content_width'] = $tmp_content_width;
199 endif; // End check for ephemeral posts.
203 * Deal with the settings when they are saved by the admin.
205 * Here is where any validation should happen.
207 * @since Twenty Fourteen 1.0
209 * @param array $new_instance New widget instance.
210 * @param array $instance Original widget instance.
211 * @return array Updated widget instance.
213 function update( $new_instance, $instance ) {
214 $instance['title'] = strip_tags( $new_instance['title'] );
215 $instance['number'] = empty( $new_instance['number'] ) ? 2 : absint( $new_instance['number'] );
216 if ( in_array( $new_instance['format'], $this->formats ) ) {
217 $instance['format'] = $new_instance['format'];
224 * Display the form for this widget on the Widgets page of the Admin area.
226 * @since Twenty Fourteen 1.0
228 * @param array $instance
231 function form( $instance ) {
232 $title = empty( $instance['title'] ) ? '' : esc_attr( $instance['title'] );
233 $number = empty( $instance['number'] ) ? 2 : absint( $instance['number'] );
234 $format = isset( $instance['format'] ) && in_array( $instance['format'], $this->formats ) ? $instance['format'] : 'aside';
236 <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'twentyfourteen' ); ?></label>
237 <input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>"></p>
239 <p><label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of posts to show:', 'twentyfourteen' ); ?></label>
240 <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>
242 <p><label for="<?php echo esc_attr( $this->get_field_id( 'format' ) ); ?>"><?php _e( 'Post format to show:', 'twentyfourteen' ); ?></label>
243 <select id="<?php echo esc_attr( $this->get_field_id( 'format' ) ); ?>" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'format' ) ); ?>">
244 <?php foreach ( $this->formats as $slug ) : ?>
245 <option value="<?php echo esc_attr( $slug ); ?>"<?php selected( $format, $slug ); ?>><?php echo get_post_format_string( $slug ); ?></option>