1 <?php |
|
2 /** |
|
3 * Custom Widget for displaying specific post formats |
|
4 * |
|
5 * Displays posts from Aside, Quote, Video, Audio, Image, Gallery, and Link formats. |
|
6 * |
|
7 * @link https://codex.wordpress.org/Widgets_API#Developing_Widgets |
|
8 * |
|
9 * @package WordPress |
|
10 * @subpackage Twenty_Fourteen |
|
11 * @since Twenty Fourteen 1.0 |
|
12 */ |
|
13 |
|
14 class Twenty_Fourteen_Ephemera_Widget extends WP_Widget { |
|
15 |
|
16 /** |
|
17 * The supported post formats. |
|
18 * |
|
19 * @since Twenty Fourteen 1.0 |
|
20 * |
|
21 * @var array |
|
22 */ |
|
23 private $formats = array( 'aside', 'image', 'video', 'audio', 'quote', 'link', 'gallery' ); |
|
24 |
|
25 /** |
|
26 * Constructor. |
|
27 * |
|
28 * @since Twenty Fourteen 1.0 |
|
29 * |
|
30 * @return Twenty_Fourteen_Ephemera_Widget |
|
31 */ |
|
32 public function __construct() { |
|
33 parent::__construct( |
|
34 '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, |
|
38 ) |
|
39 ); |
|
40 |
|
41 if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) { |
|
42 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
|
43 } |
|
44 } |
|
45 |
|
46 /** |
|
47 * Enqueue scripts. |
|
48 * |
|
49 * @since Twenty Fourteen 1.7 |
|
50 */ |
|
51 public function enqueue_scripts() { |
|
52 /** This filter is documented in wp-includes/media.php */ |
|
53 $audio_library = apply_filters( 'wp_audio_shortcode_library', 'mediaelement' ); |
|
54 /** This filter is documented in wp-includes/media.php */ |
|
55 $video_library = apply_filters( 'wp_video_shortcode_library', 'mediaelement' ); |
|
56 if ( in_array( 'mediaelement', array( $video_library, $audio_library ), true ) ) { |
|
57 wp_enqueue_style( 'wp-mediaelement' ); |
|
58 wp_enqueue_script( 'mediaelement-vimeo' ); |
|
59 wp_enqueue_script( 'wp-mediaelement' ); |
|
60 } |
|
61 } |
|
62 |
|
63 /** |
|
64 * Output the HTML for this widget. |
|
65 * |
|
66 * @since Twenty Fourteen 1.0 |
|
67 * |
|
68 * @param array $args An array of standard parameters for widgets in this theme. |
|
69 * @param array $instance An array of settings for this widget instance. |
|
70 */ |
|
71 public function widget( $args, $instance ) { |
|
72 $format = isset( $instance['format'] ) && in_array( $instance['format'], $this->formats ) ? $instance['format'] : 'aside'; |
|
73 |
|
74 switch ( $format ) { |
|
75 case 'image': |
|
76 $format_string = __( 'Images', 'twentyfourteen' ); |
|
77 $format_string_more = __( 'More images', 'twentyfourteen' ); |
|
78 break; |
|
79 case 'video': |
|
80 $format_string = __( 'Videos', 'twentyfourteen' ); |
|
81 $format_string_more = __( 'More videos', 'twentyfourteen' ); |
|
82 break; |
|
83 case 'audio': |
|
84 $format_string = __( 'Audio', 'twentyfourteen' ); |
|
85 $format_string_more = __( 'More audio', 'twentyfourteen' ); |
|
86 break; |
|
87 case 'quote': |
|
88 $format_string = __( 'Quotes', 'twentyfourteen' ); |
|
89 $format_string_more = __( 'More quotes', 'twentyfourteen' ); |
|
90 break; |
|
91 case 'link': |
|
92 $format_string = __( 'Links', 'twentyfourteen' ); |
|
93 $format_string_more = __( 'More links', 'twentyfourteen' ); |
|
94 break; |
|
95 case 'gallery': |
|
96 $format_string = __( 'Galleries', 'twentyfourteen' ); |
|
97 $format_string_more = __( 'More galleries', 'twentyfourteen' ); |
|
98 break; |
|
99 case 'aside': |
|
100 default: |
|
101 $format_string = __( 'Asides', 'twentyfourteen' ); |
|
102 $format_string_more = __( 'More asides', 'twentyfourteen' ); |
|
103 break; |
|
104 } |
|
105 |
|
106 $number = empty( $instance['number'] ) ? 2 : absint( $instance['number'] ); |
|
107 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? $format_string : $instance['title'], $instance, $this->id_base ); |
|
108 |
|
109 $ephemera = new WP_Query( |
|
110 array( |
|
111 'order' => 'DESC', |
|
112 'posts_per_page' => $number, |
|
113 'no_found_rows' => true, |
|
114 'post_status' => 'publish', |
|
115 'post__not_in' => get_option( 'sticky_posts' ), |
|
116 'tax_query' => array( |
|
117 array( |
|
118 'taxonomy' => 'post_format', |
|
119 'terms' => array( "post-format-$format" ), |
|
120 'field' => 'slug', |
|
121 'operator' => 'IN', |
|
122 ), |
|
123 ), |
|
124 ) |
|
125 ); |
|
126 |
|
127 if ( $ephemera->have_posts() ) : |
|
128 $tmp_content_width = $GLOBALS['content_width']; |
|
129 $GLOBALS['content_width'] = 306; |
|
130 |
|
131 echo $args['before_widget']; |
|
132 ?> |
|
133 <h1 class="widget-title <?php echo esc_attr( $format ); ?>"> |
|
134 <a class="entry-format" href="<?php echo esc_url( get_post_format_link( $format ) ); ?>"><?php echo esc_html( $title ); ?></a> |
|
135 </h1> |
|
136 <ol> |
|
137 |
|
138 <?php |
|
139 while ( $ephemera->have_posts() ) : |
|
140 $ephemera->the_post(); |
|
141 $tmp_more = $GLOBALS['more']; |
|
142 $GLOBALS['more'] = 0; |
|
143 ?> |
|
144 <li> |
|
145 <article <?php post_class(); ?>> |
|
146 <div class="entry-content"> |
|
147 <?php |
|
148 if ( has_post_format( 'gallery' ) ) : |
|
149 |
|
150 if ( post_password_required() ) : |
|
151 the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyfourteen' ) ); |
|
152 else : |
|
153 $images = array(); |
|
154 |
|
155 $galleries = get_post_galleries( get_the_ID(), false ); |
|
156 if ( isset( $galleries[0]['ids'] ) ) { |
|
157 $images = explode( ',', $galleries[0]['ids'] ); |
|
158 } |
|
159 |
|
160 if ( ! $images ) : |
|
161 $images = get_posts( |
|
162 array( |
|
163 'fields' => 'ids', |
|
164 'numberposts' => -1, |
|
165 'order' => 'ASC', |
|
166 'orderby' => 'menu_order', |
|
167 'post_mime_type' => 'image', |
|
168 'post_parent' => get_the_ID(), |
|
169 'post_type' => 'attachment', |
|
170 ) |
|
171 ); |
|
172 endif; |
|
173 |
|
174 $total_images = count( $images ); |
|
175 |
|
176 if ( has_post_thumbnail() ) : |
|
177 $post_thumbnail = get_the_post_thumbnail(); |
|
178 elseif ( $total_images > 0 ) : |
|
179 $image = reset( $images ); |
|
180 $post_thumbnail = wp_get_attachment_image( $image, 'post-thumbnail' ); |
|
181 endif; |
|
182 |
|
183 if ( ! empty( $post_thumbnail ) ) : |
|
184 ?> |
|
185 <a href="<?php the_permalink(); ?>"><?php echo $post_thumbnail; ?></a> |
|
186 <?php endif; ?> |
|
187 <p class="wp-caption-text"> |
|
188 <?php |
|
189 printf( |
|
190 _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' ), |
|
191 esc_url( get_permalink() ), |
|
192 number_format_i18n( $total_images ) |
|
193 ); |
|
194 ?> |
|
195 </p> |
|
196 <?php |
|
197 endif; |
|
198 |
|
199 else : |
|
200 the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyfourteen' ) ); |
|
201 endif; |
|
202 ?> |
|
203 </div><!-- .entry-content --> |
|
204 |
|
205 <header class="entry-header"> |
|
206 <div class="entry-meta"> |
|
207 <?php |
|
208 if ( ! has_post_format( 'link' ) ) : |
|
209 the_title( '<h1 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h1>' ); |
|
210 endif; |
|
211 |
|
212 printf( |
|
213 '<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>', |
|
214 esc_url( get_permalink() ), |
|
215 esc_attr( get_the_date( 'c' ) ), |
|
216 esc_html( get_the_date() ), |
|
217 esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ), |
|
218 get_the_author() |
|
219 ); |
|
220 |
|
221 if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) : |
|
222 ?> |
|
223 <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyfourteen' ), __( '1 Comment', 'twentyfourteen' ), __( '% Comments', 'twentyfourteen' ) ); ?></span> |
|
224 <?php endif; ?> |
|
225 </div><!-- .entry-meta --> |
|
226 </header><!-- .entry-header --> |
|
227 </article><!-- #post-## --> |
|
228 </li> |
|
229 <?php endwhile; ?> |
|
230 |
|
231 </ol> |
|
232 <a class="post-format-archive-link" href="<?php echo esc_url( get_post_format_link( $format ) ); ?>"> |
|
233 <?php |
|
234 /* translators: used with More archives link */ |
|
235 printf( __( '%s <span class="meta-nav">→</span>', 'twentyfourteen' ), $format_string_more ); |
|
236 ?> |
|
237 </a> |
|
238 <?php |
|
239 |
|
240 echo $args['after_widget']; |
|
241 |
|
242 // Reset the post globals as this query will have stomped on it. |
|
243 wp_reset_postdata(); |
|
244 |
|
245 $GLOBALS['more'] = $tmp_more; |
|
246 $GLOBALS['content_width'] = $tmp_content_width; |
|
247 |
|
248 endif; // End check for ephemeral posts. |
|
249 } |
|
250 |
|
251 /** |
|
252 * Deal with the settings when they are saved by the admin. |
|
253 * |
|
254 * Here is where any validation should happen. |
|
255 * |
|
256 * @since Twenty Fourteen 1.0 |
|
257 * |
|
258 * @param array $new_instance New widget instance. |
|
259 * @param array $instance Original widget instance. |
|
260 * @return array Updated widget instance. |
|
261 */ |
|
262 function update( $new_instance, $instance ) { |
|
263 $instance['title'] = strip_tags( $new_instance['title'] ); |
|
264 $instance['number'] = empty( $new_instance['number'] ) ? 2 : absint( $new_instance['number'] ); |
|
265 if ( in_array( $new_instance['format'], $this->formats ) ) { |
|
266 $instance['format'] = $new_instance['format']; |
|
267 } |
|
268 |
|
269 return $instance; |
|
270 } |
|
271 |
|
272 /** |
|
273 * Display the form for this widget on the Widgets page of the Admin area. |
|
274 * |
|
275 * @since Twenty Fourteen 1.0 |
|
276 * |
|
277 * @param array $instance |
|
278 */ |
|
279 function form( $instance ) { |
|
280 $title = empty( $instance['title'] ) ? '' : esc_attr( $instance['title'] ); |
|
281 $number = empty( $instance['number'] ) ? 2 : absint( $instance['number'] ); |
|
282 $format = isset( $instance['format'] ) && in_array( $instance['format'], $this->formats ) ? $instance['format'] : 'aside'; |
|
283 ?> |
|
284 <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'twentyfourteen' ); ?></label> |
|
285 <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> |
|
286 |
|
287 <p><label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of posts to show:', 'twentyfourteen' ); ?></label> |
|
288 <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> |
|
289 |
|
290 <p><label for="<?php echo esc_attr( $this->get_field_id( 'format' ) ); ?>"><?php _e( 'Post format to show:', 'twentyfourteen' ); ?></label> |
|
291 <select id="<?php echo esc_attr( $this->get_field_id( 'format' ) ); ?>" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'format' ) ); ?>"> |
|
292 <?php foreach ( $this->formats as $slug ) : ?> |
|
293 <option value="<?php echo esc_attr( $slug ); ?>"<?php selected( $format, $slug ); ?>><?php echo esc_html( get_post_format_string( $slug ) ); ?></option> |
|
294 <?php endforeach; ?> |
|
295 </select> |
|
296 <?php |
|
297 } |
|
298 } |
|