From 945b12bfcf555a7cdcf23cfa046846c9e8af7f4b Mon Sep 17 00:00:00 2001 From: Lance Willett Date: Thu, 22 Aug 2013 18:06:09 +0000 Subject: [PATCH] Twenty Fourteen: widgetize the Post Format front page so other widgets can be used there, and the special Post Format content can be used in other widget areas. Props obenland, see #25028. Built from https://develop.svn.wordpress.org/trunk@25090 git-svn-id: http://core.svn.wordpress.org/trunk@25074 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../themes/twentyfourteen/front-page.php | 2 +- .../themes/twentyfourteen/functions.php | 47 ++-- .../themes/twentyfourteen/inc/widgets.php | 248 ++++++++++++++++++ .../twentyfourteen/recent-formatted-posts.php | 120 --------- wp-content/themes/twentyfourteen/rtl.css | 2 +- .../themes/twentyfourteen/sidebar-content.php | 2 +- .../twentyfourteen/sidebar-ephemera.php | 27 ++ .../themes/twentyfourteen/sidebar-footer.php | 12 +- wp-content/themes/twentyfourteen/style.css | 65 +++-- 9 files changed, 344 insertions(+), 181 deletions(-) create mode 100644 wp-content/themes/twentyfourteen/inc/widgets.php delete mode 100644 wp-content/themes/twentyfourteen/recent-formatted-posts.php create mode 100644 wp-content/themes/twentyfourteen/sidebar-ephemera.php diff --git a/wp-content/themes/twentyfourteen/front-page.php b/wp-content/themes/twentyfourteen/front-page.php index 0fd9a36649..1337751290 100644 --- a/wp-content/themes/twentyfourteen/front-page.php +++ b/wp-content/themes/twentyfourteen/front-page.php @@ -38,7 +38,7 @@ get_header(); ?> - + diff --git a/wp-content/themes/twentyfourteen/functions.php b/wp-content/themes/twentyfourteen/functions.php index 9ff33c8929..25966e11b6 100644 --- a/wp-content/themes/twentyfourteen/functions.php +++ b/wp-content/themes/twentyfourteen/functions.php @@ -131,31 +131,44 @@ function twentyfourteen_has_featured_posts( $minimum = 1 ) { * @return void */ function twentyfourteen_widgets_init() { + require get_template_directory() . '/inc/widgets.php'; + register_widget( 'Twenty_Fourteen_Ephemera_Widget' ); + register_sidebar( array( - 'name' => __( 'Primary Sidebar', 'twentyfourteen' ), - 'id' => 'sidebar-1', - 'description' => __( 'Main sidebar that appears on the left.', 'twentyfourteen' ), + 'name' => __( 'Primary Sidebar', 'twentyfourteen' ), + 'id' => 'sidebar-1', + 'description' => __( 'Main sidebar that appears on the left.', 'twentyfourteen' ), 'before_widget' => '', - 'before_title' => '

', - 'after_title' => '

', + 'after_widget' => '', + 'before_title' => '

', + 'after_title' => '

', ) ); register_sidebar( array( - 'name' => __( 'Content Sidebar', 'twentyfourteen' ), - 'id' => 'sidebar-2', - 'description' => __( 'Additional sidebar that appears on the right, on single posts and pages.', 'twentyfourteen' ), + 'name' => __( 'Front Page Sidebar', 'twentyfourteen' ), + 'id' => 'sidebar-2', + 'description' => __( 'Additional sidebar that appears on the right, on the home page.', 'twentyfourteen' ), 'before_widget' => '', - 'before_title' => '

', - 'after_title' => '

', + 'after_widget' => '', + 'before_title' => '

', + 'after_title' => '

', ) ); register_sidebar( array( - 'name' => __( 'Footer Widget Area', 'twentyfourteen' ), - 'id' => 'sidebar-3', + 'name' => __( 'Content Sidebar', 'twentyfourteen' ), + 'id' => 'sidebar-3', + 'description' => __( 'Additional sidebar that appears on the right, on single posts and pages.', 'twentyfourteen' ), 'before_widget' => '', - 'before_title' => '

', - 'after_title' => '

', + 'after_widget' => '', + 'before_title' => '

', + 'after_title' => '

', + ) ); + register_sidebar( array( + 'name' => __( 'Footer Widget Area', 'twentyfourteen' ), + 'id' => 'sidebar-4', + 'description' => __( 'Appears in the footer section of the site.', 'twentyfourteen' ), + 'before_widget' => '', + 'before_title' => '

', + 'after_title' => '

