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