Make WordPress Core


Ignore:
Timestamp:
11/10/2013 11:37:36 PM (13 years ago)
Author:
lancewillett
Message:

Twenty Fourteen: improve Featured Content experience in the Customizer. Better preview and cache clearing, props obenland and rachelbaker. Fixes #25549.

File:
1 edited

Legend:

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

    r26037 r26079  
    33 * Twenty Fourteen Featured Content
    44 *
    5  * This module allows you to define a subset of posts to be
    6  * displayed in the theme's Featured Content area.
     5 * This module allows you to define a subset of posts to be displayed in the
     6 * theme's Featured Content area.
    77 *
    8  * For maximum compatibility with different methods of posting
    9  * users will designate a featured post tag to associate posts
    10  * with. Since this tag now has special meaning beyond that of a
    11  * normal tags, users will have the ability to hide it from the
    12  * front-end of their site.
     8 * For maximum compatibility with different methods of posting users will
     9 * designate a featured post tag to associate posts with. Since this tag now
     10 * has special meaning beyond that of a normal tags, users will have the
     11 * ability to hide it from the front-end of their site.
    1312 */
    1413class Featured_Content {
    1514
    1615        /**
    17          * The maximum number of posts that a Featured Content
    18          * area can contain. We define a default value here but
    19          * themes can override this by defining a "max_posts"
    20          * entry in the second parameter passed in the call to
     16         * The maximum number of posts that a Featured Content area can contain. We
     17         * define a default value here but themes can override this by defining a
     18         * "max_posts" entry in the second parameter passed in the call to
    2119         * add_theme_support( 'featured-content' ).
    2220         *
    … …  
    4038         * add_theme_support( 'featured-content' ); during after_setup_theme.
    4139         *
    42          * If no theme support is found there is no need to hook into
    43          * WordPress. We'll just return early instead.
     40         * If no theme support is found there is no need to hook into WordPress.
     41         * We'll just return early instead.
    4442         *
    4543         * @uses Featured_Content::$max_posts
    … …  
    5351
    5452                /*
    55                  * An array of named arguments must be passed as
    56                  * the second parameter of add_theme_support().
     53                 * An array of named arguments must be passed as the second parameter
     54                 * of add_theme_support().
    5755                 */
    5856                if ( ! isset( $theme_support[0] ) )
    … …  
    7674                add_action( 'customize_controls_enqueue_scripts', array( __CLASS__, 'enqueue_scripts'    )    );
    7775                add_action( 'pre_get_posts',                      array( __CLASS__, 'pre_get_posts'      )    );
    78 
    79                 // Hide "featured" tag from the front-end.
     76                add_action( 'wp_loaded',                          array( __CLASS__, 'wp_loaded'          )    );
     77        }
     78
     79        /**
     80         * Hide "featured" tag from the front-end.
     81         *
     82         * Has to run on wp_loaded so that the preview filters of the customizer
     83         * have a chance to alter the value.
     84         */
     85        public static function wp_loaded() {
    8086                if ( self::get_setting( 'hide-tag' ) ) {
    8187                        add_filter( 'get_terms',     array( __CLASS__, 'hide_featured_term'     ), 10, 2 );
    … …  
    117123         */
    118124        public static function get_featured_post_ids() {
    119                 $settings = self::get_setting();
    120 
    121                 // Return false if the user has disabled this feature.
    122                 $tag = $settings['tag-id'];
    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                 }
    130 
    131125                // Return array of cached results if they exist.
    132126                $featured_ids = get_transient( 'featured_content_ids' );
    133127                if ( ! empty( $featured_ids ) )
    134128                        return array_map( 'absint', (array) $featured_ids );
     129
     130                $settings = self::get_setting();
     131
     132                // Return sticky post ids if no tag name is set.
     133                $term = get_term_by( 'name', $settings['tag-name'], 'post_tag' );
     134                if ( $term )
     135                        $tag = $term->term_id;
     136                else
     137                        return self::get_sticky_posts();
    135138
    136139                // Query for featured posts.
    … …  
    229232         * @see Featured_Content::validate_settings().
    230233         *
    231          * @param int $tag_id the term_id of the tag that has been deleted.
     234         * @param int $tag_id The term_id of the tag that has been deleted.
    232235         * @return void
    233236         */
    … …  
    330333                $wp_customize->add_section( 'featured_content', array(
    331334                        'title'          => __( 'Featured Content', 'twentyfourteen' ),
     335                        'description'    => __( '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' ),
    332336                        'priority'       => 130,
    333337                        'theme_supports' => 'featured-content',
    … …  
    336340                // Add Featured Content settings.
    337341                $wp_customize->add_setting( 'featured-content[tag-name]', array(
    338                         'default' => 'featured',
    339                         'type'    => 'option',
     342                        'default'              => 'featured',
     343                        'type'                 => 'option',
     344                        'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ),
    340345                ) );
    341346                $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',
     347                        'default'              => true,
     348                        'type'                 => 'option',
     349                        'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ),
    348350                ) );
    349351
    … …  
    360362                        'priority' => 30,
    361363                ) );
    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                 ) ) );
    366364        }
    367365
    … …  
    376374                        'ajaxurl' => admin_url( 'admin-ajax.php' ),
    377375                ) );
    378         }
    379 
     376                wp_add_inline_style( 'customize-controls', "
     377                        .ac_results {
     378                                z-index: 500000;
     379                        }
     380                " );
     381        }
    380382
    381383        /**
    … …  
    401403                        'quantity' => 6,
    402404                        'tag-id'   => 0,
     405                        'tag-name' => 'featured',
    403406                );
    404407
    … …  
    430433                $output = array();
    431434
    432                 if ( isset( $input['tag-id'] ) )
    433                         $output['tag-id'] = absint( $input['tag-id'] );
    434 
    435                 if ( isset( $input['tag-name'] ) ) {
    436                         if ( empty( $input['tag-name'] ) ) {
    437                                 $output['tag-id'] = 0;
     435                if ( empty( $input['tag-name'] ) ) {
     436                        $output['tag-id'] = 0;
     437                } else {
     438                        $new_tag = wp_create_tag( $input['tag-name'] );
     439                        if ( ! is_wp_error( $new_tag ) && isset( $new_tag['term_id'] ) ) {
     440                                $output['tag-id'] = $new_tag['term_id'];
    438441                        } 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;
     442                                $term = get_term_by( 'name', $input['tag-name'], 'post_tag' );
     443                                $output['tag-id'] = $term ? $term->term_id : 0;
    447444                        }
     445                        $output['tag-name'] = $input['tag-name'];
    448446                }
    449447
    … …  
    478476}
    479477
    480 if ( 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 
    490478Featured_Content::setup();
Note: See TracChangeset for help on using the changeset viewer.