From daad8899b10baceca6a6485f6fd5a9c2b6f19e28 Mon Sep 17 00:00:00 2001 From: Lance Willett Date: Thu, 7 Nov 2013 20:24:24 +0000 Subject: [PATCH] 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 --- .../themes/twentyfourteen/functions.php | 2 +- .../themes/twentyfourteen/inc/customizer.php | 11 +- .../twentyfourteen/inc/featured-content.php | 182 +++++++++++------- .../js/featured-content-admin.js | 6 +- 4 files changed, 120 insertions(+), 81 deletions(-) diff --git a/src/wp-content/themes/twentyfourteen/functions.php b/src/wp-content/themes/twentyfourteen/functions.php index 7c8274f5f4..18c266863b 100644 --- a/src/wp-content/themes/twentyfourteen/functions.php +++ b/src/wp-content/themes/twentyfourteen/functions.php @@ -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 ); diff --git a/src/wp-content/themes/twentyfourteen/inc/customizer.php b/src/wp-content/themes/twentyfourteen/inc/customizer.php index 9d82665620..d7043df954 100644 --- a/src/wp-content/themes/twentyfourteen/inc/customizer.php +++ b/src/wp-content/themes/twentyfourteen/inc/customizer.php @@ -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' ), ), ) ); diff --git a/src/wp-content/themes/twentyfourteen/inc/featured-content.php b/src/wp-content/themes/twentyfourteen/inc/featured-content.php index 0b0101ffe1..92c4d8e933 100644 --- a/src/wp-content/themes/twentyfourteen/inc/featured-content.php +++ b/src/wp-content/themes/twentyfourteen/inc/featured-content.php @@ -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 ); - ?> - - 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() { + ?> + link(); ?> value="value() ); ?>"> +