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
*
2015-04-12 23:29:32 +02:00
* @ link https :// 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-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 () {
2017-12-01 00:11:00 +01:00
parent :: __construct (
2018-08-17 03:51:36 +02:00
'widget_twentyfourteen_ephemera' ,
__ ( 'Twenty Fourteen Ephemera' , 'twentyfourteen' ),
array (
2017-12-01 00:11:00 +01:00
'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 22:59:29 +01: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 06:43:51 +02:00
wp_enqueue_script ( 'mediaelement-vimeo' );
2016-03-21 22:59:29 +01:00
wp_enqueue_script ( 'wp-mediaelement' );
}
2013-08-22 20:06:09 +02:00
}
/**
2013-10-12 00:02:11 +02:00
* Output the HTML for this widget .
*
* @ 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 ) {
2014-05-02 20:46:17 +02:00
$format = isset ( $instance [ 'format' ] ) && in_array ( $instance [ 'format' ], $this -> formats ) ? $instance [ 'format' ] : 'aside' ;
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
2017-12-01 00:11:00 +01: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 20:06:09 +02:00
),
2017-12-01 00:11:00 +01:00
)
);
2013-08-22 20:06:09 +02:00
if ( $ephemera -> have_posts () ) :
2017-12-01 00:11:00 +01:00
$tmp_content_width = $GLOBALS [ 'content_width' ];
2013-08-22 20:06:09 +02:00
$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 ); ?> " >
2015-01-20 20:03:23 +01:00
< a class = " entry-format " href = " <?php echo esc_url( get_post_format_link( $format ) ); ?> " >< ? php echo esc_html ( $title ); ?> </a>
2013-08-22 20:06:09 +02:00
</ h1 >
< ol >
2014-02-07 19:03:13 +01:00
< ? php
2017-12-01 00:11:00 +01:00
while ( $ephemera -> have_posts () ) :
$ephemera -> the_post ();
$tmp_more = $GLOBALS [ 'more' ];
$GLOBALS [ 'more' ] = 0 ;
2018-08-17 03:51:36 +02:00
?>
2013-08-22 20:06:09 +02:00
< li >
2013-10-30 15:39:10 +01:00
< article < ? php post_class (); ?> >
2017-12-01 00:11:00 +01: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 15:39:10 +01:00
'post_mime_type' => 'image' ,
2017-12-01 00:11:00 +01:00
'post_parent' => get_the_ID (),
'post_type' => 'attachment' ,
)
);
endif ;
2013-10-30 15:39:10 +01:00
2017-12-01 00:11:00 +01:00
$total_images = count ( $images );
2013-10-30 15:39:10 +01:00
2017-12-01 00:11:00 +01:00
if ( has_post_thumbnail () ) :
$post_thumbnail = get_the_post_thumbnail ();
2013-10-30 15:39:10 +01:00
elseif ( $total_images > 0 ) :
2015-03-20 03:04:27 +01:00
$image = reset ( $images );
2013-10-30 15:39:10 +01:00
$post_thumbnail = wp_get_attachment_image ( $image , 'post-thumbnail' );
endif ;
2017-12-01 00:11:00 +01:00
if ( ! empty ( $post_thumbnail ) ) :
2018-08-17 03:51:36 +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 " >
2018-08-17 03:51:36 +02: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 22:15:10 +01:00
</ p >
2018-08-17 03:51:36 +02:00
< ? php
2017-12-01 00:11:00 +01:00
endif ;
2013-10-30 15:39:10 +01:00
2013-08-22 20:06:09 +02:00
else :
the_content ( __ ( 'Continue reading <span class="meta-nav">→</span>' , 'twentyfourteen' ) );
endif ;
2018-08-17 03:51:36 +02:00
?>
2013-08-22 20:06:09 +02:00
</ div ><!-- . entry - content -->
< header class = " entry-header " >
< div class = " entry-meta " >
2017-12-01 00:11:00 +01: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 20:06:09 +02:00
2017-12-01 00:11:00 +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>' ,
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 20:06:09 +02:00
2017-12-01 00:11:00 +01:00
if ( ! post_password_required () && ( comments_open () || get_comments_number () ) ) :
2013-08-22 20:06:09 +02: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 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 ) : ?>
2015-01-20 20:03:23 +01: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 20:06:09 +02:00
< ? php endforeach ; ?>
</ select >
< ? php
}
}