Make WordPress Core

Ticket #21404: 21404.diff

File 21404.diff, 10.4 KB (added by obenland, 13 years ago)

Possible implementation

  • wp-content/themes/twentytwelve/inc/theme-options.php

     
    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&#8217;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
  • wp-content/themes/twentytwelve/functions.php

     
    4343 * @since Twenty Twelve 1.0
    4444 */
    4545function twentytwelve_setup() {
    46         global $twentytwelve_options;
    4746
    4847        /**
    4948         * Make Twenty Twelve available for translation.
     
    5352         */
    5453        load_theme_textdomain( 'twentytwelve', get_template_directory() . '/languages' );
    5554
    56         // Load up our theme options page and related code.
    57         require( get_template_directory() . '/inc/theme-options.php' );
    58         $twentytwelve_options = new Twenty_Twelve_Options();
    59 
    6055        // You can define support for an editor stylesheet here; Twenty Twelve doesn't have a default one.
    6156        // Then, create a CSS file called editor-style.css and place it in your theme directory.
    6257        add_editor_style();
     
    9287 * @since Twenty Twelve 1.0
    9388 */
    9489function twentytwelve_scripts_styles() {
    95         global $twentytwelve_options;
    9690
    9791        /**
    9892         * Add JavaScript to pages with the comment form to support
     
    111105         * Load special font CSS file.
    112106         * Depends on Theme Options setting.
    113107         */
    114         $options = $twentytwelve_options->get_theme_options();
    115         if ( $options['enable_fonts'] )
     108        if ( get_theme_mod( 'enable_fonts', twentytwelve_default_theme_options()->enable_fonts ) )
    116109                wp_enqueue_style( 'twentytwelve-fonts', 'http://fonts.googleapis.com/css?family=Open+Sans:400italic,700italic,400,700' );
    117110
    118111        /**
     
    161154add_filter( 'wp_title', 'twentytwelve_wp_title', 10, 2 );
    162155
    163156/**
     157 * Add Customizer page to the admin menu.
     158 *
     159 * @since Twenty Twelve 1.0
     160 *
     161 * @return void
     162 */
     163function twentytwelve_add_page() {
     164       
     165        if ( class_exists( 'WP_Customize_Manager' ) )
     166                add_theme_page(
     167                        __( 'Customize', 'twentytwelve' ), // Name of page
     168                        __( 'Customize', 'twentytwelve' ), // Label in menu
     169                        'edit_theme_options',              // Capability required
     170                        'customize.php'                    // Menu slug, used to uniquely identify the page
     171                );
     172}
     173add_action( 'admin_menu', 'twentytwelve_add_page' );
     174
     175/**
     176 * Implement Twenty Twelve theme options into Theme Customizer.
     177 *
     178 * @since Twenty Twelve 1.0
     179 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
     180 *
     181 * @return void
     182 */
     183function twentytwelve_customize_register( $wp_customize ) {
     184        // Enable Web Fonts
     185        $wp_customize->add_section( 'twentytwelve_fonts', array(
     186                'title'    => __( 'Fonts', 'twentytwelve' ),
     187                'priority' => 35,
     188        ) );
     189
     190        $wp_customize->add_setting( 'enable_fonts', array(
     191                'default'  => twentytwelve_default_theme_options()->enable_fonts
     192        ) );
     193
     194        $wp_customize->add_control( 'twentytwelve_enable_fonts', array(
     195                'label'    => __( 'Enable Open Sans Font', 'twentytwelve' ),
     196                'section'  => 'twentytwelve_fonts',
     197                'settings' => 'enable_fonts',
     198                'type'     => 'checkbox',
     199        ) );
     200}
     201add_action( 'customize_register', 'twentytwelve_customize_register' );
     202
     203/**
     204 * Returns the default options.
     205 *
     206 * @since Twenty Twelve 1.0
     207 *
     208 * @return object
     209 */
     210function twentytwelve_default_theme_options() {
     211        $default_theme_options = array(
     212                'enable_fonts' => false,
     213        );
     214
     215        return (object) apply_filters( 'twentytwelve_default_theme_options', $default_theme_options );
     216}
     217
     218/**
    164219 * Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
    165220 *
    166221 * @since Twenty Twelve 1.0