From 34edaed317c1ae351858214c712cbcf335a7dd29 Mon Sep 17 00:00:00 2001 From: Lance Willett Date: Wed, 16 Oct 2013 04:22:09 +0000 Subject: [PATCH] Twenty Fourteen: first pass at implementing Featured Content settings, props obenland. See #25549. Built from https://develop.svn.wordpress.org/trunk@25808 git-svn-id: http://core.svn.wordpress.org/trunk@25720 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../themes/twentyfourteen/functions.php | 38 +- .../twentyfourteen/inc/featured-content.php | 446 ++++++++++++++++++ 2 files changed, 472 insertions(+), 12 deletions(-) create mode 100644 wp-content/themes/twentyfourteen/inc/featured-content.php diff --git a/wp-content/themes/twentyfourteen/functions.php b/wp-content/themes/twentyfourteen/functions.php index b42f50f76f..b35c3c6723 100644 --- a/wp-content/themes/twentyfourteen/functions.php +++ b/wp-content/themes/twentyfourteen/functions.php @@ -101,6 +101,14 @@ function twentyfourteen_setup() { 'default-color' => 'f5f5f5', ) ) ); + /* + * Add support for featured content. + */ + add_theme_support( 'featured-content', array( + 'featured_content_filter' => 'twentyfourteen_get_featured_posts', + 'max_posts' => 6, + ) ); + /* * This theme uses its own gallery styles. */ @@ -126,25 +134,22 @@ add_action( 'template_redirect', 'twentyfourteen_content_width' ); * Getter function for Featured Content Plugin. * * @since Twenty Fourteen 1.0 + * + * @return array An array of WP_Post objects. */ function twentyfourteen_get_featured_posts() { - return apply_filters( 'twentyfourteen_get_featured_posts', false ); + return apply_filters( 'twentyfourteen_get_featured_posts', array() ); } /** - * A helper conditional function that returns a boolean value - * So that we can use a condition like - * if ( twentyfourteen_has_featured_posts( 1 ) ) + * A helper conditional function that returns a boolean value. * * @since Twenty Fourteen 1.0 + * + * @return bool Whether there are featured posts. */ -function twentyfourteen_has_featured_posts( $minimum = 1 ) { - if ( is_paged() ) - return false; - - $featured_posts = apply_filters( 'twentyfourteen_get_featured_posts', array() ); - - return is_array( $featured_posts ) && count( $featured_posts ) > absint( $minimum ); +function twentyfourteen_has_featured_posts() { + return ! is_paged() && (bool) apply_filters( 'twentyfourteen_get_featured_posts', false ); } /** @@ -352,7 +357,7 @@ function twentyfourteen_list_authors() { endif; /** - * Get recent formatted posts that are not featured in FC plugin. + * Get recent formatted posts that are not featured in Featured Content area. * * @since Twenty Fourteen 1.0 * @@ -521,3 +526,12 @@ require get_template_directory() . '/inc/template-tags.php'; // Add Theme Customizer functionality. require get_template_directory() . '/inc/customizer.php'; + +/* + * Add Featured Content functionality. + * + * To overwrite in a plugin, define your own Featured_Content class on or + * before the 'setup_theme' hook. + */ +if ( ! class_exists( 'Featured_Content' ) && 'plugins.php' !== $GLOBALS['pagenow'] ) + require get_template_directory() . '/inc/featured-content.php'; diff --git a/wp-content/themes/twentyfourteen/inc/featured-content.php b/wp-content/themes/twentyfourteen/inc/featured-content.php new file mode 100644 index 0000000000..e1393f7551 --- /dev/null +++ b/wp-content/themes/twentyfourteen/inc/featured-content.php @@ -0,0 +1,446 @@ + $post_ids, + 'posts_per_page' => count( $post_ids ) + ) ); + + return $featured_posts; + } + + /** + * Get featured post IDs + * + * This function will return the an array containing the + * post IDs of all featured posts. + * + * Sets the "featured_content_ids" transient. + * + * @return array|false Array of post IDs. false if user has disabled this feature. + */ + public static function get_featured_post_ids() { + $settings = self::get_setting(); + + // Return false if the user has disabled this feature. + $tag = $settings['tag-id']; + if ( empty( $tag ) ) + return false; + + // Return array of cached results if they exist. + $featured_ids = get_transient( 'featured_content_ids' ); + if ( ! empty( $featured_ids ) ) + return array_map( 'absint', (array) $featured_ids ); + + // Query for featured posts. + $featured = get_posts( array( + 'numberposts' => $settings['quantity'], + 'tax_query' => array( + array( + 'field' => 'term_id', + 'taxonomy' => 'post_tag', + 'terms' => $tag, + ), + ), + ) ); + + // Return empty array if no Featured Content exists. + if ( ! $featured ) + return array(); + + // Ensure correct format before save/return. + $featured_ids = wp_list_pluck( (array) $featured, 'ID' ); + $featured_ids = array_map( 'absint', $featured_ids ); + + set_transient( 'featured_content_ids', $featured_ids ); + + return $featured_ids; + } + + /** + * Delete transient + * + * Hooks in the "save_post" action. + * @see Featured_Content::validate_settings(). + */ + public static function delete_transient() { + delete_transient( 'featured_content_ids' ); + } + + /** + * Exclude featured posts from the home page blog query + * + * Filter the home page posts, and remove any featured post ID's from it. Hooked + * onto the 'pre_get_posts' action, this changes the parameters of the query + * before it gets any posts. + * + * @uses Featured_Content::get_featured_post_ids(); + * @param WP_Query $query + * @return WP_Query Possibly modified WP_query + */ + public static function pre_get_posts( $query = false ) { + + // Bail if not home, not a query, not main query. + if ( ! is_home() || ! is_a( $query, 'WP_Query' ) || ! $query->is_main_query() ) + return; + + $page_on_front = get_option( 'page_on_front' ); + + // Bail if the blog page is not the front page. + if ( ! empty( $page_on_front ) ) + return; + + $featured = self::get_featured_post_ids(); + + // Bail if no featured posts. + if ( ! $featured ) + return; + + // We need to respect post ids already in the blacklist. + $post__not_in = $query->get( 'post__not_in' ); + + if ( ! empty( $post__not_in ) ) { + $featured = array_merge( (array) $post__not_in, $featured ); + $featured = array_unique( $featured ); + } + + $query->set( 'post__not_in', $featured ); + } + + /** + * Reset tag option when the saved tag is deleted + * + * It's important to mention that the transient needs to be deleted, too. + * While it may not be obvious by looking at the function alone, the transient + * is deleted by Featured_Content::validate_settings(). + * + * Hooks in the "delete_post_tag" action. + * @see Featured_Content::validate_settings(). + * + * @param int $tag_id the term_id of the tag that has been deleted. + * @return void + */ + public static function delete_post_tag( $tag_id ) { + $settings = self::get_setting(); + + if ( empty( $settings['tag-id'] ) ) + return; + + if ( $tag_id != $settings['tag-id'] ) + return; + + $settings['tag-id'] = 0; + $settings = self::validate_settings( $settings ); + update_option( 'featured-content', $settings ); + } + + /** + * Hide featured tag from displaying when global terms are queried from the front-end + * + * Hooks into the "get_terms" filter. + * + * @param array $terms A list of term objects. This is the return value of get_terms(). + * @param array $taxonomies An array of taxonomy slugs. + * @return array $terms + * + * @uses Featured_Content::get_setting() + */ + public static function hide_featured_term( $terms, $taxonomies ) { + + // This filter is only appropriate on the front-end. + if ( is_admin() ) + return $terms; + + // We only want to hide the featured tag. + if ( ! in_array( 'post_tag', $taxonomies ) ) + return $terms; + + // Bail if no terms were returned. + if ( empty( $terms ) ) + return $terms; + + foreach( $terms as $order => $term ) { + if ( self::get_setting( 'tag-id' ) == $term->term_id && 'post_tag' == $term->taxonomy ) + unset( $terms[$order] ); + } + + return $terms; + } + + /** + * Hide featured tag from display when terms associated with a post object are queried from the front-end + * + * Hooks into the "get_the_terms" filter. + * + * @param array $terms A list of term objects. This is the return value of get_the_terms(). + * @param int $id The ID field for the post object that terms are associated with. + * @param array $taxonomy An array of taxonomy slugs. + * @return array $terms + * + * @uses Featured_Content::get_setting() + */ + public static function hide_the_featured_term( $terms, $id, $taxonomy ) { + + // This filter is only appropriate on the front-end. + if ( is_admin() ) + return $terms; + + // Make sure we are in the correct taxonomy. + if ( ! 'post_tag' == $taxonomy ) + return $terms; + + // No terms? Return early! + if ( empty( $terms ) ) + return $terms; + + foreach( $terms as $order => $term ) { + if ( self::get_setting( 'tag-id' ) == $term->term_id ) + unset( $terms[$term->term_id] ); + } + + return $terms; + } + + /** + * Register custom setting on the Settings -> Reading screen + * + * @uses Featured_Content::render_form() + * @uses Featured_Content::validate_settings() + * + * @return void + */ + public static function register_setting() { + add_settings_field( 'featured-content', __( 'Featured content', 'twentyfourteen' ), array( __class__, 'render_form' ), 'reading' ); + register_setting( 'reading', 'featured-content', array( __class__, 'validate_settings' ) ); + } + + /** + * Render the form fields for Settings -> Reading screen + * + * @return void + */ + public static function render_form() { + $settings = self::get_setting(); + + $tag_name = ''; + if ( ! empty( $settings['tag-id'] ) ) { + $tag = get_term( $settings['tag-id'], 'post_tag' ); + if ( ! is_wp_error( $tag ) && isset( $tag->name ) ) + $tag_name = $tag->name; + } + ?> + + 1, + 'quantity' => 6, + 'tag-id' => 0, + ); + + $options = wp_parse_args( $saved, $defaults ); + $options = array_intersect_key( $options, $defaults ); + $options['quantity'] = self::sanitize_quantity( $options['quantity'] ); + + if ( 'all' != $key ) { + if ( isset( $options[$key] ) ) + return $options[$key]; + else + return false; + } + + return $options; + } + + /** + * Validate settings + * + * Make sure that all user supplied content is in an + * expected format before saving to the database. This + * function will also delete the transient set in + * Featured_Content::get_featured_content(). + * + * @uses Featured_Content::self::sanitize_quantity() + * @uses Featured_Content::self::delete_transient() + * + * @param array $input + * @return array $output + */ + public static function validate_settings( $input ) { + $output = array(); + + if ( isset( $input['tag-id'] ) ) + $output['tag-id'] = absint( $input['tag-id'] ); + + if ( isset( $input['tag-name'] ) && ! empty( $input['tag-name'] ) ) { + $new_tag = wp_create_tag( $input['tag-name'] ); + if ( ! is_wp_error( $new_tag ) && isset( $new_tag['term_id'] ) ) + $tag = get_term( $new_tag['term_id'], 'post_tag' ); + if ( isset( $tag->term_id ) ) + $output['tag-id'] = $tag->term_id; + } else { + unset( $output['tag-id'] ); + } + + if ( isset( $input['quantity'] ) ) + $output['quantity'] = self::sanitize_quantity( $input['quantity'] ); + + $output['hide-tag'] = ( isset( $input['hide-tag'] ) ) ? 1 : 0; + + self::delete_transient(); + + return $output; + } + + /** + * Sanitize quantity + * + * @param int $input The value to sanitize. + * @return int A number between 1 and FeaturedContent::$max_posts. + * + * @uses Featured_Content::$max_posts + */ + public static function sanitize_quantity( $input ) { + $quantity = absint( $input ); + + if ( $quantity > self::$max_posts ) + $quantity = self::$max_posts; + else if ( 1 > $quantity ) + $quantity = 1; + + return $quantity; + } +} + +Featured_Content::setup();