| 1 | | <?php |
| 2 | | /** |
| 3 | | * Twenty Twelve Theme Options |
| 4 | | * |
| 5 | | * @package WordPress |
| 6 | | * @subpackage Twenty_Twelve |
| 7 | | * @since Twenty Twelve 1.0 |
| 8 | | */ |
| 9 | | |
| 10 | | class Twenty_Twelve_Options { |
| 11 | | /** |
| 12 | | * The option value in the database will be based on get_stylesheet() |
| 13 | | * so child themes don't share the parent theme's option value. |
| 14 | | * |
| 15 | | * @access public |
| 16 | | * @var string |
| 17 | | */ |
| 18 | | public $option_key = 'twentytwelve_theme_options'; |
| 19 | | |
| 20 | | /** |
| 21 | | * Holds our options. |
| 22 | | * |
| 23 | | * @access public |
| 24 | | * @var array |
| 25 | | */ |
| 26 | | public $options = array(); |
| 27 | | |
| 28 | | /** |
| 29 | | * Constructor |
| 30 | | * |
| 31 | | * @access public |
| 32 | | * |
| 33 | | * @return Twenty_Twelve_Options |
| 34 | | */ |
| 35 | | public function __construct() { |
| 36 | | // Set option key based on get_stylesheet() |
| 37 | | if ( 'twentytwelve' != get_stylesheet() ) |
| 38 | | $this->option_key = get_stylesheet() . '_theme_options'; |
| 39 | | |
| 40 | | add_action( 'admin_init', array( $this, 'options_init' ) ); |
| 41 | | add_action( 'admin_menu', array( $this, 'add_page' ) ); |
| 42 | | add_action( 'customize_register', array( $this, 'customize_register' ) ); |
| 43 | | } |
| 44 | | |
| 45 | | /** |
| 46 | | * Register the form setting for our options array. |
| 47 | | * |
| 48 | | * This function is attached to the admin_init action hook. |
| 49 | | * |
| 50 | | * This call to register_setting() registers a validation callback, validate(), |
| 51 | | * which is used when the option is saved, to ensure that our option values are properly |
| 52 | | * formatted, and safe. |
| 53 | | * |
| 54 | | * @access public |
| 55 | | * |
| 56 | | * @return void |
| 57 | | */ |
| 58 | | public function options_init() { |
| 59 | | // Load our options for use in any method. |
| 60 | | $this->options = $this->get_theme_options(); |
| 61 | | |
| 62 | | // Register our option group. |
| 63 | | register_setting( |
| 64 | | 'twentytwelve_options', // Options group, see settings_fields() call in render_page() |
| 65 | | $this->option_key, // Database option, see get_theme_options() |
| 66 | | array( $this, 'validate' ) // The sanitization callback, see validate() |
| 67 | | ); |
| 68 | | |
| 69 | | // Register our settings field group. |
| 70 | | add_settings_section( |
| 71 | | 'general', // Unique identifier for the settings section |
| 72 | | '', // Section title (we don't want one) |
| 73 | | '__return_false', // Section callback (we don't want anything) |
| 74 | | 'theme_options' // Menu slug, used to uniquely identify the page; see add_page() |
| 75 | | ); |
| 76 | | |
| 77 | | // Register our individual settings fields. |
| 78 | | add_settings_field( |
| 79 | | 'enable_fonts', // Unique identifier for the field for this section |
| 80 | | __( 'Enable Web Fonts', 'twentytwelve' ), // Setting field label |
| 81 | | array( $this, 'settings_field_enable_fonts' ), // Function that renders the settings field |
| 82 | | 'theme_options', // Menu slug, used to uniquely identify the page; see add_page() |
| 83 | | 'general' // Settings section. Same as the first argument in the add_settings_section() above |
| 84 | | ); |
| 85 | | } |
| 86 | | |
| 87 | | /** |
| 88 | | * Add our theme options page to the admin menu. |
| 89 | | * |
| 90 | | * This function is attached to the admin_menu action hook. |
| 91 | | * |
| 92 | | * @access public |
| 93 | | * |
| 94 | | * @return void |
| 95 | | */ |
| 96 | | public function add_page() { |
| 97 | | $theme_page = add_theme_page( |
| 98 | | __( 'Theme Options', 'twentytwelve' ), // Name of page |
| 99 | | __( 'Theme Options', 'twentytwelve' ), // Label in menu |
| 100 | | 'edit_theme_options', // Capability required |
| 101 | | 'theme_options', // Menu slug, used to uniquely identify the page |
| 102 | | array( $this, 'render_page' ) // Function that renders the options page |
| 103 | | ); |
| 104 | | } |
| 105 | | |
| 106 | | /** |
| 107 | | * Returns the default options. |
| 108 | | * |
| 109 | | * @access public |
| 110 | | * |
| 111 | | * @return array |
| 112 | | */ |
| 113 | | public function get_default_theme_options() { |
| 114 | | $default_theme_options = array( |
| 115 | | 'enable_fonts' => false, |
| 116 | | ); |
| 117 | | |
| 118 | | return apply_filters( 'twentytwelve_default_theme_options', $default_theme_options ); |
| 119 | | } |
| 120 | | |
| 121 | | /** |
| 122 | | * Returns the options array. |
| 123 | | * |
| 124 | | * @access public |
| 125 | | * |
| 126 | | * @return array |
| 127 | | */ |
| 128 | | public function get_theme_options() { |
| 129 | | return get_option( $this->option_key, $this->get_default_theme_options() ); |
| 130 | | } |
| 131 | | |
| 132 | | /** |
| 133 | | * Renders the enable fonts checkbox setting field. |
| 134 | | * |
| 135 | | * @access public |
| 136 | | * |
| 137 | | * @return void |
| 138 | | */ |
| 139 | | public function settings_field_enable_fonts() { |
| 140 | | $options = $this->options; |
| 141 | | ?> |
| 142 | | <label for="enable-fonts"> |
| 143 | | <input type="checkbox" name="<?php echo $this->option_key; ?>[enable_fonts]" id="enable-fonts" <?php checked( $options['enable_fonts'] ); ?> /> |
| 144 | | <?php _e( 'Yes, I’d like to enable the gorgeous, open-source <em>Open Sans</em> typeface.', 'twentytwelve' ); ?> |
| 145 | | </label> |
| 146 | | <?php |
| 147 | | } |
| 148 | | |
| 149 | | /** |
| 150 | | * Returns the options array. |
| 151 | | * |
| 152 | | * @uses get_current_theme() for back compat, fallback for < 3.4 |
| 153 | | * @access public |
| 154 | | * |
| 155 | | * @return void |
| 156 | | */ |
| 157 | | public function render_page() { |
| 158 | | ?> |
| 159 | | <div class="wrap"> |
| 160 | | <?php screen_icon(); ?> |
| 161 | | <?php $theme_name = function_exists( 'wp_get_theme' ) ? wp_get_theme() : get_current_theme(); ?> |
| 162 | | <h2><?php printf( __( '%s Theme Options', 'twentytwelve' ), $theme_name ); ?></h2> |
| 163 | | <?php settings_errors(); ?> |
| 164 | | |
| 165 | | <form method="post" action="options.php"> |
| 166 | | <?php |
| 167 | | settings_fields( 'twentytwelve_options' ); |
| 168 | | do_settings_sections( 'theme_options' ); |
| 169 | | submit_button(); |
| 170 | | ?> |
| 171 | | </form> |
| 172 | | </div> |
| 173 | | <?php |
| 174 | | } |
| 175 | | |
| 176 | | /** |
| 177 | | * Sanitize and validate form input. Accepts an array, return a sanitized array. |
| 178 | | * |
| 179 | | * @see options_init() |
| 180 | | * @access public |
| 181 | | * @param array $input |
| 182 | | * |
| 183 | | * @return array The validated data. |
| 184 | | */ |
| 185 | | public function validate( $input ) { |
| 186 | | $output = $defaults = $this->get_default_theme_options(); |
| 187 | | |
| 188 | | // The enable fonts checkbox should boolean true or false |
| 189 | | if ( ! isset( $input['enable_fonts'] ) ) |
| 190 | | $input['enable_fonts'] = false; |
| 191 | | $output['enable_fonts'] = ( false != $input['enable_fonts'] ? true : false ); |
| 192 | | |
| 193 | | return apply_filters( 'twentytwelve_options_validate', $output, $input, $defaults ); |
| 194 | | } |
| 195 | | |
| 196 | | /** |
| 197 | | * Implement Twenty Twelve theme options into Theme Customizer. |
| 198 | | * |
| 199 | | * @since Twenty Twelve 1.0 |
| 200 | | * @access public |
| 201 | | * @param WP_Customize_Manager $wp_customize Theme Customizer object. |
| 202 | | * |
| 203 | | * @return void |
| 204 | | */ |
| 205 | | public function customize_register( $wp_customize ) { |
| 206 | | // Enable Web Fonts |
| 207 | | $wp_customize->add_section( $this->option_key . '_enable_fonts', array( |
| 208 | | 'title' => __( 'Fonts', 'twentytwelve' ), |
| 209 | | 'priority' => 35, |
| 210 | | ) ); |
| 211 | | |
| 212 | | $defaults = $this->get_default_theme_options(); |
| 213 | | |
| 214 | | $wp_customize->add_setting( $this->option_key . '[enable_fonts]', array( |
| 215 | | 'default' => $defaults['enable_fonts'], |
| 216 | | 'type' => 'option', |
| 217 | | 'capability' => 'edit_theme_options', |
| 218 | | ) ); |
| 219 | | |
| 220 | | $wp_customize->add_control( $this->option_key . '_enable_fonts', array( |
| 221 | | 'label' => __( 'Enable Web Fonts', 'twentytwelve' ), |
| 222 | | 'section' => $this->option_key . '_enable_fonts', |
| 223 | | 'settings' => $this->option_key . '[enable_fonts]', |
| 224 | | 'type' => 'checkbox', |
| 225 | | ) ); |
| 226 | | } |
| 227 | | } |
| 228 | | No newline at end of file |