3 * Custom Widget for displaying specific post formats
5 * Displays posts from Aside, Quote, Video, Audio, Image, Gallery, and Link formats.
7 * @link https://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' ),
37 'customize_selective_refresh' => true,
40 if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
41 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
48 * @since Twenty Fourteen 1.7
50 public function enqueue_scripts() {
51 /** This filter is documented in wp-includes/media.php */
52 $audio_library = apply_filters( 'wp_audio_shortcode_library', 'mediaelement' );
53 /** This filter is documented in wp-includes/media.php */
54 $video_library = apply_filters( 'wp_video_shortcode_library', 'mediaelement' );
55 if ( in_array( 'mediaelement', array( $video_library, $audio_library ), true ) ) {
56 wp_enqueue_style( 'wp-mediaelement' );
57 wp_enqueue_script( 'wp-mediaelement' );
62 * Output the HTML for this widget.
65 * @since Twenty Fourteen 1.0
67 * @param array $args An array of standard parameters for widgets in this theme.
68 * @param array $instance An array of settings for this widget instance.
70 public function widget( $args, $instance ) {
71 $format = isset( $instance['format'] ) && in_array( $instance['format'], $this->formats ) ? $instance['format'] : 'aside';
75 $format_string = __( 'Images', 'twentyfourteen' );
76 $format_string_more = __( 'More images', 'twentyfourteen' );
79 $format_string = __( 'Videos', 'twentyfourteen' );
80 $format_string_more = __( 'More videos', 'twentyfourteen' );
83 $format_string = __( 'Audio', 'twentyfourteen' );
84 $format_string_more = __( 'More audio', 'twentyfourteen' );
87 $format_string = __( 'Quotes', 'twentyfourteen' );
88 $format_string_more = __( 'More quotes', 'twentyfourteen' );
91 $format_string = __( 'Links', 'twentyfourteen' );
92 $format_string_more = __( 'More links', 'twentyfourteen' );
95 $format_string = __( 'Galleries', 'twentyfourteen' );
96 $format_string_more = __( 'More galleries', 'twentyfourteen' );
100 $format_string = __( 'Asides', 'twentyfourteen' );
101 $format_string_more = __( 'More asides', 'twentyfourteen' );
105 $number = empty( $instance['number'] ) ? 2 : absint( $instance['number'] );
106 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? $format_string : $instance['title'], $instance, $this->id_base );
108 $ephemera = new WP_Query( array(
110 'posts_per_page' => $number,
111 'no_found_rows' => true,
112 'post_status' => 'publish',
113 'post__not_in' => get_option( 'sticky_posts' ),
114 'tax_query' => array(
116 'taxonomy' => 'post_format',
117 'terms' => array( "post-format-$format" ),
124 if ( $ephemera->have_posts() ) :
125 $tmp_content_width = $GLOBALS['content_width'];
126 $GLOBALS['content_width'] = 306;
128 echo $args['before_widget'];
130 <h1 class="widget-title <?php echo esc_attr( $format ); ?>">
131 <a class="entry-format" href="<?php echo esc_url( get_post_format_link( $format ) ); ?>"><?php echo esc_html( $title ); ?></a>
136 while ( $ephemera->have_posts() ) :
137 $ephemera->the_post();
138 $tmp_more = $GLOBALS['more'];
139 $GLOBALS['more'] = 0;
142 <article <?php post_class(); ?>>
143 <div class="entry-content">
145 if ( has_post_format( 'gallery' ) ) :
147 if ( post_password_required() ) :
148 the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyfourteen' ) );
152 $galleries = get_post_galleries( get_the_ID(), false );
153 if ( isset( $galleries[0]['ids'] ) )
154 $images = explode( ',', $galleries[0]['ids'] );
157 $images = get_posts( array(
161 'orderby' => 'menu_order',
162 'post_mime_type' => 'image',
163 'post_parent' => get_the_ID(),
164 'post_type' => 'attachment',
168 $total_images = count( $images );
170 if ( has_post_thumbnail() ) :
171 $post_thumbnail = get_the_post_thumbnail();
172 elseif ( $total_images > 0 ) :
173 $image = reset( $images );
174 $post_thumbnail = wp_get_attachment_image( $image, 'post-thumbnail' );
177 if ( ! empty ( $post_thumbnail ) ) :
179 <a href="<?php the_permalink(); ?>"><?php echo $post_thumbnail; ?></a>
181 <p class="wp-caption-text">
183 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' ),
184 esc_url( get_permalink() ),
185 number_format_i18n( $total_images )
193 the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyfourteen' ) );
196 </div><!-- .entry-content -->
198 <header class="entry-header">
199 <div class="entry-meta">
201 if ( ! has_post_format( 'link' ) ) :
202 the_title( '<h1 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h1>' );
205 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>',
206 esc_url( get_permalink() ),
207 esc_attr( get_the_date( 'c' ) ),
208 esc_html( get_the_date() ),
209 esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
213 if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) :
215 <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyfourteen' ), __( '1 Comment', 'twentyfourteen' ), __( '% Comments', 'twentyfourteen' ) ); ?></span>
217 </div><!-- .entry-meta -->
218 </header><!-- .entry-header -->
219 </article><!-- #post-## -->
224 <a class="post-format-archive-link" href="<?php echo esc_url( get_post_format_link( $format ) ); ?>">
226 /* translators: used with More archives link */
227 printf( __( '%s <span class="meta-nav">→</span>', 'twentyfourteen' ), $format_string_more );
232 echo $args['after_widget'];
234 // Reset the post globals as this query will have stomped on it.
237 $GLOBALS['more'] = $tmp_more;
238 $GLOBALS['content_width'] = $tmp_content_width;
240 endif; // End check for ephemeral posts.
244 * Deal with the settings when they are saved by the admin.
246 * Here is where any validation should happen.
248 * @since Twenty Fourteen 1.0
250 * @param array $new_instance New widget instance.
251 * @param array $instance Original widget instance.
252 * @return array Updated widget instance.
254 function update( $new_instance, $instance ) {
255 $instance['title'] = strip_tags( $new_instance['title'] );
256 $instance['number'] = empty( $new_instance['number'] ) ? 2 : absint( $new_instance['number'] );
257 if ( in_array( $new_instance['format'], $this->formats ) ) {
258 $instance['format'] = $new_instance['format'];
265 * Display the form for this widget on the Widgets page of the Admin area.
267 * @since Twenty Fourteen 1.0
269 * @param array $instance
271 function form( $instance ) {
272 $title = empty( $instance['title'] ) ? '' : esc_attr( $instance['title'] );
273 $number = empty( $instance['number'] ) ? 2 : absint( $instance['number'] );
274 $format = isset( $instance['format'] ) && in_array( $instance['format'], $this->formats ) ? $instance['format'] : 'aside';
276 <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'twentyfourteen' ); ?></label>
277 <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>
279 <p><label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of posts to show:', 'twentyfourteen' ); ?></label>
280 <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>
282 <p><label for="<?php echo esc_attr( $this->get_field_id( 'format' ) ); ?>"><?php _e( 'Post format to show:', 'twentyfourteen' ); ?></label>
283 <select id="<?php echo esc_attr( $this->get_field_id( 'format' ) ); ?>" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'format' ) ); ?>">
284 <?php foreach ( $this->formats as $slug ) : ?>
285 <option value="<?php echo esc_attr( $slug ); ?>"<?php selected( $format, $slug ); ?>><?php echo esc_html( get_post_format_string( $slug ) ); ?></option>