Subversion Repositories ZenDark

Rev

Rev 3 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
/*
 * Theme Options
 */

 
/*
 * Register the form setting for our zendark_options array.
 *
 * This function is attached to the admin_init action hook.
 *
 * This call to register_setting() registers a validation callback, zendark_theme_options_validate(),
 * which is used when the option is saved, to ensure that our option values are complete, properly
 * formatted, and safe.
 *
 * We also use this function to add our theme option if it doesn't already exist.
 */

function zendark_theme_options_init() {

        // If we have no options in the database, let's add them now.
        if (false === zendark_get_theme_options())
                add_option('zendark_theme_options', zendark_get_default_theme_options());

        register_setting(
                'zendark_options',       // Options group, see settings_fields() call in theme_options_render_page()
                'zendark_theme_options', // Database option, see zendark_get_theme_options()
                'zendark_theme_options_validate' // The sanitization callback, see zendark_theme_options_validate()
        );
}
add_action('admin_init', 'zendark_theme_options_init');

/*
 * Change the capability required to save the 'zendark_options' options group.
 *
 * @see zendark_theme_options_init() First parameter to register_setting() is the name of the options group.
 * @see zendark_theme_options_add_page() The edit_theme_options capability is used for viewing the page.
 *
 * By default, the options groups for all registered settings require the manage_options capability.
 * This filter is required to change our theme options page to edit_theme_options instead.
 * By default, only administrators have either of these capabilities, but the desire here is
 * to allow for finer-grained control for roles and users.
 *
 * @param string $capability The capability used for the page, which is manage_options by default.
 * @return string The capability to actually use.
 */

function zendark_option_page_capability($capability) {
        return 'edit_theme_options';
}
add_filter('option_page_capability_zendark_options', 'zendark_option_page_capability');

/*
 * Add our theme options page to the admin menu, including some help documentation.
 *
 * This function is attached to the admin_menu action hook.
 */

function zendark_theme_options_add_page() {
        $theme_page = add_theme_page(
                __('Theme Options', 'zendark'),            // Name of page
                __('Theme Options', 'zendark'),            // Label in menu
                'edit_theme_options',                  // Capability required
                'theme_options',                       // Menu slug, used to uniquely identify the page
                'zendark_theme_options_render_page'        // Function that renders the options page
        );

        if (!$theme_page)
                return;

        $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, Simple Dark Theme, provides the following Theme Options:', 'zendark').'</p>' .
                        '<ol>' .
                                '<li>'.__( '<strong>Zenphoto integration</strong>: You can insert the URL of your Zenphoto installation (if on the same domain of this WordPress installation) to integrate it on a WordPress page.', 'zendark').'</li>'.
                        '</ol>'.
                        '<p>'.__('Remember to click "Save Changes" to save any changes you have made to the theme options.', 'zendark').'</p>'.
                        '<p><strong>'.__('For more information:', 'zendark').'</strong></p>'.
                        '<p>'.__('<a href="http://codex.wordpress.org/Appearance_Theme_Options_Screen" target="_blank">Documentation on Theme Options</a>', 'zendark').'</p>'.
                        '<p>'.__('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>', 'zendark').'</p>';

        add_contextual_help($theme_page, $help);
}
add_action('admin_menu', 'zendark_theme_options_add_page');

/*
 * Returns the default options
 */

function zendark_get_default_theme_options() {
        $default_theme_options = array(
                'zenphoto_url' => 'http://'
        );
}

/*
 * Returns the options array
 */

function zendark_get_theme_options() {
        return get_option('zendark_theme_options', zendark_get_default_theme_options());
}

/*
 * Returns the options page
 */

function zendark_theme_options_render_page() {
        ?>
        <div class="wrap">
                <?php screen_icon(); ?>
                <h2><?php printf(__('%s Theme Options', 'zendark'), get_current_theme()); ?></h2>
                <?php settings_errors(); ?>

                <form method="post" action="options.php" style="margin-top: 20px;">
                        <?php
                                settings_fields('zendark_options');
                                $options = zendark_get_theme_options();
                                $default_options = zendark_get_default_theme_options();
                        ?>
                        <fieldset>
        <label><?php _e('Zenphoto URL:', 'zendark'); ?></label>
        <legend class="screen-reader-text"><span><?php _e('Zenphoto URL', 'zendark'); ?></span></legend>
                                <input type="text" name="zendark_theme_options[zenphoto_url]" id="zenphoto-url" value="<?php echo esc_attr( $options['zenphoto_url'] ); ?>" size="60" />
                        </fieldset>
                        <?php submit_button(); ?>
                </form>
        </div>
        <?php
}

/*
 * Sanitize and validate form input. Accepts an array, return a sanitized array.
 */

function zendark_theme_options_validate($input) {
        $output = $defaults = zendark_get_default_theme_options();

        // Zenphoto URL must be in our array
        if (isset( $input['zenphoto_url']))
                $output['zenphoto_url'] = $input['zenphoto_url'];

        return apply_filters('zendark_theme_options_validate', $output, $input, $defaults);
}
?>