WordPress 4.0
[autoinstalls/wordpress.git] / wp-content / themes / twentyfourteen / inc / featured-content.php
index 64858745718562866c177e6f26b895729552f7fd..ccff4cace41b6766ac28809c6161fbd3c53e1558 100644 (file)
@@ -86,6 +86,7 @@ class Featured_Content {
                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'    )    );
@@ -105,7 +106,7 @@ class Featured_Content {
         */
        public static function wp_loaded() {
                if ( self::get_setting( 'hide-tag' ) ) {
-                       add_filter( 'get_terms',     array( __CLASS__, 'hide_featured_term'     ), 10, 2 );
+                       add_filter( 'get_terms',     array( __CLASS__, 'hide_featured_term'     ), 10, 3 );
                        add_filter( 'get_the_terms', array( __CLASS__, 'hide_the_featured_term' ), 10, 3 );
                }
        }
@@ -150,46 +151,39 @@ class Featured_Content {
         * @return array Array of post IDs.
         */
        public static function get_featured_post_ids() {
-               // Return array of cached results if they exist.
+               // Get 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();
+               if ( false === $featured_ids ) {
+                       $settings = self::get_setting();
+                       $term     = get_term_by( 'name', $settings['tag-name'], 'post_tag' );
 
-               // 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();
-               }
+                       if ( $term ) {
+                               // Query for featured posts.
+                               $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,
+                                               ),
+                                       ),
+                               ) );
+                       }
 
-               // Query for featured posts.
-               $featured = get_posts( array(
-                       'numberposts' => $settings['quantity'],
-                       'tax_query'   => array(
-                               array(
-                                       'field'    => 'term_id',
-                                       'taxonomy' => 'post_tag',
-                                       'terms'    => $tag,
-                               ),
-                       ),
-               ) );
+                       // Get sticky posts if no Featured Content exists.
+                       if ( ! $featured_ids ) {
+                               $featured_ids = self::get_sticky_posts();
+                       }
 
-               // Return array with sticky posts if no Featured Content exists.
-               if ( ! $featured ) {
-                       return self::get_sticky_posts();
+                       set_transient( 'featured_content_ids', $featured_ids );
                }
 
-               // 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;
+               // Ensure correct format before return.
+               return array_map( 'absint', $featured_ids );
        }
 
        /**
@@ -202,8 +196,7 @@ class Featured_Content {
         * @return array Array of sticky posts.
         */
        public static function get_sticky_posts() {
-               $settings = self::get_setting();
-               return array_slice( get_option( 'sticky_posts', array() ), 0, $settings['quantity'] );
+               return array_slice( get_option( 'sticky_posts', array() ), 0, self::$max_posts );
        }
 
        /**
@@ -283,7 +276,6 @@ class Featured_Content {
         * @since Twenty Fourteen 1.0
         *
         * @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();
@@ -312,7 +304,7 @@ class Featured_Content {
         *
         * @uses Featured_Content::get_setting()
         */
-       public static function hide_featured_term( $terms, $taxonomies ) {
+       public static function hide_featured_term( $terms, $taxonomies, $args ) {
 
                // This filter is only appropriate on the front-end.
                if ( is_admin() ) {
@@ -329,8 +321,14 @@ class Featured_Content {
                        return $terms;
                }
 
+               // Bail if term objects are unavailable.
+               if ( 'all' != $args['fields'] ) {
+                       return $terms;
+               }
+
+               $settings = self::get_setting();
                foreach( $terms as $order => $term ) {
-                       if ( self::get_setting( 'tag-id' ) == $term->term_id && 'post_tag' == $term->taxonomy ) {
+                       if ( ( $settings['tag-id'] === $term->term_id || $settings['tag-name'] === $term->name ) && 'post_tag' === $term->taxonomy ) {
                                unset( $terms[ $order ] );
                        }
                }
@@ -372,8 +370,9 @@ class Featured_Content {
                        return $terms;
                }
 
+               $settings = self::get_setting();
                foreach( $terms as $order => $term ) {
-                       if ( self::get_setting( 'tag-id' ) == $term->term_id ) {
+                       if ( ( $settings['tag-id'] === $term->term_id || $settings['tag-name'] === $term->name ) && 'post_tag' === $term->taxonomy ) {
                                unset( $terms[ $term->term_id ] );
                        }
                }
@@ -387,8 +386,6 @@ class Featured_Content {
         * @static
         * @access public
         * @since Twenty Fourteen 1.0
-        *
-        * @return void
         */
        public static function register_setting() {
                register_setting( 'featured-content', 'featured-content', array( __CLASS__, 'validate_settings' ) );
@@ -406,14 +403,17 @@ class Featured_Content {
        public static function customize_register( $wp_customize ) {
                $wp_customize->add_section( 'featured_content', array(
                        'title'          => __( 'Featured Content', 'twentyfourteen' ),
-                       'description'    => sprintf( __( 'Use the <a href="%1$s">"featured" tag</a> to feature your posts. You can change this to a tag of your choice; if no posts match the tag, <a href="%2$s">sticky posts</a> will be displayed instead.', 'twentyfourteen' ), admin_url( '/edit.php?tag=featured' ), admin_url( '/edit.php?show_sticky=1' ) ),
+                       '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',
                ) );
 
                // Add Featured Content settings.
                $wp_customize->add_setting( 'featured-content[tag-name]', array(
-                       'default'              => 'featured',
+                       'default'              => _x( 'featured', 'featured content default tag slug', 'twentyfourteen' ),
                        'type'                 => 'option',
                        'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ),
                ) );
@@ -472,14 +472,12 @@ class Featured_Content {
 
                $defaults = array(
                        'hide-tag' => 1,
-                       'quantity' => 6,
                        'tag-id'   => 0,
-                       'tag-name' => 'featured',
+                       'tag-name' => _x( 'featured', 'featured content default tag slug', 'twentyfourteen' ),
                );
 
                $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;
@@ -523,10 +521,6 @@ class Featured_Content {
                        $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;
 
                // Delete the featured post ids transient.
@@ -534,29 +528,6 @@ class Featured_Content {
 
                return $output;
        }
-
-       /**
-        * Sanitize quantity of featured posts.
-        *
-        * @static
-        * @access public
-        * @since Twenty Fourteen 1.0
-        *
-        * @param int $input The value to sanitize.
-        * @return int A number between 1 and FeaturedContent::$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
 
 Featured_Content::setup();