widgets.php 9.6 KB

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