| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 https://developer.wordpress.org/themes/functionality/widgets/#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 /** 16 * The supported post formats. 17 * 18 * @since Twenty Fourteen 1.0 19 * 20 * @var array 21 */ 22 private $formats = array( 'aside', 'image', 'video', 'audio', 'quote', 'link', 'gallery' ); 23 24 /** 25 * Constructor. 26 * 27 * @since Twenty Fourteen 1.0 28 * 29 * @return Twenty_Fourteen_Ephemera_Widget Widget instance. 30 */ 31 public function __construct() { 32 parent::__construct( 33 'widget_twentyfourteen_ephemera', 34 __( 'Twenty Fourteen Ephemera', 'twentyfourteen' ), 35 array( 36 'classname' => 'widget_twentyfourteen_ephemera', 37 'description' => __( 'Use this widget to list your recent Aside, Quote, Video, Audio, Image, Gallery, and Link posts.', 'twentyfourteen' ), 38 'customize_selective_refresh' => true, 39 ) 40 ); 41 42 if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) { 43 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 44 } 45 } 46 47 /** 48 * Enqueues scripts. 49 * 50 * @since Twenty Fourteen 1.7 51 */ 52 public function enqueue_scripts() { 53 /** This filter is documented in wp-includes/media.php */ 54 $audio_library = apply_filters( 'wp_audio_shortcode_library', 'mediaelement' ); 55 /** This filter is documented in wp-includes/media.php */ 56 $video_library = apply_filters( 'wp_video_shortcode_library', 'mediaelement' ); 57 if ( in_array( 'mediaelement', array( $video_library, $audio_library ), true ) ) { 58 wp_enqueue_style( 'wp-mediaelement' ); 59 wp_enqueue_script( 'mediaelement-vimeo' ); 60 wp_enqueue_script( 'wp-mediaelement' ); 61 } 62 } 63 64 /** 65 * Outputs the HTML for this widget. 66 * 67 * @since Twenty Fourteen 1.0 68 * 69 * @global int $content_width Content width. 70 * @global int $more 71 * 72 * @param array $args An array of standard parameters for widgets in this theme. 73 * @param array $instance An array of settings for this widget instance. 74 */ 75 public function widget( $args, $instance ) { 76 $format = isset( $instance['format'] ) ? $instance['format'] : ''; 77 78 if ( ! $format || ! in_array( $format, $this->formats, true ) ) { 79 $format = 'aside'; 80 } 81 82 switch ( $format ) { 83 case 'image': 84 $format_string = __( 'Images', 'twentyfourteen' ); 85 $format_string_more = __( 'More images', 'twentyfourteen' ); 86 break; 87 case 'video': 88 $format_string = __( 'Videos', 'twentyfourteen' ); 89 $format_string_more = __( 'More videos', 'twentyfourteen' ); 90 break; 91 case 'audio': 92 $format_string = __( 'Audio', 'twentyfourteen' ); 93 $format_string_more = __( 'More audio', 'twentyfourteen' ); 94 break; 95 case 'quote': 96 $format_string = __( 'Quotes', 'twentyfourteen' ); 97 $format_string_more = __( 'More quotes', 'twentyfourteen' ); 98 break; 99 case 'link': 100 $format_string = __( 'Links', 'twentyfourteen' ); 101 $format_string_more = __( 'More links', 'twentyfourteen' ); 102 break; 103 case 'gallery': 104 $format_string = __( 'Galleries', 'twentyfourteen' ); 105 $format_string_more = __( 'More galleries', 'twentyfourteen' ); 106 break; 107 case 'aside': 108 default: 109 $format_string = __( 'Asides', 'twentyfourteen' ); 110 $format_string_more = __( 'More asides', 'twentyfourteen' ); 111 break; 112 } 113 114 $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : 2; 115 $title = ! empty( $instance['title'] ) ? $instance['title'] : $format_string; 116 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ 117 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); 118 119 $ephemera = new WP_Query( 120 array( 121 'order' => 'DESC', 122 'posts_per_page' => $number, 123 'no_found_rows' => true, 124 'post_status' => 'publish', 125 'post__not_in' => get_option( 'sticky_posts' ), 126 'tax_query' => array( 127 array( 128 'taxonomy' => 'post_format', 129 'terms' => array( "post-format-$format" ), 130 'field' => 'slug', 131 'operator' => 'IN', 132 ), 133 ), 134 ) 135 ); 136 137 if ( $ephemera->have_posts() ) : 138 $tmp_content_width = $GLOBALS['content_width']; 139 $GLOBALS['content_width'] = 306; 140 141 echo $args['before_widget']; 142 ?> 143 <h1 class="widget-title <?php echo esc_attr( $format ); ?>"> 144 <a class="entry-format" href="<?php echo esc_url( get_post_format_link( $format ) ); ?>"><?php echo esc_html( $title ); ?></a> 145 </h1> 146 <ol> 147 148 <?php 149 while ( $ephemera->have_posts() ) : 150 $ephemera->the_post(); 151 $tmp_more = $GLOBALS['more']; 152 $GLOBALS['more'] = 0; 153 ?> 154 <li> 155 <article <?php post_class(); ?>> 156 <div class="entry-content"> 157 <?php 158 if ( has_post_format( 'gallery' ) ) : 159 160 if ( post_password_required() ) : 161 the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyfourteen' ) ); 162 else : 163 $images = array(); 164 165 $galleries = get_post_galleries( get_the_ID(), false ); 166 if ( isset( $galleries[0]['ids'] ) ) { 167 $images = explode( ',', $galleries[0]['ids'] ); 168 } 169 170 if ( ! $images ) : 171 $images = get_posts( 172 array( 173 'fields' => 'ids', 174 'numberposts' => -1, 175 'order' => 'ASC', 176 'orderby' => 'menu_order', 177 'post_mime_type' => 'image', 178 'post_parent' => get_the_ID(), 179 'post_type' => 'attachment', 180 ) 181 ); 182 endif; 183 184 $total_images = count( $images ); 185 186 if ( has_post_thumbnail() ) : 187 $post_thumbnail = get_the_post_thumbnail(); 188 elseif ( $total_images > 0 ) : 189 $image = reset( $images ); 190 $post_thumbnail = wp_get_attachment_image( $image, 'post-thumbnail' ); 191 endif; 192 193 if ( ! empty( $post_thumbnail ) ) : 194 ?> 195 <a href="<?php the_permalink(); ?>"><?php echo $post_thumbnail; ?></a> 196 <?php endif; ?> 197 <p class="wp-caption-text"> 198 <?php 199 printf( 200 /* translators: 1: Post permalink, 2: Number of images in the gallery. */ 201 _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' ), 202 esc_url( get_permalink() ), 203 number_format_i18n( $total_images ) 204 ); 205 ?> 206 </p> 207 <?php 208 endif; 209 210 else : 211 the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyfourteen' ) ); 212 endif; 213 ?> 214 </div><!-- .entry-content --> 215 216 <header class="entry-header"> 217 <div class="entry-meta"> 218 <?php 219 if ( ! has_post_format( 'link' ) ) : 220 the_title( '<h1 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h1>' ); 221 endif; 222 223 printf( 224 '<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>', 225 esc_url( get_permalink() ), 226 esc_attr( get_the_date( 'c' ) ), 227 esc_html( get_the_date() ), 228 esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ), 229 get_the_author() 230 ); 231 232 if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) : 233 ?> 234 <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyfourteen' ), __( '1 Comment', 'twentyfourteen' ), __( '% Comments', 'twentyfourteen' ) ); ?></span> 235 <?php endif; ?> 236 </div><!-- .entry-meta --> 237 </header><!-- .entry-header --> 238 </article><!-- #post-<?php the_ID(); ?> --> 239 </li> 240 <?php endwhile; ?> 241 242 </ol> 243 <a class="post-format-archive-link" href="<?php echo esc_url( get_post_format_link( $format ) ); ?>"> 244 <?php 245 /* translators: Used with More archives link. */ 246 printf( __( '%s <span class="meta-nav">→</span>', 'twentyfourteen' ), $format_string_more ); 247 ?> 248 </a> 249 <?php 250 251 echo $args['after_widget']; 252 253 // Reset the post globals as this query will have stomped on it. 254 wp_reset_postdata(); 255 256 $GLOBALS['more'] = $tmp_more; 257 $GLOBALS['content_width'] = $tmp_content_width; 258 259 endif; // End check for ephemeral posts. 260 } 261 262 /** 263 * Deals with the settings when they are saved by the admin. 264 * 265 * Here is where any validation should happen. 266 * 267 * @since Twenty Fourteen 1.0 268 * @since Twenty Fourteen 3.3 Renamed `$instance` to `$old_instance` to match 269 * parent class for PHP 8 named parameter support. 270 * 271 * @param array $new_instance New widget instance. 272 * @param array $old_instance Original widget instance. 273 * @return array Updated widget instance. 274 */ 275 public function update( $new_instance, $old_instance ) { 276 $old_instance['title'] = strip_tags( $new_instance['title'] ); 277 $old_instance['number'] = empty( $new_instance['number'] ) ? 2 : absint( $new_instance['number'] ); 278 279 if ( in_array( $new_instance['format'], $this->formats, true ) ) { 280 $old_instance['format'] = $new_instance['format']; 281 } 282 283 return $old_instance; 284 } 285 286 /** 287 * Displays the form for this widget on the Widgets page of the Admin area. 288 * 289 * @since Twenty Fourteen 1.0 290 * 291 * @param array $instance 292 */ 293 public function form( $instance ) { 294 $title = ! empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; 295 $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : 2; 296 $format = isset( $instance['format'] ) ? $instance['format'] : ''; 297 298 if ( ! $format || ! in_array( $format, $this->formats, true ) ) { 299 $format = 'aside'; 300 } 301 ?> 302 <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'twentyfourteen' ); ?></label> 303 <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> 304 305 <p><label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of posts to show:', 'twentyfourteen' ); ?></label> 306 <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> 307 308 <p><label for="<?php echo esc_attr( $this->get_field_id( 'format' ) ); ?>"><?php _e( 'Post format to show:', 'twentyfourteen' ); ?></label> 309 <select id="<?php echo esc_attr( $this->get_field_id( 'format' ) ); ?>" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'format' ) ); ?>"> 310 <?php foreach ( $this->formats as $slug ) : ?> 311 <option value="<?php echo esc_attr( $slug ); ?>"<?php selected( $format, $slug ); ?>><?php echo esc_html( get_post_format_string( $slug ) ); ?></option> 312 <?php endforeach; ?> 313 </select> 314 <?php 315 } 316 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Mon Aug 3 08:20:19 2026 | Cross-referenced by PHPXref |