[ XREF Home ] [ Index ]

PHP Cross Reference of WordPress Trunk

Provided by Yoast

title

Body

[close]

/wp-content/themes/twentyeleven/inc/ -> theme-options.php (source)

   1  <?php
   2  /**
   3   * Twenty Eleven Theme Options
   4   *
   5   * @package WordPress
   6   * @subpackage Twenty_Eleven
   7   * @since Twenty Eleven 1.0
   8   */
   9  
  10  /**
  11   * Properly enqueue styles and scripts for our theme options page.
  12   *
  13   * This function is attached to the admin_enqueue_scripts action hook.
  14   *
  15   * @since Twenty Eleven 1.0
  16   *
  17   * @param string $hook_suffix The action passes the current page to the function. We don't
  18   *     do anything if we're not on our theme options page.
  19   */
  20  function twentyeleven_admin_enqueue_scripts( $hook_suffix ) {
  21      if ( $hook_suffix != 'appearance_page_theme_options' )
  22          return;
  23  
  24      wp_enqueue_style( 'twentyeleven-theme-options', get_template_directory_uri() . '/inc/theme-options.css', false, '2011-04-28' );
  25      wp_enqueue_script( 'twentyeleven-theme-options', get_template_directory_uri() . '/inc/theme-options.js', array( 'farbtastic' ), '2011-04-28' );
  26      wp_enqueue_style( 'farbtastic' );
  27  }
  28  add_action( 'admin_enqueue_scripts', 'twentyeleven_admin_enqueue_scripts' );
  29  
  30  /**
  31   * Register the form setting for our twentyeleven_options array.
  32   *
  33   * This function is attached to the admin_init action hook.
  34   *
  35   * This call to register_setting() registers a validation callback, twentyeleven_theme_options_validate(),
  36   * which is used when the option is saved, to ensure that our option values are complete, properly
  37   * formatted, and safe.
  38   *
  39   * We also use this function to add our theme option if it doesn't already exist.
  40   *
  41   * @since Twenty Eleven 1.0
  42   */
  43  function twentyeleven_theme_options_init() {
  44  
  45      // If we have no options in the database, let's add them now.
  46      if ( false === twentyeleven_get_theme_options() )
  47          add_option( 'twentyeleven_theme_options', twentyeleven_get_default_theme_options() );
  48  
  49      register_setting(
  50          'twentyeleven_options',       // Options group, see settings_fields() call in theme_options_render_page()
  51          'twentyeleven_theme_options', // Database option, see twentyeleven_get_theme_options()
  52          'twentyeleven_theme_options_validate' // The sanitization callback, see twentyeleven_theme_options_validate()
  53      );
  54  }
  55  add_action( 'admin_init', 'twentyeleven_theme_options_init' );
  56  
  57  /**
  58   * Change the capability required to save the 'twentyeleven_options' options group.
  59   *
  60   * @see twentyeleven_theme_options_init() First parameter to register_setting() is the name of the options group.
  61   * @see twentyeleven_theme_options_add_page() The edit_theme_options capability is used for viewing the page.
  62   *
  63   * By default, the options groups for all registered settings require the manage_options capability.
  64   * This filter is required to change our theme options page to edit_theme_options instead.
  65   * By default, only administrators have either of these capabilities, but the desire here is
  66   * to allow for finer-grained control for roles and users.
  67   *
  68   * @param string $capability The capability used for the page, which is manage_options by default.
  69   * @return string The capability to actually use.
  70   */
  71  function twentyeleven_option_page_capability( $capability ) {
  72      return 'edit_theme_options';
  73  }
  74  add_filter( 'option_page_capability_twentyeleven_options', 'twentyeleven_option_page_capability' );
  75  
  76  /**
  77   * Add our theme options page to the admin menu.
  78   *
  79   * This function is attached to the admin_menu action hook.
  80   *
  81   * @since Twenty Eleven 1.0
  82   */
  83  function twentyeleven_theme_options_add_page() {
  84      add_theme_page(
  85          __( 'Theme Options', 'twentyeleven' ), // Name of page
  86          __( 'Theme Options', 'twentyeleven' ), // Label in menu
  87          'edit_theme_options',                  // Capability required
  88          'theme_options',                       // Menu slug, used to uniquely identify the page
  89          'theme_options_render_page'            // Function that renders the options page
  90      );
  91  }
  92  add_action( 'admin_menu', 'twentyeleven_theme_options_add_page' );
  93  
  94  /**
  95   * Returns an array of color schemes registered for Twenty Eleven.
  96   *
  97   * @since Twenty Eleven 1.0
  98   */
  99  function twentyeleven_color_schemes() {
 100      $color_scheme_options = array(
 101          'light' => array(
 102              'value' => 'light',
 103              'label' => __( 'Light', 'twentyeleven' ),
 104              'thumbnail' => get_template_directory_uri() . '/inc/images/light.png',
 105          ),
 106          'dark' => array(
 107              'value' => 'dark',
 108              'label' => __( 'Dark', 'twentyeleven' ),
 109              'thumbnail' => get_template_directory_uri() . '/inc/images/dark.png',
 110          ),
 111      );
 112  
 113      return apply_filters( 'twentyeleven_color_schemes', $color_scheme_options );
 114  }
 115  
 116  /**
 117   * Returns an array of layout options registered for Twenty Eleven.
 118   *
 119   * @since Twenty Eleven 1.0
 120   */
 121  function twentyeleven_layouts() {
 122      $layout_options = array(
 123          'content-sidebar' => array(
 124              'value' => 'content-sidebar',
 125              'label' => __( 'Content on left', 'twentyeleven' ),
 126              'thumbnail' => get_template_directory_uri() . '/inc/images/content-sidebar.png',
 127          ),
 128          'sidebar-content' => array(
 129              'value' => 'sidebar-content',
 130              'label' => __( 'Content on right', 'twentyeleven' ),
 131              'thumbnail' => get_template_directory_uri() . '/inc/images/sidebar-content.png',
 132          ),
 133          'content' => array(
 134              'value' => 'content',
 135              'label' => __( 'One-column, no Sidebar', 'twentyeleven' ),
 136              'thumbnail' => get_template_directory_uri() . '/inc/images/content.png',
 137          ),
 138      );
 139  
 140      return apply_filters( 'twentyeleven_layouts', $layout_options );
 141  }
 142  
 143  /**
 144   * Returns the default options for Twenty Eleven.
 145   *
 146   * @since Twenty Eleven 1.0
 147   */
 148  function twentyeleven_get_default_theme_options() {
 149      $default_theme_options = array(
 150          'color_scheme' => 'light',
 151          'link_color'   => '#1b8be0',
 152          'theme_layout' => 'content-sidebar',
 153      );
 154  
 155      return apply_filters( 'twentyeleven_default_theme_options', $default_theme_options );
 156  }
 157  
 158  /**
 159   * Returns the options array for Twenty Eleven.
 160   *
 161   * @since Twenty Eleven 1.0
 162   */
 163  function twentyeleven_get_theme_options() {
 164      return get_option( 'twentyeleven_theme_options' );
 165  }
 166  
 167  /**
 168   * Returns the options array for Twenty Eleven.
 169   *
 170   * @since Twenty Eleven 1.0
 171   */
 172  function theme_options_render_page() {
 173      ?>
 174      <div class="wrap">
 175          <?php screen_icon(); ?>
 176          <h2><?php printf( __( '%s Theme Options', 'twentyeleven' ), get_current_theme() ); ?></h2>
 177          <?php settings_errors(); ?>
 178  
 179          <form method="post" action="options.php">
 180              <?php
 181                  settings_fields( 'twentyeleven_options' );
 182                  $options = twentyeleven_get_theme_options();
 183                  $default_options = twentyeleven_get_default_theme_options();
 184              ?>
 185  
 186              <table class="form-table">
 187  
 188                  <tr valign="top" class="image-radio-option"><th scope="row"><?php _e( 'Color Scheme', 'twentyeleven' ); ?></th>
 189                      <td>
 190                          <fieldset><legend class="screen-reader-text"><span><?php _e( 'Color Scheme', 'twentyeleven' ); ?></span></legend>
 191                          <?php
 192                              foreach ( twentyeleven_color_schemes() as $color ) {
 193                                  ?>
 194                                  <div class="layout">
 195                                  <label class="description">
 196                                      <input type="radio" name="twentyeleven_theme_options[color_scheme]" value="<?php echo esc_attr( $color['value'] ); ?>" <?php checked( $options['color_scheme'], $color['value'] ); ?> />
 197                                      <span>
 198                                          <img src="<?php echo esc_url( $color['thumbnail'] ); ?>" alt=""/>
 199                                          <?php echo $color['label']; ?>
 200                                      </span>
 201                                  </label>
 202                                  </div>
 203                                  <?php
 204                              }
 205                          ?>
 206                          </fieldset>
 207                      </td>
 208                  </tr>
 209  
 210                  <tr valign="top"><th scope="row"><?php _e( 'Link Color', 'twentyeleven' ); ?></th>
 211                      <td>
 212                          <fieldset><legend class="screen-reader-text"><span><?php _e( 'Link Color', 'twentyeleven' ); ?></span></legend>
 213                              <input type="text" name="twentyeleven_theme_options[link_color]" id="link-color" value="<?php echo esc_attr( $options['link_color'] ); ?>" />
 214                              <a href="#" class="pickcolor hide-if-no-js" id="link-color-example"></a>
 215                              <input type="button" class="pickcolor button hide-if-no-js" value="<?php esc_attr_e( 'Select a Color', 'twentyeleven' ); ?>" />
 216                              <div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
 217                              <br />
 218                              <small class="description"><?php printf( __( 'Default color: %s', 'twentyeleven' ), $default_options['link_color'] ); ?></small>
 219                          </fieldset>
 220                      </td>
 221                  </tr>
 222  
 223                  <tr valign="top" class="image-radio-option"><th scope="row"><?php _e( 'Layout', 'twentyeleven' ); ?></th>
 224                      <td>
 225                          <fieldset><legend class="screen-reader-text"><span><?php _e( 'Color Scheme', 'twentyeleven' ); ?></span></legend>
 226                          <?php
 227                              foreach ( twentyeleven_layouts() as $layout ) {
 228                                  ?>
 229                                  <div class="layout">
 230                                  <label class="description">
 231                                      <input type="radio" name="twentyeleven_theme_options[theme_layout]" value="<?php echo esc_attr( $layout['value'] ); ?>" <?php checked( $options['theme_layout'], $layout['value'] ); ?> />
 232                                      <span>
 233                                          <img src="<?php echo esc_url( $layout['thumbnail'] ); ?>" alt=""/>
 234                                          <?php echo $layout['label']; ?>
 235                                      </span>
 236                                  </label>
 237                                  </div>
 238                                  <?php
 239                              }
 240                          ?>
 241                          </fieldset>
 242                      </td>
 243                  </tr>
 244              </table>
 245  
 246              <?php submit_button(); ?>
 247          </form>
 248      </div>
 249      <?php
 250  }
 251  
 252  /**
 253   * Sanitize and validate form input. Accepts an array, return a sanitized array.
 254   *
 255   * @see twentyeleven_theme_options_init()
 256   * @todo set up Reset Options action
 257   *
 258   * @since Twenty Eleven 1.0
 259   */
 260  function twentyeleven_theme_options_validate( $input ) {
 261      $output = $defaults = twentyeleven_get_default_theme_options();
 262  
 263      // Color scheme must be in our array of color scheme options
 264      if ( isset( $input['color_scheme'] ) && array_key_exists( $input['color_scheme'], twentyeleven_color_schemes() ) )
 265          $output['color_scheme'] = $input['color_scheme'];
 266  
 267      // Link color must be 3 or 6 hexadecimal characters
 268      if ( isset( $input['link_color'] ) && preg_match( '/^#?([a-f0-9]{3}){1,2}$/i', $input['link_color'] ) )
 269              $output['link_color'] = '#' . strtolower( ltrim( $input['link_color'], '#' ) );
 270  
 271      // Theme layout must be in our array of theme layout options
 272      if ( isset( $input['theme_layout'] ) && array_key_exists( $input['theme_layout'], twentyeleven_layouts() ) )
 273          $output['theme_layout'] = $input['theme_layout'];
 274  
 275      return apply_filters( 'twentyeleven_theme_options_validate', $output, $input, $defaults );
 276  }
 277  
 278  /**
 279   * Enqueue the styles for the current color scheme.
 280   *
 281   * @since Twenty Eleven 1.0
 282   */
 283  function twentyeleven_enqueue_color_scheme() {
 284      $options = twentyeleven_get_theme_options();
 285      $color_scheme = $options['color_scheme'];
 286  
 287      if ( 'dark' == $color_scheme )
 288          wp_enqueue_style( 'dark', get_template_directory_uri() . '/colors/dark.css', array(), null );
 289  
 290      do_action( 'twentyeleven_enqueue_color_scheme', $color_scheme );
 291  }
 292  add_action( 'wp_enqueue_scripts', 'twentyeleven_enqueue_color_scheme' );
 293  
 294  /**
 295   * Add a style block to the theme for the current link color.
 296   *
 297   * This function is attached to the wp_head action hook.
 298   *
 299   * @since Twenty Eleven 1.0
 300   */
 301  function twentyeleven_print_link_color_style() {
 302      $options = twentyeleven_get_theme_options();
 303      $link_color = $options['link_color'];
 304  
 305      $default_options = twentyeleven_get_default_theme_options();
 306  
 307      // Don't do anything if the current link color is the default.
 308      if ( $default_options['link_color'] == $link_color )
 309          return;
 310  ?>
 311      <style>
 312          /* Link color */
 313          a,
 314          #site-title a:focus,
 315          #site-title a:hover,
 316          #site-title a:active,
 317          .entry-title a:hover,
 318          .entry-title a:focus,
 319          .entry-title a:active,
 320          .widget_twentyeleven_ephemera .comments-link a:hover,
 321          section.recent-posts .other-recent-posts a[rel="bookmark"]:hover,
 322          section.recent-posts .other-recent-posts .comments-link a:hover,
 323          .format-image footer.entry-meta a:hover,
 324          #site-generator a:hover {
 325              color: <?php echo $link_color; ?>;
 326          }
 327          section.recent-posts .other-recent-posts .comments-link a:hover {
 328              border-color: <?php echo $link_color; ?>;
 329          }    
 330          article.feature-image.small .entry-summary p a:hover,
 331          .entry-header .comments-link a:hover,
 332          .entry-header .comments-link a:focus,
 333          .entry-header .comments-link a:active {
 334              background: <?php echo $link_color; ?>;
 335          }
 336      </style>
 337  <?php
 338  }
 339  add_action( 'wp_head', 'twentyeleven_print_link_color_style' );
 340  
 341  /**
 342   * Adds Twenty Eleven layout classes to the array of body classes.
 343   *
 344   * @since Twenty Eleven 1.0
 345   */
 346  function twentyeleven_layout_classes( $existing_classes ) {
 347      $options = twentyeleven_get_theme_options();
 348      $current_layout = $options['theme_layout'];
 349  
 350      if ( in_array( $current_layout, array( 'content-sidebar', 'sidebar-content' ) ) )
 351          $classes = array( 'two-column' );
 352      else
 353          $classes = array( 'one-column' );
 354  
 355      $classes[] = $current_layout;
 356  
 357      $classes = apply_filters( 'twentyeleven_layout_classes', $classes, $current_layout );
 358  
 359      return array_merge( $existing_classes, $classes );
 360  }
 361  add_filter( 'body_class', 'twentyeleven_layout_classes' );


Generated: Wed Jun 1 08:30:02 2011 Cross-referenced by PHPXref 0.7
Provided by Yoast and awesome WordPress Hosting