Ticket #25549: 25549.customizer.1.diff
File 25549.customizer.1.diff, 11.8 KB (added by , 11 years ago) |
---|
-
wp-content/themes/twentyfourteen/inc/customizer.php
15 15 * @param WP_Customize_Manager $wp_customize Theme Customizer object. 16 16 */ 17 17 function 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 18 33 // Add postMessage support for site title and description. 19 34 $wp_customize->get_setting( 'blogname' )->transport = 'postMessage'; 20 35 $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage'; … … 40 55 // Add the featured content layout setting and control. 41 56 $wp_customize->add_setting( 'featured_content_layout', array( 42 57 'default' => 'grid', 43 'type' => 'theme_mod',44 'capability' => 'edit_theme_options',45 58 ) ); 46 59 47 60 $wp_customize->add_control( 'featured_content_layout', array( … … 53 66 'slider' => __( 'Slider', 'twentyfourteen' ), 54 67 ), 55 68 ) ); 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 56 100 } 57 101 add_action( 'customize_register', 'twentyfourteen_customize_register' ); 58 102 59 103 /** 104 * Enqueue the tag suggestion script. 105 * 106 * @since Twenty Fourteen 1.0 107 */ 108 function 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 } 111 add_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 */ 118 function twentyfourteen_customize_controls_print_scripts() { 119 echo '<script type="text/javascript">ajaxurl = "' . admin_url('admin-ajax.php') . '"; </script>'; 120 } 121 add_action( 'customize_controls_print_scripts', 'twentyfourteen_customize_controls_print_scripts' ); 122 123 /** 60 124 * Bind JS handlers to make Theme Customizer preview reload changes asynchronously. 61 125 * 62 126 * @since Twenty Fourteen 1.0 … … 67 131 add_action( 'customize_preview_init', 'twentyfourteen_customize_preview_js' ); 68 132 69 133 /** 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 */ 141 function 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 */ 163 function 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 /** 70 183 * Generate two variants of the accent color, return the original, and 71 184 * save the others as theme mods. 72 185 * -
wp-content/themes/twentyfourteen/inc/featured-content.php
69 69 self::$max_posts = absint( $theme_support[0]['max_posts'] ); 70 70 71 71 add_filter( $filter, array( __CLASS__, 'get_featured_posts' ) ); 72 add_action( 'admin_init', array( __CLASS__, 'register_setting' ) );73 72 add_action( 'save_post', array( __CLASS__, 'delete_transient' ) ); 74 73 add_action( 'delete_post_tag', array( __CLASS__, 'delete_post_tag' ) ); 75 74 add_action( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ) ); … … 95 94 if ( false === $post_ids ) 96 95 return false; 97 96 98 // No need to query if there isare no featured posts.97 // No need to query if there are no featured posts. 99 98 if ( empty( $post_ids ) ) 100 99 return array(); 101 100 … … 300 299 } 301 300 302 301 /** 303 * Register custom setting on the Settings -> Reading screen304 *305 * @uses Featured_Content::render_form()306 * @uses Featured_Content::validate_settings()307 *308 * @return void309 */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 screen317 *318 * @return void319 */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 <?php348 }349 350 /**351 302 * Get settings 352 303 * 353 304 * Get all settings recognized by this module. This function will return … … 357 308 * In the event that you only require one setting, you may pass its name 358 309 * as the first parameter to the function and only that value will be returned. 359 310 * 360 * @uses Featured_Content::self::sanitize_quantity()361 *362 311 * @param string $key The key of a recognized setting. 363 312 * @return mixed Array of all settings by default. A single value if passed as first parameter. 364 313 */ 365 314 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 ); 367 320 368 321 $defaults = array( 369 322 'hide-tag' => 1, … … 373 326 374 327 $options = wp_parse_args( $saved, $defaults ); 375 328 $options = array_intersect_key( $options, $defaults ); 376 $options['quantity'] = self::sanitize_quantity( $options['quantity'] );377 329 378 330 if ( 'all' != $key ) { 379 331 if ( isset( $options[$key] ) ) … … 384 336 385 337 return $options; 386 338 } 387 388 /**389 * Validate settings390 *391 * Make sure that all user supplied content is in an392 * expected format before saving to the database. This393 * function will also delete the transient set in394 * Featured_Content::get_featured_content().395 *396 * @uses Featured_Content::self::sanitize_quantity()397 * @uses Featured_Content::self::delete_transient()398 *399 * @param array $input400 * @return array $output401 */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 quantity430 *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_posts435 */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 }446 339 } 447 340 448 341 Featured_Content::setup(); 449 No newline at end of file -
wp-content/themes/twentyfourteen/js/featured-content-admin.js
1 1 jQuery( 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 } ); 3 3 } ); 4 No newline at end of file