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