| [ 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 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); 117 118 $ephemera = new WP_Query( 119 array( 120 'order' => 'DESC', 121 'posts_per_page' => $number, 122 'no_found_rows' => true, 123 'post_status' => 'publish', 124 'post__not_in' => get_option( 'sticky_posts' ), 125 'tax_query' => array( 126 array( 127 'taxonomy' => 'post_format', 128 'terms' => array( "post-format-$format" ), 129 'field' => 'slug', 130 'operator' => 'IN', 131 ), 132 ), 133 ) 134 ); 135 136 if ( $ephemera->have_posts() ) : 137 $tmp_content_width = $GLOBALS['content_width']; 138 $GLOBALS['content_width'] = 306; 139 140 echo $args['before_widget']; 141 ?> 142 <h1 class="widget-title <?php echo esc_attr( $format ); ?>"> 143 <a class="entry-format" href="<?php echo esc_url( get_post_format_link( $format ) ); ?>"><?php echo esc_html( $title ); ?></a> 144 </h1> 145 <ol> 146 147 <?php 148 while ( $ephemera->have_posts() ) : 149 $ephemera->the_post(); 150 $tmp_more = $GLOBALS['more']; 151 $GLOBALS['more'] = 0; 152 ?> 153 <li> 154 <article <?php post_class(); ?>> 155 <div class="entry-content"> 156 <?php 157 if ( has_post_format( 'gallery' ) ) : 158 159 if ( post_password_required() ) : 160 the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyfourteen' ) ); 161 else : 162 $images = array(); 163 164 $galleries = get_post_galleries( get_the_ID(), false ); 165 if ( isset( $galleries[0]['ids'] ) ) { 166 $images = explode( ',', $galleries[0]['ids'] ); 167 } 168 169 if ( ! $images ) : 170 $images = get_posts( 171 array( 172 'fields' => 'ids', 173 'numberposts' => -1, 174 'order' => 'ASC', 175 'orderby' => 'menu_order', 176 'post_mime_type' => 'image', 177 'post_parent' => get_the_ID(), 178 'post_type' => 'attachment', 179 ) 180 ); 181 endif; 182 183 $total_images = count( $images ); 184 185 if ( has_post_thumbnail() ) : 186 $post_thumbnail = get_the_post_thumbnail(); 187 elseif ( $total_images > 0 ) : 188 $image = reset( $images ); 189 $post_thumbnail = wp_get_attachment_image( $image, 'post-thumbnail' ); 190 endif; 191 192 if ( ! empty( $post_thumbnail ) ) : 193 ?> 194 <a href="<?php the_permalink(); ?>"><?php echo $post_thumbnail; ?></a> 195 <?php endif; ?> 196 <p class="wp-caption-text"> 197 <?php 198 printf( 199 /* translators: 1: Post permalink, 2: Number of images in the gallery. */ 200 _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' ), 201 esc_url( get_permalink() ), 202 number_format_i18n( $total_images ) 203 ); 204 ?> 205 </p> 206 <?php 207 endif; 208 209 else : 210 the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyfourteen' ) ); 211 endif; 212 ?> 213 </div><!-- .entry-content --> 214 215 <header class="entry-header"> 216 <div class="entry-meta"> 217 <?php 218 if ( ! has_post_format( 'link' ) ) : 219 the_title( '<h1 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h1>' ); 220 endif; 221 222 printf( 223 '<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>', 224 esc_url( get_permalink() ), 225 esc_attr( get_the_date( 'c' ) ), 226 esc_html( get_the_date() ), 227 esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ), 228 get_the_author() 229 ); 230 231 if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) : 232 ?> 233 <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyfourteen' ), __( '1 Comment', 'twentyfourteen' ), __( '% Comments', 'twentyfourteen' ) ); ?></span> 234 <?php endif; ?> 235 </div><!-- .entry-meta --> 236 </header><!-- .entry-header --> 237 </article><!-- #post-<?php the_ID(); ?> --> 238 </li> 239 <?php endwhile; ?> 240 241 </ol> 242 <a class="post-format-archive-link" href="<?php echo esc_url( get_post_format_link( $format ) ); ?>"> 243 <?php 244 /* translators: Used with More archives link. */ 245 printf( __( '%s <span class="meta-nav">→</span>', 'twentyfourteen' ), $format_string_more ); 246 ?> 247 </a> 248 <?php 249 250 echo $args['after_widget']; 251 252 // Reset the post globals as this query will have stomped on it. 253 wp_reset_postdata(); 254 255 $GLOBALS['more'] = $tmp_more; 256 $GLOBALS['content_width'] = $tmp_content_width; 257 258 endif; // End check for ephemeral posts. 259 } 260 261 /** 262 * Deals with the settings when they are saved by the admin. 263 * 264 * Here is where any validation should happen. 265 * 266 * @since Twenty Fourteen 1.0 267 * @since Twenty Fourteen 3.3 Renamed `$instance` to `$old_instance` to match 268 * parent class for PHP 8 named parameter support. 269 * 270 * @param array $new_instance New widget instance. 271 * @param array $old_instance Original widget instance. 272 * @return array Updated widget instance. 273 */ 274 public function update( $new_instance, $old_instance ) { 275 $old_instance['title'] = strip_tags( $new_instance['title'] ); 276 $old_instance['number'] = empty( $new_instance['number'] ) ? 2 : absint( $new_instance['number'] ); 277 278 if ( in_array( $new_instance['format'], $this->formats, true ) ) { 279 $old_instance['format'] = $new_instance['format']; 280 } 281 282 return $old_instance; 283 } 284 285 /** 286 * Displays the form for this widget on the Widgets page of the Admin area. 287 * 288 * @since Twenty Fourteen 1.0 289 * 290 * @param array $instance 291 */ 292 public function form( $instance ) { 293 $title = ! empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; 294 $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : 2; 295 $format = isset( $instance['format'] ) ? $instance['format'] : ''; 296 297 if ( ! $format || ! in_array( $format, $this->formats, true ) ) { 298 $format = 'aside'; 299 } 300 ?> 301 <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'twentyfourteen' ); ?></label> 302 <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> 303 304 <p><label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of posts to show:', 'twentyfourteen' ); ?></label> 305 <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> 306 307 <p><label for="<?php echo esc_attr( $this->get_field_id( 'format' ) ); ?>"><?php _e( 'Post format to show:', 'twentyfourteen' ); ?></label> 308 <select id="<?php echo esc_attr( $this->get_field_id( 'format' ) ); ?>" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'format' ) ); ?>"> 309 <?php foreach ( $this->formats as $slug ) : ?> 310 <option value="<?php echo esc_attr( $slug ); ?>"<?php selected( $format, $slug ); ?>><?php echo esc_html( get_post_format_string( $slug ) ); ?></option> 311 <?php endforeach; ?> 312 </select> 313 <?php 314 } 315 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Fri Jun 19 08:20:10 2026 | Cross-referenced by PHPXref |