3 * Twenty Fourteen Featured Content
5 * This module allows you to define a subset of posts to be displayed
6 * in the theme's Featured Content area.
8 * For maximum compatibility with different methods of posting users
9 * will designate a featured post tag to associate posts with. Since
10 * this tag now has special meaning beyond that of a normal tags, users
11 * will have the ability to hide it from the front-end of their site.
13 class Featured_Content {
16 * The maximum number of posts a Featured Content area can contain.
18 * We define a default value here but themes can override
19 * this by defining a "max_posts" entry in the second parameter
20 * passed in the call to add_theme_support( 'featured-content' ).
22 * @see Featured_Content::init()
24 * @since Twenty Fourteen 1.0
30 public static $max_posts = 15;
35 * All custom functionality will be hooked into the "init" action.
39 * @since Twenty Fourteen 1.0
41 public static function setup() {
42 add_action( 'init', array( __CLASS__, 'init' ), 30 );
46 * Conditionally hook into WordPress.
48 * Theme must declare that they support this module by adding
49 * add_theme_support( 'featured-content' ); during after_setup_theme.
51 * If no theme support is found there is no need to hook into WordPress.
52 * We'll just return early instead.
56 * @since Twenty Fourteen 1.0
58 public static function init() {
59 $theme_support = get_theme_support( 'featured-content' );
61 // Return early if theme does not support Featured Content.
62 if ( ! $theme_support ) {
67 * An array of named arguments must be passed as the second parameter
68 * of add_theme_support().
70 if ( ! isset( $theme_support[0] ) ) {
74 // Return early if "featured_content_filter" has not been defined.
75 if ( ! isset( $theme_support[0]['featured_content_filter'] ) ) {
79 $filter = $theme_support[0]['featured_content_filter'];
81 // Theme can override the number of max posts.
82 if ( isset( $theme_support[0]['max_posts'] ) ) {
83 self::$max_posts = absint( $theme_support[0]['max_posts'] );
86 add_filter( $filter, array( __CLASS__, 'get_featured_posts' ) );
87 add_action( 'customize_register', array( __CLASS__, 'customize_register' ), 9 );
88 add_action( 'admin_init', array( __CLASS__, 'register_setting' ) );
89 add_action( 'switch_theme', array( __CLASS__, 'delete_transient' ) );
90 add_action( 'save_post', array( __CLASS__, 'delete_transient' ) );
91 add_action( 'delete_post_tag', array( __CLASS__, 'delete_post_tag' ) );
92 add_action( 'customize_controls_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) );
93 add_action( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ) );
94 add_action( 'wp_loaded', array( __CLASS__, 'wp_loaded' ) );
98 * Hide "featured" tag from the front-end.
100 * Has to run on wp_loaded so that the preview filters of the customizer
101 * have a chance to alter the value.
105 * @since Twenty Fourteen 1.0
107 public static function wp_loaded() {
108 if ( self::get_setting( 'hide-tag' ) ) {
109 add_filter( 'get_terms', array( __CLASS__, 'hide_featured_term' ), 10, 3 );
110 add_filter( 'get_the_terms', array( __CLASS__, 'hide_the_featured_term' ), 10, 3 );
115 * Get featured posts.
119 * @since Twenty Fourteen 1.0
121 * @return array Array of featured posts.
123 public static function get_featured_posts() {
124 $post_ids = self::get_featured_post_ids();
126 // No need to query if there is are no featured posts.
127 if ( empty( $post_ids ) ) {
131 $featured_posts = get_posts( array(
132 'include' => $post_ids,
133 'posts_per_page' => count( $post_ids ),
136 return $featured_posts;
140 * Get featured post IDs
142 * This function will return the an array containing the
143 * post IDs of all featured posts.
145 * Sets the "featured_content_ids" transient.
149 * @since Twenty Fourteen 1.0
151 * @return array Array of post IDs.
153 public static function get_featured_post_ids() {
154 // Get array of cached results if they exist.
155 $featured_ids = get_transient( 'featured_content_ids' );
157 if ( false === $featured_ids ) {
158 $settings = self::get_setting();
159 $term = get_term_by( 'name', $settings['tag-name'], 'post_tag' );
162 // Query for featured posts.
163 $featured_ids = get_posts( array(
165 'numberposts' => self::$max_posts,
166 'suppress_filters' => false,
167 'tax_query' => array(
169 'field' => 'term_id',
170 'taxonomy' => 'post_tag',
171 'terms' => $term->term_id,
177 // Get sticky posts if no Featured Content exists.
178 if ( ! $featured_ids ) {
179 $featured_ids = self::get_sticky_posts();
182 set_transient( 'featured_content_ids', $featured_ids );
185 // Ensure correct format before return.
186 return array_map( 'absint', $featured_ids );
190 * Return an array with IDs of posts maked as sticky.
194 * @since Twenty Fourteen 1.0
196 * @return array Array of sticky posts.
198 public static function get_sticky_posts() {
199 return array_slice( get_option( 'sticky_posts', array() ), 0, self::$max_posts );
203 * Delete featured content ids transient.
205 * Hooks in the "save_post" action.
207 * @see Featured_Content::validate_settings().
211 * @since Twenty Fourteen 1.0
213 public static function delete_transient() {
214 delete_transient( 'featured_content_ids' );
218 * Exclude featured posts from the home page blog query.
220 * Filter the home page posts, and remove any featured post ID's from it.
221 * Hooked onto the 'pre_get_posts' action, this changes the parameters of
222 * the query before it gets any posts.
226 * @since Twenty Fourteen 1.0
228 * @param WP_Query $query WP_Query object.
229 * @return WP_Query Possibly-modified WP_Query.
231 public static function pre_get_posts( $query ) {
233 // Bail if not home or not main query.
234 if ( ! $query->is_home() || ! $query->is_main_query() ) {
238 $page_on_front = get_option( 'page_on_front' );
240 // Bail if the blog page is not the front page.
241 if ( ! empty( $page_on_front ) ) {
245 $featured = self::get_featured_post_ids();
247 // Bail if no featured posts.
252 // We need to respect post ids already in the blacklist.
253 $post__not_in = $query->get( 'post__not_in' );
255 if ( ! empty( $post__not_in ) ) {
256 $featured = array_merge( (array) $post__not_in, $featured );
257 $featured = array_unique( $featured );
260 $query->set( 'post__not_in', $featured );
264 * Reset tag option when the saved tag is deleted.
266 * It's important to mention that the transient needs to be deleted,
267 * too. While it may not be obvious by looking at the function alone,
268 * the transient is deleted by Featured_Content::validate_settings().
270 * Hooks in the "delete_post_tag" action.
272 * @see Featured_Content::validate_settings().
276 * @since Twenty Fourteen 1.0
278 * @param int $tag_id The term_id of the tag that has been deleted.
280 public static function delete_post_tag( $tag_id ) {
281 $settings = self::get_setting();
283 if ( empty( $settings['tag-id'] ) || $tag_id != $settings['tag-id'] ) {
287 $settings['tag-id'] = 0;
288 $settings = self::validate_settings( $settings );
289 update_option( 'featured-content', $settings );
293 * Hide featured tag from displaying when global terms are queried from the front-end.
295 * Hooks into the "get_terms" filter.
299 * @since Twenty Fourteen 1.0
301 * @param array $terms List of term objects. This is the return value of get_terms().
302 * @param array $taxonomies An array of taxonomy slugs.
303 * @return array A filtered array of terms.
305 * @uses Featured_Content::get_setting()
307 public static function hide_featured_term( $terms, $taxonomies, $args ) {
309 // This filter is only appropriate on the front-end.
314 // We only want to hide the featured tag.
315 if ( ! in_array( 'post_tag', $taxonomies ) ) {
319 // Bail if no terms were returned.
320 if ( empty( $terms ) ) {
324 // Bail if term objects are unavailable.
325 if ( 'all' != $args['fields'] ) {
329 $settings = self::get_setting();
330 foreach( $terms as $order => $term ) {
331 if ( ( $settings['tag-id'] === $term->term_id || $settings['tag-name'] === $term->name ) && 'post_tag' === $term->taxonomy ) {
332 unset( $terms[ $order ] );
340 * Hide featured tag from display when terms associated with a post object
341 * are queried from the front-end.
343 * Hooks into the "get_the_terms" filter.
347 * @since Twenty Fourteen 1.0
349 * @param array $terms A list of term objects. This is the return value of get_the_terms().
350 * @param int $id The ID field for the post object that terms are associated with.
351 * @param array $taxonomy An array of taxonomy slugs.
352 * @return array Filtered array of terms.
354 * @uses Featured_Content::get_setting()
356 public static function hide_the_featured_term( $terms, $id, $taxonomy ) {
358 // This filter is only appropriate on the front-end.
363 // Make sure we are in the correct taxonomy.
364 if ( 'post_tag' != $taxonomy ) {
368 // No terms? Return early!
369 if ( empty( $terms ) ) {
373 $settings = self::get_setting();
374 foreach( $terms as $order => $term ) {
375 if ( ( $settings['tag-id'] === $term->term_id || $settings['tag-name'] === $term->name ) && 'post_tag' === $term->taxonomy ) {
376 unset( $terms[ $term->term_id ] );
384 * Register custom setting on the Settings -> Reading screen.
388 * @since Twenty Fourteen 1.0
390 public static function register_setting() {
391 register_setting( 'featured-content', 'featured-content', array( __CLASS__, 'validate_settings' ) );
395 * Add settings to the Customizer.
399 * @since Twenty Fourteen 1.0
401 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
403 public static function customize_register( $wp_customize ) {
404 $wp_customize->add_section( 'featured_content', array(
405 'title' => __( 'Featured Content', 'twentyfourteen' ),
406 'description' => sprintf( __( 'Use a <a href="%1$s">tag</a> to feature your posts. If no posts match the tag, <a href="%2$s">sticky posts</a> will be displayed instead.', 'twentyfourteen' ),
407 esc_url( add_query_arg( 'tag', _x( 'featured', 'featured content default tag slug', 'twentyfourteen' ), admin_url( 'edit.php' ) ) ),
408 admin_url( 'edit.php?show_sticky=1' )
411 'theme_supports' => 'featured-content',
414 // Add Featured Content settings.
415 $wp_customize->add_setting( 'featured-content[tag-name]', array(
416 'default' => _x( 'featured', 'featured content default tag slug', 'twentyfourteen' ),
418 'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ),
420 $wp_customize->add_setting( 'featured-content[hide-tag]', array(
423 'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ),
426 // Add Featured Content controls.
427 $wp_customize->add_control( 'featured-content[tag-name]', array(
428 'label' => __( 'Tag Name', 'twentyfourteen' ),
429 'section' => 'featured_content',
432 $wp_customize->add_control( 'featured-content[hide-tag]', array(
433 'label' => __( 'Don’t display tag on front end.', 'twentyfourteen' ),
434 'section' => 'featured_content',
435 'type' => 'checkbox',
441 * Enqueue the tag suggestion script.
445 * @since Twenty Fourteen 1.0
447 public static function enqueue_scripts() {
448 wp_enqueue_script( 'featured-content-suggest', get_template_directory_uri() . '/js/featured-content-admin.js', array( 'jquery', 'suggest' ), '20131022', true );
452 * Get featured content settings.
454 * Get all settings recognized by this module. This function
455 * will return all settings whether or not they have been stored
456 * in the database yet. This ensures that all keys are available
459 * In the event that you only require one setting, you may pass
460 * its name as the first parameter to the function and only that
461 * value will be returned.
465 * @since Twenty Fourteen 1.0
467 * @param string $key The key of a recognized setting.
468 * @return mixed Array of all settings by default. A single value if passed as first parameter.
470 public static function get_setting( $key = 'all' ) {
471 $saved = (array) get_option( 'featured-content' );
476 'tag-name' => _x( 'featured', 'featured content default tag slug', 'twentyfourteen' ),
479 $options = wp_parse_args( $saved, $defaults );
480 $options = array_intersect_key( $options, $defaults );
482 if ( 'all' != $key ) {
483 return isset( $options[ $key ] ) ? $options[ $key ] : false;
490 * Validate featured content settings.
492 * Make sure that all user supplied content is in an expected
493 * format before saving to the database. This function will also
494 * delete the transient set in Featured_Content::get_featured_content().
498 * @since Twenty Fourteen 1.0
500 * @param array $input Array of settings input.
501 * @return array Validated settings output.
503 public static function validate_settings( $input ) {
506 if ( empty( $input['tag-name'] ) ) {
507 $output['tag-id'] = 0;
509 $term = get_term_by( 'name', $input['tag-name'], 'post_tag' );
512 $output['tag-id'] = $term->term_id;
514 $new_tag = wp_create_tag( $input['tag-name'] );
516 if ( ! is_wp_error( $new_tag ) && isset( $new_tag['term_id'] ) ) {
517 $output['tag-id'] = $new_tag['term_id'];
521 $output['tag-name'] = $input['tag-name'];
524 $output['hide-tag'] = isset( $input['hide-tag'] ) && $input['hide-tag'] ? 1 : 0;
526 // Delete the featured post ids transient.
527 self::delete_transient();
531 } // Featured_Content
533 Featured_Content::setup();