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