PageRenderTime 43ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

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