~canonical-sysadmins/wordpress/4.7.2

« back to all changes in this revision

Viewing changes to wp-content/themes/twentyfourteen/inc/widgets.php

  • Committer: Jacek Nykis
  • Date: 2015-01-05 16:17:05 UTC
  • Revision ID: jacek.nykis@canonical.com-20150105161705-w544l1h5mcg7u4w9
Initial commit

Show diffs side-by-side

added added

removed removed

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