Make WordPress Core


Ignore:
Timestamp:
04/27/2011 11:03:27 PM (13 years ago)
Author:
lancewillett
Message:

Twenty Eleven: theme options - see #17198

  • First pass at Link Color CSS rules
  • Add new function to return default values
  • Implement better validation for hex color value
  • Fix missing esc_attr()
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-content/themes/twentyeleven/inc/theme-options/theme-options.php

    r17721 r17732  
    7777
    7878/**
    79  *  Return the current Twenty Eleven theme options, with default values as fallback
    80  */
    81 function twentyeleven_get_theme_options() {
    82     $defaults = array(
     79 *  Return the default Twenty Eleven theme option values
     80 */
     81function twentyeleven_get_default_theme_options() {
     82    return array(
    8383        'color_scheme' => 'light',
    84         'link_color' => '1b8be0',
     84        'link_color' => '#1b8be0',
    8585        'theme_layout' => 'content-sidebar',
    8686    );
     87}
     88
     89/**
     90 *  Return the current Twenty Eleven theme options, with default values as fallback
     91 */
     92function twentyeleven_get_theme_options() {
     93    $defaults = twentyeleven_get_default_theme_options();
    8794    $options = get_option( 'twentyeleven_theme_options', $defaults );
    8895
     
    206213
    207214            <p class="submit">
    208                 <input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'twentyeleven' ); ?>" />
     215                <input type="submit" class="button-primary" value="<?php esc_attr_e( 'Save Options', 'twentyeleven' ); ?>" />
    209216            </p>
    210217        </form>
     
    215222/**
    216223 * Sanitize and validate input. Accepts an array, return a sanitized array.
     224 *
     225 * todo set up Reset Options action
    217226 */
    218227function twentyeleven_theme_options_validate( $input ) {
    219     // todo get defaults, and use insteadd of null for fallback
    220     // could also be used to trigger a Reset Options action
    221 
    222     // Our color scheme option must actually be in our array of color scheme options
    223     if ( ! isset( $input['color_scheme'] ) )
    224         $input['color_scheme'] = null;
    225     if ( ! array_key_exists( $input['color_scheme'], twentyeleven_color_schemes() ) )
    226         $input['color_scheme'] = null;
    227 
    228     // Our link color option must be safe text with no HTML tags
    229     $input['link_color'] = wp_filter_nohtml_kses( $input['link_color'] );
    230 
    231     // Our theme layout option must actually be in our array of theme layout options
    232     if ( ! isset( $input['theme_layout'] ) )
    233         $input['theme_layout'] = null;
    234     if ( ! array_key_exists( $input['theme_layout'], twentyeleven_layouts() ) )
    235         $input['theme_layout'] = null;
     228    $defaults = twentyeleven_get_default_theme_options();
     229
     230    // Color scheme must be in our array of color scheme options
     231    if ( ! isset( $input['color_scheme'] ) || ! array_key_exists( $input['color_scheme'], twentyeleven_color_schemes() ) )
     232        $input['color_scheme'] = $defaults['color_scheme'];
     233
     234    // Link color must be 3 or 6 hexadecimal characters
     235    if ( ! isset( $input[ 'link_color' ] ) ) {
     236        $input['link_color'] = $defaults['link_color'];
     237    } else {
     238        if ( preg_match( '/^#?([a-f0-9]{3}){1,2}$/i', $input['link_color'] ) ) {
     239            $link_color = $input['link_color'];
     240            // If color value doesn't have a preceding hash, add it
     241            if ( false === strpos( $link_color, '#' ) )
     242                $link_color = '#' . $link_color;
     243        } else {
     244            $input['link_color'] = $defaults['link_color'];
     245        }
     246    }
     247
     248    // Theme layout must be in our array of theme layout options
     249    if ( ! isset( $input['theme_layout'] ) || ! array_key_exists( $input['theme_layout'], twentyeleven_layouts() ) )
     250        $input['theme_layout'] = $defaults['theme_layout'];
    236251
    237252    return $input;
     
    287302
    288303    // Is the link color just the default color?
    289     if ( '1b8be0' == $current_link_color ) :
     304    if ( '#1b8be0' == $current_link_color ) :
    290305        return; // we don't need to do anything then
    291 
    292306    else :
    293307        ?>
     
    296310                a,
    297311                .entry-title a:hover {
    298                     color: <?php echo $current_link_color ?>;
     312                    color: <?php echo $current_link_color; ?>;
    299313                }
    300314            </style>
Note: See TracChangeset for help on using the changeset viewer.