PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/themes/OneMozilla/inc/theme-options.php

https://github.com/fvignals/One-Mozilla-blog
PHP | 314 lines | 204 code | 41 blank | 69 comment | 18 complexity | f7bd79f7eca72369645cc1c482181a11 MD5 | raw file
  1. <?php
  2. /**
  3. * Properly enqueue styles and scripts for our theme options page.
  4. * This function is attached to the admin_enqueue_scripts action hook.
  5. */
  6. function onemozilla_admin_enqueue_scripts( $hook_suffix ) {
  7. wp_enqueue_style( 'onemozilla-theme-options', get_template_directory_uri() . '/inc/theme-options.css', false, '2012-03-06' );
  8. // wp_enqueue_script( 'onemozilla-theme-options', get_template_directory_uri() . '/inc/theme-options.js', array( 'farbtastic' ), '2011-06-10' );
  9. }
  10. add_action( 'admin_print_styles-appearance_page_theme_options', 'onemozilla_admin_enqueue_scripts' );
  11. /**
  12. * Register the form setting for our onemozilla_options array.
  13. *
  14. * This function is attached to the admin_init action hook.
  15. *
  16. * This call to register_setting() registers a validation callback, onemozilla_theme_options_validate(),
  17. * which is used when the option is saved, to ensure that our option values are complete, properly
  18. * formatted, and safe.
  19. *
  20. * We also use this function to add our theme option if it doesn't already exist.
  21. */
  22. function onemozilla_theme_options_init() {
  23. // If we have no options in the database, let's add them now.
  24. if ( false === onemozilla_get_theme_options() )
  25. add_option( 'onemozilla_theme_options', onemozilla_get_default_theme_options() );
  26. register_setting(
  27. 'onemozilla_options', // Options group, see settings_fields() call in onemozilla_theme_options_render_page()
  28. 'onemozilla_theme_options', // Database option, see onemozilla_get_theme_options()
  29. 'onemozilla_theme_options_validate' // The sanitization callback, see onemozilla_theme_options_validate()
  30. );
  31. // Register our settings field group
  32. add_settings_section(
  33. 'general', // Unique identifier for the settings section
  34. '', // Section title (we don't want one)
  35. '__return_false', // Section callback (we don't want anything)
  36. 'theme_options' // Menu slug, used to uniquely identify the page; see onemozilla_theme_options_add_page()
  37. );
  38. // Register our individual settings fields
  39. add_settings_field(
  40. 'color_scheme', // Unique identifier for the field for this section
  41. __( 'Color Scheme', 'onemozilla' ), // Setting field label
  42. 'onemozilla_settings_field_color_scheme', // Function that renders the settings field
  43. 'theme_options', // Menu slug, used to uniquely identify the page; see onemozilla_theme_options_add_page()
  44. 'general' // Settings section. Same as the first argument in the add_settings_section() above
  45. );
  46. add_settings_field(
  47. 'hide_author',
  48. __( 'Hide post authors', 'onemozilla' ),
  49. 'onemozilla_settings_field_hide_author',
  50. 'theme_options',
  51. 'general'
  52. );
  53. add_settings_field(
  54. 'share_posts',
  55. __( 'Social sharing', 'onemozilla' ),
  56. 'onemozilla_settings_field_share_posts',
  57. 'theme_options',
  58. 'general'
  59. );
  60. }
  61. add_action( 'admin_init', 'onemozilla_theme_options_init' );
  62. /**
  63. * Change the capability required to save the 'onemozilla_options' options group.
  64. *
  65. * @see onemozilla_theme_options_init() First parameter to register_setting() is the name of the options group.
  66. * @see onemozilla_theme_options_add_page() The edit_theme_options capability is used for viewing the page.
  67. *
  68. * By default, the options groups for all registered settings require the manage_options capability.
  69. * This filter is required to change our theme options page to edit_theme_options instead.
  70. * By default, only administrators have either of these capabilities, but the desire here is
  71. * to allow for finer-grained control for roles and users.
  72. *
  73. * @param string $capability The capability used for the page, which is manage_options by default.
  74. * @return string The capability to actually use.
  75. */
  76. function onemozilla_option_page_capability( $capability ) {
  77. return 'edit_theme_options';
  78. }
  79. add_filter( 'option_page_capability_onemozilla_options', 'onemozilla_option_page_capability' );
  80. /**
  81. * Add our theme options page to the admin menu, including some help documentation.
  82. * This function is attached to the admin_menu action hook.
  83. */
  84. function onemozilla_theme_options_add_page() {
  85. $theme_page = add_theme_page(
  86. __( 'Theme Options', 'onemozilla' ), // Name of page
  87. __( 'Theme Options', 'onemozilla' ), // Label in menu
  88. 'edit_theme_options', // Capability required
  89. 'theme_options', // Menu slug, used to uniquely identify the page
  90. 'onemozilla_theme_options_render_page' // Function that renders the options page
  91. );
  92. if ( ! $theme_page )
  93. return;
  94. add_action( "load-$theme_page", 'onemozilla_theme_options_help' );
  95. }
  96. add_action( 'admin_menu', 'onemozilla_theme_options_add_page' );
  97. function onemozilla_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, One Mozilla, provides the following Theme Options:', 'onemozilla' ) . '</p>' .
  99. '<ol>' .
  100. '<li>' . __( '<strong>Color Scheme</strong>: You can choose from a few different color schemes: “Sand” (orange-tan background) is suitable for most Mozilla blogs. “Sky” (light blue/gray background) suitable for Firefox-related blogs. “Obsidian” is a light-on-dark scheme. “Stone” is the default scheme, a light, neutral gray.' , 'onemozilla' ) . '</li>' .
  101. '<li>' . __( '<strong>Hide post authors</strong>: Post authors are shown by default, with an author bio in the sidebar on single post pages. You can choose to hide post authors and the blog will be anonymous to the public, though authors will still be visible in the administration panel.', 'onemozilla' ) . '</li>' .
  102. '</ol>' .
  103. '<p>' . __( 'Remember to click “Save Changes” to save any changes you have made to the theme options.', 'onemozilla' ) . '</p>';
  104. $sidebar = '<p><strong>' . __( 'For more information:', 'onemozilla' ) . '</strong></p>' .
  105. '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Theme_Options_Screen" target="_blank">Documentation on Theme Options</a>', 'onemozilla' ) . '</p>' .
  106. '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>', 'onemozilla' ) . '</p>';
  107. $screen = get_current_screen();
  108. if ( method_exists( $screen, 'add_help_tab' ) ) {
  109. // WordPress 3.3
  110. $screen->add_help_tab( array(
  111. 'title' => __( 'Overview', 'onemozilla' ),
  112. 'id' => 'theme-options-help',
  113. 'content' => $help,
  114. )
  115. );
  116. $screen->set_help_sidebar( $sidebar );
  117. } else {
  118. // WordPress 3.2
  119. add_contextual_help( $screen, $help . $sidebar );
  120. }
  121. }
  122. /**
  123. * Returns the default options for One Mozilla.
  124. */
  125. function onemozilla_get_default_theme_options() {
  126. $default_theme_options = array(
  127. 'color_scheme' => 'stone',
  128. 'hide_author' => '0',
  129. 'share_posts' => '0'
  130. );
  131. return apply_filters( 'onemozilla_default_theme_options', $default_theme_options );
  132. }
  133. /**
  134. * Returns an array of color schemes registered for One Mozilla.
  135. */
  136. function onemozilla_color_schemes() {
  137. $color_scheme_options = array(
  138. 'stone' => array(
  139. 'value' => 'stone',
  140. 'label' => __( 'Stone', 'onemozilla' ),
  141. 'thumbnail' => get_template_directory_uri() . '/colors/stone/stone.jpg',
  142. ),
  143. 'sand' => array(
  144. 'value' => 'sand',
  145. 'label' => __( 'Sand', 'onemozilla' ),
  146. 'thumbnail' => get_template_directory_uri() . '/colors/sand/sand.jpg',
  147. ),
  148. 'sky' => array(
  149. 'value' => 'sky',
  150. 'label' => __( 'Sky', 'onemozilla' ),
  151. 'thumbnail' => get_template_directory_uri() . '/colors/sky/sky.jpg',
  152. ),
  153. 'obsidian' => array(
  154. 'value' => 'obsidian',
  155. 'label' => __( 'Obsidian', 'onemozilla' ),
  156. 'thumbnail' => get_template_directory_uri() . '/colors/obsidian/obsidian.jpg',
  157. ),
  158. );
  159. return apply_filters( 'onemozilla_color_schemes', $color_scheme_options );
  160. }
  161. /**
  162. * Returns the options array for One Mozilla.
  163. */
  164. function onemozilla_get_theme_options() {
  165. return get_option( 'onemozilla_theme_options', onemozilla_get_default_theme_options() );
  166. }
  167. /**
  168. * Renders the Color Scheme setting field.
  169. */
  170. function onemozilla_settings_field_color_scheme() {
  171. $options = onemozilla_get_theme_options();
  172. foreach ( onemozilla_color_schemes() as $scheme ) {
  173. ?>
  174. <div class="layout image-radio-option color-scheme">
  175. <label class="description">
  176. <input type="radio" name="onemozilla_theme_options[color_scheme]" value="<?php echo esc_attr( $scheme['value'] ); ?>" <?php checked( $options['color_scheme'], $scheme['value'] ); ?> />
  177. <span>
  178. <img src="<?php echo esc_url( $scheme['thumbnail'] ); ?>" width="140" height="140" alt="">
  179. <?php echo esc_attr( $scheme['label'] ); ?>
  180. </span>
  181. </label>
  182. </div>
  183. <?php
  184. }
  185. }
  186. /**
  187. * Renders the Show Author setting field.
  188. */
  189. function onemozilla_settings_field_hide_author() {
  190. $options = onemozilla_get_theme_options();
  191. ?>
  192. <div class="layout hide-author">
  193. <label class="description">
  194. <input type="checkbox" name="onemozilla_theme_options[hide_author]" value="1" <?php checked( '1', esc_attr($options['hide_author']) ); ?> />
  195. <span>
  196. <?php _e('Hide post authors (makes posts anonymous to the public)', 'onemozilla'); ?>
  197. </span>
  198. </label>
  199. </div>
  200. <?php
  201. }
  202. /**
  203. * Renders the Social Sharing setting field.
  204. */
  205. function onemozilla_settings_field_share_posts() {
  206. $options = onemozilla_get_theme_options();
  207. ?>
  208. <div class="layout share-posts">
  209. <label class="description">
  210. <input type="checkbox" name="onemozilla_theme_options[share_posts]" value="1" <?php checked( '1', esc_attr($options['share_posts']) ); ?> />
  211. <span>
  212. <?php _e('Add social sharing buttons to posts and pages', 'onemozilla'); ?>
  213. </span>
  214. </label>
  215. </div>
  216. <?php
  217. }
  218. /**
  219. * Returns the options array for One Mozilla.
  220. */
  221. function onemozilla_theme_options_render_page() {
  222. ?>
  223. <div class="wrap">
  224. <?php screen_icon(); ?>
  225. <h2><?php printf( __( '%s Theme Options', 'onemozilla' ), get_current_theme() ); ?></h2>
  226. <?php settings_errors(); ?>
  227. <form method="post" action="options.php">
  228. <?php
  229. settings_fields( 'onemozilla_options' );
  230. do_settings_sections( 'theme_options' );
  231. submit_button();
  232. ?>
  233. </form>
  234. </div>
  235. <?php
  236. }
  237. /**
  238. * Sanitize and validate form input. Accepts an array, return a sanitized array.
  239. */
  240. function onemozilla_theme_options_validate( $input ) {
  241. $output = $defaults = onemozilla_get_default_theme_options();
  242. // Color scheme must be in our array of color scheme options
  243. if ( isset( $input['color_scheme'] ) && array_key_exists( $input['color_scheme'], onemozilla_color_schemes() ) )
  244. $output['color_scheme'] = $input['color_scheme'];
  245. // Our checkbox value is either 0 or 1
  246. if ( ! isset( $input['hide_author'] ) ) {
  247. $input['hide_author'] = $defaults['hide_author'];
  248. }
  249. $output['hide_author'] = ( $input['hide_author'] == 1 ? 1 : 0 );
  250. // Our checkbox value is either 0 or 1
  251. if ( ! isset( $input['share_posts'] ) ) {
  252. $input['share_posts'] = $defaults['share_posts'];
  253. }
  254. $output['share_posts'] = ( $input['share_posts'] == 1 ? 1 : 0 );
  255. return apply_filters( 'onemozilla_theme_options_validate', $output, $input, $defaults );
  256. }
  257. /**
  258. * Enqueue the styles for the current color scheme.
  259. */
  260. function onemozilla_enqueue_color_scheme() {
  261. $options = onemozilla_get_theme_options();
  262. $color_scheme = $options['color_scheme'];
  263. if ( 'sand' == $color_scheme )
  264. wp_enqueue_style( 'sand', get_template_directory_uri() . '/colors/sand/sand.css', array(), null );
  265. if ( 'sky' == $color_scheme )
  266. wp_enqueue_style( 'sky', get_template_directory_uri() . '/colors/sky/sky.css', array(), null );
  267. if ( 'stone' == $color_scheme )
  268. wp_enqueue_style( 'stone', get_template_directory_uri() . '/colors/stone/stone.css', array(), null );
  269. if ( 'obsidian' == $color_scheme )
  270. wp_enqueue_style( 'obsidian', get_template_directory_uri() . '/colors/obsidian/obsidian.css', array(), null );
  271. do_action( 'onemozilla_enqueue_color_scheme', $color_scheme );
  272. }
  273. add_action( 'wp_enqueue_scripts', 'onemozilla_enqueue_color_scheme' );