PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

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

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