Twenty Fourteen: improvements to Accent Color behavior, including a case where options were published too soon. Props celloexpressions, fixes #25580.

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


git-svn-id: http://core.svn.wordpress.org/trunk@25952 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Lance Willett 2013-11-05 23:29:08 +00:00
parent c1b0fa9039
commit 16831f259d

View File

@ -22,7 +22,7 @@ function twentyfourteen_customize_register( $wp_customize ) {
// Add the custom accent color setting and control.
$wp_customize->add_setting( 'accent_color', array(
'default' => '#24890d',
'sanitize_callback' => 'twentyfourteen_generate_accent_colors',
'sanitize_callback' => 'sanitize_hex_color',
) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'accent_color', array(
@ -31,6 +31,9 @@ function twentyfourteen_customize_register( $wp_customize ) {
'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.
$wp_customize->add_section( 'featured_content', array(
'title' => __( 'Featured Content', 'twentyfourteen' ),
@ -66,24 +69,6 @@ function twentyfourteen_customize_preview_js() {
}
add_action( 'customize_preview_init', 'twentyfourteen_customize_preview_js' );
/**
* Generate two variants of the accent color, return the original, and
* save the others as theme mods.
*
* @since Twenty Fourteen 1.0
*
* @param string $color The original color.
* @return string $color The original color, sanitized.
*/
function twentyfourteen_generate_accent_colors( $color ) {
$color = sanitize_hex_color( $color );
set_theme_mod( 'accent_lighter', twentyfourteen_adjust_color( $color, 29 ) );
set_theme_mod( 'accent_much_lighter', twentyfourteen_adjust_color( $color, 49 ) );
return $color;
}
/**
* Tweak the brightness of a color by adjusting the RGB values by the given interval.
*
@ -106,20 +91,56 @@ function twentyfourteen_adjust_color( $color, $steps ) {
// Convert hex to rgb.
$rgb = array( hexdec( substr( $color, 1, 2 ) ), hexdec( substr( $color, 3, 2 ) ), hexdec( substr( $color, 5, 2 ) ) );
// Adjust color and switch back to hex.
// Adjust color and switch back to 6-digit hex.
$hex = '#';
foreach ( $rgb as $c ) {
$c += $steps;
if ( $c > 255 )
$c = 255;
elseif ( $c < 0 )
$c = 0;
$hex .= str_pad( dechex( $c ), 2, '0', STR_PAD_LEFT);
foreach ( $rgb as $value ) {
$value += $steps;
if ( $value > 255 )
$value = 255;
elseif ( $value < 0 )
$value = 0;
$hex .= str_pad( dechex( $value ), 2, '0', STR_PAD_LEFT);
}
return $hex;
}
/**
* Returns a slightly lighter color than what is set as the theme's
* accent color.
*
* @since Twenty Fourteen 1.0
*
* @return string
*/
function twentyfourteen_accent_mid() {
return twentyfourteen_adjust_color( get_theme_mod( 'accent_color' ), 29 );
}
/**
* Returns a lighter color than what is set as the theme's accent color.
*
* @since Twenty Fourteen 1.0
*
* @return string
*/
function twentyfourteen_accent_light() {
return twentyfourteen_adjust_color( get_theme_mod( 'accent_color' ), 49 );
}
/**
* Caches the generated variants of the theme's accent color.
*
* @since Twenty Fourteen 1.0
*
* @return void
*/
function twentyfourteen_rebuild_accent_colors() {
set_theme_mod( 'accent_mid', twentyfourteen_accent_mid() );
set_theme_mod( 'accent_light', twentyfourteen_accent_light() );
}
add_action( 'update_option_theme_mods_twentyfourteen', 'twentyfourteen_rebuild_accent_colors' );
/**
* Output the CSS for the Theme Customizer options.
*
@ -128,14 +149,14 @@ function twentyfourteen_adjust_color( $color, $steps ) {
* @return void
*/
function twentyfourteen_customizer_styles() {
$accent_color = get_theme_mod( 'accent_color' );
$accent_color = get_theme_mod( 'accent_color', '#24890d' );
// Don't do anything if the current color is the default.
if ( '#24890d' === $accent_color )
return;
$accent_lighter = get_theme_mod( 'accent_lighter' );
$accent_much_lighter = get_theme_mod( 'accent_much_lighter' );
$accent_mid = get_theme_mod( 'accent_mid' );
$accent_light = get_theme_mod( 'accent_light' );
$css = '/* Custom accent color. */
a,
@ -192,7 +213,7 @@ function twentyfourteen_customizer_styles() {
}
}
/* Generated variant of custom accent color: slightly lighter. */
/* Generated "mid" variant of custom accent color. */
button:hover,
button:focus,
.contributor-posts-link:hover,
@ -220,7 +241,7 @@ function twentyfourteen_customizer_styles() {
.content-sidebar .widget input[type="submit"]:hover,
.content-sidebar .widget input[type="submit"]:focus,
.slider-control-paging a:hover:before {
background-color: ' . $accent_lighter . ';
background-color: ' . $accent_mid . ';
}
a:active,
@ -245,33 +266,33 @@ function twentyfourteen_customizer_styles() {
.content-sidebar .widget_twentyfourteen_ephemera .entry-meta a:hover,
.site-info a:hover,
.featured-content a:hover {
color: ' . $accent_lighter . ';
color: ' . $accent_mid . ';
}
.page-links a:hover,
.paging-navigation a:hover {
border-color: ' . $accent_lighter . ';
border-color: ' . $accent_mid . ';
}
.tag-links a:hover:before {
border-right-color: ' . $accent_lighter . ';
border-right-color: ' . $accent_mid . ';
}
@media screen and (min-width: 782px) {
.primary-navigation ul ul a:hover,
.primary-navigation ul ul li.focus > a {
background-color: ' . $accent_lighter . ';
background-color: ' . $accent_mid . ';
}
}
@media screen and (min-width: 1008px) {
.secondary-navigation ul ul a:hover,
.secondary-navigation ul ul li.focus > a {
background-color: ' . $accent_lighter . ';
background-color: ' . $accent_mid . ';
}
}
/* Generated variant of custom accent color: much lighter. */
/* Generated "light" variant of custom accent color. */
button:active,
.contributor-posts-link:active,
input[type="button"]:active,
@ -283,14 +304,14 @@ function twentyfourteen_customizer_styles() {
.content-sidebar .widget input[type="button"]:active,
.content-sidebar .widget input[type="reset"]:active,
.content-sidebar .widget input[type="submit"]:active {
background-color: ' . $accent_much_lighter . ';
background-color: ' . $accent_light . ';
}
.site-navigation .current_page_item > a,
.site-navigation .current_page_ancestor > a,
.site-navigation .current-menu-item > a,
.site-navigation .current-menu-ancestor > a {
color: ' . $accent_much_lighter . ';
color: ' . $accent_light . ';
}';