$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 Array of post IDs
*/
public static function get_featured_post_ids() {
// 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 );
}
$settings = self::get_setting();
// Return sticky post ids if no tag name is set.
$term = get_term_by( 'name', $settings['tag-name'], 'post_tag' );
if ( $term ) {
$tag = $term->term_id;
} else {
return self::get_sticky_posts();
}
// Query for featured posts.
$featured = get_posts( array(
'numberposts' => $settings['quantity'],
'tax_query' => array(
array(
'field' => 'term_id',
'taxonomy' => 'post_tag',
'terms' => $tag,
),
),
) );
// Return array with sticky posts if no Featured Content exists.
if ( ! $featured ) {
return self::get_sticky_posts();
}
// 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;
}
/**
* Returns an array with IDs of posts maked as sticky.
*
* @return array
*/
public static function get_sticky_posts() {
$settings = self::get_setting();
return array_slice( get_option( 'sticky_posts', array() ), 0, $settings['quantity'] );
}
/**
* 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 ) {
// Bail if not home or not main query.
if ( ! $query->is_home() || ! $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'] ) || $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() {
register_setting( 'featured-content', 'featured-content', array( __CLASS__, 'validate_settings' ) );
}
/**
* Add settings to the Customizer.
*
* @param WP_Customize_Manager $wp_customize Theme Customizer object.
*/
public static function customize_register( $wp_customize ) {
$wp_customize->add_section( 'featured_content', array(
'title' => __( 'Featured Content', 'twentyfourteen' ),
'description' => sprintf( __( 'Easily feature all posts with the "featured" tag or a tag of your choice; if no posts match the tag, "sticky" posts will be displayed instead.', 'twentyfourteen' ), admin_url( '/edit.php?tag=featured' ), admin_url( '/edit.php?show_sticky=1' ) ),
'priority' => 130,
'theme_supports' => 'featured-content',
) );
// Add Featured Content settings.
$wp_customize->add_setting( 'featured-content[tag-name]', array(
'default' => 'featured',
'type' => 'option',
'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ),
) );
$wp_customize->add_setting( 'featured-content[hide-tag]', array(
'default' => true,
'type' => 'option',
'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ),
) );
// Add Featured Content controls.
$wp_customize->add_control( 'featured-content[tag-name]', array(
'label' => __( 'Tag name', 'twentyfourteen' ),
'section' => 'featured_content',
'priority' => 20,
) );
$wp_customize->add_control( 'featured-content[hide-tag]', array(
'label' => __( 'Hide tag from displaying in post meta and tag clouds.', 'twentyfourteen' ),
'section' => 'featured_content',
'type' => 'checkbox',
'priority' => 30,
) );
}
/**
* Enqueue the tag suggestion script.
*
* @since Twenty Fourteen 1.0
*/
public static function enqueue_scripts() {
wp_enqueue_script( 'featured-content-suggest', get_template_directory_uri() . '/js/featured-content-admin.js', array( 'jquery', 'suggest' ), '20131022', true );
}
/**
* Get settings
*
* Get all settings recognized by this module. This function will return
* all settings whether or not they have been stored in the database yet.
* This ensures that all keys are available at all times.
*
* In the event that you only require one setting, you may pass its name
* as the first parameter to the function and only that value will be returned.
*
* @uses Featured_Content::sanitize_quantity()
*
* @param string $key The key of a recognized setting.
* @return mixed Array of all settings by default. A single value if passed as first parameter.
*/
public static function get_setting( $key = 'all' ) {
$saved = (array) get_option( 'featured-content' );
$defaults = array(
'hide-tag' => 1,
'quantity' => 6,
'tag-id' => 0,
'tag-name' => 'featured',
);
$options = wp_parse_args( $saved, $defaults );
$options = array_intersect_key( $options, $defaults );
$options['quantity'] = self::sanitize_quantity( $options['quantity'] );
if ( 'all' != $key ) {
return isset( $options[ $key ] ) ? $options[ $key ] : 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 ( empty( $input['tag-name'] ) ) {
$output['tag-id'] = 0;
} else {
$new_tag = wp_create_tag( $input['tag-name'] );
if ( ! is_wp_error( $new_tag ) && isset( $new_tag['term_id'] ) ) {
$output['tag-id'] = $new_tag['term_id'];
} else {
$term = get_term_by( 'name', $input['tag-name'], 'post_tag' );
$output['tag-id'] = $term ? $term->term_id : 0;
}
$output['tag-name'] = $input['tag-name'];
}
if ( isset( $input['quantity'] ) ) {
$output['quantity'] = self::sanitize_quantity( $input['quantity'] );
}
$output['hide-tag'] = isset( $input['hide-tag'] ) && $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();