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