Make WordPress Core


Ignore:
Timestamp:
11/07/2013 08:24:24 PM (10 years ago)
Author:
lancewillett
Message:

Twenty Fourteen: move all controls for managing Featured Content to the Customizer. Also implement smart fallbacks to not using a custom tag with the "featured" tag and sticky posts. Props obenland and celloexpressions, see #25549.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-content/themes/twentyfourteen/inc/featured-content.php

    r25853 r26037  
    6969            self::$max_posts = absint( $theme_support[0]['max_posts'] );
    7070
    71         add_filter( $filter,                 array( __CLASS__, 'get_featured_posts' ) );
    72         add_action( 'admin_init',            array( __CLASS__, 'register_setting' ) );
    73         add_action( 'save_post',             array( __CLASS__, 'delete_transient' ) );
    74         add_action( 'delete_post_tag',       array( __CLASS__, 'delete_post_tag' ) );
    75         add_action( 'pre_get_posts',         array( __CLASS__, 'pre_get_posts' ) );
     71        add_filter( $filter,                              array( __CLASS__, 'get_featured_posts' )    );
     72        add_action( 'customize_register',                 array( __CLASS__, 'customize_register' ), 9 );
     73        add_action( 'admin_init',                         array( __CLASS__, 'register_setting'   )    );
     74        add_action( 'save_post',                          array( __CLASS__, 'delete_transient'   )    );
     75        add_action( 'delete_post_tag',                    array( __CLASS__, 'delete_post_tag'    )    );
     76        add_action( 'customize_controls_enqueue_scripts', array( __CLASS__, 'enqueue_scripts'    )    );
     77        add_action( 'pre_get_posts',                      array( __CLASS__, 'pre_get_posts'      )    );
    7678
    7779        // Hide "featured" tag from the front-end.
    7880        if ( self::get_setting( 'hide-tag' ) ) {
    79             add_filter( 'get_terms',     array( __CLASS__, 'hide_featured_term' ), 10, 2 );
     81            add_filter( 'get_terms',     array( __CLASS__, 'hide_featured_term'     ), 10, 2 );
    8082            add_filter( 'get_the_terms', array( __CLASS__, 'hide_the_featured_term' ), 10, 3 );
    8183        }
     
    8789     * @uses Featured_Content::get_featured_post_ids()
    8890     *
    89      * @return array|bool
     91     * @return array
    9092     */
    9193    public static function get_featured_posts() {
    9294        $post_ids = self::get_featured_post_ids();
    93 
    94         // User has disabled Featured Content.
    95         if ( false === $post_ids )
    96             return false;
    9795
    9896        // No need to query if there is are no featured posts.
     
    102100        $featured_posts = get_posts( array(
    103101            'include'        => $post_ids,
    104             'posts_per_page' => count( $post_ids )
     102            'posts_per_page' => count( $post_ids ),
    105103        ) );
    106104
     
    116114     * Sets the "featured_content_ids" transient.
    117115     *
    118      * @return array|false Array of post IDs. false if user has disabled this feature.
     116     * @return array Array of post IDs
    119117     */
    120118    public static function get_featured_post_ids() {
     
    123121        // Return false if the user has disabled this feature.
    124122        $tag = $settings['tag-id'];
    125         if ( empty( $tag ) )
    126             return false;
     123        if ( empty( $tag ) ) {
     124            $term = get_term_by( 'name', 'featured', 'post_tag' );
     125            if ( $term )
     126                $tag = $term->term_id;
     127            else
     128                return self::get_sticky_posts();
     129        }
    127130
    128131        // Return array of cached results if they exist.
     
    143146        ) );
    144147
    145         // Return empty array if no Featured Content exists.
     148        // Return array with sticky posts if no Featured Content exists.
    146149        if ( ! $featured )
    147             return array();
     150            return self::get_sticky_posts();
    148151
    149152        // Ensure correct format before save/return.
     
    157160
    158161    /**
     162     * Returns an array with IDs of posts maked as sticky.
     163     *
     164     * @return array
     165     */
     166    public static function get_sticky_posts() {
     167        $settings = self::get_setting();
     168        return array_slice( get_option( 'sticky_posts', array() ), 0, $settings['quantity'] );
     169    }
     170
     171    /**
    159172     * Delete transient
    160173     *
     
    222235        $settings = self::get_setting();
    223236
    224         if ( empty( $settings['tag-id'] ) )
    225             return;
    226 
    227         if ( $tag_id != $settings['tag-id'] )
     237        if ( empty( $settings['tag-id'] ) || $tag_id != $settings['tag-id'] )
    228238            return;
    229239
     
    260270        foreach( $terms as $order => $term ) {
    261271            if ( self::get_setting( 'tag-id' ) == $term->term_id && 'post_tag' == $term->taxonomy )
    262                 unset( $terms[$order] );
     272                unset( $terms[ $order ] );
    263273        }
    264274
     
    285295
    286296        // Make sure we are in the correct taxonomy.
    287         if ( ! 'post_tag' == $taxonomy )
     297        if ( 'post_tag' != $taxonomy )
    288298            return $terms;
    289299
     
    294304        foreach( $terms as $order => $term ) {
    295305            if ( self::get_setting( 'tag-id' ) == $term->term_id )
    296                 unset( $terms[$term->term_id] );
     306                unset( $terms[ $term->term_id ] );
    297307        }
    298308
     
    309319     */
    310320    public static function register_setting() {
    311         add_settings_field( 'featured-content', __( 'Featured content', 'twentyfourteen' ), array( __CLASS__, 'render_form' ), 'reading' );
    312         register_setting( 'reading', 'featured-content', array( __CLASS__, 'validate_settings' ) );
    313     }
    314 
    315     /**
    316      * Render the form fields for Settings -> Reading screen
    317      *
    318      * @return void
    319      */
    320     public static function render_form() {
    321         $settings = self::get_setting();
    322 
    323         $tag_name = '';
    324         if ( ! empty( $settings['tag-id'] ) ) {
    325             $tag = get_term( $settings['tag-id'], 'post_tag' );
    326             if ( ! is_wp_error( $tag ) && isset( $tag->name ) )
    327                 $tag_name = $tag->name;
    328         }
    329 
    330         wp_enqueue_script( 'twentyfourteen-admin', get_template_directory_uri() . '/js/featured-content-admin.js', array( 'jquery', 'suggest' ), '20131016', true );
    331         ?>
    332         <div id="featured-content-ui">
    333             <p>
    334                 <label for="featured-content-tag-name"><?php echo _e( 'Tag name:', 'twentyfourteen' ); ?></label>
    335                 <input type="text" id="featured-content-tag-name" name="featured-content[tag-name]" value="<?php echo esc_attr( $tag_name ); ?>">
    336                 <input type="hidden" id="featured-content-tag-id" name="featured-content[tag-id]" value="<?php echo esc_attr( $settings['tag-id'] ); ?>">
    337             </p>
    338             <p>
    339                 <label for="featured-content-quantity"><?php _e( 'Number of posts:', 'twentyfourteen' ); ?></label>
    340                 <input class="small-text" type="number" step="1" min="1" max="<?php echo esc_attr( self::$max_posts ); ?>" id="featured-content-quantity" name="featured-content[quantity]" value="<?php echo esc_attr( $settings['quantity'] ); ?>">
    341             </p>
    342             <p>
    343                 <input type="checkbox" id="featured-content-hide-tag" name="featured-content[hide-tag]" <?php checked( $settings['hide-tag'], 1 ); ?>>
    344                 <label for="featured-content-hide-tag"><?php _e( 'Hide tag from displaying in post meta and tag clouds.', 'twentyfourteen' ); ?></label>
    345             </p>
    346         </div>
    347         <?php
    348     }
     321        register_setting( 'featured-content', 'featured-content', array( __CLASS__, 'validate_settings' ) );
     322    }
     323
     324    /**
     325     * Add settings to the Customizer.
     326     *
     327     * @param WP_Customize_Manager $wp_customize Theme Customizer object.
     328     */
     329    public static function customize_register( $wp_customize ) {
     330        $wp_customize->add_section( 'featured_content', array(
     331            'title'          => __( 'Featured Content', 'twentyfourteen' ),
     332            'priority'       => 130,
     333            'theme_supports' => 'featured-content',
     334        ) );
     335
     336        // Add Featured Content settings.
     337        $wp_customize->add_setting( 'featured-content[tag-name]', array(
     338            'default' => 'featured',
     339            'type'    => 'option',
     340        ) );
     341        $wp_customize->add_setting( 'featured-content[hide-tag]', array(
     342            'default' => true,
     343            'type'    => 'option',
     344        ) );
     345        $wp_customize->add_setting( 'featured-content[tag-id]', array(
     346            'default' => 0,
     347            'type'    => 'option',
     348        ) );
     349
     350        // Add Featured Content controls.
     351        $wp_customize->add_control( 'featured-content[tag-name]', array(
     352            'label'    => __( 'Tag name', 'twentyfourteen' ),
     353            'section'  => 'featured_content',
     354            'priority' => 20,
     355        ) );
     356        $wp_customize->add_control( 'featured-content[hide-tag]', array(
     357            'label'    => __( 'Hide tag from displaying in post meta and tag clouds.', 'twentyfourteen' ),
     358            'section'  => 'featured_content',
     359            'type'     => 'checkbox',
     360            'priority' => 30,
     361        ) );
     362        $wp_customize->add_control( new Featured_Content_Customize_Hidden_Control( $wp_customize, 'featured-content[tag-id]', array(
     363            'section'  => 'featured_content',
     364            'priority' => 999,
     365        ) ) );
     366    }
     367
     368    /**
     369     * Enqueue the tag suggestion script.
     370     *
     371     * @since Twenty Fourteen 1.0
     372     */
     373    public static function enqueue_scripts() {
     374        wp_enqueue_script( 'featured-content-suggest', get_template_directory_uri() . '/js/featured-content-admin.js', array( 'jquery', 'suggest' ), '20131022', true );
     375        wp_localize_script( 'featured-content-suggest', 'featuredContent', array(
     376            'ajaxurl' => admin_url( 'admin-ajax.php' ),
     377        ) );
     378    }
     379
    349380
    350381    /**
     
    358389     * as the first parameter to the function and only that value will be returned.
    359390     *
    360      * @uses Featured_Content::self::sanitize_quantity()
     391     * @uses Featured_Content::sanitize_quantity()
    361392     *
    362393     * @param string $key The key of a recognized setting.
     
    376407        $options['quantity'] = self::sanitize_quantity( $options['quantity'] );
    377408
    378         if ( 'all' != $key ) {
    379             if ( isset( $options[$key] ) )
    380                 return $options[$key];
    381             else
    382                 return false;
    383         }
     409        if ( 'all' != $key )
     410            return isset( $options[ $key ] ) ? $options[ $key ] : false;
    384411
    385412        return $options;
     
    406433            $output['tag-id'] = absint( $input['tag-id'] );
    407434
    408         if ( isset( $input['tag-name'] ) && ! empty( $input['tag-name'] ) ) {
    409             $new_tag = wp_create_tag( $input['tag-name'] );
    410             if ( ! is_wp_error( $new_tag ) && isset( $new_tag['term_id'] ) )
    411                 $tag = get_term( $new_tag['term_id'], 'post_tag' );
    412             if ( isset( $tag->term_id ) )
    413                 $output['tag-id'] = $tag->term_id;
    414         } else {
    415             unset( $output['tag-id'] );
     435        if ( isset( $input['tag-name'] ) ) {
     436            if ( empty( $input['tag-name'] ) ) {
     437                $output['tag-id'] = 0;
     438            } else {
     439                $new_tag = wp_create_tag( $input['tag-name'] );
     440                if ( ! is_wp_error( $new_tag ) && isset( $new_tag['term_id'] ) )
     441                    $tag = get_term( $new_tag['term_id'], 'post_tag' );
     442                if ( isset( $tag->term_id ) )
     443                    $output['tag-id'] = $tag->term_id;
     444                if ( is_int( $new_tag ) )
     445                    $output['tag-id'] = $new_tag;
     446                $output['tag-name'] = get_term( $output['tag-id'], 'post_tag' )->name;
     447            }
    416448        }
    417449
     
    419451            $output['quantity'] = self::sanitize_quantity( $input['quantity'] );
    420452
    421         $output['hide-tag'] = ( isset( $input['hide-tag'] ) ) ? 1 : 0;
     453        $output['hide-tag'] = isset( $input['hide-tag'] ) && $input['hide-tag'] ? 1 : 0;
    422454
    423455        self::delete_transient();
     
    446478}
    447479
     480if ( class_exists( 'WP_Customize_Control' ) ) {
     481    class Featured_Content_Customize_Hidden_Control extends WP_Customize_Control {
     482        public function render_content() {
     483            ?>
     484            <input type="hidden" <?php $this->link(); ?> value="<?php echo esc_attr( $this->value() ); ?>">
     485            <?php
     486        }
     487    }
     488}
     489
    448490Featured_Content::setup();
Note: See TracChangeset for help on using the changeset viewer.