]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-content/themes/twentyeleven/inc/theme-options.php
Wordpress 3.3
[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 twentyeleven_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         // Register our settings field group
51         add_settings_section(
52                 'general', // Unique identifier for the settings section
53                 '', // Section title (we don't want one)
54                 '__return_false', // Section callback (we don't want anything)
55                 'theme_options' // Menu slug, used to uniquely identify the page; see twentyeleven_theme_options_add_page()
56         );
57
58         // Register our individual settings fields
59         add_settings_field(
60                 'color_scheme',  // Unique identifier for the field for this section
61                 __( 'Color Scheme', 'twentyeleven' ), // Setting field label
62                 'twentyeleven_settings_field_color_scheme', // Function that renders the settings field
63                 'theme_options', // Menu slug, used to uniquely identify the page; see twentyeleven_theme_options_add_page()
64                 'general' // Settings section. Same as the first argument in the add_settings_section() above
65         );
66
67         add_settings_field( 'link_color', __( 'Link Color',     'twentyeleven' ), 'twentyeleven_settings_field_link_color', 'theme_options', 'general' );
68         add_settings_field( 'layout',     __( 'Default Layout', 'twentyeleven' ), 'twentyeleven_settings_field_layout',     'theme_options', 'general' );
69 }
70 add_action( 'admin_init', 'twentyeleven_theme_options_init' );
71
72 /**
73  * Change the capability required to save the 'twentyeleven_options' options group.
74  *
75  * @see twentyeleven_theme_options_init() First parameter to register_setting() is the name of the options group.
76  * @see twentyeleven_theme_options_add_page() The edit_theme_options capability is used for viewing the page.
77  *
78  * By default, the options groups for all registered settings require the manage_options capability.
79  * This filter is required to change our theme options page to edit_theme_options instead.
80  * By default, only administrators have either of these capabilities, but the desire here is
81  * to allow for finer-grained control for roles and users.
82  *
83  * @param string $capability The capability used for the page, which is manage_options by default.
84  * @return string The capability to actually use.
85  */
86 function twentyeleven_option_page_capability( $capability ) {
87         return 'edit_theme_options';
88 }
89 add_filter( 'option_page_capability_twentyeleven_options', 'twentyeleven_option_page_capability' );
90
91 /**
92  * Add our theme options page to the admin menu, including some help documentation.
93  *
94  * This function is attached to the admin_menu action hook.
95  *
96  * @since Twenty Eleven 1.0
97  */
98 function twentyeleven_theme_options_add_page() {
99         $theme_page = add_theme_page(
100                 __( 'Theme Options', 'twentyeleven' ),   // Name of page
101                 __( 'Theme Options', 'twentyeleven' ),   // Label in menu
102                 'edit_theme_options',                    // Capability required
103                 'theme_options',                         // Menu slug, used to uniquely identify the page
104                 'twentyeleven_theme_options_render_page' // Function that renders the options page
105         );
106
107         if ( ! $theme_page )
108                 return;
109
110         add_action( "load-$theme_page", 'twentyeleven_theme_options_help' );
111 }
112 add_action( 'admin_menu', 'twentyeleven_theme_options_add_page' );
113
114 function twentyeleven_theme_options_help() {
115
116         $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>' .
117                         '<ol>' .
118                                 '<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>' .
119                                 '<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>' .
120                                 '<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>' .
121                         '</ol>' .
122                         '<p>' . __( 'Remember to click "Save Changes" to save any changes you have made to the theme options.', 'twentyeleven' ) . '</p>';
123
124         $sidebar = '<p><strong>' . __( 'For more information:', 'twentyeleven' ) . '</strong></p>' .
125                 '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Theme_Options_Screen" target="_blank">Documentation on Theme Options</a>', 'twentyeleven' ) . '</p>' .
126                 '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>', 'twentyeleven' ) . '</p>';
127
128         $screen = get_current_screen();
129
130         if ( method_exists( $screen, 'add_help_tab' ) ) {
131                 // WordPress 3.3
132                 $screen->add_help_tab( array(
133                         'title' => __( 'Overview', 'twentyeleven' ),
134                         'id' => 'theme-options-help',
135                         'content' => $help,
136                         )
137                 );
138
139                 $screen->set_help_sidebar( $sidebar );
140         } else {
141                 // WordPress 3.2
142                 add_contextual_help( $screen, $help . $sidebar );
143         }
144 }
145
146 /**
147  * Returns an array of color schemes registered for Twenty Eleven.
148  *
149  * @since Twenty Eleven 1.0
150  */
151 function twentyeleven_color_schemes() {
152         $color_scheme_options = array(
153                 'light' => array(
154                         'value' => 'light',
155                         'label' => __( 'Light', 'twentyeleven' ),
156                         'thumbnail' => get_template_directory_uri() . '/inc/images/light.png',
157                         'default_link_color' => '#1b8be0',
158                 ),
159                 'dark' => array(
160                         'value' => 'dark',
161                         'label' => __( 'Dark', 'twentyeleven' ),
162                         'thumbnail' => get_template_directory_uri() . '/inc/images/dark.png',
163                         'default_link_color' => '#e4741f',
164                 ),
165         );
166
167         return apply_filters( 'twentyeleven_color_schemes', $color_scheme_options );
168 }
169
170 /**
171  * Returns an array of layout options registered for Twenty Eleven.
172  *
173  * @since Twenty Eleven 1.0
174  */
175 function twentyeleven_layouts() {
176         $layout_options = array(
177                 'content-sidebar' => array(
178                         'value' => 'content-sidebar',
179                         'label' => __( 'Content on left', 'twentyeleven' ),
180                         'thumbnail' => get_template_directory_uri() . '/inc/images/content-sidebar.png',
181                 ),
182                 'sidebar-content' => array(
183                         'value' => 'sidebar-content',
184                         'label' => __( 'Content on right', 'twentyeleven' ),
185                         'thumbnail' => get_template_directory_uri() . '/inc/images/sidebar-content.png',
186                 ),
187                 'content' => array(
188                         'value' => 'content',
189                         'label' => __( 'One-column, no sidebar', 'twentyeleven' ),
190                         'thumbnail' => get_template_directory_uri() . '/inc/images/content.png',
191                 ),
192         );
193
194         return apply_filters( 'twentyeleven_layouts', $layout_options );
195 }
196
197 /**
198  * Returns the default options for Twenty Eleven.
199  *
200  * @since Twenty Eleven 1.0
201  */
202 function twentyeleven_get_default_theme_options() {
203         $default_theme_options = array(
204                 'color_scheme' => 'light',
205                 'link_color'   => twentyeleven_get_default_link_color( 'light' ),
206                 'theme_layout' => 'content-sidebar',
207         );
208
209         if ( is_rtl() )
210                 $default_theme_options['theme_layout'] = 'sidebar-content';
211
212         return apply_filters( 'twentyeleven_default_theme_options', $default_theme_options );
213 }
214
215 /**
216  * Returns the default link color for Twenty Eleven, based on color scheme.
217  *
218  * @since Twenty Eleven 1.0
219  *
220  * @param $string $color_scheme Color scheme. Defaults to the active color scheme.
221  * @return $string Color.
222 */
223 function twentyeleven_get_default_link_color( $color_scheme = null ) {
224         if ( null === $color_scheme ) {
225                 $options = twentyeleven_get_theme_options();
226                 $color_scheme = $options['color_scheme'];
227         }
228
229         $color_schemes = twentyeleven_color_schemes();
230         if ( ! isset( $color_schemes[ $color_scheme ] ) )
231                 return false;
232
233         return $color_schemes[ $color_scheme ]['default_link_color'];
234 }
235
236 /**
237  * Returns the options array for Twenty Eleven.
238  *
239  * @since Twenty Eleven 1.0
240  */
241 function twentyeleven_get_theme_options() {
242         return get_option( 'twentyeleven_theme_options', twentyeleven_get_default_theme_options() );
243 }
244
245 /**
246  * Renders the Color Scheme setting field.
247  *
248  * @since Twenty Eleven 1.3
249  */
250 function twentyeleven_settings_field_color_scheme() {
251         $options = twentyeleven_get_theme_options();
252
253         foreach ( twentyeleven_color_schemes() as $scheme ) {
254         ?>
255         <div class="layout image-radio-option color-scheme">
256         <label class="description">
257                 <input type="radio" name="twentyeleven_theme_options[color_scheme]" value="<?php echo esc_attr( $scheme['value'] ); ?>" <?php checked( $options['color_scheme'], $scheme['value'] ); ?> />
258                 <input type="hidden" id="default-color-<?php echo esc_attr( $scheme['value'] ); ?>" value="<?php echo esc_attr( $scheme['default_link_color'] ); ?>" />
259                 <span>
260                         <img src="<?php echo esc_url( $scheme['thumbnail'] ); ?>" width="136" height="122" alt="" />
261                         <?php echo $scheme['label']; ?>
262                 </span>
263         </label>
264         </div>
265         <?php
266         }
267 }
268
269 /**
270  * Renders the Link Color setting field.
271  *
272  * @since Twenty Eleven 1.3
273  */
274 function twentyeleven_settings_field_link_color() {
275         $options = twentyeleven_get_theme_options();
276         ?>
277         <input type="text" name="twentyeleven_theme_options[link_color]" id="link-color" value="<?php echo esc_attr( $options['link_color'] ); ?>" />
278         <a href="#" class="pickcolor hide-if-no-js" id="link-color-example"></a>
279         <input type="button" class="pickcolor button hide-if-no-js" value="<?php esc_attr_e( 'Select a Color', 'twentyeleven' ); ?>" />
280         <div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
281         <br />
282         <span><?php printf( __( 'Default color: %s', 'twentyeleven' ), '<span id="default-color">' . twentyeleven_get_default_link_color( $options['color_scheme'] ) . '</span>' ); ?></span>
283         <?php
284 }
285
286 /**
287  * Renders the Layout setting field.
288  *
289  * @since Twenty Eleven 1.3
290  */
291 function twentyeleven_settings_field_layout() {
292         $options = twentyeleven_get_theme_options();
293         foreach ( twentyeleven_layouts() as $layout ) {
294                 ?>
295                 <div class="layout image-radio-option theme-layout">
296                 <label class="description">
297                         <input type="radio" name="twentyeleven_theme_options[theme_layout]" value="<?php echo esc_attr( $layout['value'] ); ?>" <?php checked( $options['theme_layout'], $layout['value'] ); ?> />
298                         <span>
299                                 <img src="<?php echo esc_url( $layout['thumbnail'] ); ?>" width="136" height="122" alt="" />
300                                 <?php echo $layout['label']; ?>
301                         </span>
302                 </label>
303                 </div>
304                 <?php
305         }
306 }
307
308 /**
309  * Returns the options array for Twenty Eleven.
310  *
311  * @since Twenty Eleven 1.2
312  */
313 function twentyeleven_theme_options_render_page() {
314         ?>
315         <div class="wrap">
316                 <?php screen_icon(); ?>
317                 <h2><?php printf( __( '%s Theme Options', 'twentyeleven' ), get_current_theme() ); ?></h2>
318                 <?php settings_errors(); ?>
319
320                 <form method="post" action="options.php">
321                         <?php
322                                 settings_fields( 'twentyeleven_options' );
323                                 do_settings_sections( 'theme_options' );
324                                 submit_button();
325                         ?>
326                 </form>
327         </div>
328         <?php
329 }
330
331 /**
332  * Sanitize and validate form input. Accepts an array, return a sanitized array.
333  *
334  * @see twentyeleven_theme_options_init()
335  * @todo set up Reset Options action
336  *
337  * @since Twenty Eleven 1.0
338  */
339 function twentyeleven_theme_options_validate( $input ) {
340         $output = $defaults = twentyeleven_get_default_theme_options();
341
342         // Color scheme must be in our array of color scheme options
343         if ( isset( $input['color_scheme'] ) && array_key_exists( $input['color_scheme'], twentyeleven_color_schemes() ) )
344                 $output['color_scheme'] = $input['color_scheme'];
345
346         // Our defaults for the link color may have changed, based on the color scheme.
347         $output['link_color'] = $defaults['link_color'] = twentyeleven_get_default_link_color( $output['color_scheme'] );
348
349         // Link color must be 3 or 6 hexadecimal characters
350         if ( isset( $input['link_color'] ) && preg_match( '/^#?([a-f0-9]{3}){1,2}$/i', $input['link_color'] ) )
351                 $output['link_color'] = '#' . strtolower( ltrim( $input['link_color'], '#' ) );
352
353         // Theme layout must be in our array of theme layout options
354         if ( isset( $input['theme_layout'] ) && array_key_exists( $input['theme_layout'], twentyeleven_layouts() ) )
355                 $output['theme_layout'] = $input['theme_layout'];
356
357         return apply_filters( 'twentyeleven_theme_options_validate', $output, $input, $defaults );
358 }
359
360 /**
361  * Enqueue the styles for the current color scheme.
362  *
363  * @since Twenty Eleven 1.0
364  */
365 function twentyeleven_enqueue_color_scheme() {
366         $options = twentyeleven_get_theme_options();
367         $color_scheme = $options['color_scheme'];
368
369         if ( 'dark' == $color_scheme )
370                 wp_enqueue_style( 'dark', get_template_directory_uri() . '/colors/dark.css', array(), null );
371
372         do_action( 'twentyeleven_enqueue_color_scheme', $color_scheme );
373 }
374 add_action( 'wp_enqueue_scripts', 'twentyeleven_enqueue_color_scheme' );
375
376 /**
377  * Add a style block to the theme for the current link color.
378  *
379  * This function is attached to the wp_head action hook.
380  *
381  * @since Twenty Eleven 1.0
382  */
383 function twentyeleven_print_link_color_style() {
384         $options = twentyeleven_get_theme_options();
385         $link_color = $options['link_color'];
386
387         $default_options = twentyeleven_get_default_theme_options();
388
389         // Don't do anything if the current link color is the default.
390         if ( $default_options['link_color'] == $link_color )
391                 return;
392 ?>
393         <style>
394                 /* Link color */
395                 a,
396                 #site-title a:focus,
397                 #site-title a:hover,
398                 #site-title a:active,
399                 .entry-title a:hover,
400                 .entry-title a:focus,
401                 .entry-title a:active,
402                 .widget_twentyeleven_ephemera .comments-link a:hover,
403                 section.recent-posts .other-recent-posts a[rel="bookmark"]:hover,
404                 section.recent-posts .other-recent-posts .comments-link a:hover,
405                 .format-image footer.entry-meta a:hover,
406                 #site-generator a:hover {
407                         color: <?php echo $link_color; ?>;
408                 }
409                 section.recent-posts .other-recent-posts .comments-link a:hover {
410                         border-color: <?php echo $link_color; ?>;
411                 }
412                 article.feature-image.small .entry-summary p a:hover,
413                 .entry-header .comments-link a:hover,
414                 .entry-header .comments-link a:focus,
415                 .entry-header .comments-link a:active,
416                 .feature-slider a.active {
417                         background-color: <?php echo $link_color; ?>;
418                 }
419         </style>
420 <?php
421 }
422 add_action( 'wp_head', 'twentyeleven_print_link_color_style' );
423
424 /**
425  * Adds Twenty Eleven layout classes to the array of body classes.
426  *
427  * @since Twenty Eleven 1.0
428  */
429 function twentyeleven_layout_classes( $existing_classes ) {
430         $options = twentyeleven_get_theme_options();
431         $current_layout = $options['theme_layout'];
432
433         if ( in_array( $current_layout, array( 'content-sidebar', 'sidebar-content' ) ) )
434                 $classes = array( 'two-column' );
435         else
436                 $classes = array( 'one-column' );
437
438         if ( 'content-sidebar' == $current_layout )
439                 $classes[] = 'right-sidebar';
440         elseif ( 'sidebar-content' == $current_layout )
441                 $classes[] = 'left-sidebar';
442         else
443                 $classes[] = $current_layout;
444
445         $classes = apply_filters( 'twentyeleven_layout_classes', $classes, $current_layout );
446
447         return array_merge( $existing_classes, $classes );
448 }
449 add_filter( 'body_class', 'twentyeleven_layout_classes' );