PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/twentyeleven/inc/theme-options.php

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