Ticket #25549: 25549.6.diff
File 25549.6.diff, 14.3 KB (added by , 11 years ago) |
---|
-
wp-content/themes/twentyfourteen/functions.php
238 238 if ( is_active_sidebar( 'sidebar-3' ) ) 239 239 wp_enqueue_script( 'jquery-masonry' ); 240 240 241 if ( 'slider' == get_theme_mod( 'featured_content_layout' ) )241 if ( is_front_page() && 'slider' == get_theme_mod( 'featured_content_layout' ) ) 242 242 wp_enqueue_script( 'twentyfourteen-slider', get_template_directory_uri() . '/js/slider.js', array( 'jquery' ), '20131028', true ); 243 243 244 244 wp_enqueue_script( 'twentyfourteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20131102', true ); -
wp-content/themes/twentyfourteen/inc/customizer.php
28 28 $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'accent_color', array( 29 29 'label' => __( 'Accent Color', 'twentyfourteen' ), 30 30 'section' => 'colors', 31 'settings' => 'accent_color',32 31 ) ) ); 33 32 34 33 add_filter( 'theme_mod_accent_mid', 'twentyfourteen_accent_mid' ); 35 34 add_filter( 'theme_mod_accent_light', 'twentyfourteen_accent_light' ); 36 35 37 // Add the featured content section .36 // Add the featured content section in case it's not already there. 38 37 $wp_customize->add_section( 'featured_content', array( 39 38 'title' => __( 'Featured Content', 'twentyfourteen' ), 40 'priority' => 1 20,39 'priority' => 130, 41 40 ) ); 42 41 43 42 // Add the featured content layout setting and control. 44 43 $wp_customize->add_setting( 'featured_content_layout', array( 45 'default' => 'grid', 46 'type' => 'theme_mod', 47 'capability' => 'edit_theme_options', 44 'default' => 'grid', 48 45 ) ); 49 46 50 47 $wp_customize->add_control( 'featured_content_layout', array( … … 52 49 'section' => 'featured_content', 53 50 'type' => 'select', 54 51 'choices' => array( 55 'grid' => __( 'Grid', 'twentyfourteen' ),52 'grid' => __( 'Grid', 'twentyfourteen' ), 56 53 'slider' => __( 'Slider', 'twentyfourteen' ), 57 54 ), 58 55 ) ); -
wp-content/themes/twentyfourteen/inc/featured-content.php
68 68 if ( isset( $theme_support[0]['max_posts'] ) ) 69 69 self::$max_posts = absint( $theme_support[0]['max_posts'] ); 70 70 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' ) ); 76 78 77 79 // Hide "featured" tag from the front-end. 78 80 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 ); 80 82 add_filter( 'get_the_terms', array( __CLASS__, 'hide_the_featured_term' ), 10, 3 ); 81 83 } 82 84 } … … 86 88 * 87 89 * @uses Featured_Content::get_featured_post_ids() 88 90 * 89 * @return array |bool91 * @return array 90 92 */ 91 93 public static function get_featured_posts() { 92 94 $post_ids = self::get_featured_post_ids(); 93 95 94 // User has disabled Featured Content.95 if ( false === $post_ids )96 return false;97 98 96 // No need to query if there is are no featured posts. 99 97 if ( empty( $post_ids ) ) 100 98 return array(); 101 99 102 100 $featured_posts = get_posts( array( 103 101 'include' => $post_ids, 104 'posts_per_page' => count( $post_ids ) 102 'posts_per_page' => count( $post_ids ), 105 103 ) ); 106 104 107 105 return $featured_posts; … … 115 113 * 116 114 * Sets the "featured_content_ids" transient. 117 115 * 118 * @return array |false Array of post IDs. false if user has disabled this feature.116 * @return array Array of post IDs 119 117 */ 120 118 public static function get_featured_post_ids() { 121 119 $settings = self::get_setting(); 122 120 123 121 // Return false if the user has disabled this feature. 124 122 $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 } 127 130 128 131 // Return array of cached results if they exist. 129 132 $featured_ids = get_transient( 'featured_content_ids' ); … … 142 145 ), 143 146 ) ); 144 147 145 // Return empty arrayif no Featured Content exists.148 // Return array with sticky posts if no Featured Content exists. 146 149 if ( ! $featured ) 147 return array();150 return self::get_sticky_posts(); 148 151 149 152 // Ensure correct format before save/return. 150 153 $featured_ids = wp_list_pluck( (array) $featured, 'ID' ); … … 156 159 } 157 160 158 161 /** 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 /** 159 172 * Delete transient 160 173 * 161 174 * Hooks in the "save_post" action. … … 221 234 public static function delete_post_tag( $tag_id ) { 222 235 $settings = self::get_setting(); 223 236 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'] ) 228 238 return; 229 239 230 240 $settings['tag-id'] = 0; … … 259 269 260 270 foreach( $terms as $order => $term ) { 261 271 if ( self::get_setting( 'tag-id' ) == $term->term_id && 'post_tag' == $term->taxonomy ) 262 unset( $terms[ $order] );272 unset( $terms[ $order ] ); 263 273 } 264 274 265 275 return $terms; … … 284 294 return $terms; 285 295 286 296 // Make sure we are in the correct taxonomy. 287 if ( ! 'post_tag' == $taxonomy )297 if ( 'post_tag' != $taxonomy ) 288 298 return $terms; 289 299 290 300 // No terms? Return early! … … 293 303 294 304 foreach( $terms as $order => $term ) { 295 305 if ( self::get_setting( 'tag-id' ) == $term->term_id ) 296 unset( $terms[ $term->term_id] );306 unset( $terms[ $term->term_id ] ); 297 307 } 298 308 299 309 return $terms; … … 308 318 * @return void 309 319 */ 310 320 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' ) ); 321 register_setting( 'featured-content', 'featured-content', array( __CLASS__, 'validate_settings' ) ); 313 322 } 314 323 315 324 /** 316 * Render the form fields for Settings -> Reading screen325 * Add settings to the Customizer. 317 326 * 318 * @ return void327 * @param WP_Customize_Manager $wp_customize Theme Customizer object. 319 328 */ 320 public static function render_form() { 321 $settings = self::get_setting(); 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 ) ); 322 335 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 } 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 ) ); 329 349 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 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 ) ) ); 348 366 } 349 367 350 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 380 381 /** 351 382 * Get settings 352 383 * 353 384 * Get all settings recognized by this module. This function will return … … 357 388 * In the event that you only require one setting, you may pass its name 358 389 * as the first parameter to the function and only that value will be returned. 359 390 * 360 * @uses Featured_Content::s elf::sanitize_quantity()391 * @uses Featured_Content::sanitize_quantity() 361 392 * 362 393 * @param string $key The key of a recognized setting. 363 394 * @return mixed Array of all settings by default. A single value if passed as first parameter. … … 375 406 $options = array_intersect_key( $options, $defaults ); 376 407 $options['quantity'] = self::sanitize_quantity( $options['quantity'] ); 377 408 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; 384 411 385 412 return $options; 386 413 } … … 405 432 if ( isset( $input['tag-id'] ) ) 406 433 $output['tag-id'] = absint( $input['tag-id'] ); 407 434 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 } 416 448 } 417 449 418 450 if ( isset( $input['quantity'] ) ) 419 451 $output['quantity'] = self::sanitize_quantity( $input['quantity'] ); 420 452 421 $output['hide-tag'] = ( isset( $input['hide-tag'] ) )? 1 : 0;453 $output['hide-tag'] = isset( $input['hide-tag'] ) && $input['hide-tag'] ? 1 : 0; 422 454 423 455 self::delete_transient(); 424 456 … … 445 477 } 446 478 } 447 479 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 448 490 Featured_Content::setup(); -
wp-content/themes/twentyfourteen/js/featured-content-admin.js
1 jQuery( document ).ready( function( $ ) { 2 $( '#featured-content-tag-name' ).suggest( ajaxurl + '?action=ajax-tag-search&tax=post_tag', { delay: 500, minchars: 2 } ); 3 } ); 4 No newline at end of file 1 jQuery( function( $ ) { 2 $( '#customize-control-featured-content-tag-name input' ).suggest( featuredContent.ajaxurl + '?action=ajax-tag-search&tax=post_tag', { delay: 500, minchars: 2 } ); 3 } );