2013-08-22 14:06:09 -04:00
< ? php
/**
2013-10-11 18:02:11 -04:00
* Custom Widget for displaying specific post formats
2013-08-22 14:06:09 -04:00
*
2013-11-05 16:52:09 -05:00
* Displays posts from Aside , Quote , Video , Audio , Image , Gallery , and Link formats .
2013-10-11 18:02:11 -04:00
*
2015-04-12 17:29:32 -04:00
* @ link https :// codex . wordpress . org / Widgets_API #Developing_Widgets
2013-08-22 14:06:09 -04:00
*
* @ package WordPress
* @ subpackage Twenty_Fourteen
2013-10-11 18:02:11 -04:00
* @ since Twenty Fourteen 1.0
2013-08-22 14:06:09 -04:00
*/
class Twenty_Fourteen_Ephemera_Widget extends WP_Widget {
/**
* The supported post formats .
*
2013-10-11 18:02:11 -04:00
* @ since Twenty Fourteen 1.0
*
2013-08-22 14:06:09 -04:00
* @ var array
*/
2013-11-05 16:52:09 -05:00
private $formats = array ( 'aside' , 'image' , 'video' , 'audio' , 'quote' , 'link' , 'gallery' );
2013-08-22 14:06:09 -04:00
/**
* Constructor .
*
2013-10-11 18:02:11 -04:00
* @ since Twenty Fourteen 1.0
*
2013-08-22 14:06:09 -04:00
* @ return Twenty_Fourteen_Ephemera_Widget
*/
public function __construct () {
2017-11-30 18:11:00 -05:00
parent :: __construct (
'widget_twentyfourteen_ephemera' , __ ( 'Twenty Fourteen Ephemera' , 'twentyfourteen' ), array (
'classname' => 'widget_twentyfourteen_ephemera' ,
'description' => __ ( 'Use this widget to list your recent Aside, Quote, Video, Audio, Image, Gallery, and Link posts.' , 'twentyfourteen' ),
'customize_selective_refresh' => true ,
)
);
2016-03-21 17:59:29 -04:00
if ( is_active_widget ( false , false , $this -> id_base ) || is_customize_preview () ) {
add_action ( 'wp_enqueue_scripts' , array ( $this , 'enqueue_scripts' ) );
}
}
/**
* Enqueue scripts .
*
* @ since Twenty Fourteen 1.7
*/
public function enqueue_scripts () {
/** This filter is documented in wp-includes/media.php */
$audio_library = apply_filters ( 'wp_audio_shortcode_library' , 'mediaelement' );
/** This filter is documented in wp-includes/media.php */
$video_library = apply_filters ( 'wp_video_shortcode_library' , 'mediaelement' );
if ( in_array ( 'mediaelement' , array ( $video_library , $audio_library ), true ) ) {
wp_enqueue_style ( 'wp-mediaelement' );
2017-08-01 00:43:51 -04:00
wp_enqueue_script ( 'mediaelement-vimeo' );
2016-03-21 17:59:29 -04:00
wp_enqueue_script ( 'wp-mediaelement' );
}
2013-08-22 14:06:09 -04:00
}
/**
2013-10-11 18:02:11 -04:00
* Output the HTML for this widget .
*
* @ since Twenty Fourteen 1.0
2013-08-22 14:06:09 -04:00
*
2013-12-03 12:06:11 -05:00
* @ param array $args An array of standard parameters for widgets in this theme .
2013-08-22 14:06:09 -04:00
* @ param array $instance An array of settings for this widget instance .
*/
public function widget ( $args , $instance ) {
2014-05-02 14:46:17 -04:00
$format = isset ( $instance [ 'format' ] ) && in_array ( $instance [ 'format' ], $this -> formats ) ? $instance [ 'format' ] : 'aside' ;
2014-03-18 14:50:13 -04:00
switch ( $format ) {
case 'image' :
$format_string = __ ( 'Images' , 'twentyfourteen' );
$format_string_more = __ ( 'More images' , 'twentyfourteen' );
break ;
case 'video' :
$format_string = __ ( 'Videos' , 'twentyfourteen' );
$format_string_more = __ ( 'More videos' , 'twentyfourteen' );
break ;
case 'audio' :
$format_string = __ ( 'Audio' , 'twentyfourteen' );
$format_string_more = __ ( 'More audio' , 'twentyfourteen' );
break ;
case 'quote' :
$format_string = __ ( 'Quotes' , 'twentyfourteen' );
$format_string_more = __ ( 'More quotes' , 'twentyfourteen' );
break ;
case 'link' :
$format_string = __ ( 'Links' , 'twentyfourteen' );
$format_string_more = __ ( 'More links' , 'twentyfourteen' );
break ;
case 'gallery' :
$format_string = __ ( 'Galleries' , 'twentyfourteen' );
$format_string_more = __ ( 'More galleries' , 'twentyfourteen' );
break ;
case 'aside' :
default :
$format_string = __ ( 'Asides' , 'twentyfourteen' );
$format_string_more = __ ( 'More asides' , 'twentyfourteen' );
break ;
}
2013-08-22 14:06:09 -04:00
$number = empty ( $instance [ 'number' ] ) ? 2 : absint ( $instance [ 'number' ] );
2014-03-18 14:50:13 -04:00
$title = apply_filters ( 'widget_title' , empty ( $instance [ 'title' ] ) ? $format_string : $instance [ 'title' ], $instance , $this -> id_base );
2013-08-22 14:06:09 -04:00
2017-11-30 18:11:00 -05:00
$ephemera = new WP_Query (
array (
'order' => 'DESC' ,
'posts_per_page' => $number ,
'no_found_rows' => true ,
'post_status' => 'publish' ,
'post__not_in' => get_option ( 'sticky_posts' ),
'tax_query' => array (
array (
'taxonomy' => 'post_format' ,
'terms' => array ( " post-format- $format " ),
'field' => 'slug' ,
'operator' => 'IN' ,
),
2013-08-22 14:06:09 -04:00
),
2017-11-30 18:11:00 -05:00
)
);
2013-08-22 14:06:09 -04:00
if ( $ephemera -> have_posts () ) :
2017-11-30 18:11:00 -05:00
$tmp_content_width = $GLOBALS [ 'content_width' ];
2013-08-22 14:06:09 -04:00
$GLOBALS [ 'content_width' ] = 306 ;
2013-12-03 13:47:09 -05:00
echo $args [ 'before_widget' ];
2013-08-22 14:06:09 -04:00
?>
2013-09-19 14:05:12 -04:00
< h1 class = " widget-title <?php echo esc_attr( $format ); ?> " >
2015-01-20 14:03:23 -05:00
< a class = " entry-format " href = " <?php echo esc_url( get_post_format_link( $format ) ); ?> " >< ? php echo esc_html ( $title ); ?> </a>
2013-08-22 14:06:09 -04:00
</ h1 >
< ol >
2014-02-07 13:03:13 -05:00
< ? php
2017-11-30 18:11:00 -05:00
while ( $ephemera -> have_posts () ) :
$ephemera -> the_post ();
$tmp_more = $GLOBALS [ 'more' ];
$GLOBALS [ 'more' ] = 0 ;
2014-02-07 13:03:13 -05:00
?>
2013-08-22 14:06:09 -04:00
< li >
2013-10-30 10:39:10 -04:00
< article < ? php post_class (); ?> >
2017-11-30 18:11:00 -05:00
< div class = " entry-content " >
< ? php
if ( has_post_format ( 'gallery' ) ) :
if ( post_password_required () ) :
the_content ( __ ( 'Continue reading <span class="meta-nav">→</span>' , 'twentyfourteen' ) );
else :
$images = array ();
$galleries = get_post_galleries ( get_the_ID (), false );
if ( isset ( $galleries [ 0 ][ 'ids' ] ) ) {
$images = explode ( ',' , $galleries [ 0 ][ 'ids' ] );
}
if ( ! $images ) :
$images = get_posts (
array (
'fields' => 'ids' ,
'numberposts' => - 1 ,
'order' => 'ASC' ,
'orderby' => 'menu_order' ,
2013-10-30 10:39:10 -04:00
'post_mime_type' => 'image' ,
2017-11-30 18:11:00 -05:00
'post_parent' => get_the_ID (),
'post_type' => 'attachment' ,
)
);
endif ;
2013-10-30 10:39:10 -04:00
2017-11-30 18:11:00 -05:00
$total_images = count ( $images );
2013-10-30 10:39:10 -04:00
2017-11-30 18:11:00 -05:00
if ( has_post_thumbnail () ) :
$post_thumbnail = get_the_post_thumbnail ();
2013-10-30 10:39:10 -04:00
elseif ( $total_images > 0 ) :
2015-03-19 22:04:27 -04:00
$image = reset ( $images );
2013-10-30 10:39:10 -04:00
$post_thumbnail = wp_get_attachment_image ( $image , 'post-thumbnail' );
endif ;
2017-11-30 18:11:00 -05:00
if ( ! empty ( $post_thumbnail ) ) :
2013-08-22 14:06:09 -04:00
?>
2013-11-15 16:15:10 -05:00
< a href = " <?php the_permalink(); ?> " >< ? php echo $post_thumbnail ; ?> </a>
< ? php endif ; ?>
< p class = " wp-caption-text " >
2017-11-30 18:11:00 -05:00
< ? php
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' ),
esc_url ( get_permalink () ),
number_format_i18n ( $total_images )
);
?>
2013-11-15 16:15:10 -05:00
</ p >
< ? php
2017-11-30 18:11:00 -05:00
endif ;
2013-10-30 10:39:10 -04:00
2013-08-22 14:06:09 -04:00
else :
the_content ( __ ( 'Continue reading <span class="meta-nav">→</span>' , 'twentyfourteen' ) );
endif ;
?>
</ div ><!-- . entry - content -->
< header class = " entry-header " >
< div class = " entry-meta " >
2017-11-30 18:11:00 -05:00
< ? php
if ( ! has_post_format ( 'link' ) ) :
the_title ( '<h1 class="entry-title"><a href="' . esc_url ( get_permalink () ) . '" rel="bookmark">' , '</a></h1>' );
endif ;
2013-08-22 14:06:09 -04:00
2017-11-30 18:11:00 -05:00
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>' ,
esc_url ( get_permalink () ),
esc_attr ( get_the_date ( 'c' ) ),
esc_html ( get_the_date () ),
esc_url ( get_author_posts_url ( get_the_author_meta ( 'ID' ) ) ),
get_the_author ()
);
2013-08-22 14:06:09 -04:00
2017-11-30 18:11:00 -05:00
if ( ! post_password_required () && ( comments_open () || get_comments_number () ) ) :
2013-08-22 14:06:09 -04:00
?>
< span class = " comments-link " >< ? php comments_popup_link ( __ ( 'Leave a comment' , 'twentyfourteen' ), __ ( '1 Comment' , 'twentyfourteen' ), __ ( '% Comments' , 'twentyfourteen' ) ); ?> </span>
< ? php endif ; ?>
</ div ><!-- . entry - meta -->
</ header ><!-- . entry - header -->
</ article ><!-- #post-## -->
</ li >
< ? php endwhile ; ?>
</ ol >
2014-03-18 14:50:13 -04:00
< a class = " post-format-archive-link " href = " <?php echo esc_url( get_post_format_link( $format ) ); ?> " >
< ? php
/* translators: used with More archives link */
printf ( __ ( '%s <span class="meta-nav">→</span>' , 'twentyfourteen' ), $format_string_more );
?>
</ a >
2013-08-22 14:06:09 -04:00
< ? php
2013-12-03 13:47:09 -05:00
echo $args [ 'after_widget' ];
2013-08-22 14:06:09 -04:00
// Reset the post globals as this query will have stomped on it.
wp_reset_postdata ();
2014-02-07 13:03:13 -05:00
$GLOBALS [ 'more' ] = $tmp_more ;
2013-08-22 14:06:09 -04:00
$GLOBALS [ 'content_width' ] = $tmp_content_width ;
endif ; // End check for ephemeral posts.
}
/**
2013-12-03 12:06:11 -05:00
* Deal with the settings when they are saved by the admin .
*
* Here is where any validation should happen .
2013-10-11 18:02:11 -04:00
*
* @ since Twenty Fourteen 1.0
2013-08-22 14:06:09 -04:00
*
2013-12-03 12:06:11 -05:00
* @ param array $new_instance New widget instance .
* @ param array $instance Original widget instance .
* @ return array Updated widget instance .
2013-08-22 14:06:09 -04:00
*/
function update ( $new_instance , $instance ) {
$instance [ 'title' ] = strip_tags ( $new_instance [ 'title' ] );
$instance [ 'number' ] = empty ( $new_instance [ 'number' ] ) ? 2 : absint ( $new_instance [ 'number' ] );
2013-11-18 18:11:10 -05:00
if ( in_array ( $new_instance [ 'format' ], $this -> formats ) ) {
2013-08-22 14:06:09 -04:00
$instance [ 'format' ] = $new_instance [ 'format' ];
2013-11-18 18:11:10 -05:00
}
2013-08-22 14:06:09 -04:00
return $instance ;
}
/**
2013-10-11 18:02:11 -04:00
* Display the form for this widget on the Widgets page of the Admin area .
*
* @ since Twenty Fourteen 1.0
2013-08-22 14:06:09 -04:00
*
* @ param array $instance
*/
function form ( $instance ) {
$title = empty ( $instance [ 'title' ] ) ? '' : esc_attr ( $instance [ 'title' ] );
$number = empty ( $instance [ 'number' ] ) ? 2 : absint ( $instance [ 'number' ] );
$format = isset ( $instance [ 'format' ] ) && in_array ( $instance [ 'format' ], $this -> formats ) ? $instance [ 'format' ] : 'aside' ;
?>
< p >< label for = " <?php echo esc_attr( $this->get_field_id ( 'title' ) ); ?> " >< ? php _e ( 'Title:' , 'twentyfourteen' ); ?> </label>
2013-12-05 12:54:09 -05:00
< 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 >
2013-08-22 14:06:09 -04:00
< p >< label for = " <?php echo esc_attr( $this->get_field_id ( 'number' ) ); ?> " >< ? php _e ( 'Number of posts to show:' , 'twentyfourteen' ); ?> </label>
2013-12-05 12:54:09 -05:00
< 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 >
2013-08-22 14:06:09 -04:00
< p >< label for = " <?php echo esc_attr( $this->get_field_id ( 'format' ) ); ?> " >< ? php _e ( 'Post format to show:' , 'twentyfourteen' ); ?> </label>
< select id = " <?php echo esc_attr( $this->get_field_id ( 'format' ) ); ?> " class = " widefat " name = " <?php echo esc_attr( $this->get_field_name ( 'format' ) ); ?> " >
< ? php foreach ( $this -> formats as $slug ) : ?>
2015-01-20 14:03:23 -05:00
< option value = " <?php echo esc_attr( $slug ); ?> " < ? php selected ( $format , $slug ); ?> ><?php echo esc_html( get_post_format_string( $slug ) ); ?></option>
2013-08-22 14:06:09 -04:00
< ? php endforeach ; ?>
</ select >
< ? php
}
}