diff --git a/functions.php b/functions.php index 04fd34b54..06d28d63d 100644 --- a/functions.php +++ b/functions.php @@ -111,10 +111,8 @@ function twentytwelve_scripts_styles() { * Depends on Theme Options setting. */ $options = $twentytwelve_options->get_theme_options(); - if ( $options['enable_fonts'] ) { - $protocol = is_ssl() ? 'https' : 'http'; - wp_enqueue_style( 'twentytwelve-fonts', "$protocol://fonts.googleapis.com/css?family=Open+Sans:400italic,700italic,400,700" ); - } + if ( $options['enable_fonts'] ) + wp_enqueue_style( 'twentytwelve-fonts', $twentytwelve_options->custom_fonts_url() ); /** * Load our main CSS file. @@ -373,15 +371,4 @@ function twentytwelve_content_width() { $content_width = 960; } } -add_action( 'template_redirect', 'twentytwelve_content_width' ); - -/** - * Bind JS handler to make Theme Customizer preview reload - * custom background `body_class` value changes asynchronously. - * - * @since Twenty Twelve 1.0 - */ -function twentytwelve_customize_preview_js() { - wp_enqueue_script( 'twentytwelve-customizer', get_template_directory_uri() . '/js/theme-customizer.js', array( 'customize-preview' ), '20120725', true ); -} -add_action( 'customize_preview_init', 'twentytwelve_customize_preview_js' ); \ No newline at end of file +add_action( 'template_redirect', 'twentytwelve_content_width' ); \ No newline at end of file diff --git a/inc/theme-options.php b/inc/theme-options.php index 8a6780ef0..ea2d77062 100644 --- a/inc/theme-options.php +++ b/inc/theme-options.php @@ -37,9 +37,10 @@ class Twenty_Twelve_Options { if ( 'twentytwelve' != get_stylesheet() ) $this->option_key = get_stylesheet() . '_theme_options'; - add_action( 'admin_init', array( $this, 'options_init' ) ); - add_action( 'admin_menu', array( $this, 'add_page' ) ); - add_action( 'customize_register', array( $this, 'customize_register' ) ); + add_action( 'admin_init', array( $this, 'options_init' ) ); + add_action( 'admin_menu', array( $this, 'add_page' ) ); + add_action( 'customize_register', array( $this, 'customize_register' ) ); + add_action( 'customize_preview_init', array( $this, 'customize_preview_js' ) ); } /** @@ -203,6 +204,11 @@ class Twenty_Twelve_Options { * @return void */ public function customize_register( $wp_customize ) { + + // Add postMessage support for site title and tagline + $wp_customize->get_setting( 'blogname' )->transport = 'postMessage'; + $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage'; + // Enable Web Fonts $wp_customize->add_section( $this->option_key . '_enable_fonts', array( 'title' => __( 'Fonts', 'twentytwelve' ), @@ -215,6 +221,7 @@ class Twenty_Twelve_Options { 'default' => $defaults['enable_fonts'], 'type' => 'option', 'capability' => 'edit_theme_options', + 'transport' => 'postMessage', ) ); $wp_customize->add_control( $this->option_key . '_enable_fonts', array( @@ -224,4 +231,33 @@ class Twenty_Twelve_Options { 'type' => 'checkbox', ) ); } + + /** + * Bind JS handlers to make Theme Customizer preview reload changes asynchronously. + * + * @since Twenty Twelve 1.0 + * @access public + * + * @return void + */ + public function customize_preview_js() { + wp_enqueue_script( 'twentytwelve-customizer', get_template_directory_uri() . '/js/theme-customizer.js', array( 'customize-preview' ), '20120725', true ); + wp_localize_script( 'twentytwelve-customizer', 'twentytwelve_customizer', array( + 'option_key' => $this->option_key, + 'link' => $this->custom_fonts_url(), + ) ); + } + + /** + * Create path to load fonts CSS file with correct protocol. + * + * @since Twenty Twelve 1.0 + * @access public + * + * @return string Path to load fonts CSS. + */ + public function custom_fonts_url() { + $protocol = is_ssl() ? 'https' : 'http'; + return $protocol . '://fonts.googleapis.com/css?family=Open+Sans:400italic,700italic,400,700'; + } } \ No newline at end of file diff --git a/js/theme-customizer.js b/js/theme-customizer.js index e11d2b775..4460812b9 100644 --- a/js/theme-customizer.js +++ b/js/theme-customizer.js @@ -1,4 +1,36 @@ -( function( $ ){ +/** + * Theme Customizer enhancements for a better user experience. + * + * Contains handlers to make Theme Customizer preview reload changes asynchronously. + * Things like fonts, site title and description, and background color changes. + * + * See related settings in Twenty_Twelve_Options::customize_preview_js() + */ + +( function( $ ) { + // Site title and description. + wp.customize( 'blogname', function( value ) { + value.bind( function( to ) { + $( '.site-title a' ).html( to ); + } ); + } ); + wp.customize( 'blogdescription', function( value ) { + value.bind( function( to ) { + $( '.site-description' ).html( to ); + } ); + } ); + + // Custom fonts. + wp.customize( twentytwelve_customizer.option_key + '[enable_fonts]', function( value ) { + value.bind( function( to ) { + if ( to ) { + $( 'head' ).append( '' ); + } else { + $( '#twentytwelve-fonts-css' ).remove(); + } + } ); + } ); + // Hook into background color change and adjust body class value as needed. wp.customize( 'background_color', function( value ) { var body = $( 'body' );