2013-08-22 20:06:09 +02:00
< ? php
/**
2013-10-12 00:02:11 +02:00
* Custom Widget for displaying specific post formats
2013-08-22 20:06:09 +02:00
*
2013-11-05 22:52:09 +01:00
* Displays posts from Aside , Quote , Video , Audio , Image , Gallery , and Link formats .
2013-10-12 00:02:11 +02:00
*
* @ link http :// codex . wordpress . org / Widgets_API #Developing_Widgets
2013-08-22 20:06:09 +02:00
*
* @ package WordPress
* @ subpackage Twenty_Fourteen
2013-10-12 00:02:11 +02:00
* @ since Twenty Fourteen 1.0
2013-08-22 20:06:09 +02:00
*/
class Twenty_Fourteen_Ephemera_Widget extends WP_Widget {
/**
* The supported post formats .
*
2013-12-03 18:06:11 +01:00
* @ access private
2013-10-12 00:02:11 +02:00
* @ since Twenty Fourteen 1.0
*
2013-08-22 20:06:09 +02:00
* @ var array
*/
2013-11-05 22:52:09 +01:00
private $formats = array ( 'aside' , 'image' , 'video' , 'audio' , 'quote' , 'link' , 'gallery' );
2013-08-22 20:06:09 +02:00
/**
* Constructor .
*
2013-10-12 00:02:11 +02:00
* @ since Twenty Fourteen 1.0
*
2013-08-22 20:06:09 +02:00
* @ return Twenty_Fourteen_Ephemera_Widget
*/
public function __construct () {
parent :: __construct ( 'widget_twentyfourteen_ephemera' , __ ( 'Twenty Fourteen Ephemera' , 'twentyfourteen' ), array (
'classname' => 'widget_twentyfourteen_ephemera' ,
2014-03-05 18:26:17 +01:00
'description' => __ ( 'Use this widget to list your recent Aside, Quote, Video, Audio, Image, Gallery, and Link posts.' , 'twentyfourteen' ),
2013-08-22 20:06:09 +02:00
) );
}
/**
2013-10-12 00:02:11 +02:00
* Output the HTML for this widget .
*
2013-12-03 18:06:11 +01:00
* @ access public
2013-10-12 00:02:11 +02:00
* @ since Twenty Fourteen 1.0
2013-08-22 20:06:09 +02:00
*
2013-12-03 18:06:11 +01:00
* @ param array $args An array of standard parameters for widgets in this theme .
2013-08-22 20:06:09 +02:00
* @ param array $instance An array of settings for this widget instance .
*/
public function widget ( $args , $instance ) {
$format = $instance [ 'format' ];
2014-03-18 19:50:13 +01: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 20:06:09 +02:00
$number = empty ( $instance [ 'number' ] ) ? 2 : absint ( $instance [ 'number' ] );
2014-03-18 19:50:13 +01:00
$title = apply_filters ( 'widget_title' , empty ( $instance [ 'title' ] ) ? $format_string : $instance [ 'title' ], $instance , $this -> id_base );
2013-08-22 20:06:09 +02: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' ,
),
),
) );
if ( $ephemera -> have_posts () ) :
$tmp_content_width = $GLOBALS [ 'content_width' ];
$GLOBALS [ 'content_width' ] = 306 ;
2013-12-03 19:47:09 +01:00
echo $args [ 'before_widget' ];
2013-08-22 20:06:09 +02:00
?>
2013-09-19 20:05:12 +02:00
< h1 class = " widget-title <?php echo esc_attr( $format ); ?> " >
2013-08-22 20:06:09 +02:00
< a class = " entry-format " href = " <?php echo esc_url( get_post_format_link( $format ) ); ?> " >< ? php echo $title ; ?> </a>
</ h1 >
< ol >
2014-02-07 19:03:13 +01:00
< ? php
while ( $ephemera -> have_posts () ) :
$ephemera -> the_post ();
$tmp_more = $GLOBALS [ 'more' ];
$GLOBALS [ 'more' ] = 0 ;
?>
2013-08-22 20:06:09 +02:00
< li >
2013-10-30 15:39:10 +01:00
< article < ? php post_class (); ?> >
2013-08-22 20:06:09 +02:00
< div class = " entry-content " >
< ? php
if ( has_post_format ( 'gallery' ) ) :
2013-10-30 15:39:10 +01:00
if ( post_password_required () ) :
the_content ( __ ( 'Continue reading <span class="meta-nav">→</span>' , 'twentyfourteen' ) );
else :
$images = array ();
2013-11-15 22:15:10 +01:00
$galleries = get_post_galleries ( get_the_ID (), false );
if ( isset ( $galleries [ 0 ][ 'ids' ] ) )
$images = explode ( ',' , $galleries [ 0 ][ 'ids' ] );
2013-10-30 15:39:10 +01:00
if ( ! $images ) :
$images = get_posts ( array (
'fields' => 'ids' ,
2013-11-15 22:15:10 +01:00
'numberposts' => - 1 ,
2013-10-30 15:39:10 +01:00
'order' => 'ASC' ,
'orderby' => 'menu_order' ,
'post_mime_type' => 'image' ,
'post_parent' => get_the_ID (),
'post_type' => 'attachment' ,
) );
endif ;
$total_images = count ( $images );
if ( has_post_thumbnail () ) :
2013-11-14 05:50:11 +01:00
$post_thumbnail = get_the_post_thumbnail ();
2013-10-30 15:39:10 +01:00
elseif ( $total_images > 0 ) :
$image = array_shift ( $images );
$post_thumbnail = wp_get_attachment_image ( $image , 'post-thumbnail' );
endif ;
if ( ! empty ( $post_thumbnail ) ) :
2013-08-22 20:06:09 +02:00
?>
2013-11-15 22:15:10 +01:00
< a href = " <?php the_permalink(); ?> " >< ? php echo $post_thumbnail ; ?> </a>
< ? php endif ; ?>
< p class = " wp-caption-text " >
2013-08-22 20:06:09 +02:00
< ? php
2013-11-15 22:15:10 +01:00
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 )
);
?>
</ p >
< ? php
2013-10-30 15:39:10 +01:00
endif ;
2013-08-22 20:06:09 +02:00
else :
the_content ( __ ( 'Continue reading <span class="meta-nav">→</span>' , 'twentyfourteen' ) );
endif ;
?>
</ div ><!-- . entry - content -->
< header class = " entry-header " >
< div class = " entry-meta " >
< ? php
if ( ! has_post_format ( 'link' ) ) :
the_title ( '<h1 class="entry-title"><a href="' . esc_url ( get_permalink () ) . '" rel="bookmark">' , '</a></h1>' );
endif ;
2013-12-05 23:47:10 +01: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>' ,
2013-08-22 20:06:09 +02:00
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 ()
);
if ( ! post_password_required () && ( comments_open () || get_comments_number () ) ) :
?>
< 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 19:50:13 +01: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 20:06:09 +02:00
< ? php
2013-12-03 19:47:09 +01:00
echo $args [ 'after_widget' ];
2013-08-22 20:06:09 +02:00
// Reset the post globals as this query will have stomped on it.
wp_reset_postdata ();
2014-02-07 19:03:13 +01:00
$GLOBALS [ 'more' ] = $tmp_more ;
2013-08-22 20:06:09 +02:00
$GLOBALS [ 'content_width' ] = $tmp_content_width ;
endif ; // End check for ephemeral posts.
}
/**
2013-12-03 18:06:11 +01:00
* Deal with the settings when they are saved by the admin .
*
* Here is where any validation should happen .
2013-10-12 00:02:11 +02:00
*
* @ since Twenty Fourteen 1.0
2013-08-22 20:06:09 +02:00
*
2013-12-03 18:06:11 +01:00
* @ param array $new_instance New widget instance .
* @ param array $instance Original widget instance .
* @ return array Updated widget instance .
2013-08-22 20:06:09 +02: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-19 00:11:10 +01:00
if ( in_array ( $new_instance [ 'format' ], $this -> formats ) ) {
2013-08-22 20:06:09 +02:00
$instance [ 'format' ] = $new_instance [ 'format' ];
2013-11-19 00:11:10 +01:00
}
2013-08-22 20:06:09 +02:00
return $instance ;
}
/**
2013-10-12 00:02:11 +02:00
* Display the form for this widget on the Widgets page of the Admin area .
*
* @ since Twenty Fourteen 1.0
2013-08-22 20:06:09 +02: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 18:54:09 +01: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 20:06:09 +02: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 18:54:09 +01: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 20:06:09 +02: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 ) : ?>
< option value = " <?php echo esc_attr( $slug ); ?> " < ? php selected ( $format , $slug ); ?> ><?php echo get_post_format_string( $slug ); ?></option>
< ? php endforeach ; ?>
</ select >
< ? php
}
}