Make WordPress Core

Changeset 26021


Ignore:
Timestamp:
11/05/2013 11:28:07 PM (13 years ago)
Author:
lancewillett
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-content/themes/twentyfourteen/inc/customizer.php

    r26003 r26021  
    2323        $wp_customize->add_setting( 'accent_color', array(
    2424                'default'           => '#24890d',
    25                 'sanitize_callback' => 'twentyfourteen_generate_accent_colors',
     25                'sanitize_callback' => 'sanitize_hex_color',
    2626        ) );
    2727
     
    3131                'settings' => 'accent_color',
    3232        ) ) );
     33
     34        add_filter( 'theme_mod_accent_mid',  'twentyfourteen_accent_mid'  );
     35        add_filter( 'theme_mod_accent_light', 'twentyfourteen_accent_light' );
    3336
    3437        // Add the featured content section.
     
    6871
    6972/**
    70  * Generate two variants of the accent color, return the original, and
    71  * save the others as theme mods.
    72  *
    73  * @since Twenty Fourteen 1.0
    74  *
    75  * @param string $color The original color.
    76  * @return string $color The original color, sanitized.
    77  */
    78 function twentyfourteen_generate_accent_colors( $color ) {
    79         $color = sanitize_hex_color( $color );
    80 
    81         set_theme_mod( 'accent_lighter', twentyfourteen_adjust_color( $color, 29 ) );
    82         set_theme_mod( 'accent_much_lighter', twentyfourteen_adjust_color( $color, 49 ) );
    83 
    84         return $color;
    85 }
    86 
    87 /**
    8873 * Tweak the brightness of a color by adjusting the RGB values by the given interval.
    8974 *
     
    10792        $rgb = array( hexdec( substr( $color, 1, 2 ) ), hexdec( substr( $color, 3, 2 ) ), hexdec( substr( $color, 5, 2 ) ) );
    10893
    109         // Adjust color and switch back to hex.
     94        // Adjust color and switch back to 6-digit hex.
    11095        $hex = '#';
    111         foreach ( $rgb as $c ) {
    112                 $c += $steps;
    113                 if ( $c > 255 )
    114                         $c = 255;
    115                 elseif ( $c < 0 )
    116                         $c = 0;
    117                 $hex .= str_pad( dechex( $c ), 2, '0', STR_PAD_LEFT);
     96        foreach ( $rgb as $value ) {
     97                $value += $steps;
     98                if ( $value > 255 )
     99                        $value = 255;
     100                elseif ( $value < 0 )
     101                        $value = 0;
     102                $hex .= str_pad( dechex( $value ), 2, '0', STR_PAD_LEFT);
    118103        }
    119104
     
    121106}
    122107
     108 /**
     109 * Returns a slightly lighter color than what is set as the theme's
     110 * accent color.
     111 *
     112 * @since Twenty Fourteen 1.0
     113 *
     114 * @return string
     115 */
     116function twentyfourteen_accent_mid() {
     117        return twentyfourteen_adjust_color( get_theme_mod( 'accent_color' ), 29 );
     118}
     119
     120/**
     121 * Returns a lighter color than what is set as the theme's accent color.
     122 *
     123 * @since Twenty Fourteen 1.0
     124 *
     125 * @return string
     126 */
     127function twentyfourteen_accent_light() {
     128        return twentyfourteen_adjust_color( get_theme_mod( 'accent_color' ), 49 );
     129}
     130
     131/**
     132 * Caches the generated variants of the theme's accent color.
     133 *
     134 * @since Twenty Fourteen 1.0
     135 *
     136 * @return void
     137 */
     138function twentyfourteen_rebuild_accent_colors() {
     139        set_theme_mod( 'accent_mid',  twentyfourteen_accent_mid()  );
     140        set_theme_mod( 'accent_light', twentyfourteen_accent_light() );
     141}
     142add_action( 'update_option_theme_mods_twentyfourteen', 'twentyfourteen_rebuild_accent_colors' );
     143
    123144/**
    124145 * Output the CSS for the Theme Customizer options.
     
    129150 */
    130151function twentyfourteen_customizer_styles() {
    131         $accent_color = get_theme_mod( 'accent_color' );
     152        $accent_color = get_theme_mod( 'accent_color', '#24890d' );
    132153
    133154        // Don't do anything if the current color is the default.
     
    135156                return;
    136157
    137         $accent_lighter = get_theme_mod( 'accent_lighter' );
    138         $accent_much_lighter = get_theme_mod( 'accent_much_lighter' );
     158        $accent_mid = get_theme_mod( 'accent_mid' );
     159        $accent_light = get_theme_mod( 'accent_light' );
    139160
    140161        $css = '/* Custom accent color. */
     
    193214                }
    194215
    195                 /* Generated variant of custom accent color: slightly lighter. */
     216                /* Generated "mid" variant of custom accent color. */
    196217                button:hover,
    197218                button:focus,
     
    221242                .content-sidebar .widget input[type="submit"]:focus,
    222243                .slider-control-paging a:hover:before {
    223                         background-color: ' . $accent_lighter . ';
     244                        background-color: ' . $accent_mid . ';
    224245                }
    225246
     
    246267                .site-info a:hover,
    247268                .featured-content a:hover {
    248                         color: ' . $accent_lighter . ';
     269                        color: ' . $accent_mid . ';
    249270                }
    250271
    251272                .page-links a:hover,
    252273                .paging-navigation a:hover {
    253                         border-color: ' . $accent_lighter . ';
     274                        border-color: ' . $accent_mid . ';
    254275                }
    255276
    256277                .tag-links a:hover:before {
    257                         border-right-color: ' . $accent_lighter . ';
     278                        border-right-color: ' . $accent_mid . ';
    258279                }
    259280
     
    261282                        .primary-navigation ul ul a:hover,
    262283                        .primary-navigation ul ul li.focus > a {
    263                                 background-color: ' . $accent_lighter . ';
     284                                background-color: ' . $accent_mid . ';
    264285                        }
    265286                }
     
    268289                        .secondary-navigation ul ul a:hover,
    269290                        .secondary-navigation ul ul li.focus > a {
    270                                 background-color: ' . $accent_lighter . ';
     291                                background-color: ' . $accent_mid . ';
    271292                        }
    272293                }
    273294
    274                 /* Generated variant of custom accent color: much lighter. */
     295                /* Generated "light" variant of custom accent color. */
    275296                button:active,
    276297                .contributor-posts-link:active,
     
    284305                .content-sidebar .widget input[type="reset"]:active,
    285306                .content-sidebar .widget input[type="submit"]:active {
    286                         background-color: ' . $accent_much_lighter . ';
     307                        background-color: ' . $accent_light . ';
    287308                }
    288309
     
    291312                .site-navigation .current-menu-item > a,
    292313                .site-navigation .current-menu-ancestor > a {
    293                         color: ' . $accent_much_lighter . ';
     314                        color: ' . $accent_light . ';
    294315                }';
    295316
Note: See TracChangeset for help on using the changeset viewer.