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.

Built from https://develop.svn.wordpress.org/trunk@26037


git-svn-id: http://core.svn.wordpress.org/trunk@25966 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Lance Willett 2013-11-07 20:25:09 +00:00
parent 26c7409f9c
commit 5420155231
4 changed files with 120 additions and 81 deletions

View File

@ -239,7 +239,7 @@ function twentyfourteen_scripts() {
if ( is_active_sidebar( 'sidebar-3' ) )
wp_enqueue_script( 'jquery-masonry' );
if ( 'slider' == get_theme_mod( 'featured_content_layout' ) )
if ( is_front_page() && 'slider' == get_theme_mod( 'featured_content_layout' ) )
wp_enqueue_script( 'twentyfourteen-slider', get_template_directory_uri() . '/js/slider.js', array( 'jquery' ), '20131028', true );
wp_enqueue_script( 'twentyfourteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20131102', true );

View File

@ -28,23 +28,20 @@ function twentyfourteen_customize_register( $wp_customize ) {
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'accent_color', array(
'label' => __( 'Accent Color', 'twentyfourteen' ),
'section' => 'colors',
'settings' => 'accent_color',
) ) );
add_filter( 'theme_mod_accent_mid', 'twentyfourteen_accent_mid' );
add_filter( 'theme_mod_accent_light', 'twentyfourteen_accent_light' );
// Add the featured content section.
// Add the featured content section in case it's not already there.
$wp_customize->add_section( 'featured_content', array(
'title' => __( 'Featured Content', 'twentyfourteen' ),
'priority' => 120,
'priority' => 130,
) );
// Add the featured content layout setting and control.
$wp_customize->add_setting( 'featured_content_layout', array(
'default' => 'grid',
'type' => 'theme_mod',
'capability' => 'edit_theme_options',
'default' => 'grid',
) );
$wp_customize->add_control( 'featured_content_layout', array(
@ -52,7 +49,7 @@ function twentyfourteen_customize_register( $wp_customize ) {
'section' => 'featured_content',
'type' => 'select',
'choices' => array(
'grid' => __( 'Grid', 'twentyfourteen' ),
'grid' => __( 'Grid', 'twentyfourteen' ),
'slider' => __( 'Slider', 'twentyfourteen' ),
),
) );

View File

@ -68,15 +68,17 @@ class Featured_Content {
if ( isset( $theme_support[0]['max_posts'] ) )
self::$max_posts = absint( $theme_support[0]['max_posts'] );
add_filter( $filter, array( __CLASS__, 'get_featured_posts' ) );
add_action( 'admin_init', array( __CLASS__, 'register_setting' ) );
add_action( 'save_post', array( __CLASS__, 'delete_transient' ) );
add_action( 'delete_post_tag', array( __CLASS__, 'delete_post_tag' ) );
add_action( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ) );
add_filter( $filter, array( __CLASS__, 'get_featured_posts' ) );
add_action( 'customize_register', array( __CLASS__, 'customize_register' ), 9 );
add_action( 'admin_init', array( __CLASS__, 'register_setting' ) );
add_action( 'save_post', array( __CLASS__, 'delete_transient' ) );
add_action( 'delete_post_tag', array( __CLASS__, 'delete_post_tag' ) );
add_action( 'customize_controls_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) );
add_action( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ) );
// Hide "featured" tag from the front-end.
if ( self::get_setting( 'hide-tag' ) ) {
add_filter( 'get_terms', array( __CLASS__, 'hide_featured_term' ), 10, 2 );
add_filter( 'get_terms', array( __CLASS__, 'hide_featured_term' ), 10, 2 );
add_filter( 'get_the_terms', array( __CLASS__, 'hide_the_featured_term' ), 10, 3 );
}
}
@ -86,22 +88,18 @@ class Featured_Content {
*
* @uses Featured_Content::get_featured_post_ids()
*
* @return array|bool
* @return array
*/
public static function get_featured_posts() {
$post_ids = self::get_featured_post_ids();
// User has disabled Featured Content.
if ( false === $post_ids )
return false;
// No need to query if there is are no featured posts.
if ( empty( $post_ids ) )
return array();
$featured_posts = get_posts( array(
'include' => $post_ids,
'posts_per_page' => count( $post_ids )
'posts_per_page' => count( $post_ids ),
) );
return $featured_posts;
@ -115,15 +113,20 @@ class Featured_Content {
*
* Sets the "featured_content_ids" transient.
*
* @return array|false Array of post IDs. false if user has disabled this feature.
* @return array Array of post IDs
*/
public static function get_featured_post_ids() {
$settings = self::get_setting();
// Return false if the user has disabled this feature.
$tag = $settings['tag-id'];
if ( empty( $tag ) )
return false;
if ( empty( $tag ) ) {
$term = get_term_by( 'name', 'featured', 'post_tag' );
if ( $term )
$tag = $term->term_id;
else
return self::get_sticky_posts();
}
// Return array of cached results if they exist.
$featured_ids = get_transient( 'featured_content_ids' );
@ -142,9 +145,9 @@ class Featured_Content {
),
) );
// Return empty array if no Featured Content exists.
// Return array with sticky posts if no Featured Content exists.
if ( ! $featured )
return array();
return self::get_sticky_posts();
// Ensure correct format before save/return.
$featured_ids = wp_list_pluck( (array) $featured, 'ID' );
@ -155,6 +158,16 @@ class Featured_Content {
return $featured_ids;
}
/**
* Returns an array with IDs of posts maked as sticky.
*
* @return array
*/
public static function get_sticky_posts() {
$settings = self::get_setting();
return array_slice( get_option( 'sticky_posts', array() ), 0, $settings['quantity'] );
}
/**
* Delete transient
*
@ -221,10 +234,7 @@ class Featured_Content {
public static function delete_post_tag( $tag_id ) {
$settings = self::get_setting();
if ( empty( $settings['tag-id'] ) )
return;
if ( $tag_id != $settings['tag-id'] )
if ( empty( $settings['tag-id'] ) || $tag_id != $settings['tag-id'] )
return;
$settings['tag-id'] = 0;
@ -259,7 +269,7 @@ class Featured_Content {
foreach( $terms as $order => $term ) {
if ( self::get_setting( 'tag-id' ) == $term->term_id && 'post_tag' == $term->taxonomy )
unset( $terms[$order] );
unset( $terms[ $order ] );
}
return $terms;
@ -284,7 +294,7 @@ class Featured_Content {
return $terms;
// Make sure we are in the correct taxonomy.
if ( ! 'post_tag' == $taxonomy )
if ( 'post_tag' != $taxonomy )
return $terms;
// No terms? Return early!
@ -293,7 +303,7 @@ class Featured_Content {
foreach( $terms as $order => $term ) {
if ( self::get_setting( 'tag-id' ) == $term->term_id )
unset( $terms[$term->term_id] );
unset( $terms[ $term->term_id ] );
}
return $terms;
@ -308,45 +318,66 @@ class Featured_Content {
* @return void
*/
public static function register_setting() {
add_settings_field( 'featured-content', __( 'Featured content', 'twentyfourteen' ), array( __CLASS__, 'render_form' ), 'reading' );
register_setting( 'reading', 'featured-content', array( __CLASS__, 'validate_settings' ) );
register_setting( 'featured-content', 'featured-content', array( __CLASS__, 'validate_settings' ) );
}
/**
* Render the form fields for Settings -> Reading screen
* Add settings to the Customizer.
*
* @return void
* @param WP_Customize_Manager $wp_customize Theme Customizer object.
*/
public static function render_form() {
$settings = self::get_setting();
public static function customize_register( $wp_customize ) {
$wp_customize->add_section( 'featured_content', array(
'title' => __( 'Featured Content', 'twentyfourteen' ),
'priority' => 130,
'theme_supports' => 'featured-content',
) );
$tag_name = '';
if ( ! empty( $settings['tag-id'] ) ) {
$tag = get_term( $settings['tag-id'], 'post_tag' );
if ( ! is_wp_error( $tag ) && isset( $tag->name ) )
$tag_name = $tag->name;
}
// Add Featured Content settings.
$wp_customize->add_setting( 'featured-content[tag-name]', array(
'default' => 'featured',
'type' => 'option',
) );
$wp_customize->add_setting( 'featured-content[hide-tag]', array(
'default' => true,
'type' => 'option',
) );
$wp_customize->add_setting( 'featured-content[tag-id]', array(
'default' => 0,
'type' => 'option',
) );
wp_enqueue_script( 'twentyfourteen-admin', get_template_directory_uri() . '/js/featured-content-admin.js', array( 'jquery', 'suggest' ), '20131016', true );
?>
<div id="featured-content-ui">
<p>
<label for="featured-content-tag-name"><?php echo _e( 'Tag name:', 'twentyfourteen' ); ?></label>
<input type="text" id="featured-content-tag-name" name="featured-content[tag-name]" value="<?php echo esc_attr( $tag_name ); ?>">
<input type="hidden" id="featured-content-tag-id" name="featured-content[tag-id]" value="<?php echo esc_attr( $settings['tag-id'] ); ?>">
</p>
<p>
<label for="featured-content-quantity"><?php _e( 'Number of posts:', 'twentyfourteen' ); ?></label>
<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'] ); ?>">
</p>
<p>
<input type="checkbox" id="featured-content-hide-tag" name="featured-content[hide-tag]" <?php checked( $settings['hide-tag'], 1 ); ?>>
<label for="featured-content-hide-tag"><?php _e( 'Hide tag from displaying in post meta and tag clouds.', 'twentyfourteen' ); ?></label>
</p>
</div>
<?php
// Add Featured Content controls.
$wp_customize->add_control( 'featured-content[tag-name]', array(
'label' => __( 'Tag name', 'twentyfourteen' ),
'section' => 'featured_content',
'priority' => 20,
) );
$wp_customize->add_control( 'featured-content[hide-tag]', array(
'label' => __( 'Hide tag from displaying in post meta and tag clouds.', 'twentyfourteen' ),
'section' => 'featured_content',
'type' => 'checkbox',
'priority' => 30,
) );
$wp_customize->add_control( new Featured_Content_Customize_Hidden_Control( $wp_customize, 'featured-content[tag-id]', array(
'section' => 'featured_content',
'priority' => 999,
) ) );
}
/**
* Enqueue the tag suggestion script.
*
* @since Twenty Fourteen 1.0
*/
public static function enqueue_scripts() {
wp_enqueue_script( 'featured-content-suggest', get_template_directory_uri() . '/js/featured-content-admin.js', array( 'jquery', 'suggest' ), '20131022', true );
wp_localize_script( 'featured-content-suggest', 'featuredContent', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
) );
}
/**
* Get settings
*
@ -357,7 +388,7 @@ class Featured_Content {
* In the event that you only require one setting, you may pass its name
* as the first parameter to the function and only that value will be returned.
*
* @uses Featured_Content::self::sanitize_quantity()
* @uses Featured_Content::sanitize_quantity()
*
* @param string $key The key of a recognized setting.
* @return mixed Array of all settings by default. A single value if passed as first parameter.
@ -375,12 +406,8 @@ class Featured_Content {
$options = array_intersect_key( $options, $defaults );
$options['quantity'] = self::sanitize_quantity( $options['quantity'] );
if ( 'all' != $key ) {
if ( isset( $options[$key] ) )
return $options[$key];
else
return false;
}
if ( 'all' != $key )
return isset( $options[ $key ] ) ? $options[ $key ] : false;
return $options;
}
@ -405,20 +432,25 @@ class Featured_Content {
if ( isset( $input['tag-id'] ) )
$output['tag-id'] = absint( $input['tag-id'] );
if ( isset( $input['tag-name'] ) && ! empty( $input['tag-name'] ) ) {
$new_tag = wp_create_tag( $input['tag-name'] );
if ( ! is_wp_error( $new_tag ) && isset( $new_tag['term_id'] ) )
$tag = get_term( $new_tag['term_id'], 'post_tag' );
if ( isset( $tag->term_id ) )
$output['tag-id'] = $tag->term_id;
} else {
unset( $output['tag-id'] );
if ( isset( $input['tag-name'] ) ) {
if ( empty( $input['tag-name'] ) ) {
$output['tag-id'] = 0;
} else {
$new_tag = wp_create_tag( $input['tag-name'] );
if ( ! is_wp_error( $new_tag ) && isset( $new_tag['term_id'] ) )
$tag = get_term( $new_tag['term_id'], 'post_tag' );
if ( isset( $tag->term_id ) )
$output['tag-id'] = $tag->term_id;
if ( is_int( $new_tag ) )
$output['tag-id'] = $new_tag;
$output['tag-name'] = get_term( $output['tag-id'], 'post_tag' )->name;
}
}
if ( isset( $input['quantity'] ) )
$output['quantity'] = self::sanitize_quantity( $input['quantity'] );
$output['hide-tag'] = ( isset( $input['hide-tag'] ) ) ? 1 : 0;
$output['hide-tag'] = isset( $input['hide-tag'] ) && $input['hide-tag'] ? 1 : 0;
self::delete_transient();
@ -445,4 +477,14 @@ class Featured_Content {
}
}
if ( class_exists( 'WP_Customize_Control' ) ) {
class Featured_Content_Customize_Hidden_Control extends WP_Customize_Control {
public function render_content() {
?>
<input type="hidden" <?php $this->link(); ?> value="<?php echo esc_attr( $this->value() ); ?>">
<?php
}
}
}
Featured_Content::setup();

View File

@ -1,3 +1,3 @@
jQuery( document ).ready( function( $ ) {
$( '#featured-content-tag-name' ).suggest( ajaxurl + '?action=ajax-tag-search&tax=post_tag', { delay: 500, minchars: 2 } );
jQuery( function( $ ) {
$( '#customize-control-featured-content-tag-name input' ).suggest( featuredContent.ajaxurl + '?action=ajax-tag-search&tax=post_tag', { delay: 500, minchars: 2 } );
} );