123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531 |
- <?php
- class Featured_Content {
-
- public static $max_posts = 15;
-
- public static function setup() {
- add_action( 'init', array( __CLASS__, 'init' ), 30 );
- }
-
- public static function init() {
- $theme_support = get_theme_support( 'featured-content' );
-
- if ( ! $theme_support ) {
- return;
- }
-
- if ( ! isset( $theme_support[0] ) ) {
- return;
- }
-
- if ( ! isset( $theme_support[0]['featured_content_filter'] ) ) {
- return;
- }
- $filter = $theme_support[0]['featured_content_filter'];
-
- if ( isset( $theme_support[0]['max_posts'] ) ) {
- self::$max_posts = absint( $theme_support[0]['max_posts'] );
- }
- add_filter( $filter, array( __CLASS__, 'get_featured_posts' ) );
- add_action( 'customize_register', array( __CLASS__, 'customize_register' ), 9 );
- add_action( 'admin_init', array( __CLASS__, 'register_setting' ) );
- add_action( 'switch_theme', array( __CLASS__, 'delete_transient' ) );
- add_action( 'save_post', array( __CLASS__, 'delete_transient' ) );
- add_action( 'delete_post_tag', array( __CLASS__, 'delete_post_tag' ) );
- add_action( 'customize_controls_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) );
- add_action( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ) );
- add_action( 'wp_loaded', array( __CLASS__, 'wp_loaded' ) );
- }
-
- public static function wp_loaded() {
- if ( self::get_setting( 'hide-tag' ) ) {
- add_filter( 'get_terms', array( __CLASS__, 'hide_featured_term' ), 10, 3 );
- add_filter( 'get_the_terms', array( __CLASS__, 'hide_the_featured_term' ), 10, 3 );
- }
- }
-
- public static function get_featured_posts() {
- $post_ids = self::get_featured_post_ids();
-
- if ( empty( $post_ids ) ) {
- return array();
- }
- $featured_posts = get_posts( array(
- 'include' => $post_ids,
- 'posts_per_page' => count( $post_ids ),
- ) );
- return $featured_posts;
- }
-
- public static function get_featured_post_ids() {
-
- $featured_ids = get_transient( 'featured_content_ids' );
- if ( false === $featured_ids ) {
- $settings = self::get_setting();
- $term = get_term_by( 'name', $settings['tag-name'], 'post_tag' );
- if ( $term ) {
-
- $featured_ids = get_posts( array(
- 'fields' => 'ids',
- 'numberposts' => self::$max_posts,
- 'suppress_filters' => false,
- 'tax_query' => array(
- array(
- 'field' => 'term_id',
- 'taxonomy' => 'post_tag',
- 'terms' => $term->term_id,
- ),
- ),
- ) );
- }
-
- if ( ! $featured_ids ) {
- $featured_ids = self::get_sticky_posts();
- }
- set_transient( 'featured_content_ids', $featured_ids );
- }
-
- return array_map( 'absint', $featured_ids );
- }
-
- public static function get_sticky_posts() {
- return array_slice( get_option( 'sticky_posts', array() ), 0, self::$max_posts );
- }
-
- public static function delete_transient() {
- delete_transient( 'featured_content_ids' );
- }
-
- public static function pre_get_posts( $query ) {
-
- if ( ! $query->is_home() || ! $query->is_main_query() ) {
- return;
- }
-
- if ( 'posts' !== get_option( 'show_on_front' ) ) {
- return;
- }
- $featured = self::get_featured_post_ids();
-
- if ( ! $featured ) {
- return;
- }
-
- $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 );
- }
-
- 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 );
- }
-
- public static function hide_featured_term( $terms, $taxonomies, $args ) {
-
- if ( is_admin() ) {
- return $terms;
- }
-
- if ( ! in_array( 'post_tag', $taxonomies ) ) {
- return $terms;
- }
-
- if ( empty( $terms ) ) {
- return $terms;
- }
-
- if ( 'all' != $args['fields'] ) {
- return $terms;
- }
- $settings = self::get_setting();
- foreach ( $terms as $order => $term ) {
- if ( ( $settings['tag-id'] === $term->term_id || $settings['tag-name'] === $term->name ) && 'post_tag' === $term->taxonomy ) {
- unset( $terms[ $order ] );
- }
- }
- return $terms;
- }
-
- public static function hide_the_featured_term( $terms, $id, $taxonomy ) {
-
- if ( is_admin() ) {
- return $terms;
- }
-
- if ( 'post_tag' != $taxonomy ) {
- return $terms;
- }
-
- if ( empty( $terms ) ) {
- return $terms;
- }
- $settings = self::get_setting();
- foreach ( $terms as $order => $term ) {
- if ( ( $settings['tag-id'] === $term->term_id || $settings['tag-name'] === $term->name ) && 'post_tag' === $term->taxonomy ) {
- unset( $terms[ $term->term_id ] );
- }
- }
- return $terms;
- }
-
- public static function register_setting() {
- register_setting( 'featured-content', 'featured-content', array( __CLASS__, 'validate_settings' ) );
- }
-
- public static function customize_register( $wp_customize ) {
- $wp_customize->add_section( 'featured_content', array(
- 'title' => __( 'Featured Content', 'twentyfourteen' ),
- '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' ),
- esc_url( add_query_arg( 'tag', _x( 'featured', 'featured content default tag slug', 'twentyfourteen' ), admin_url( 'edit.php' ) ) ),
- admin_url( 'edit.php?show_sticky=1' )
- ),
- 'priority' => 130,
- 'theme_supports' => 'featured-content',
- ) );
-
- $wp_customize->add_setting( 'featured-content[tag-name]', array(
- 'default' => _x( 'featured', 'featured content default tag slug', 'twentyfourteen' ),
- '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' ),
- ) );
-
- $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' => __( 'Don’t display tag on front end.', 'twentyfourteen' ),
- 'section' => 'featured_content',
- 'type' => 'checkbox',
- 'priority' => 30,
- ) );
- }
-
- 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 );
- }
-
- public static function get_setting( $key = 'all' ) {
- $saved = (array) get_option( 'featured-content' );
- $defaults = array(
- 'hide-tag' => 1,
- 'tag-id' => 0,
- 'tag-name' => _x( 'featured', 'featured content default tag slug', 'twentyfourteen' ),
- );
- $options = wp_parse_args( $saved, $defaults );
- $options = array_intersect_key( $options, $defaults );
- if ( 'all' != $key ) {
- return isset( $options[ $key ] ) ? $options[ $key ] : false;
- }
- return $options;
- }
-
- public static function validate_settings( $input ) {
- $output = array();
- if ( empty( $input['tag-name'] ) ) {
- $output['tag-id'] = 0;
- } else {
- $term = get_term_by( 'name', $input['tag-name'], 'post_tag' );
- if ( $term ) {
- $output['tag-id'] = $term->term_id;
- } 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'];
- }
- }
- $output['tag-name'] = $input['tag-name'];
- }
- $output['hide-tag'] = isset( $input['hide-tag'] ) && $input['hide-tag'] ? 1 : 0;
-
- self::delete_transient();
- return $output;
- }
- }
- Featured_Content::setup();
|