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.
git-svn-id: https://develop.svn.wordpress.org/trunk@26037 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
976df5f9ed
commit
daad8899b1
@ -239,7 +239,7 @@ function twentyfourteen_scripts() {
|
|||||||
if ( is_active_sidebar( 'sidebar-3' ) )
|
if ( is_active_sidebar( 'sidebar-3' ) )
|
||||||
wp_enqueue_script( 'jquery-masonry' );
|
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-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 );
|
wp_enqueue_script( 'twentyfourteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20131102', true );
|
||||||
|
@ -28,23 +28,20 @@ function twentyfourteen_customize_register( $wp_customize ) {
|
|||||||
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'accent_color', array(
|
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'accent_color', array(
|
||||||
'label' => __( 'Accent Color', 'twentyfourteen' ),
|
'label' => __( 'Accent Color', 'twentyfourteen' ),
|
||||||
'section' => 'colors',
|
'section' => 'colors',
|
||||||
'settings' => 'accent_color',
|
|
||||||
) ) );
|
) ) );
|
||||||
|
|
||||||
add_filter( 'theme_mod_accent_mid', 'twentyfourteen_accent_mid' );
|
add_filter( 'theme_mod_accent_mid', 'twentyfourteen_accent_mid' );
|
||||||
add_filter( 'theme_mod_accent_light', 'twentyfourteen_accent_light' );
|
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(
|
$wp_customize->add_section( 'featured_content', array(
|
||||||
'title' => __( 'Featured Content', 'twentyfourteen' ),
|
'title' => __( 'Featured Content', 'twentyfourteen' ),
|
||||||
'priority' => 120,
|
'priority' => 130,
|
||||||
) );
|
) );
|
||||||
|
|
||||||
// Add the featured content layout setting and control.
|
// Add the featured content layout setting and control.
|
||||||
$wp_customize->add_setting( 'featured_content_layout', array(
|
$wp_customize->add_setting( 'featured_content_layout', array(
|
||||||
'default' => 'grid',
|
'default' => 'grid',
|
||||||
'type' => 'theme_mod',
|
|
||||||
'capability' => 'edit_theme_options',
|
|
||||||
) );
|
) );
|
||||||
|
|
||||||
$wp_customize->add_control( 'featured_content_layout', array(
|
$wp_customize->add_control( 'featured_content_layout', array(
|
||||||
@ -52,7 +49,7 @@ function twentyfourteen_customize_register( $wp_customize ) {
|
|||||||
'section' => 'featured_content',
|
'section' => 'featured_content',
|
||||||
'type' => 'select',
|
'type' => 'select',
|
||||||
'choices' => array(
|
'choices' => array(
|
||||||
'grid' => __( 'Grid', 'twentyfourteen' ),
|
'grid' => __( 'Grid', 'twentyfourteen' ),
|
||||||
'slider' => __( 'Slider', 'twentyfourteen' ),
|
'slider' => __( 'Slider', 'twentyfourteen' ),
|
||||||
),
|
),
|
||||||
) );
|
) );
|
||||||
|
@ -68,15 +68,17 @@ class Featured_Content {
|
|||||||
if ( isset( $theme_support[0]['max_posts'] ) )
|
if ( isset( $theme_support[0]['max_posts'] ) )
|
||||||
self::$max_posts = absint( $theme_support[0]['max_posts'] );
|
self::$max_posts = absint( $theme_support[0]['max_posts'] );
|
||||||
|
|
||||||
add_filter( $filter, array( __CLASS__, 'get_featured_posts' ) );
|
add_filter( $filter, array( __CLASS__, 'get_featured_posts' ) );
|
||||||
add_action( 'admin_init', array( __CLASS__, 'register_setting' ) );
|
add_action( 'customize_register', array( __CLASS__, 'customize_register' ), 9 );
|
||||||
add_action( 'save_post', array( __CLASS__, 'delete_transient' ) );
|
add_action( 'admin_init', array( __CLASS__, 'register_setting' ) );
|
||||||
add_action( 'delete_post_tag', array( __CLASS__, 'delete_post_tag' ) );
|
add_action( 'save_post', array( __CLASS__, 'delete_transient' ) );
|
||||||
add_action( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ) );
|
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.
|
// Hide "featured" tag from the front-end.
|
||||||
if ( self::get_setting( 'hide-tag' ) ) {
|
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 );
|
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()
|
* @uses Featured_Content::get_featured_post_ids()
|
||||||
*
|
*
|
||||||
* @return array|bool
|
* @return array
|
||||||
*/
|
*/
|
||||||
public static function get_featured_posts() {
|
public static function get_featured_posts() {
|
||||||
$post_ids = self::get_featured_post_ids();
|
$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.
|
// No need to query if there is are no featured posts.
|
||||||
if ( empty( $post_ids ) )
|
if ( empty( $post_ids ) )
|
||||||
return array();
|
return array();
|
||||||
|
|
||||||
$featured_posts = get_posts( array(
|
$featured_posts = get_posts( array(
|
||||||
'include' => $post_ids,
|
'include' => $post_ids,
|
||||||
'posts_per_page' => count( $post_ids )
|
'posts_per_page' => count( $post_ids ),
|
||||||
) );
|
) );
|
||||||
|
|
||||||
return $featured_posts;
|
return $featured_posts;
|
||||||
@ -115,15 +113,20 @@ class Featured_Content {
|
|||||||
*
|
*
|
||||||
* Sets the "featured_content_ids" transient.
|
* 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() {
|
public static function get_featured_post_ids() {
|
||||||
$settings = self::get_setting();
|
$settings = self::get_setting();
|
||||||
|
|
||||||
// Return false if the user has disabled this feature.
|
// Return false if the user has disabled this feature.
|
||||||
$tag = $settings['tag-id'];
|
$tag = $settings['tag-id'];
|
||||||
if ( empty( $tag ) )
|
if ( empty( $tag ) ) {
|
||||||
return false;
|
$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.
|
// Return array of cached results if they exist.
|
||||||
$featured_ids = get_transient( 'featured_content_ids' );
|
$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 )
|
if ( ! $featured )
|
||||||
return array();
|
return self::get_sticky_posts();
|
||||||
|
|
||||||
// Ensure correct format before save/return.
|
// Ensure correct format before save/return.
|
||||||
$featured_ids = wp_list_pluck( (array) $featured, 'ID' );
|
$featured_ids = wp_list_pluck( (array) $featured, 'ID' );
|
||||||
@ -155,6 +158,16 @@ class Featured_Content {
|
|||||||
return $featured_ids;
|
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
|
* Delete transient
|
||||||
*
|
*
|
||||||
@ -221,10 +234,7 @@ class Featured_Content {
|
|||||||
public static function delete_post_tag( $tag_id ) {
|
public static function delete_post_tag( $tag_id ) {
|
||||||
$settings = self::get_setting();
|
$settings = self::get_setting();
|
||||||
|
|
||||||
if ( empty( $settings['tag-id'] ) )
|
if ( empty( $settings['tag-id'] ) || $tag_id != $settings['tag-id'] )
|
||||||
return;
|
|
||||||
|
|
||||||
if ( $tag_id != $settings['tag-id'] )
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
$settings['tag-id'] = 0;
|
$settings['tag-id'] = 0;
|
||||||
@ -259,7 +269,7 @@ class Featured_Content {
|
|||||||
|
|
||||||
foreach( $terms as $order => $term ) {
|
foreach( $terms as $order => $term ) {
|
||||||
if ( self::get_setting( 'tag-id' ) == $term->term_id && 'post_tag' == $term->taxonomy )
|
if ( self::get_setting( 'tag-id' ) == $term->term_id && 'post_tag' == $term->taxonomy )
|
||||||
unset( $terms[$order] );
|
unset( $terms[ $order ] );
|
||||||
}
|
}
|
||||||
|
|
||||||
return $terms;
|
return $terms;
|
||||||
@ -284,7 +294,7 @@ class Featured_Content {
|
|||||||
return $terms;
|
return $terms;
|
||||||
|
|
||||||
// Make sure we are in the correct taxonomy.
|
// Make sure we are in the correct taxonomy.
|
||||||
if ( ! 'post_tag' == $taxonomy )
|
if ( 'post_tag' != $taxonomy )
|
||||||
return $terms;
|
return $terms;
|
||||||
|
|
||||||
// No terms? Return early!
|
// No terms? Return early!
|
||||||
@ -293,7 +303,7 @@ class Featured_Content {
|
|||||||
|
|
||||||
foreach( $terms as $order => $term ) {
|
foreach( $terms as $order => $term ) {
|
||||||
if ( self::get_setting( 'tag-id' ) == $term->term_id )
|
if ( self::get_setting( 'tag-id' ) == $term->term_id )
|
||||||
unset( $terms[$term->term_id] );
|
unset( $terms[ $term->term_id ] );
|
||||||
}
|
}
|
||||||
|
|
||||||
return $terms;
|
return $terms;
|
||||||
@ -308,45 +318,66 @@ class Featured_Content {
|
|||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function register_setting() {
|
public static function register_setting() {
|
||||||
add_settings_field( 'featured-content', __( 'Featured content', 'twentyfourteen' ), array( __CLASS__, 'render_form' ), 'reading' );
|
register_setting( 'featured-content', 'featured-content', array( __CLASS__, 'validate_settings' ) );
|
||||||
register_setting( 'reading', '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() {
|
public static function customize_register( $wp_customize ) {
|
||||||
$settings = self::get_setting();
|
$wp_customize->add_section( 'featured_content', array(
|
||||||
|
'title' => __( 'Featured Content', 'twentyfourteen' ),
|
||||||
|
'priority' => 130,
|
||||||
|
'theme_supports' => 'featured-content',
|
||||||
|
) );
|
||||||
|
|
||||||
$tag_name = '';
|
// Add Featured Content settings.
|
||||||
if ( ! empty( $settings['tag-id'] ) ) {
|
$wp_customize->add_setting( 'featured-content[tag-name]', array(
|
||||||
$tag = get_term( $settings['tag-id'], 'post_tag' );
|
'default' => 'featured',
|
||||||
if ( ! is_wp_error( $tag ) && isset( $tag->name ) )
|
'type' => 'option',
|
||||||
$tag_name = $tag->name;
|
) );
|
||||||
}
|
$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 );
|
// Add Featured Content controls.
|
||||||
?>
|
$wp_customize->add_control( 'featured-content[tag-name]', array(
|
||||||
<div id="featured-content-ui">
|
'label' => __( 'Tag name', 'twentyfourteen' ),
|
||||||
<p>
|
'section' => 'featured_content',
|
||||||
<label for="featured-content-tag-name"><?php echo _e( 'Tag name:', 'twentyfourteen' ); ?></label>
|
'priority' => 20,
|
||||||
<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'] ); ?>">
|
$wp_customize->add_control( 'featured-content[hide-tag]', array(
|
||||||
</p>
|
'label' => __( 'Hide tag from displaying in post meta and tag clouds.', 'twentyfourteen' ),
|
||||||
<p>
|
'section' => 'featured_content',
|
||||||
<label for="featured-content-quantity"><?php _e( 'Number of posts:', 'twentyfourteen' ); ?></label>
|
'type' => 'checkbox',
|
||||||
<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'] ); ?>">
|
'priority' => 30,
|
||||||
</p>
|
) );
|
||||||
<p>
|
$wp_customize->add_control( new Featured_Content_Customize_Hidden_Control( $wp_customize, 'featured-content[tag-id]', array(
|
||||||
<input type="checkbox" id="featured-content-hide-tag" name="featured-content[hide-tag]" <?php checked( $settings['hide-tag'], 1 ); ?>>
|
'section' => 'featured_content',
|
||||||
<label for="featured-content-hide-tag"><?php _e( 'Hide tag from displaying in post meta and tag clouds.', 'twentyfourteen' ); ?></label>
|
'priority' => 999,
|
||||||
</p>
|
) ) );
|
||||||
</div>
|
|
||||||
<?php
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
* Get settings
|
||||||
*
|
*
|
||||||
@ -357,7 +388,7 @@ class Featured_Content {
|
|||||||
* In the event that you only require one setting, you may pass its name
|
* 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.
|
* 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.
|
* @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.
|
* @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 = array_intersect_key( $options, $defaults );
|
||||||
$options['quantity'] = self::sanitize_quantity( $options['quantity'] );
|
$options['quantity'] = self::sanitize_quantity( $options['quantity'] );
|
||||||
|
|
||||||
if ( 'all' != $key ) {
|
if ( 'all' != $key )
|
||||||
if ( isset( $options[$key] ) )
|
return isset( $options[ $key ] ) ? $options[ $key ] : false;
|
||||||
return $options[$key];
|
|
||||||
else
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $options;
|
return $options;
|
||||||
}
|
}
|
||||||
@ -405,20 +432,25 @@ class Featured_Content {
|
|||||||
if ( isset( $input['tag-id'] ) )
|
if ( isset( $input['tag-id'] ) )
|
||||||
$output['tag-id'] = absint( $input['tag-id'] );
|
$output['tag-id'] = absint( $input['tag-id'] );
|
||||||
|
|
||||||
if ( isset( $input['tag-name'] ) && ! empty( $input['tag-name'] ) ) {
|
if ( isset( $input['tag-name'] ) ) {
|
||||||
$new_tag = wp_create_tag( $input['tag-name'] );
|
if ( empty( $input['tag-name'] ) ) {
|
||||||
if ( ! is_wp_error( $new_tag ) && isset( $new_tag['term_id'] ) )
|
$output['tag-id'] = 0;
|
||||||
$tag = get_term( $new_tag['term_id'], 'post_tag' );
|
} else {
|
||||||
if ( isset( $tag->term_id ) )
|
$new_tag = wp_create_tag( $input['tag-name'] );
|
||||||
$output['tag-id'] = $tag->term_id;
|
if ( ! is_wp_error( $new_tag ) && isset( $new_tag['term_id'] ) )
|
||||||
} else {
|
$tag = get_term( $new_tag['term_id'], 'post_tag' );
|
||||||
unset( $output['tag-id'] );
|
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'] ) )
|
if ( isset( $input['quantity'] ) )
|
||||||
$output['quantity'] = self::sanitize_quantity( $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();
|
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();
|
Featured_Content::setup();
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
jQuery( document ).ready( function( $ ) {
|
jQuery( function( $ ) {
|
||||||
$( '#featured-content-tag-name' ).suggest( ajaxurl + '?action=ajax-tag-search&tax=post_tag', { delay: 500, minchars: 2 } );
|
$( '#customize-control-featured-content-tag-name input' ).suggest( featuredContent.ajaxurl + '?action=ajax-tag-search&tax=post_tag', { delay: 500, minchars: 2 } );
|
||||||
} );
|
} );
|
||||||
|
Loading…
Reference in New Issue
Block a user