Make WordPress Core

Ticket #25549: 25549.customizer.1.diff

File 25549.customizer.1.diff, 11.8 KB (added by celloexpressions, 11 years ago)

Rough first pass at moving the featured content settings to the customizer. List of issues forthcoming.

  • wp-content/themes/twentyfourteen/inc/customizer.php

     
    1515 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
    1616 */
    1717function twentyfourteen_customize_register( $wp_customize ) {
     18        // Define custom customizer control for the number input type.
     19        class Customize_Number_Control extends WP_Customize_Control {
     20                public $type = 'number';
     21                public $min  = 1;
     22                public $max  = 15;
     23               
     24                public function render_content() {
     25                        ?>
     26                        <label>
     27                        <span class="customize-control-title"><?php echo $this->label; ?></span>
     28                        <input type="number" value="<php echo esc_attr( $this->value() ); ?>" min="<?php echo $this->min; ?>" max="<?php echo $this->max; ?>" <?php $this->link(); ?> style="width: 50px;" />
     29                        <?php
     30                }
     31        }
     32
    1833        // Add postMessage support for site title and description.
    1934        $wp_customize->get_setting( 'blogname' )->transport        = 'postMessage';
    2035        $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';
     
    4055        // Add the featured content layout setting and control.
    4156        $wp_customize->add_setting( 'featured_content_layout', array(
    4257                'default'    => 'grid',
    43                 'type'       => 'theme_mod',
    44                 'capability' => 'edit_theme_options',
    4558        ) );
    4659
    4760        $wp_customize->add_control( 'featured_content_layout', array(
     
    5366                        'slider' => __( 'Slider', 'twentyfourteen' ),
    5467                ),
    5568        ) );
     69
     70        // Add Featured Content settings.
     71        $wp_customize->add_setting( 'featured_content_tag_name', array(
     72                'default'           => 'featured',
     73                'sanitize_callback' => 'twentyfourteen_sanitize_fc_tag',
     74        ) );
     75        $wp_customize->add_setting( 'featured_content_quantity', array(
     76                'default'           => 6,
     77                'sanitize_callback' => 'twentyfourteen_sanitize_fc_quantity',
     78        ) );
     79        $wp_customize->add_setting( 'featured_content_hide_tag', array(
     80                'default'           => true,
     81        ) );
     82       
     83        // Add Featured Content controls.
     84        $wp_customize->add_control( 'featured_content_tag_name', array(
     85                'label'    => __( 'Tag name' ),
     86                'section'  => 'featured_content',
     87        ) );
     88        $wp_customize->add_control( new Customize_Number_Control( $wp_customize, 'featured_content_quantity', array(
     89                'label'    => __( 'Number of posts' ),
     90                'section'  => 'featured_content',
     91                'min'      => 1,               
     92                'max'      => 6,               
     93        ) ) );
     94        $wp_customize->add_control( 'featured_content_hide_tag', array(
     95                'label'    => __( 'Hide tag from displaying in post meta and tag clouds.' ),
     96                'section'  => 'featured_content',
     97                'type'     => 'checkbox',
     98        ) );
     99
    56100}
    57101add_action( 'customize_register', 'twentyfourteen_customize_register' );
    58102
    59103/**
     104 * Enqueue the tag suggestion script.
     105 *
     106 * @since Twenty Fourteen 1.0
     107 */
     108function twentyfourteen_customize_controls_js() {
     109        wp_enqueue_script( 'twentyfourteen-featured-content-admin', get_template_directory_uri() . '/js/featured-content-admin.js', array( 'jquery', 'suggest' ), '20131022', true );
     110}
     111add_action( 'customize_controls_enqueue_scripts', 'twentyfourteen_customize_controls_js' );
     112
     113/**
     114 * ajaxurl is not defined in the customizer, and we need it for the featued content suggest script.
     115 *
     116 * @todo This should probably be in core.
     117 */
     118function twentyfourteen_customize_controls_print_scripts() {
     119        echo '<script type="text/javascript">ajaxurl = "' . admin_url('admin-ajax.php') . '"; </script>';
     120}
     121add_action( 'customize_controls_print_scripts', 'twentyfourteen_customize_controls_print_scripts' );
     122
     123/**
    60124 * Bind JS handlers to make Theme Customizer preview reload changes asynchronously.
    61125 *
    62126 * @since Twenty Fourteen 1.0
     
    67131add_action( 'customize_preview_init', 'twentyfourteen_customize_preview_js' );
    68132
    69133/**
     134 * Sanitize featured content quantity
     135 *
     136 * @param int $input The value to sanitize.
     137 * @return int $quantity A number between 1 and 6.
     138 *
     139 * @since Twenty Fourteen 1.0
     140 */
     141function twentyfourteen_sanitize_fc_quantity( $input ) {
     142        $quantity = absint( $input );
     143
     144        if ( $quantity > 6 )
     145                $quantity = 6;
     146        else if ( 1 > $quantity )
     147                $quantity = 1;
     148
     149        return $quantity;
     150}
     151
     152/**
     153 * Validate the featured content tag and define the corresponding id.
     154 *
     155 * Make sure that the user-specified tag exists,
     156 * and save its id if it does. This function
     157 * will also delete the transient set in
     158 * Featured_Content::get_featured_content().
     159 *
     160 * @param string $input User-specified tag name.
     161 * @return string $output The tag name if it exists, or an empty string.
     162 */
     163function twentyfourteen_sanitize_fc_tag( $input ) {
     164        $output = '';
     165        if ( $input ) {
     166                $new_tag = wp_create_tag( $input );
     167                if ( ! is_wp_error( $new_tag ) && isset( $new_tag['term_id'] ) )
     168                        $tag = get_term( $new_tag['term_id'], 'post_tag' );
     169                if ( isset( $tag->term_id ) ) {
     170                        set_theme_mod( 'featured_content_tag_id', $tag->term_id );
     171                        $output = $tag->name;
     172                }
     173        } else {
     174                delete_theme_mod( 'featured_content_tag_id' );
     175        }
     176       
     177        delete_transient( 'featured_content_ids' );
     178
     179        return $output;
     180}
     181
     182/**
    70183 * Generate two variants of the accent color, return the original, and
    71184 * save the others as theme mods.
    72185 *
  • wp-content/themes/twentyfourteen/inc/featured-content.php

     
    6969                        self::$max_posts = absint( $theme_support[0]['max_posts'] );
    7070
    7171                add_filter( $filter,                 array( __CLASS__, 'get_featured_posts' ) );
    72                 add_action( 'admin_init',            array( __CLASS__, 'register_setting' ) );
    7372                add_action( 'save_post',             array( __CLASS__, 'delete_transient' ) );
    7473                add_action( 'delete_post_tag',       array( __CLASS__, 'delete_post_tag' ) );
    7574                add_action( 'pre_get_posts',         array( __CLASS__, 'pre_get_posts' ) );
     
    9594                if ( false === $post_ids )
    9695                        return false;
    9796
    98                 // No need to query if there is are no featured posts.
     97                // No need to query if there are no featured posts.
    9998                if ( empty( $post_ids ) )
    10099                        return array();
    101100
     
    300299        }
    301300
    302301        /**
    303          * Register custom setting on the Settings -> Reading screen
    304          *
    305          * @uses Featured_Content::render_form()
    306          * @uses Featured_Content::validate_settings()
    307          *
    308          * @return void
    309          */
    310         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         }
    349 
    350         /**
    351302         * Get settings
    352303         *
    353304         * Get all settings recognized by this module. This function will return
     
    357308         * In the event that you only require one setting, you may pass its name
    358309         * as the first parameter to the function and only that value will be returned.
    359310         *
    360          * @uses Featured_Content::self::sanitize_quantity()
    361          *
    362311         * @param string $key The key of a recognized setting.
    363312         * @return mixed Array of all settings by default. A single value if passed as first parameter.
    364313         */
    365314        public static function get_setting( $key = 'all' ) {
    366                 $saved = (array) get_option( 'featured-content' );
     315                $saved = array(
     316                        'hide-tag' => get_theme_mod( 'featured_content_hide_tag' ),
     317                        'quantity' => get_theme_mod( 'featured_content_quantity' ),
     318                        'tag-id'   => get_theme_mod( 'featured_content_tag_id' ),
     319                );
    367320
    368321                $defaults = array(
    369322                        'hide-tag' => 1,
     
    373326
    374327                $options = wp_parse_args( $saved, $defaults );
    375328                $options = array_intersect_key( $options, $defaults );
    376                 $options['quantity'] = self::sanitize_quantity( $options['quantity'] );
    377329
    378330                if ( 'all' != $key ) {
    379331                        if ( isset( $options[$key] ) )
     
    384336
    385337                return $options;
    386338        }
    387 
    388         /**
    389          * Validate settings
    390          *
    391          * Make sure that all user supplied content is in an
    392          * expected format before saving to the database. This
    393          * function will also delete the transient set in
    394          * Featured_Content::get_featured_content().
    395          *
    396          * @uses Featured_Content::self::sanitize_quantity()
    397          * @uses Featured_Content::self::delete_transient()
    398          *
    399          * @param array $input
    400          * @return array $output
    401          */
    402         public static function validate_settings( $input ) {
    403                 $output = array();
    404 
    405                 if ( isset( $input['tag-id'] ) )
    406                         $output['tag-id'] = absint( $input['tag-id'] );
    407 
    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'] );
    416                 }
    417 
    418                 if ( isset( $input['quantity'] ) )
    419                         $output['quantity'] = self::sanitize_quantity( $input['quantity'] );
    420 
    421                 $output['hide-tag'] = ( isset( $input['hide-tag'] ) ) ? 1 : 0;
    422 
    423                 self::delete_transient();
    424 
    425                 return $output;
    426         }
    427 
    428         /**
    429          * Sanitize quantity
    430          *
    431          * @param int $input The value to sanitize.
    432          * @return int A number between 1 and FeaturedContent::$max_posts.
    433          *
    434          * @uses Featured_Content::$max_posts
    435          */
    436         public static function sanitize_quantity( $input ) {
    437                 $quantity = absint( $input );
    438 
    439                 if ( $quantity > self::$max_posts )
    440                         $quantity = self::$max_posts;
    441                 else if ( 1 > $quantity )
    442                         $quantity = 1;
    443 
    444                 return $quantity;
    445         }
    446339}
    447340
    448341Featured_Content::setup();
    449  No newline at end of file
  • wp-content/themes/twentyfourteen/js/featured-content-admin.js

     
    11jQuery( document ).ready( function( $ ) {
    2         $( '#featured-content-tag-name' ).suggest( ajaxurl + '?action=ajax-tag-search&tax=post_tag', { delay: 500, minchars: 2 } );
     2        $( '#customize-control-featured_content_tag_name input' ).suggest( ajaxurl + '?action=ajax-tag-search&tax=post_tag', { delay: 500, minchars: 2 } );
    33} );
     4 No newline at end of file