widgets.php 9.0 KB

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