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' );
29 * @since Twenty Fourteen 1.0
31 * @return Twenty_Fourteen_Ephemera_Widget
33 public function __construct() {
34 parent::__construct( 'widget_twentyfourteen_ephemera', __( 'Twenty Fourteen Ephemera', 'twentyfourteen' ), array(
35 'classname' => 'widget_twentyfourteen_ephemera',
36 'description' => __( 'Use this widget to list your recent Aside, Quote, Video, Audio, Image, Gallery, and Link posts.', 'twentyfourteen' ),
41 * Output the HTML for this widget.
44 * @since Twenty Fourteen 1.0
46 * @param array $args An array of standard parameters for widgets in this theme.
47 * @param array $instance An array of settings for this widget instance.
49 public function widget( $args, $instance ) {
50 $format = isset( $instance['format'] ) && in_array( $instance['format'], $this->formats ) ? $instance['format'] : 'aside';
54 $format_string = __( 'Images', 'twentyfourteen' );
55 $format_string_more = __( 'More images', 'twentyfourteen' );
58 $format_string = __( 'Videos', 'twentyfourteen' );
59 $format_string_more = __( 'More videos', 'twentyfourteen' );
62 $format_string = __( 'Audio', 'twentyfourteen' );
63 $format_string_more = __( 'More audio', 'twentyfourteen' );
66 $format_string = __( 'Quotes', 'twentyfourteen' );
67 $format_string_more = __( 'More quotes', 'twentyfourteen' );
70 $format_string = __( 'Links', 'twentyfourteen' );
71 $format_string_more = __( 'More links', 'twentyfourteen' );
74 $format_string = __( 'Galleries', 'twentyfourteen' );
75 $format_string_more = __( 'More galleries', 'twentyfourteen' );
79 $format_string = __( 'Asides', 'twentyfourteen' );
80 $format_string_more = __( 'More asides', 'twentyfourteen' );
84 $number = empty( $instance['number'] ) ? 2 : absint( $instance['number'] );
85 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? $format_string : $instance['title'], $instance, $this->id_base );
87 $ephemera = new WP_Query( array(
89 'posts_per_page' => $number,
90 'no_found_rows' => true,
91 'post_status' => 'publish',
92 'post__not_in' => get_option( 'sticky_posts' ),
95 'taxonomy' => 'post_format',
96 'terms' => array( "post-format-$format" ),
103 if ( $ephemera->have_posts() ) :
104 $tmp_content_width = $GLOBALS['content_width'];
105 $GLOBALS['content_width'] = 306;
107 echo $args['before_widget'];
109 <h1 class="widget-title <?php echo esc_attr( $format ); ?>">
110 <a class="entry-format" href="<?php echo esc_url( get_post_format_link( $format ) ); ?>"><?php echo $title; ?></a>
115 while ( $ephemera->have_posts() ) :
116 $ephemera->the_post();
117 $tmp_more = $GLOBALS['more'];
118 $GLOBALS['more'] = 0;
121 <article <?php post_class(); ?>>
122 <div class="entry-content">
124 if ( has_post_format( 'gallery' ) ) :
126 if ( post_password_required() ) :
127 the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyfourteen' ) );
131 $galleries = get_post_galleries( get_the_ID(), false );
132 if ( isset( $galleries[0]['ids'] ) )
133 $images = explode( ',', $galleries[0]['ids'] );
136 $images = get_posts( array(
140 'orderby' => 'menu_order',
141 'post_mime_type' => 'image',
142 'post_parent' => get_the_ID(),
143 'post_type' => 'attachment',
147 $total_images = count( $images );
149 if ( has_post_thumbnail() ) :
150 $post_thumbnail = get_the_post_thumbnail();
151 elseif ( $total_images > 0 ) :
152 $image = array_shift( $images );
153 $post_thumbnail = wp_get_attachment_image( $image, 'post-thumbnail' );
156 if ( ! empty ( $post_thumbnail ) ) :
158 <a href="<?php the_permalink(); ?>"><?php echo $post_thumbnail; ?></a>
160 <p class="wp-caption-text">
162 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' ),
163 esc_url( get_permalink() ),
164 number_format_i18n( $total_images )
172 the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyfourteen' ) );
175 </div><!-- .entry-content -->
177 <header class="entry-header">
178 <div class="entry-meta">
180 if ( ! has_post_format( 'link' ) ) :
181 the_title( '<h1 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h1>' );
184 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>',
185 esc_url( get_permalink() ),
186 esc_attr( get_the_date( 'c' ) ),
187 esc_html( get_the_date() ),
188 esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
192 if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) :
194 <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyfourteen' ), __( '1 Comment', 'twentyfourteen' ), __( '% Comments', 'twentyfourteen' ) ); ?></span>
196 </div><!-- .entry-meta -->
197 </header><!-- .entry-header -->
198 </article><!-- #post-## -->
203 <a class="post-format-archive-link" href="<?php echo esc_url( get_post_format_link( $format ) ); ?>">
205 /* translators: used with More archives link */
206 printf( __( '%s <span class="meta-nav">→</span>', 'twentyfourteen' ), $format_string_more );
211 echo $args['after_widget'];
213 // Reset the post globals as this query will have stomped on it.
216 $GLOBALS['more'] = $tmp_more;
217 $GLOBALS['content_width'] = $tmp_content_width;
219 endif; // End check for ephemeral posts.
223 * Deal with the settings when they are saved by the admin.
225 * Here is where any validation should happen.
227 * @since Twenty Fourteen 1.0
229 * @param array $new_instance New widget instance.
230 * @param array $instance Original widget instance.
231 * @return array Updated widget instance.
233 function update( $new_instance, $instance ) {
234 $instance['title'] = strip_tags( $new_instance['title'] );
235 $instance['number'] = empty( $new_instance['number'] ) ? 2 : absint( $new_instance['number'] );
236 if ( in_array( $new_instance['format'], $this->formats ) ) {
237 $instance['format'] = $new_instance['format'];
244 * Display the form for this widget on the Widgets page of the Admin area.
246 * @since Twenty Fourteen 1.0
248 * @param array $instance
250 function form( $instance ) {
251 $title = empty( $instance['title'] ) ? '' : esc_attr( $instance['title'] );
252 $number = empty( $instance['number'] ) ? 2 : absint( $instance['number'] );
253 $format = isset( $instance['format'] ) && in_array( $instance['format'], $this->formats ) ? $instance['format'] : 'aside';
255 <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'twentyfourteen' ); ?></label>
256 <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>
258 <p><label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of posts to show:', 'twentyfourteen' ); ?></label>
259 <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>
261 <p><label for="<?php echo esc_attr( $this->get_field_id( 'format' ) ); ?>"><?php _e( 'Post format to show:', 'twentyfourteen' ); ?></label>
262 <select id="<?php echo esc_attr( $this->get_field_id( 'format' ) ); ?>" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'format' ) ); ?>">
263 <?php foreach ( $this->formats as $slug ) : ?>
264 <option value="<?php echo esc_attr( $slug ); ?>"<?php selected( $format, $slug ); ?>><?php echo get_post_format_string( $slug ); ?></option>