Twenty_Fourteen_Ephemera_Widget

Custom Widget for displaying specific post formats.

Defined (1)

The class is defined in the following location(s).

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