', ) ); } add_action( 'widgets_init', 'twentyfourteen_widgets_init' ); diff --git a/wp-content/themes/twentyfourteen/inc/widgets.php b/wp-content/themes/twentyfourteen/inc/widgets.php new file mode 100644 index 0000000000..c7c3232e39 --- /dev/null +++ b/wp-content/themes/twentyfourteen/inc/widgets.php @@ -0,0 +1,248 @@ + 'widget_twentyfourteen_ephemera', + 'description' => __( 'Use this widget to list your recent Aside, Quote, Video, Image, Gallery, and Link posts', 'twentyfourteen' ), + ) ); + + /** + * @todo http://core.trac.wordpress.org/ticket/23257 + */ + $this->format_strings = array( + 'aside' => __( 'Asides', 'twentyfourteen' ), + 'image' => __( 'Images', 'twentyfourteen' ), + 'video' => __( 'Videos', 'twentyfourteen' ), + 'quote' => __( 'Quotes', 'twentyfourteen' ), + 'link' => __( 'Links', 'twentyfourteen' ), + 'gallery' => __( 'Galleries', 'twentyfourteen' ), + ); + + add_action( 'save_post', array( $this, 'flush_widget_cache' ) ); + add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) ); + add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) ); + } + + /** + * Outputs the HTML for this widget. + * + * @param array $args An array of standard parameters for widgets in this theme. + * @param array $instance An array of settings for this widget instance. + * @return void Echoes its output. + */ + public function widget( $args, $instance ) { + // If called directly, assign an unique index for caching. + if ( -1 == $this->number ) { + static $num = -1; + $this->_set( --$num ); + } + + $content = get_transient( $this->id ); + + if ( false !== $content ) { + echo $content; + return; + } + + ob_start(); + extract( $args, EXTR_SKIP ); + + $format = $instance['format']; + $number = empty( $instance['number'] ) ? 2 : absint( $instance['number'] ); + $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? $this->format_strings[ $format ] : $instance['title'], $instance, $this->id_base ); + + $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; + + echo $before_widget; + ?> +

+ +

+
    + + have_posts() ) : $ephemera->the_post(); ?> +
  1. +
    > +
    + get_post()->post_parent, + 'fields' => 'ids', + 'numberposts' => -1, + 'post_status' => 'inherit', + 'post_type' => 'attachment', + 'post_mime_type' => 'image', + 'order' => 'ASC', + 'orderby' => 'menu_order ID' + ) ); + $total_images = count( $images ); + + if ( has_post_thumbnail() ) : + $featured_image = get_the_post_thumbnail( get_the_ID(), 'featured-thumbnail-formatted' ); + elseif ( $total_images > 0 ) : + $image = array_shift( $images ); + $featured_image = wp_get_attachment_image( $image, 'featured-thumbnail-formatted' ); + endif; + ?> + +

    + %2$s photo.', 'This gallery contains %2$s photos.', $total_images, 'twentyfourteen' ), + esc_url( get_permalink() ), + number_format_i18n( $total_images ) + ); + ?> +

    + →', 'twentyfourteen' ) ); + endif; + ?> +
    + +
    + +
    +
    +
  2. + + +
+ →', 'twentyfourteen' ), $this->format_strings[ $format ] ); ?> + id, ob_get_flush() ); + } + + /** + * Deals with the settings when they are saved by the admin. Here is where + * any validation should be dealt with. + * + * @param array $new_instance + * @param array $instance + * @return array + */ + function update( $new_instance, $instance ) { + $instance['title'] = strip_tags( $new_instance['title'] ); + $instance['number'] = empty( $new_instance['number'] ) ? 2 : absint( $new_instance['number'] ); + if ( in_array( $new_instance['format'], $this->formats ) ) + $instance['format'] = $new_instance['format']; + + $this->flush_widget_cache(); + + return $instance; + } + + /** + * Deletes the transient. + * + * @return void + */ + function flush_widget_cache() { + delete_transient( $this->id ); + } + + /** + * Displays the form for this widget on the Widgets page of the Admin area. + * + * @param array $instance + * @return void + */ + 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'; + ?> +

+

+ +

+

+ +

+ + - -

-have_posts() ) : -?> -
-

- -

- have_posts() ) : $recent_videos->the_post(); - get_template_part( 'content', 'recent-formatted-post' ); - endwhile; - ?> - →', 'twentyfourteen' ); ?> -
- - -have_posts() ) : -?> -
-

- -

- have_posts() ) : $recent_images->the_post(); - get_template_part( 'content', 'recent-formatted-post' ); - endwhile; - ?> - →', 'twentyfourteen' ); ?> -
- - -have_posts() ) : -?> -
-

- -

- have_posts() ) : $recent_galleries->the_post(); - get_template_part( 'content', 'recent-formatted-post' ); - endwhile; - ?> - →', 'twentyfourteen' ); ?> -
- - -have_posts() ) : -?> -
-

- -

- have_posts() ) : $recent_asides->the_post(); - get_template_part( 'content', 'recent-formatted-post' ); - endwhile; - ?> - →', 'twentyfourteen' ); ?> -
- - -have_posts() ) : -?> - - - -have_posts() ) : -?> -
-

- -

- have_posts() ) : $recent_quotes->the_post(); - get_template_part( 'content', 'recent-formatted-post' ); - endwhile; - ?> - →', 'twentyfourteen' ); ?> -
- - - - -
\ No newline at end of file diff --git a/wp-content/themes/twentyfourteen/rtl.css b/wp-content/themes/twentyfourteen/rtl.css index 98e6ffc8c1..d04bd5158e 100644 --- a/wp-content/themes/twentyfourteen/rtl.css +++ b/wp-content/themes/twentyfourteen/rtl.css @@ -219,7 +219,7 @@ blockquote.pull.alignleft { /* =Post Formatted posts column ----------------------------------------------- */ -.format-title:before { +.ephemera .widget-title:before { margin-left: 10px; margin-left: 1.0rem; margin-right: auto; diff --git a/wp-content/themes/twentyfourteen/sidebar-content.php b/wp-content/themes/twentyfourteen/sidebar-content.php index cfb9754598..2b3cd01296 100644 --- a/wp-content/themes/twentyfourteen/sidebar-content.php +++ b/wp-content/themes/twentyfourteen/sidebar-content.php @@ -9,7 +9,7 @@