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